Skip to content

Flash SPIFFS over the air (OTA) #802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 50 additions & 23 deletions hardware/esp8266com/esp8266/cores/esp8266/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ UpdaterClass::UpdaterClass()
, _size(0)
, _startAddress(0)
, _currentAddress(0)
, _command(U_FLASH)
{
}

Expand All @@ -30,17 +31,24 @@ void UpdaterClass::_reset() {
_startAddress = 0;
_currentAddress = 0;
_size = 0;
_command = U_FLASH;
}

bool UpdaterClass::begin(size_t size){
bool UpdaterClass::begin(size_t size, int command) {
if(_size > 0){
#ifdef DEBUG_UPDATER
DEBUG_UPDATER.println("already running");
#endif
return false;
}

if(size == 0){
#ifdef DEBUG_UPDATER
if (command == U_SPIFFS) {
DEBUG_UPDATER.println("Update SPIFFS.");
}
#endif

if(size == 0) {
_error = UPDATE_ERROR_SIZE;
#ifdef DEBUG_UPDATER
printError(DEBUG_UPDATER);
Expand All @@ -51,20 +59,33 @@ bool UpdaterClass::begin(size_t size){
_reset();
_error = 0;

//size of current sketch rounded to a sector
uint32_t currentSketchSize = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
//address of the end of the space available for sketch and update
uint32_t updateEndAddress = (uint32_t)&_SPIFFS_start - 0x40200000;
//size of the update rounded to a sector
uint32_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
//address where we will start writing the update
uint32_t updateStartAddress = updateEndAddress - roundedSize;

//make sure that the size of both sketches is less than the total space (updateEndAddress)
if(updateStartAddress < currentSketchSize){
_error = UPDATE_ERROR_SPACE;
uint32_t updateStartAddress = 0;
if (command == U_FLASH) {
//size of current sketch rounded to a sector
uint32_t currentSketchSize = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
//address of the end of the space available for sketch and update
uint32_t updateEndAddress = (uint32_t)&_SPIFFS_start - 0x40200000;
//size of the update rounded to a sector
uint32_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
//address where we will start writing the update
updateStartAddress = updateEndAddress - roundedSize;

//make sure that the size of both sketches is less than the total space (updateEndAddress)
if(updateStartAddress < currentSketchSize) {
_error = UPDATE_ERROR_SPACE;
#ifdef DEBUG_UPDATER
printError(DEBUG_UPDATER);
printError(DEBUG_UPDATER);
#endif
return false;
}
}
else if (command == U_SPIFFS) {
updateStartAddress = (uint32_t)&_SPIFFS_start - 0x40200000;
}
else {
// unknown command
#ifdef DEBUG_UPDATER
DEBUG_UPDATER.println("Unknown update command.");
#endif
return false;
}
Expand All @@ -74,6 +95,7 @@ bool UpdaterClass::begin(size_t size){
_currentAddress = _startAddress;
_size = size;
_buffer = new uint8_t[FLASH_SECTOR_SIZE];
_command = command;

return true;
}
Expand All @@ -95,23 +117,28 @@ bool UpdaterClass::end(bool evenIfRemaining){
return false;
}

if(evenIfRemaining){
if(_bufferLen > 0){
if(evenIfRemaining) {
if(_bufferLen > 0) {
_writeBuffer();
}
_size = progress();
}

eboot_command ebcmd;
ebcmd.action = ACTION_COPY_RAW;
ebcmd.args[0] = _startAddress;
ebcmd.args[1] = 0x00000;
ebcmd.args[2] = _size;
eboot_command_write(&ebcmd);
if (_command == U_FLASH) {
eboot_command ebcmd;
ebcmd.action = ACTION_COPY_RAW;
ebcmd.args[0] = _startAddress;
ebcmd.args[1] = 0x00000;
ebcmd.args[2] = _size;
eboot_command_write(&ebcmd);

#ifdef DEBUG_UPDATER
DEBUG_UPDATER.printf("Staged: address:0x%08X, size:0x%08X\n", _startAddress, _size);
}
else if (_command == U_SPIFFS) {
DEBUG_UPDATER.printf("SPIFFS: address:0x%08X, size:0x%08X\n", _startAddress, _size);
#endif
}

_reset();
return true;
Expand Down
6 changes: 5 additions & 1 deletion hardware/esp8266com/esp8266/cores/esp8266/Updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#define UPDATE_ERROR_SIZE 4
#define UPDATE_ERROR_STREAM 5

#define U_FLASH 0
#define U_SPIFFS 100

//#define DEBUG_UPDATER Serial1

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

/*
Writes a buffer to the flash and increments the address
Expand Down Expand Up @@ -116,6 +119,7 @@ class UpdaterClass {
size_t _size;
uint32_t _startAddress;
uint32_t _currentAddress;
uint32_t _command;
};

extern UpdaterClass Update;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ localIP KEYWORD2
subnetMask KEYWORD2
gatewayIP KEYWORD2
SSID KEYWORD2
psk KEYWORD2
BSSID KEYWORD2
RSSI KEYWORD2
encryptionType KEYWORD2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase, int32_t ch
return status();
}

int ESP8266WiFiClass::begin()
{
ETS_UART_INTR_DISABLE();
wifi_station_connect();
ETS_UART_INTR_ENABLE();

if(!_useStaticIp)
wifi_station_dhcpc_start();
return status();
}

uint8_t ESP8266WiFiClass::waitForConnectResult(){
if ((wifi_get_opmode() & 1) == 0)//1 and 3 have STA enabled
return WL_DISCONNECTED;
Expand Down Expand Up @@ -366,6 +377,13 @@ char* ESP8266WiFiClass::SSID()
return reinterpret_cast<char*>(conf.ssid);
}

const char* ESP8266WiFiClass::psk()
{
static struct station_config conf;
wifi_station_get_config(&conf);
return reinterpret_cast<const char*>(conf.password);
}

uint8_t* ESP8266WiFiClass::BSSID(void)
{
static struct station_config conf;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class ESP8266WiFiClass
int begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, uint8_t bssid[6] = NULL);
int begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, uint8_t bssid[6] = NULL);

// Use sdk config to connect.
int begin();


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

/*
* Return the current pre shared key associated with the network
*
* return: psk string
*/
const char* psk();

/*
* Return the current bssid / mac associated with the network if configured
*
Expand Down
Loading