@@ -38,14 +38,14 @@ void UpdaterClass::_reset() {
38
38
bool UpdaterClass::begin (size_t size, int command) {
39
39
if (_size > 0 ){
40
40
#ifdef DEBUG_UPDATER
41
- DEBUG_UPDATER.println (" already running" );
41
+ DEBUG_UPDATER.println (" [begin] already running" );
42
42
#endif
43
43
return false ;
44
44
}
45
45
46
46
#ifdef DEBUG_UPDATER
47
47
if (command == U_SPIFFS) {
48
- DEBUG_UPDATER.println (" Update SPIFFS." );
48
+ DEBUG_UPDATER.println (" [begin] Update SPIFFS." );
49
49
}
50
50
#endif
51
51
@@ -73,6 +73,12 @@ bool UpdaterClass::begin(size_t size, int command) {
73
73
// address where we will start writing the update
74
74
updateStartAddress = updateEndAddress - roundedSize;
75
75
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
+
76
82
// make sure that the size of both sketches is less than the total space (updateEndAddress)
77
83
if (updateStartAddress < currentSketchSize) {
78
84
_error = UPDATE_ERROR_SPACE;
@@ -88,7 +94,7 @@ bool UpdaterClass::begin(size_t size, int command) {
88
94
else {
89
95
// unknown command
90
96
#ifdef DEBUG_UPDATER
91
- DEBUG_UPDATER.println (" Unknown update command." );
97
+ DEBUG_UPDATER.println (" [begin] Unknown update command." );
92
98
#endif
93
99
return false ;
94
100
}
@@ -100,6 +106,12 @@ bool UpdaterClass::begin(size_t size, int command) {
100
106
_buffer = new uint8_t [FLASH_SECTOR_SIZE];
101
107
_command = command;
102
108
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
+
103
115
_md5.begin ();
104
116
return true ;
105
117
}
@@ -168,9 +180,14 @@ bool UpdaterClass::end(bool evenIfRemaining){
168
180
}
169
181
170
182
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
+ }
171
190
yield ();
172
- bool result = ESP.flashEraseSector (_currentAddress/FLASH_SECTOR_SIZE) &&
173
- ESP.flashWrite (_currentAddress, (uint32_t *) _buffer, _bufferLen);
174
191
175
192
if (!result) {
176
193
_error = UPDATE_ERROR_WRITE;
@@ -217,29 +234,32 @@ size_t UpdaterClass::write(uint8_t *data, size_t len) {
217
234
}
218
235
219
236
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);
231
250
#ifdef DEBUG_UPDATER
232
- printError (DEBUG_UPDATER);
251
+ printError (DEBUG_UPDATER);
233
252
#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 ();
235
261
}
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;
243
263
}
244
264
245
265
void UpdaterClass::printError (Stream &out){
0 commit comments