1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-02 14:22:55 +03:00

New Update library, example, upload and more

Proper error handling in the uploading python script
Much faster OTA example sketch with better results
New Update class that simplifies updating the firmware from any source
Updated Esp.updateSketch() to use the new class
This commit is contained in:
John Doe
2015-07-03 13:59:11 +03:00
committed by Ivan Grokhotkov
parent 86cf9b2c4f
commit 6f2069deac
7 changed files with 446 additions and 130 deletions

119
cores/esp8266/Updater.h Normal file
View File

@ -0,0 +1,119 @@
#ifndef ESP8266UPDATER_H
#define ESP8266UPDATER_H
#include "Arduino.h"
#include "flash_utils.h"
#define UPDATE_ERROR_OK 0
#define UPDATE_ERROR_WRITE 1
#define UPDATE_ERROR_ERASE 2
#define UPDATE_ERROR_SPACE 3
#define UPDATE_ERROR_SIZE 4
#define UPDATE_ERROR_STREAM 5
class UpdaterClass {
public:
UpdaterClass();
/*
Call this to check and erase the space needed for the update
Will return false if there is not enough space
Or the erase of the flash failed
*/
bool begin(size_t size);
/*
Writes a buffer to the flash and increments the address
Returns the amount written
*/
size_t write(uint8_t *data, size_t len);
/*
Writes the remaining bytes from the Sream to the flash
Uses readBytes() and sets UPDATE_ERROR_STREAM on timeout
Returns the bytes written
Should be equal to the remaining bytes when called
Usable for slow streams like Serial
*/
size_t writeStream(Stream &data);
/*
If all bytes are written
this call will write the config to eboot
and return true
If there is already an update running but is not finished
or there is an error
this will clear everything and return false
the last error is available through getError()
*/
bool end();
/*
Prints the last error to an output stream
*/
void printError(Stream &out);
//Helpers
uint8_t getError(){ return _error; }
void clearError(){ _error = UPDATE_ERROR_OK; }
bool hasError(){ return _error != UPDATE_ERROR_OK; }
bool isRunning(){ return _size > 0; }
bool isFinished(){ return hasError()?true:(_currentAddress == (_startAddress + _size)); }
size_t size(){ return _size; }
size_t progress(){ return _currentAddress - _startAddress; }
size_t remaining(){ return hasError()?0:(size() - progress()); }
/*
Template to write from objects that expose
available() and read(uint8_t*, size_t) methods
faster than the readStream method
writes only what is available
*/
template<typename T>
size_t write(T &data){
size_t written = 0;
if(hasError())
return 0;
size_t available = data.available();
while(available){
if((_bufferLen + available) > remaining()){
available = (remaining() - _bufferLen);
}
if((_bufferLen + available) > FLASH_SECTOR_SIZE){
size_t toBuff = FLASH_SECTOR_SIZE - _bufferLen;
data.read(_buffer + _bufferLen, toBuff);
_bufferLen += toBuff;
if(!_writeBuffer())
return written;
written += toBuff;
} else {
data.read(_buffer + _bufferLen, available);
_bufferLen += available;
written += available;
if(_bufferLen == remaining()){
if(!_writeBuffer()){
return written;
}
}
}
if(remaining() == 0)
return written;
yield();
available = data.available();
}
return written;
}
private:
uint8_t *_buffer;
uint8_t _error;
size_t _bufferLen;
size_t _size;
uint32_t _startAddress;
uint32_t _currentAddress;
bool _writeBuffer();
};
extern UpdaterClass Update;
#endif