Skip to content

Commit

Permalink
small flash config improvement plus more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tve committed Aug 20, 2015
1 parent dcadb70 commit cd07540
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 7 additions & 4 deletions user/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ FlashConfig flashDefault = {
"esp-link\0 ", // hostname
0, 0x00ffffff, 0, // static ip, netmask, gateway
0, // log mode
0, // swap uart (don't by default)
};

typedef union {
Expand Down Expand Up @@ -46,15 +47,14 @@ static void memDump(void *addr, int len) {
#endif

bool ICACHE_FLASH_ATTR configSave(void) {

FlashFull ff;
memset(&ff, 0, sizeof(ff));
memcpy(&ff, &flashConfig, sizeof(FlashConfig));
uint32_t seq = ff.fc.seq+1;
// erase secondary
uint32_t addr = FLASH_ADDR + (1-flash_pri)*FLASH_SECT;
if (spi_flash_erase_sector(addr>>12) != SPI_FLASH_RESULT_OK)
return false; // no harm done, give up
goto fail; // no harm done, give up
// calculate CRC
ff.fc.seq = seq;
ff.fc.magic = FLASH_MAGIC;
Expand All @@ -66,11 +66,11 @@ bool ICACHE_FLASH_ATTR configSave(void) {
// write primary with incorrect seq
ff.fc.seq = 0xffffffff;
if (spi_flash_write(addr, (void *)&ff, sizeof(ff)) != SPI_FLASH_RESULT_OK)
return false; // no harm done, give up
goto fail; // no harm done, give up
// fill in correct seq
ff.fc.seq = seq;
if (spi_flash_write(addr, (void *)&ff, sizeof(uint32_t)) != SPI_FLASH_RESULT_OK)
return false; // most likely failed, but no harm if successful
goto fail; // most likely failed, but no harm if successful
// now that we have safely written the new version, erase old primary
addr = FLASH_ADDR + flash_pri*FLASH_SECT;
flash_pri = 1-flash_pri;
Expand All @@ -83,6 +83,9 @@ bool ICACHE_FLASH_ATTR configSave(void) {
ff.fc.seq = seq;
spi_flash_write(addr, (void *)&ff, sizeof(uint32_t));
return true;
fail:
os_printf("*** Failed to save config ***\n");
return false;
}

void ICACHE_FLASH_ATTR configWipe(void) {
Expand Down
4 changes: 4 additions & 0 deletions user/config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef CONFIG_H
#define CONFIG_H

// Flash configuration settings. When adding new items always add them at the end and formulate
// them such that a value of zero is an appropriate default or backwards compatible. Existing
// modules that are upgraded will have zero in the new fields. This ensures that an upgrade does
// not wipe out the old settings.
typedef struct {
uint32_t seq; // flash write sequence number
uint16_t magic, crc;
Expand Down

0 comments on commit cd07540

Please sign in to comment.