Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By piersfinlayson
#42822
OldBikerPete wrote:Great. Thank you for the answers.
Responding to answers in the order they were given.
if I do esptool.py erase_flash, will it erase ALL the flash, including the Arduino bootloader?

But the D1 mini has 1024Kb program flash. Where will the WiFi config be stored in that case? Still in the last 0x4000 bytes of program flash? IE at address 0xFc000 ? What sector number would that be?


Yes, erase_flash will blow away the entire flash.

The esp8266 SDK (which the arduino framework is based on, will always write its own configuration to the last 0x4000 of the actual flash chip itself. (Wifi config starts at 0x2000 before the end.) However, it doesn't actually calculate the flash chip size and then subtract 0x4000. Instead it reads a certain location on the flash where the size itself has been written. If this hasn't been written it assumes it's a 512KB flash, and therefore the location is 0x7c000-0x7ffff.

So, if the D1-mini flash has been properly prepared as a 4MB flash (all my d1-minis are 4MB) the location will be 0x3fc000-0x3fc000.

If you don't want to completely erase the flash, what I'd do is read the most likely locations using esptool.py:

Code: Select allesptool.py read_flash 0x7c000 0x4000 flash.bin


And then use hexedit (on linux, something else on windows) to look inside flash.bin and see if it's the right location. Remember wifi starts at 0x2000 in.

The most likely locations for SDK info are 0x7c000, 0xfc000 and 0x3fc000. (Or 0x7e000, 0xfe000, 0x3fe000 for the wifi stuff specifically.)

As the other poster says you can then clear this with spi_flash_erase_sector(location / 0x1000); (This erases a sector, so 0x1000 bytes.)
User avatar
By OldBikerPete
#42874 Ok. In response to the answers offered, I chased through the ESP8266 folder in the Arduino install folder for the strings in the answers offered. This resulted in me adding the following as the very first statement in setup() to be executed.

WiFi.persistent(false); // Forget once-known connections

ESP.flashEraseSector(0x7c); // Erase remembered connection info.
ESP.flashEraseSector(0x7d); // Erase remembered connection info.
ESP.flashEraseSector(0x7e); // Erase remembered connection info.
ESP.flashEraseSector(0x7f); // Erase remembered connection info.
ESP.flashEraseSector(0xfc); // Erase remembered connection info.
ESP.flashEraseSector(0xfd); // Erase remembered connection info.
ESP.flashEraseSector(0xfe); // Erase remembered connection info.
ESP.flashEraseSector(0xff); // Erase remembered connection info.

I expect that the WiFi.persistent() call would prevent the caching of connection info in the future and I expected that the shotgun-like erasure of sectors following would erase information cached in the past.

After compiling and downloading this new sketch, the debug window still shows that the ESP8266 in my D1 mini is still merrily searching for my secure access point by name.
Anyone else care to have a crack?
User avatar
By OldBikerPete
#42879 Thank you all.
I placed the statement disabling auto-connect in user_init();

I placed these statements as the first executed in setup();

WiFi.persistent(false); // Do not memorise new connections
// ESP.flashEraseSector(0x3fe); // Erase remembered connection info. (Only do this once).

The sketch has now stopped trying to connect to my secure access point and hopefully will now obey instructions telling the D1 mini to be a web server on an unsecured access point and then reconfigure when told.

Ain't programming FUN!
Peter.
User avatar
By OldBikerPete
#44507 OK. This functionality is now working as I hoped. I'm happy to post the relevant code if anyone wants to use it.

At the moment the SSID, Passphrase and WiFi mode information is all being stored in a file on the SD card shield of the D1 mini. I'd like to store that either amongst or just below the 0x4000 bytes at the top of program memory using ESP.flashRead() and ESP.flashWrite(). It's not an operation that would be performed often so there shouldn't be any problem with EEPROM write cycle lifetime.

Can anyone tell me how to or where to do that without clashing with the sketch or the SDK configuration?