You’re right, the problem is that it injects the flag way too early. “Verbose Upload” shows
“c:\users\max\appdata\local\programs\python\python38\python.exe” “C:\Users\Max.platformio\packages\tool-esptoolpy@1.30000.201119\esptool.py” --erase-all --before default_reset --after hard_reset --chip esp8266 --port “COM4” --baud 115200 write_flash 0x0 .pio\build\nodemcuv2\firmware.bin
but the --erase-all
must come after write_flash
.
Which means the only way I can see it work is by duplicating the original upload logic using an extra_script that injects the flag in the right position.
If I do
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
framework = arduino
upload_protocol = custom
extra_scripts = erase_before_upload.py
With the erase_before_upload.py
file being
from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild,
Builder, Default, DefaultEnvironment)
from os.path import join
Import("env")
platform = env.PioPlatform()
env.AutodetectUploadPort()
def get_esptoolpy_reset_flags(resetmethod):
# no dtr, no_sync
resets = ("no_reset_no_sync", "soft_reset")
if resetmethod == "nodemcu":
# dtr
resets = ("default_reset", "hard_reset")
elif resetmethod == "ck":
# no dtr
resets = ("no_reset", "soft_reset")
return ["--before", resets[0], "--after", resets[1]]
env.Replace(
UPLOADER=join(
platform.get_package_dir("tool-esptoolpy") or "", "esptool.py"),
UPLOADERFLAGS=[
"--chip", "esp8266",
"--port", '"$UPLOAD_PORT"',
"--baud", "$UPLOAD_SPEED",
"write_flash",
"--erase-all"
],
UPLOADCMD='"$PYTHONEXE" "$UPLOADER" $UPLOADERFLAGS 0x0 $SOURCE'
)
for image in env.get("FLASH_EXTRA_IMAGES", []):
env.Append(UPLOADERFLAGS=[image[0], env.subst(image[1])])
if "uploadfs" in COMMAND_LINE_TARGETS:
env.Replace(
UPLOADERFLAGS=[
"--chip", "esp8266",
"--port", '"$UPLOAD_PORT"',
"--baud", "$UPLOAD_SPEED",
"write_flash",
"--erase-all",
"$FS_START"
],
UPLOADCMD='"$PYTHONEXE" "$UPLOADER" $UPLOADERFLAGS $SOURCE',
)
env.Prepend(
UPLOADERFLAGS=get_esptoolpy_reset_flags(env.subst("$UPLOAD_RESETMETHOD"))
)
upload_actions = [
env.VerboseAction(env.AutodetectUploadPort,
"Looking for upload port..."),
env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")
]
I get as a verbose upload
"c:\users\max\appdata\local\programs\python\python38\python.exe" "C:\Users\Max\.platformio\packages\tool-esptoolpy@1.30000.201119\esptool.py" --before default_reset --after hard_reset --chip esp8266 --port "COM4" --baud 115200 write_flash --erase-all 0x0 .pio\build\nodemcuv2\firmware.bin
esptool.py v3.0
Serial port COM4
Connecting....
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 60:01:94:0d:d6:c2
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Erasing flash (this may take a while)...
Chip erase completed successfully in 9.0s
Compressed 264240 bytes to 194515...
Writing at 0x00000000... (8 %)
Writing at 0x00004000... (16 %)
Writing at 0x00008000... (25 %)
Writing at 0x0000c000... (33 %)
Writing at 0x00010000... (41 %)
Writing at 0x00014000... (50 %)
Writing at 0x00018000... (58 %)
Writing at 0x0001c000... (66 %)
Writing at 0x00020000... (75 %)
Writing at 0x00024000... (83 %)
Writing at 0x00028000... (91 %)
Writing at 0x0002c000... (100 %)
Wrote 264240 bytes (194515 compressed) at 0x00000000 in 17.9 seconds (effective 118.3 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...
============================================= [SUCCESS] Took 32.63 seconds =============================================
looks like it’s working.