Skip to content

Commit 119512d

Browse files
committed
Merge pull request #802 from pgollor/SPIFFS-OTA
Flash SPIFFS over the air (OTA)
2 parents 96b0dde + 5cbff32 commit 119512d

File tree

9 files changed

+526
-47
lines changed

9 files changed

+526
-47
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;

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ localIP KEYWORD2
4141
subnetMask KEYWORD2
4242
gatewayIP KEYWORD2
4343
SSID KEYWORD2
44+
psk KEYWORD2
4445
BSSID KEYWORD2
4546
RSSI KEYWORD2
4647
encryptionType KEYWORD2

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch
145145
return status();
146146
}
147147

148+
int ESP8266WiFiClass::begin()
149+
{
150+
ETS_UART_INTR_DISABLE();
151+
wifi_station_connect();
152+
ETS_UART_INTR_ENABLE();
153+
154+
if(!_useStaticIp)
155+
wifi_station_dhcpc_start();
156+
return status();
157+
}
158+
148159
uint8_t ESP8266WiFiClass::waitForConnectResult(){
149160
if ((wifi_get_opmode() & 1) == 0)//1 and 3 have STA enabled
150161
return WL_DISCONNECTED;
@@ -366,6 +377,13 @@ char* ESP8266WiFiClass::SSID()
366377
return reinterpret_cast<char*>(conf.ssid);
367378
}
368379

380+
const char* ESP8266WiFiClass::psk()
381+
{
382+
static struct station_config conf;
383+
wifi_station_get_config(&conf);
384+
return reinterpret_cast<const char*>(conf.password);
385+
}
386+
369387
uint8_t* ESP8266WiFiClass::BSSID(void)
370388
{
371389
static struct station_config conf;

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class ESP8266WiFiClass
5858
int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, uint8_t bssid[6] = NULL);
5959
int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, uint8_t bssid[6] = NULL);
6060

61+
// Use sdk config to connect.
62+
int begin();
63+
6164

6265
/* Wait for Wifi connection to reach a result
6366
* returns the status reached or disconnect if STA is off
@@ -172,6 +175,13 @@ class ESP8266WiFiClass
172175
*/
173176
char* SSID();
174177

178+
/*
179+
* Return the current pre shared key associated with the network
180+
*
181+
* return: psk string
182+
*/
183+
const char* psk();
184+
175185
/*
176186
* Return the current bssid / mac associated with the network if configured
177187
*

0 commit comments

Comments
 (0)