How to upload esp8266 sketch with configuration of Erase flash

Hello

I’m trying to mimic the Arduino IDE option provided in Erase flash, Witch gives 3 different options :

  • Only Sketch
  • Sketch + WiFi Settings
  • All Flash Contents

I could found the corresponding lines in ESP8266/boards.txt. But couldn’t know how to add them to platformio.ini

I don’t know if this is Ok or not. please correct me.

build_flags = 
  -Tnodemcuv2.menu.wipe.sdk
  -Tnodemcuv2.menu.wipe.sdk.upload.erase_cmd

I’m using nodemcuv2.

Thanks

Close, but also not even close… :confounded: those are just the entries which add the menu option for erase…

You do want these lines…

but more specifically you want what’s after the upload.erase_cmd for the specific one you want… either sketch only (so no erase), Sketch + WiFi Settings (erase region rfcal), or erase all (erase flash).

Once you know that, you want to somehow add that to the upload command… there are two possible ways, not sure which one will actually work.

The easier option (if it works) is upload_flags = erase_region "{build.rfcal_addr}" 0x4000 or upload_flags = erase_flash as needed. However, since anything specified after upload_flags will be added to the end of uploader command, I’m concerned it may not work. So, Plan B is to use an extra_script based on the custom upload tool example to edit the upload command…

Import("env")

ERASE_CMD="erase_flash"
;ERASE_CMD="erase_region "{build.rfcal_addr}" 0x4000"

upload_protocol = env.subst("$UPLOAD_PROTOCOL")

if upload_protocol == "espota":
    env.Replace(UPLOADCMD="$UPLOADER $UPLOADERFLAGS $ERASE_CMD $SOURCE")
elif upload_protocol == "esptool":
    env.Replace(UPLOADCMD='"$PYTHONEXE" "$UPLOADER" $UPLOADERFLAGS $ERASE_CMD $SOURCE')

… not forgetting to also add the following to your platformio.ini

`extra_scripts = extra_script.py

… assuming you called it extra_script.py, and that it’s located in the root folder of your project, i.e. next to the platformio.ini.

Note, I don’t have time to test this atm, so it may break… but it should get you started.

I don’t see why this can’t be added as a standard platformio.ini parameter in the future… who wants to volunteer to add it? :laughing: (thinking since he said it he’ll probably do it…)

1 Like

Thanks for this advanced starting point. @pfeerick

I’ve discovered a bit deeper based on your script :

We should extract “{build.rfcal_addr}” from the same source :

So replacing it with the actual address.

That follows the esptool command doc :

My platformio.ini :

[env:nodemcuv2]

platform = espressif8266@2.3.1

board = nodemcuv2

framework = arduino

build_flags =

-Wl,-Teagle.flash.4m1m.ld

-D BEARSSL_SSL_BASIC

lib_deps = Blynk

monitor_speed = 115200

upload_speed = 921600

upload_flags =

–erase_region 0x3FC000 0x4000

But gives me this error :

CURRENT: upload_protocol = esptool
Looking for upload port…
Auto-detected: COM3
Uploading .pio\build\nodemcuv2\firmware.bin
usage: esptool [-h] [–chip {auto,esp8266,esp32}] [–port PORT] [–baud BAUD]
[–before {default_reset,no_reset,no_reset_no_sync}]
[–after {hard_reset,soft_reset,no_reset}] [–no-stub]
[–trace] [–override-vddsdio [{1.8V,1.9V,OFF}]]
{load_ram,dump_mem,read_mem,write_mem,write_flash,run,image_info,make_image,elf2image,read_mac,chip_id,flash_id,read_flash_status,write_flash_status,read_flash,verify_flash,erase_flash,erase_region,version}

esptool: error: argument operation: invalid choice: ‘–erase_region 0x3FC000 0x4000’ (choose from ‘load_ram’, ‘dump_mem’, ‘read_mem’, ‘write_mem’, ‘write_flash’, ‘run’, ‘image_info’, ‘make_image’, ‘elf2image’, ‘read_mac’, ‘chip_id’, ‘flash_id’, ‘read_flash_status’, ‘write_flash_status’, ‘read_flash’, ‘verify_flash’, ‘erase_flash’, ‘erase_region’, ‘version’)
*** [upload] Error 2

I guess the issue comes out at over-riding options ? Or am I doing it wrong ?

By using extra_script.py as you mentioned, I’ve got another error :

my extra_script.py :

Import(“env”)

ERASE_CMD=“erase_flash”

ERASE_CMD=“erase_region 0x3FC000 0x4000”

upload_protocol = env.subst(“$UPLOAD_PROTOCOL”)

if upload_protocol == “espota”:
env.Replace(UPLOADCMD=“$UPLOADER $UPLOADERFLAGS $ERASE_CMD $SOURCE”)
elif upload_protocol == “esptool”:
env.Replace(UPLOADCMD=‘“$PYTHONEXE” “$UPLOADER” $UPLOADERFLAGS $ERASE_CMD $SOURCE’)

error msg :

Uploading .pio\build\nodemcuv2\firmware.bin
usage: esptool write_flash [-h] [–erase-all]
[–flash_freq {keep,40m,26m,20m,80m}]
[–flash_mode {keep,qio,qout,dio,dout}]
[–flash_size FLASH_SIZE]
[–spi-connection SPI_CONNECTION] [–no-progress]
[–verify] [–encrypt]
[–ignore-flash-encryption-efuse-setting]
[–compress | --no-compress]
[ …]
esptool write_flash: error: argument : Address “.pio\build\nodemcuv2\firmware.bin” must be a number
*** [upload] Error 2