From fab5104cffdf135e66c933d736769baf7671f9e0 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Thu, 30 Apr 2015 15:02:05 +0200 Subject: [PATCH 1/8] use User-defined literals for kHz, MHz, GHz, kBit, MBit, GBit, kB, MB and GB see #145 --- cores/esp8266/Arduino.h | 44 +++++++++++++++++++++++++++++++++++++++++ cores/esp8266/Esp.cpp | 30 +++++++++------------------- 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index 19c25e899..6b9aedcfb 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -186,4 +186,48 @@ long map(long, long, long, long, long); #include "pins_arduino.h" +/** + * User-defined Literals + * usage: + * + * uint32_t = test = 10_MHz; // --> 10000000 + */ +#ifdef __cplusplus +unsigned long long operator"" _kHz(unsigned long long x) { + return x * 1000; +} + +unsigned long long operator"" _MHz(unsigned long long x) { + return x * 1000 * 1000; +} + +unsigned long long operator"" _GHz(unsigned long long x) { + return x * 1000 * 1000 * 1000; +} + +unsigned long long operator"" _kBit(unsigned long long x) { + return x * 1024; +} + +unsigned long long operator"" _MBit(unsigned long long x) { + return x * 1024 * 1024; +} + +unsigned long long operator"" _GBit(unsigned long long x) { + return x * 1024 * 1024 * 1024; +} + +unsigned long long operator"" _kB(unsigned long long x) { + return x * 1024; +} + +unsigned long long operator"" _MB(unsigned long long x) { + return x * 1024 * 1024; +} + +unsigned long long operator"" _GB(unsigned long long x) { + return x * 1024 * 1024 * 1024; +} +#endif + #endif diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index 190ff3009..5dafde53e 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -24,18 +24,6 @@ extern "C" { #include "user_interface.h" } -#define kHz (1000L) -#define MHz (1000L*kHz) -#define GHz (1000L*MHz) - -#define kBit (1024L) -#define MBit (1024L*kBit) -#define GBit (1024L*MBit) - -#define kB (1024L) -#define MB (1024L*kB) -#define GB (1024L*MB) - //extern "C" void ets_wdt_init(uint32_t val); extern "C" void ets_wdt_enable(void); extern "C" void ets_wdt_disable(void); @@ -134,15 +122,15 @@ uint32_t EspClass::getFlashChipSize(void) if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) { switch((bytes[3] & 0xf0) >> 4) { case 0x0: // 4 Mbit (512KB) - return (512 * kB); + return (512_kB); case 0x1: // 2 MBit (256KB) - return (256 * kB); + return (256_kB); case 0x2: // 8 MBit (1MB) - return (1 * MB); + return (1_MB); case 0x3: // 16 MBit (2MB) - return (2 * MB); + return (2_MB); case 0x4: // 32 MBit (4MB) - return (4 * MB); + return (4_MB); default: // fail? return 0; } @@ -158,13 +146,13 @@ uint32_t EspClass::getFlashChipSpeed(void) if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) { switch(bytes[3] & 0x0F) { case 0x0: // 40 MHz - return (40 * MHz); + return (40_MHz); case 0x1: // 26 MHz - return (26 * MHz); + return (26_MHz); case 0x2: // 20 MHz - return (20 * MHz); + return (20_MHz); case 0xf: // 80 MHz - return (80 * MHz); + return (80_MHz); default: // fail? return 0; } From 55b2d88d07f531f8887b2a5d94e510044b5e0328 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Thu, 30 Apr 2015 15:19:36 +0200 Subject: [PATCH 2/8] in Arduino.h make some errors by compiling in some cases moved to Esp.cpp --- cores/esp8266/Arduino.h | 44 ----------------------------------------- cores/esp8266/Esp.cpp | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index 6b9aedcfb..19c25e899 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -186,48 +186,4 @@ long map(long, long, long, long, long); #include "pins_arduino.h" -/** - * User-defined Literals - * usage: - * - * uint32_t = test = 10_MHz; // --> 10000000 - */ -#ifdef __cplusplus -unsigned long long operator"" _kHz(unsigned long long x) { - return x * 1000; -} - -unsigned long long operator"" _MHz(unsigned long long x) { - return x * 1000 * 1000; -} - -unsigned long long operator"" _GHz(unsigned long long x) { - return x * 1000 * 1000 * 1000; -} - -unsigned long long operator"" _kBit(unsigned long long x) { - return x * 1024; -} - -unsigned long long operator"" _MBit(unsigned long long x) { - return x * 1024 * 1024; -} - -unsigned long long operator"" _GBit(unsigned long long x) { - return x * 1024 * 1024 * 1024; -} - -unsigned long long operator"" _kB(unsigned long long x) { - return x * 1024; -} - -unsigned long long operator"" _MB(unsigned long long x) { - return x * 1024 * 1024; -} - -unsigned long long operator"" _GB(unsigned long long x) { - return x * 1024 * 1024 * 1024; -} -#endif - #endif diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index 5dafde53e..26f31bb4c 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -29,6 +29,50 @@ extern "C" void ets_wdt_enable(void); extern "C" void ets_wdt_disable(void); extern "C" void wdt_feed(void); +/** + * User-defined Literals + * usage: + * + * uint32_t = test = 10_MHz; // --> 10000000 + */ + +unsigned long long operator"" _kHz(unsigned long long x) { + return x * 1000; +} + +unsigned long long operator"" _MHz(unsigned long long x) { + return x * 1000 * 1000; +} + +unsigned long long operator"" _GHz(unsigned long long x) { + return x * 1000 * 1000 * 1000; +} + +unsigned long long operator"" _kBit(unsigned long long x) { + return x * 1024; +} + +unsigned long long operator"" _MBit(unsigned long long x) { + return x * 1024 * 1024; +} + +unsigned long long operator"" _GBit(unsigned long long x) { + return x * 1024 * 1024 * 1024; +} + +unsigned long long operator"" _kB(unsigned long long x) { + return x * 1024; +} + +unsigned long long operator"" _MB(unsigned long long x) { + return x * 1024 * 1024; +} + +unsigned long long operator"" _GB(unsigned long long x) { + return x * 1024 * 1024 * 1024; +} + + EspClass ESP; EspClass::EspClass() From 1d2d8f8dd213f5aeb966b8236063200fd2e35793 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Thu, 30 Apr 2015 16:06:01 +0200 Subject: [PATCH 3/8] add function to get flash size based of flash Chip id --- cores/esp8266/Esp.cpp | 29 +++++++++++++++++++++++++++++ cores/esp8266/Esp.h | 1 + 2 files changed, 30 insertions(+) diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index 26f31bb4c..b2126b627 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -218,3 +218,32 @@ FlashMode_t EspClass::getFlashChipMode(void) } return mode; } + +/** + * Infos from + * http://www.wlxmall.com/images/stock_item/att/A1010004.pdf + * http://www.gigadevice.com/product-series/5.html?locale=en_US + */ +uint32_t EspClass::getFlashChipSizeByChipId(void) { + uint32_t chipId = getFlashChipId(); + switch(chipId) { + case 0x1740C8: // GD25Q64B + return (8_MB); + case 0x1640C8: // GD25Q32B + return (4_MB); + case 0x1540C8: // GD25Q16B + return (2_MB); + case 0x1440C8: // GD25Q80 + return (1_MB); + case 0x1340C8: // GD25Q40 + return (512_kB); + case 0x1240C8: // GD25Q20 + return (256_kB); + case 0x1140C8: // GD25Q10 + return (128_kB); + case 0x1040C8: // GD25Q12 + return (64_kB); + default: + return 0; + } +} diff --git a/cores/esp8266/Esp.h b/cores/esp8266/Esp.h index 34fa3e13b..78b468163 100644 --- a/cores/esp8266/Esp.h +++ b/cores/esp8266/Esp.h @@ -90,6 +90,7 @@ class EspClass { uint32_t getFlashChipSize(void); uint32_t getFlashChipSpeed(void); FlashMode_t getFlashChipMode(void); + uint32_t getFlashChipSizeByChipId(void); }; From 344eb6e8dbda74acb331ca24e62169ecd6fb6a16 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Fri, 1 May 2015 14:04:06 +0200 Subject: [PATCH 4/8] move digitalWrite and digitalRead to ram if in flash it can produce uncalculated lag. fix warning --- cores/esp8266/core_esp8266_wiring_digital.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/core_esp8266_wiring_digital.c b/cores/esp8266/core_esp8266_wiring_digital.c index 5ac670e63..33fbe8c49 100644 --- a/cores/esp8266/core_esp8266_wiring_digital.c +++ b/cores/esp8266/core_esp8266_wiring_digital.c @@ -81,7 +81,7 @@ extern void __pinMode(uint8_t pin, uint8_t mode) { } } -extern void __digitalWrite(uint8_t pin, uint8_t val) { +extern void ICACHE_RAM_ATTR __digitalWrite(uint8_t pin, uint8_t val) { val &= 0x01; if(pin < 16){ if(val) GPOS = (1 << pin); @@ -92,12 +92,13 @@ extern void __digitalWrite(uint8_t pin, uint8_t val) { } } -extern int __digitalRead(uint8_t pin) { +extern int ICACHE_RAM_ATTR __digitalRead(uint8_t pin) { if(pin < 16){ return GPIP(pin); } else if(pin == 16){ return GP16I & 0x01; } + return 0; } /* From dd844fc0f3cfbc2a79212dcf25f82e1260ca9b57 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Fri, 1 May 2015 14:08:01 +0200 Subject: [PATCH 5/8] remove libg libg is debugging version of libc, no sense to link both at the same time! --- platform.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform.txt b/platform.txt index b3aa18f36..72915c027 100644 --- a/platform.txt +++ b/platform.txt @@ -23,7 +23,7 @@ compiler.S.flags=-c -g -x assembler-with-cpp -MMD compiler.c.elf.ldscript=eagle.app.v6.ld compiler.c.elf.flags=-nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static "-L{compiler.sdk.path}/lib" "-L{compiler.sdk.path}/ld" "-T{compiler.c.elf.ldscript}" compiler.c.elf.cmd=xtensa-lx106-elf-gcc -compiler.c.elf.libs=-lc -lm -lg -lgcc -lhal -lphy -lnet80211 -llwip -lwpa -lmain -lpp -lsmartconfig +compiler.c.elf.libs=-lm -lc -lgcc -lhal -lphy -lnet80211 -llwip -lwpa -lmain -lpp -lsmartconfig compiler.cpp.cmd=xtensa-lx106-elf-g++ compiler.cpp.flags=-c -Os -mlongcalls -mtext-section-literals -fno-exceptions -fno-rtti -std=c++11 -MMD From 0a712e78c7ffd3e91a97bb57f5a1ea085e31b0d0 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Fri, 1 May 2015 14:11:09 +0200 Subject: [PATCH 6/8] fix 160Mhz mode add missing 0 16000000L != 160000000L --- cores/esp8266/core_esp8266_main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index 326d3264b..d45165443 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -48,7 +48,7 @@ extern void setup(); void preloop_update_frequency() __attribute__((weak)); void preloop_update_frequency() { -#if defined(F_CPU) && (F_CPU == 16000000L) +#if defined(F_CPU) && (F_CPU == 160000000L) REG_SET_BIT(0x3ff00014, BIT(0)); ets_update_cpu_frequency(160); #endif From d4e6561b5253348c87ca0222adea1cea3e303865 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Fri, 1 May 2015 14:46:08 +0200 Subject: [PATCH 7/8] add back pull-down support --- cores/esp8266/core_esp8266_wiring_digital.c | 12 ++++++++---- cores/esp8266/esp8266_peri.h | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cores/esp8266/core_esp8266_wiring_digital.c b/cores/esp8266/core_esp8266_wiring_digital.c index 33fbe8c49..b61901bbb 100644 --- a/cores/esp8266/core_esp8266_wiring_digital.c +++ b/cores/esp8266/core_esp8266_wiring_digital.c @@ -59,13 +59,17 @@ extern void __pinMode(uint8_t pin, uint8_t mode) { GPF(pin) = GPFFS(GPFFS_GPIO(pin));//Set mode to GPIO GPC(pin) = (GPC(pin) & (0xF << GPCI)); //SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED) GPES = (1 << pin); //Enable - } else if(mode == INPUT || mode == INPUT_PULLUP){ + } else if(mode == INPUT || mode == INPUT_PULLUP || mode == INPUT_PULLDOWN){ GPF(pin) = GPFFS(GPFFS_GPIO(pin));//Set mode to GPIO GPC(pin) = (GPC(pin) & (0xF << GPCI)) | (1 << GPCD); //SOURCE(GPIO) | DRIVER(OPEN_DRAIN) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED) GPEC = (1 << pin); //Disable - if(mode == INPUT_PULLUP){ - GPF(pin) |= (1 << GPFPU);//Enable Pullup - } + if(mode == INPUT_PULLUP) { + GPF(pin) &= ~(1 << GPFPD); // Disable Pulldown + GPF(pin) |= (1 << GPFPU); // Enable Pullup + } else if(mode == INPUT_PULLDOWN) { + GPF(pin) &= ~(1 << GPFPU); // Disable Pullup + GPF(pin) |= (1 << GPFPD); // Enable Pulldown + } } } else if(pin == 16){ GPF16 = GP16FFS(GPFFS_GPIO(pin));//Set mode to GPIO diff --git a/cores/esp8266/esp8266_peri.h b/cores/esp8266/esp8266_peri.h index f905d34b3..8b92a07f5 100644 --- a/cores/esp8266/esp8266_peri.h +++ b/cores/esp8266/esp8266_peri.h @@ -93,9 +93,11 @@ static uint8_t esp8266_gpioToFn[16] = {0x34, 0x18, 0x38, 0x14, 0x3C, 0x40, 0x1C, //GPIO (0-15) PIN Function Bits #define GPFSOE 0 //Sleep OE #define GPFSS 1 //Sleep Sel +#define GPFSPD 2 //Sleep Pulldown #define GPFSPU 3 //Sleep Pullup #define GPFFS0 4 //Function Select bit 0 #define GPFFS1 5 //Function Select bit 1 +#define GPFPD 6 //Pulldown #define GPFPU 7 //Pullup #define GPFFS2 8 //Function Select bit 2 #define GPFFS(f) (((((f) & 4) != 0) << GPFFS2) | ((((f) & 2) != 0) << GPFFS1) | ((((f) & 1) != 0) << GPFFS0)) From a250492c20f080e7a23f30821b57dae3ab5636ad Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Fri, 1 May 2015 14:58:48 +0200 Subject: [PATCH 8/8] disable debug on Serial.end() if debug on this interface. --- cores/esp8266/HardwareSerial.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cores/esp8266/HardwareSerial.cpp b/cores/esp8266/HardwareSerial.cpp index ad8766444..9bd3e35a8 100644 --- a/cores/esp8266/HardwareSerial.cpp +++ b/cores/esp8266/HardwareSerial.cpp @@ -510,6 +510,9 @@ void ICACHE_FLASH_ATTR HardwareSerial::begin(unsigned long baud, byte config) { } void ICACHE_FLASH_ATTR HardwareSerial::end() { + if(uart_get_debug() == _uart_nr) { + uart_set_debug(UART_NO); + } uart_uninit(_uart); delete _rx_buffer; delete _tx_buffer;