ESP8266 Webserver Project

Moderator: Sprite_tm

User avatar
By Israel Lot
#15278 You can save data in flash. I use the bottom sectors of the chip so it does't get erased on a reprogram as well.
spi_flash_write and spi_flash_read does what you need.
The only concern is that reading and writing to flash has to be 4byte aligned.
Last edited by Israel Lot on Wed Apr 22, 2015 9:46 am, edited 1 time in total.
User avatar
By Israel Lot
#15285 First choose a sector of the flash memory to write to.
Doing some tests I realized the last two sectors are used for storing wifi config, so get the Last - 4 for example.
The 515k flash has 0x80 sectors, so:
Code: Select all#define CONFIG_SECTOR 0x80-4


Define your struct:
Code: Select alltypedef struct {
   uint32_t A;
   char B[256];
} config;


In ESP you have to erase the whole sector, so:
Code: Select allspi_flash_erase_sector(CONFIG_SECTOR);


To write , you need the sector address.
Code: Select all #define CONFIG_ADDRESS = ( CONFIG_SECTOR * 4096 )

Note that the sector size in ESP' flash is usually 4096

To write, simply :
Code: Select allspi_flash_write(CONFIG_ADDRESS ,(uint32 *)(some_config_pointer),sizeof(config));


And to read :
Code: Select allspi_flash_read(CONFIG_ADDRESS ,(uint32 *)(some_config_pointer),sizeof(config));


Note the need to cast to uint32 pointer and always remember that size should be 4byte aligned.