diff --git a/cores/esp8266/Updater.cpp b/cores/esp8266/Updater.cpp index 28830fba52..7ad21300a9 100644 --- a/cores/esp8266/Updater.cpp +++ b/cores/esp8266/Updater.cpp @@ -38,14 +38,14 @@ void UpdaterClass::_reset() { bool UpdaterClass::begin(size_t size, int command) { if(_size > 0){ #ifdef DEBUG_UPDATER - DEBUG_UPDATER.println("already running"); + DEBUG_UPDATER.println("[begin] already running"); #endif return false; } #ifdef DEBUG_UPDATER if (command == U_SPIFFS) { - DEBUG_UPDATER.println("Update SPIFFS."); + DEBUG_UPDATER.println("[begin] Update SPIFFS."); } #endif @@ -73,6 +73,12 @@ bool UpdaterClass::begin(size_t size, int command) { //address where we will start writing the update updateStartAddress = updateEndAddress - roundedSize; +#ifdef DEBUG_UPDATER + DEBUG_UPDATER.printf("[begin] roundedSize: 0x%08X (%d)\n", roundedSize, roundedSize); + DEBUG_UPDATER.printf("[begin] updateEndAddress: 0x%08X (%d)\n", updateEndAddress, updateEndAddress); + DEBUG_UPDATER.printf("[begin] currentSketchSize: 0x%08X (%d)\n", currentSketchSize, currentSketchSize); +#endif + //make sure that the size of both sketches is less than the total space (updateEndAddress) if(updateStartAddress < currentSketchSize) { _error = UPDATE_ERROR_SPACE; @@ -88,7 +94,7 @@ bool UpdaterClass::begin(size_t size, int command) { else { // unknown command #ifdef DEBUG_UPDATER - DEBUG_UPDATER.println("Unknown update command."); + DEBUG_UPDATER.println("[begin] Unknown update command."); #endif return false; } @@ -100,6 +106,12 @@ bool UpdaterClass::begin(size_t size, int command) { _buffer = new uint8_t[FLASH_SECTOR_SIZE]; _command = command; +#ifdef DEBUG_UPDATER + DEBUG_UPDATER.printf("[begin] _startAddress: 0x%08X (%d)\n", _startAddress, _startAddress); + DEBUG_UPDATER.printf("[begin] _currentAddress: 0x%08X (%d)\n", _currentAddress, _currentAddress); + DEBUG_UPDATER.printf("[begin] _size: 0x%08X (%d)\n", _size, _size); +#endif + _md5.begin(); return true; } @@ -168,9 +180,14 @@ bool UpdaterClass::end(bool evenIfRemaining){ } bool UpdaterClass::_writeBuffer(){ + + yield(); + bool result = ESP.flashEraseSector(_currentAddress/FLASH_SECTOR_SIZE); + yield(); + if (result) { + result = ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen); + } yield(); - bool result = ESP.flashEraseSector(_currentAddress/FLASH_SECTOR_SIZE) && - ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen); if (!result) { _error = UPDATE_ERROR_WRITE; @@ -217,29 +234,32 @@ size_t UpdaterClass::write(uint8_t *data, size_t len) { } size_t UpdaterClass::writeStream(Stream &data) { - size_t written = 0; - size_t toRead = 0; - if(hasError() || !isRunning()) - return 0; - - while(remaining()) { - toRead = FLASH_SECTOR_SIZE - _bufferLen; - toRead = data.readBytes(_buffer + _bufferLen, toRead); - if(toRead == 0){ //Timeout - _error = UPDATE_ERROR_STREAM; - _currentAddress = (_startAddress + _size); + size_t written = 0; + size_t toRead = 0; + if(hasError() || !isRunning()) + return 0; + + while(remaining()) { + toRead = data.readBytes(_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen)); + if(toRead == 0) { //Timeout + delay(100); + toRead = data.readBytes(_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen)); + if(toRead == 0) { //Timeout + _error = UPDATE_ERROR_STREAM; + _currentAddress = (_startAddress + _size); #ifdef DEBUG_UPDATER - printError(DEBUG_UPDATER); + printError(DEBUG_UPDATER); #endif - return written; + } + return written; + } + _bufferLen += toRead; + if((_bufferLen == remaining() || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer()) + return written; + written += toRead; + yield(); } - _bufferLen += toRead; - if((_bufferLen == remaining() || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer()) - return written; - written += toRead; - yield(); - } - return written; + return written; } void UpdaterClass::printError(Stream &out){ diff --git a/cores/esp8266/WString.cpp b/cores/esp8266/WString.cpp index f92344b84e..4ba4b1f9f2 100644 --- a/cores/esp8266/WString.cpp +++ b/cores/esp8266/WString.cpp @@ -121,6 +121,7 @@ ICACHE_FLASH_ATTR String::~String() { if(buffer) { free(buffer); } + init(); } // /*********************************************/ @@ -136,8 +137,7 @@ inline void String::init(void) { void ICACHE_FLASH_ATTR String::invalidate(void) { if(buffer) free(buffer); - buffer = NULL; - capacity = len = 0; + init(); } unsigned char ICACHE_FLASH_ATTR String::reserve(unsigned int size) { diff --git a/cores/esp8266/WString.h b/cores/esp8266/WString.h index 4c502bfe54..3f216cee4d 100644 --- a/cores/esp8266/WString.h +++ b/cores/esp8266/WString.h @@ -76,7 +76,11 @@ class String { // invalid string (i.e., "if (s)" will be true afterwards) unsigned char reserve(unsigned int size); inline unsigned int length(void) const { - return len; + if(buffer) { + return len; + } else { + return 0; + } } // creates a copy of the assigned value. if the value is null or