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

Updater: if low on heap memory, use smaller write buffer

This commit is contained in:
Ivan Grokhotkov 2017-05-09 17:33:29 +08:00 committed by Ivan Grokhotkov
parent 9b0ad39e94
commit 01e1c586cb
2 changed files with 24 additions and 15 deletions

View File

@ -127,7 +127,12 @@ bool UpdaterClass::begin(size_t size, int command) {
_startAddress = updateStartAddress; _startAddress = updateStartAddress;
_currentAddress = _startAddress; _currentAddress = _startAddress;
_size = size; _size = size;
_buffer = new uint8_t[FLASH_SECTOR_SIZE]; if (ESP.getFreeHeap() > 2 * FLASH_SECTOR_SIZE) {
_bufferSize = FLASH_SECTOR_SIZE;
} else {
_bufferSize = 256;
}
_buffer = new uint8_t[_bufferSize];
_command = command; _command = command;
#ifdef DEBUG_UPDATER #ifdef DEBUG_UPDATER
@ -218,13 +223,16 @@ bool UpdaterClass::end(bool evenIfRemaining){
bool UpdaterClass::_writeBuffer(){ bool UpdaterClass::_writeBuffer(){
if(!_async) yield(); bool result = true;
bool result = ESP.flashEraseSector(_currentAddress/FLASH_SECTOR_SIZE); if (_currentAddress % FLASH_SECTOR_SIZE == 0) {
if(!_async) yield(); if(!_async) yield();
if (result) { result = ESP.flashEraseSector(_currentAddress/FLASH_SECTOR_SIZE);
result = ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen); }
if (result) {
if(!_async) yield();
result = ESP.flashWrite(_currentAddress, (uint32_t*) _buffer, _bufferLen);
} }
if(!_async) yield();
if (!result) { if (!result) {
_error = UPDATE_ERROR_WRITE; _error = UPDATE_ERROR_WRITE;
@ -253,8 +261,8 @@ size_t UpdaterClass::write(uint8_t *data, size_t len) {
size_t left = len; size_t left = len;
while((_bufferLen + left) > FLASH_SECTOR_SIZE) { while((_bufferLen + left) > _bufferSize) {
size_t toBuff = FLASH_SECTOR_SIZE - _bufferLen; size_t toBuff = _bufferSize - _bufferLen;
memcpy(_buffer + _bufferLen, data + (len - left), toBuff); memcpy(_buffer + _bufferLen, data + (len - left), toBuff);
_bufferLen += toBuff; _bufferLen += toBuff;
if(!_writeBuffer()){ if(!_writeBuffer()){
@ -340,10 +348,10 @@ size_t UpdaterClass::writeStream(Stream &data) {
} }
while(remaining()) { while(remaining()) {
toRead = data.readBytes(_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen)); toRead = data.readBytes(_buffer + _bufferLen, (_bufferSize - _bufferLen));
if(toRead == 0) { //Timeout if(toRead == 0) { //Timeout
delay(100); delay(100);
toRead = data.readBytes(_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen)); toRead = data.readBytes(_buffer + _bufferLen, (_bufferSize - _bufferLen));
if(toRead == 0) { //Timeout if(toRead == 0) { //Timeout
_error = UPDATE_ERROR_STREAM; _error = UPDATE_ERROR_STREAM;
_currentAddress = (_startAddress + _size); _currentAddress = (_startAddress + _size);
@ -355,7 +363,7 @@ size_t UpdaterClass::writeStream(Stream &data) {
} }
} }
_bufferLen += toRead; _bufferLen += toRead;
if((_bufferLen == remaining() || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer()) if((_bufferLen == remaining() || _bufferLen == _bufferSize) && !_writeBuffer())
return written; return written;
written += toRead; written += toRead;
yield(); yield();

View File

@ -116,8 +116,8 @@ class UpdaterClass {
if(_bufferLen + available > remaining()){ if(_bufferLen + available > remaining()){
available = remaining() - _bufferLen; available = remaining() - _bufferLen;
} }
if(_bufferLen + available > FLASH_SECTOR_SIZE) { if(_bufferLen + available > _bufferSize) {
size_t toBuff = FLASH_SECTOR_SIZE - _bufferLen; size_t toBuff = _bufferSize - _bufferLen;
data.read(_buffer + _bufferLen, toBuff); data.read(_buffer + _bufferLen, toBuff);
_bufferLen += toBuff; _bufferLen += toBuff;
if(!_writeBuffer()) if(!_writeBuffer())
@ -151,7 +151,8 @@ class UpdaterClass {
bool _async; bool _async;
uint8_t _error; uint8_t _error;
uint8_t *_buffer; uint8_t *_buffer;
size_t _bufferLen; size_t _bufferLen; // amount of data written into _buffer
size_t _bufferSize; // total size of _buffer
size_t _size; size_t _size;
uint32_t _startAddress; uint32_t _startAddress;
uint32_t _currentAddress; uint32_t _currentAddress;