Skip to content

Commit 617f6ca

Browse files
committed
Merge pull request #1191 from Links2004/httpUpdate
fix update
2 parents ba4b425 + f4deee6 commit 617f6ca

File tree

3 files changed

+52
-28
lines changed

3 files changed

+52
-28
lines changed

cores/esp8266/Updater.cpp

+45-25
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ void UpdaterClass::_reset() {
3838
bool UpdaterClass::begin(size_t size, int command) {
3939
if(_size > 0){
4040
#ifdef DEBUG_UPDATER
41-
DEBUG_UPDATER.println("already running");
41+
DEBUG_UPDATER.println("[begin] already running");
4242
#endif
4343
return false;
4444
}
4545

4646
#ifdef DEBUG_UPDATER
4747
if (command == U_SPIFFS) {
48-
DEBUG_UPDATER.println("Update SPIFFS.");
48+
DEBUG_UPDATER.println("[begin] Update SPIFFS.");
4949
}
5050
#endif
5151

@@ -73,6 +73,12 @@ bool UpdaterClass::begin(size_t size, int command) {
7373
//address where we will start writing the update
7474
updateStartAddress = updateEndAddress - roundedSize;
7575

76+
#ifdef DEBUG_UPDATER
77+
DEBUG_UPDATER.printf("[begin] roundedSize: 0x%08X (%d)\n", roundedSize, roundedSize);
78+
DEBUG_UPDATER.printf("[begin] updateEndAddress: 0x%08X (%d)\n", updateEndAddress, updateEndAddress);
79+
DEBUG_UPDATER.printf("[begin] currentSketchSize: 0x%08X (%d)\n", currentSketchSize, currentSketchSize);
80+
#endif
81+
7682
//make sure that the size of both sketches is less than the total space (updateEndAddress)
7783
if(updateStartAddress < currentSketchSize) {
7884
_error = UPDATE_ERROR_SPACE;
@@ -88,7 +94,7 @@ bool UpdaterClass::begin(size_t size, int command) {
8894
else {
8995
// unknown command
9096
#ifdef DEBUG_UPDATER
91-
DEBUG_UPDATER.println("Unknown update command.");
97+
DEBUG_UPDATER.println("[begin] Unknown update command.");
9298
#endif
9399
return false;
94100
}
@@ -100,6 +106,12 @@ bool UpdaterClass::begin(size_t size, int command) {
100106
_buffer = new uint8_t[FLASH_SECTOR_SIZE];
101107
_command = command;
102108

109+
#ifdef DEBUG_UPDATER
110+
DEBUG_UPDATER.printf("[begin] _startAddress: 0x%08X (%d)\n", _startAddress, _startAddress);
111+
DEBUG_UPDATER.printf("[begin] _currentAddress: 0x%08X (%d)\n", _currentAddress, _currentAddress);
112+
DEBUG_UPDATER.printf("[begin] _size: 0x%08X (%d)\n", _size, _size);
113+
#endif
114+
103115
_md5.begin();
104116
return true;
105117
}
@@ -168,9 +180,14 @@ bool UpdaterClass::end(bool evenIfRemaining){
168180
}
169181

170182
bool UpdaterClass::_writeBuffer(){
183+
184+
yield();
185+
bool result = ESP.flashEraseSector(_currentAddress/FLASH_SECTOR_SIZE);
186+
yield();
187+
if (result) {
188+
result = ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen);
189+
}
171190
yield();
172-
bool result = ESP.flashEraseSector(_currentAddress/FLASH_SECTOR_SIZE) &&
173-
ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen);
174191

175192
if (!result) {
176193
_error = UPDATE_ERROR_WRITE;
@@ -217,29 +234,32 @@ size_t UpdaterClass::write(uint8_t *data, size_t len) {
217234
}
218235

219236
size_t UpdaterClass::writeStream(Stream &data) {
220-
size_t written = 0;
221-
size_t toRead = 0;
222-
if(hasError() || !isRunning())
223-
return 0;
224-
225-
while(remaining()) {
226-
toRead = FLASH_SECTOR_SIZE - _bufferLen;
227-
toRead = data.readBytes(_buffer + _bufferLen, toRead);
228-
if(toRead == 0){ //Timeout
229-
_error = UPDATE_ERROR_STREAM;
230-
_currentAddress = (_startAddress + _size);
237+
size_t written = 0;
238+
size_t toRead = 0;
239+
if(hasError() || !isRunning())
240+
return 0;
241+
242+
while(remaining()) {
243+
toRead = data.readBytes(_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen));
244+
if(toRead == 0) { //Timeout
245+
delay(100);
246+
toRead = data.readBytes(_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen));
247+
if(toRead == 0) { //Timeout
248+
_error = UPDATE_ERROR_STREAM;
249+
_currentAddress = (_startAddress + _size);
231250
#ifdef DEBUG_UPDATER
232-
printError(DEBUG_UPDATER);
251+
printError(DEBUG_UPDATER);
233252
#endif
234-
return written;
253+
}
254+
return written;
255+
}
256+
_bufferLen += toRead;
257+
if((_bufferLen == remaining() || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer())
258+
return written;
259+
written += toRead;
260+
yield();
235261
}
236-
_bufferLen += toRead;
237-
if((_bufferLen == remaining() || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer())
238-
return written;
239-
written += toRead;
240-
yield();
241-
}
242-
return written;
262+
return written;
243263
}
244264

245265
void UpdaterClass::printError(Stream &out){

cores/esp8266/WString.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ ICACHE_FLASH_ATTR String::~String() {
121121
if(buffer) {
122122
free(buffer);
123123
}
124+
init();
124125
}
125126

126127
// /*********************************************/
@@ -136,8 +137,7 @@ inline void String::init(void) {
136137
void ICACHE_FLASH_ATTR String::invalidate(void) {
137138
if(buffer)
138139
free(buffer);
139-
buffer = NULL;
140-
capacity = len = 0;
140+
init();
141141
}
142142

143143
unsigned char ICACHE_FLASH_ATTR String::reserve(unsigned int size) {

cores/esp8266/WString.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ class String {
7676
// invalid string (i.e., "if (s)" will be true afterwards)
7777
unsigned char reserve(unsigned int size);
7878
inline unsigned int length(void) const {
79-
return len;
79+
if(buffer) {
80+
return len;
81+
} else {
82+
return 0;
83+
}
8084
}
8185

8286
// creates a copy of the assigned value. if the value is null or

0 commit comments

Comments
 (0)