Skip to content

Commit a7846ae

Browse files
committed
update UpdaterClass to flashing SPIFFS image
1 parent 1e57e44 commit a7846ae

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

hardware/esp8266com/esp8266/cores/esp8266/Updater.cpp

+50-23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ UpdaterClass::UpdaterClass()
1919
, _size(0)
2020
, _startAddress(0)
2121
, _currentAddress(0)
22+
, _command(U_FLASH)
2223
{
2324
}
2425

@@ -30,17 +31,24 @@ void UpdaterClass::_reset() {
3031
_startAddress = 0;
3132
_currentAddress = 0;
3233
_size = 0;
34+
_command = U_FLASH;
3335
}
3436

35-
bool UpdaterClass::begin(size_t size){
37+
bool UpdaterClass::begin(size_t size, int command) {
3638
if(_size > 0){
3739
#ifdef DEBUG_UPDATER
3840
DEBUG_UPDATER.println("already running");
3941
#endif
4042
return false;
4143
}
4244

43-
if(size == 0){
45+
#ifdef DEBUG_UPDATER
46+
if (command == U_SPIFFS) {
47+
DEBUG_UPDATER.println("Update SPIFFS.");
48+
}
49+
#endif
50+
51+
if(size == 0) {
4452
_error = UPDATE_ERROR_SIZE;
4553
#ifdef DEBUG_UPDATER
4654
printError(DEBUG_UPDATER);
@@ -51,20 +59,33 @@ bool UpdaterClass::begin(size_t size){
5159
_reset();
5260
_error = 0;
5361

54-
//size of current sketch rounded to a sector
55-
uint32_t currentSketchSize = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
56-
//address of the end of the space available for sketch and update
57-
uint32_t updateEndAddress = (uint32_t)&_SPIFFS_start - 0x40200000;
58-
//size of the update rounded to a sector
59-
uint32_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
60-
//address where we will start writing the update
61-
uint32_t updateStartAddress = updateEndAddress - roundedSize;
62-
63-
//make sure that the size of both sketches is less than the total space (updateEndAddress)
64-
if(updateStartAddress < currentSketchSize){
65-
_error = UPDATE_ERROR_SPACE;
62+
uint32_t updateStartAddress = 0;
63+
if (command == U_FLASH) {
64+
//size of current sketch rounded to a sector
65+
uint32_t currentSketchSize = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
66+
//address of the end of the space available for sketch and update
67+
uint32_t updateEndAddress = (uint32_t)&_SPIFFS_start - 0x40200000;
68+
//size of the update rounded to a sector
69+
uint32_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
70+
//address where we will start writing the update
71+
updateStartAddress = updateEndAddress - roundedSize;
72+
73+
//make sure that the size of both sketches is less than the total space (updateEndAddress)
74+
if(updateStartAddress < currentSketchSize) {
75+
_error = UPDATE_ERROR_SPACE;
6676
#ifdef DEBUG_UPDATER
67-
printError(DEBUG_UPDATER);
77+
printError(DEBUG_UPDATER);
78+
#endif
79+
return false;
80+
}
81+
}
82+
else if (command == U_SPIFFS) {
83+
updateStartAddress = (uint32_t)&_SPIFFS_start - 0x40200000;
84+
}
85+
else {
86+
// unknown command
87+
#ifdef DEBUG_UPDATER
88+
DEBUG_UPDATER.println("Unknown update command.");
6889
#endif
6990
return false;
7091
}
@@ -74,6 +95,7 @@ bool UpdaterClass::begin(size_t size){
7495
_currentAddress = _startAddress;
7596
_size = size;
7697
_buffer = new uint8_t[FLASH_SECTOR_SIZE];
98+
_command = command;
7799

78100
return true;
79101
}
@@ -95,23 +117,28 @@ bool UpdaterClass::end(bool evenIfRemaining){
95117
return false;
96118
}
97119

98-
if(evenIfRemaining){
99-
if(_bufferLen > 0){
120+
if(evenIfRemaining) {
121+
if(_bufferLen > 0) {
100122
_writeBuffer();
101123
}
102124
_size = progress();
103125
}
104126

105-
eboot_command ebcmd;
106-
ebcmd.action = ACTION_COPY_RAW;
107-
ebcmd.args[0] = _startAddress;
108-
ebcmd.args[1] = 0x00000;
109-
ebcmd.args[2] = _size;
110-
eboot_command_write(&ebcmd);
127+
if (_command == U_FLASH) {
128+
eboot_command ebcmd;
129+
ebcmd.action = ACTION_COPY_RAW;
130+
ebcmd.args[0] = _startAddress;
131+
ebcmd.args[1] = 0x00000;
132+
ebcmd.args[2] = _size;
133+
eboot_command_write(&ebcmd);
111134

112135
#ifdef DEBUG_UPDATER
113136
DEBUG_UPDATER.printf("Staged: address:0x%08X, size:0x%08X\n", _startAddress, _size);
137+
}
138+
else if (_command == U_SPIFFS) {
139+
DEBUG_UPDATER.printf("SPIFFS: address:0x%08X, size:0x%08X\n", _startAddress, _size);
114140
#endif
141+
}
115142

116143
_reset();
117144
return true;

hardware/esp8266com/esp8266/cores/esp8266/Updater.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#define UPDATE_ERROR_SIZE 4
1212
#define UPDATE_ERROR_STREAM 5
1313

14+
#define U_FLASH 0
15+
#define U_SPIFFS 100
16+
1417
//#define DEBUG_UPDATER Serial1
1518

1619
class UpdaterClass {
@@ -20,7 +23,7 @@ class UpdaterClass {
2023
Call this to check the space needed for the update
2124
Will return false if there is not enough space
2225
*/
23-
bool begin(size_t size);
26+
bool begin(size_t size, int = U_FLASH);
2427

2528
/*
2629
Writes a buffer to the flash and increments the address
@@ -116,6 +119,7 @@ class UpdaterClass {
116119
size_t _size;
117120
uint32_t _startAddress;
118121
uint32_t _currentAddress;
122+
uint32_t _command;
119123
};
120124

121125
extern UpdaterClass Update;

0 commit comments

Comments
 (0)