Skip to content

fix update #1191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 45 additions & 25 deletions cores/esp8266/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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){
Expand Down
4 changes: 2 additions & 2 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ ICACHE_FLASH_ATTR String::~String() {
if(buffer) {
free(buffer);
}
init();
}

// /*********************************************/
Expand All @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion cores/esp8266/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down