diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index 42715605c1..b18574dad2 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -132,9 +132,43 @@ uint64_t EspClass::deepSleepMax() } +/* +Layout of RTC Memory is as follows: +Ref: Espressif doc 2C-ESP8266_Non_OS_SDK_API_Reference, section 3.3.23 (system_rtc_mem_write) + +|<------system data (256 bytes)------->|<-----------------user data (512 bytes)--------------->| + +SDK function signature: +bool system_rtc_mem_read ( + uint32 des_addr, + void * src_addr, + uint32 save_size +) + +The system data section can't be used by the user, so: +des_addr must be >=64 (i.e.: 256/4) and <192 (i.e.: 768/4) +src_addr is a pointer to data +save_size is the number of bytes to write + +For the method interface: +offset is the user block number (block size is 4 bytes) must be >= 0 and <128 +data is a pointer to data, 4-byte aligned +size is number of bytes in the block pointed to by data + +Same for write. + +Note: If the Updater class is in play, e.g.: the application uses OTA, the eboot +command will be stored into the first 128 bytes of user data, then it will be +retrieved by eboot on boot. That means that user data present there will be lost. +Ref: +- discussion in PR #5330. +- https://github.com/esp8266/esp8266-wiki/wiki/Memory-Map#memmory-mapped-io-registers +- Arduino/bootloaders/eboot/eboot_command.h RTC_MEM definition +*/ + bool EspClass::rtcUserMemoryRead(uint32_t offset, uint32_t *data, size_t size) { - if (size + offset > 512) { + if (offset*4 + size > 512 || size == 0) { return false; } else { return system_rtc_mem_read(64 + offset, data, size); @@ -143,13 +177,15 @@ bool EspClass::rtcUserMemoryRead(uint32_t offset, uint32_t *data, size_t size) bool EspClass::rtcUserMemoryWrite(uint32_t offset, uint32_t *data, size_t size) { - if (size + offset > 512) { + if (offset*4 + size > 512 || size == 0) { return false; } else { return system_rtc_mem_write(64 + offset, data, size); } } + + extern "C" void __real_system_restart_local(); void EspClass::reset(void) { diff --git a/cores/esp8266/flash_utils.h b/cores/esp8266/flash_utils.h index 67dc6ebadd..932a2c7c54 100644 --- a/cores/esp8266/flash_utils.h +++ b/cores/esp8266/flash_utils.h @@ -21,6 +21,8 @@ #ifndef FLASH_UTILS_H #define FLASH_UTILS_H +#include "spi_flash_sec_size.h" + #ifdef __cplusplus extern "C" { #endif @@ -31,7 +33,7 @@ int SPIRead(uint32_t addr, void *dest, size_t size); int SPIWrite(uint32_t addr, void *src, size_t size); int SPIEraseAreaEx(const uint32_t start, const uint32_t size); -#define FLASH_SECTOR_SIZE 0x1000 +#define FLASH_SECTOR_SIZE SPI_FLASH_SEC_SIZE #define FLASH_BLOCK_SIZE 0x10000 #define APP_START_OFFSET 0x1000 diff --git a/tests/host/common/spi_flash_sec_size.h b/tests/host/common/spi_flash_sec_size.h new file mode 100644 index 0000000000..a4096f7a65 --- /dev/null +++ b/tests/host/common/spi_flash_sec_size.h @@ -0,0 +1,8 @@ +#ifndef SPI_FLASH_SEC_SIZE_H +#define SPI_FLASH_SEC_SIZE_H + +//pulled this define from tools/sdk/include/spi_flash.h for reuse in the Arduino core without pulling in a bunch of other stuff +#define SPI_FLASH_SEC_SIZE 4096 + + +#endif diff --git a/tools/sdk/include/spi_flash.h b/tools/sdk/include/spi_flash.h index 00f8a08b79..d5c7e9a348 100644 --- a/tools/sdk/include/spi_flash.h +++ b/tools/sdk/include/spi_flash.h @@ -25,6 +25,8 @@ #ifndef SPI_FLASH_H #define SPI_FLASH_H +#include "spi_flash_sec_size.h" + #ifdef __cplusplus extern "C" { #endif @@ -44,8 +46,6 @@ typedef struct{ uint32 status_mask; } SpiFlashChip; -#define SPI_FLASH_SEC_SIZE 4096 - extern SpiFlashChip * flashchip; // in ram ROM-BIOS uint32 spi_flash_get_id(void); diff --git a/tools/sdk/include/spi_flash_sec_size.h b/tools/sdk/include/spi_flash_sec_size.h new file mode 100644 index 0000000000..48724d0df2 --- /dev/null +++ b/tools/sdk/include/spi_flash_sec_size.h @@ -0,0 +1,8 @@ +#ifndef SPI_FLASH_SEC_SIZE_H +#define SPI_FLASH_SEC_SIZE_H + +//pulled this define from spi_flash.h for reuse in the Arduino core without pulling in a bunch of other stuff +#define SPI_FLASH_SEC_SIZE 4096 + + +#endif