Discuss here different C compiler set ups, and compiling executables for the ESP8266

User avatar
By sshakuf
#10147 Hi,

i would like to save data to the flash, so i can have this data even if the power was off.
i tried to use spi_flash_write but i got an error

Code: Select all 
    char ssid[32] = SSID;
    char test[32];

    SpiFlashOpResult result = spi_flash_write(0x40200000, (uint32 *)&ssid[0], 4);
    os_printf("Write result - %d\n", result);                                        // i get 1
    result = spi_flash_read(0x40200000, (uint32 *)&test[0], 4);
    os_printf("spi_flash_read - %s", test);                                          // no string...
    os_printf("   read result - %d\n", result);                                     // i get 1


does anyone know how/if i can do it ?

Thanks
User avatar
By joostn
#10208 You shouldn't use 0x40200000; instead use the offset relative to the start of the flash area. i.e. 0x0000 is the first address of the flash.

Also you have to erase a flash block first before you can write to it.

Keep in mind that flashing to address 0x00 will overwrite your firmware. Blocks at the end of the flash are available, but the last 4 blocks (normally 0x7c000 - 0x7ffff) are used by the ESP libs. So you can use block 7b at 0x7b000
User avatar
By sshakuf
#10248 Thank you, but I have update the code and still get an error

Code: Select allstruct plug_saved_param {
    uint8_t status;
    uint8_t pad[3];
};

#define PRIV_PARAM_START_SEC        0x7b
#define PRIV_PARAM_SAVE     0

    spi_flash_erase_sector(PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE);
    os_printf("\nerase\n");

    spi_flash_write((PRIV_PARAM_START_SEC + PRIV_PARAM_SAVE) * SPI_FLASH_SEC_SIZE,
                (uint32 *)&plug_param, sizeof(struct plug_saved_param));


here is the output:

erase
Fatal exception (9):
epc1=0x4000432b, epc2=0x00000000, epc3=0x00000000, excvaddr=0x3ffffc17, depc=0x00000000
User avatar
By kenn
#10254 just a guess (my C's not strong and I'm away from my dev system) but try the spi_flash_erase_sector first with a literal address (as a uint16) . Maybe the defines need to be cast to uint16.