1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

Merge pull request #1191 from Links2004/httpUpdate

fix update
This commit is contained in:
Markus 2015-12-10 18:00:20 +01:00
commit 617f6cad1e
3 changed files with 51 additions and 27 deletions

View File

@ -38,14 +38,14 @@ void UpdaterClass::_reset() {
bool UpdaterClass::begin(size_t size, int command) { bool UpdaterClass::begin(size_t size, int command) {
if(_size > 0){ if(_size > 0){
#ifdef DEBUG_UPDATER #ifdef DEBUG_UPDATER
DEBUG_UPDATER.println("already running"); DEBUG_UPDATER.println("[begin] already running");
#endif #endif
return false; return false;
} }
#ifdef DEBUG_UPDATER #ifdef DEBUG_UPDATER
if (command == U_SPIFFS) { if (command == U_SPIFFS) {
DEBUG_UPDATER.println("Update SPIFFS."); DEBUG_UPDATER.println("[begin] Update SPIFFS.");
} }
#endif #endif
@ -73,6 +73,12 @@ bool UpdaterClass::begin(size_t size, int command) {
//address where we will start writing the update //address where we will start writing the update
updateStartAddress = updateEndAddress - roundedSize; 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) //make sure that the size of both sketches is less than the total space (updateEndAddress)
if(updateStartAddress < currentSketchSize) { if(updateStartAddress < currentSketchSize) {
_error = UPDATE_ERROR_SPACE; _error = UPDATE_ERROR_SPACE;
@ -88,7 +94,7 @@ bool UpdaterClass::begin(size_t size, int command) {
else { else {
// unknown command // unknown command
#ifdef DEBUG_UPDATER #ifdef DEBUG_UPDATER
DEBUG_UPDATER.println("Unknown update command."); DEBUG_UPDATER.println("[begin] Unknown update command.");
#endif #endif
return false; return false;
} }
@ -100,6 +106,12 @@ bool UpdaterClass::begin(size_t size, int command) {
_buffer = new uint8_t[FLASH_SECTOR_SIZE]; _buffer = new uint8_t[FLASH_SECTOR_SIZE];
_command = command; _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(); _md5.begin();
return true; return true;
} }
@ -168,9 +180,14 @@ bool UpdaterClass::end(bool evenIfRemaining){
} }
bool UpdaterClass::_writeBuffer(){ bool UpdaterClass::_writeBuffer(){
yield();
bool result = ESP.flashEraseSector(_currentAddress/FLASH_SECTOR_SIZE);
yield();
if (result) {
result = ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen);
}
yield(); yield();
bool result = ESP.flashEraseSector(_currentAddress/FLASH_SECTOR_SIZE) &&
ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen);
if (!result) { if (!result) {
_error = UPDATE_ERROR_WRITE; _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 UpdaterClass::writeStream(Stream &data) {
size_t written = 0; size_t written = 0;
size_t toRead = 0; size_t toRead = 0;
if(hasError() || !isRunning()) if(hasError() || !isRunning())
return 0; return 0;
while(remaining()) { while(remaining()) {
toRead = FLASH_SECTOR_SIZE - _bufferLen; toRead = data.readBytes(_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen));
toRead = data.readBytes(_buffer + _bufferLen, toRead); if(toRead == 0) { //Timeout
if(toRead == 0){ //Timeout delay(100);
_error = UPDATE_ERROR_STREAM; toRead = data.readBytes(_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen));
_currentAddress = (_startAddress + _size); if(toRead == 0) { //Timeout
_error = UPDATE_ERROR_STREAM;
_currentAddress = (_startAddress + _size);
#ifdef DEBUG_UPDATER #ifdef DEBUG_UPDATER
printError(DEBUG_UPDATER); printError(DEBUG_UPDATER);
#endif #endif
return written; }
return written;
}
_bufferLen += toRead;
if((_bufferLen == remaining() || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer())
return written;
written += toRead;
yield();
} }
_bufferLen += toRead; return written;
if((_bufferLen == remaining() || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer())
return written;
written += toRead;
yield();
}
return written;
} }
void UpdaterClass::printError(Stream &out){ void UpdaterClass::printError(Stream &out){

View File

@ -121,6 +121,7 @@ ICACHE_FLASH_ATTR String::~String() {
if(buffer) { if(buffer) {
free(buffer); free(buffer);
} }
init();
} }
// /*********************************************/ // /*********************************************/
@ -136,8 +137,7 @@ inline void String::init(void) {
void ICACHE_FLASH_ATTR String::invalidate(void) { void ICACHE_FLASH_ATTR String::invalidate(void) {
if(buffer) if(buffer)
free(buffer); free(buffer);
buffer = NULL; init();
capacity = len = 0;
} }
unsigned char ICACHE_FLASH_ATTR String::reserve(unsigned int size) { unsigned char ICACHE_FLASH_ATTR String::reserve(unsigned int size) {

View File

@ -76,7 +76,11 @@ class String {
// invalid string (i.e., "if (s)" will be true afterwards) // invalid string (i.e., "if (s)" will be true afterwards)
unsigned char reserve(unsigned int size); unsigned char reserve(unsigned int size);
inline unsigned int length(void) const { 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 // creates a copy of the assigned value. if the value is null or