PlatformIO Community

Advanced scripting debugging

Hi everyone.

I’m trying to dig into advanced scripts, and I’m trying to achieve a few step sequence for my project as post action:

  1. execute pio run -t buildfs for each environment.
  2. copy out the folders generated for each env from .pio/build BUT keep only the .bin files in them into one main output/build folder.
  3. execute esptool.py to merge firmware and filesystem binaries into one in every folder copied out above.

Since I never used scripts before, I’m wondering how is it possible to debug a script in work, without executing pio run every time? Where can I find more information about what can be accessed "env" and "projenv", beside of this blog? (here I’m referring to relative paths for example)
Do you have some hints for these steps from above?

Thank you very much,
Sorry if I’m duplicating a topic.

Hello again. I went through some example scripts, like tasmota’s scripts, and I realized that there a lot of APIs, which are not documented. So with a more python-based solution I came up with this script.

Import("env")
import os
import shutil
import esptool

def post_program_action(source, target, env):

    targetfile = os.path.abspath(target[0].get_abspath())
    filename = os.path.basename(targetfile)
    startpath = os.path.dirname(targetfile)
    basedir = os.path.basename(startpath)
    destpath = os.path.normpath(os.path.join(startpath, '../../../../build', basedir))

    print("\nCopying " + filename + " file to the build directory...\n")
    print("Target file: " + targetfile)
    print("Destination directory: " + destpath)

    # create directories if they don't exist
    if not os.path.exists(destpath):
        os.makedirs(destpath)

    # copy the target file to the destination, if it exist
    if os.path.exists(targetfile):
        shutil.copy(targetfile, destpath)

    os.chdir(destpath)
    command = ['--chip', 'esp8266', 'merge_bin', '-o', 'combined.bin', '0x0', 'firmware.bin', '0x300000', 'littlefs.bin']
    print('Using esptool.py %s' % ' '.join(command))
    esptool.main(command)

env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", post_program_action)
env.AddPostAction("$BUILD_DIR/littlefs.bin", post_program_action)

This will grab your firmware.bin or littlefs.bin file from your workspace, and copy it to the destpath varaible’s location. It will also create directories, if they’re missing.

To combine the scripts, I ran pip install esptool, because the esptool.py file downloaded for ESP8266 seems to be older, and does not contain the merge_bin.

There is tool-esptool (older version, only used for ESP8266) and tool-esptoolpy (ESP32+ESP8266 tool, recent). That has has merge_bin no problems

>pio pkg exec -p “tool-esptoolpy” -c “esptool.py --help”
Using tool-esptoolpy@1.40201.0 package
usage: esptool [-h]
[–chip {auto,esp8266,esp32,esp32s2,esp32s3beta2,esp32s3,esp32c3,esp32c6beta,esp32h2beta1,esp32h2beta2,esp32c2}]
[–port PORT] [–baud BAUD] [–before {default_reset,usb_reset,no_reset,no_reset_no_sync}]
[–after {hard_reset,soft_reset,no_reset,no_reset_stub}] [–no-stub] [–trace]
[–override-vddsdio [{1.8V,1.9V,OFF}]] [–connect-attempts CONNECT_ATTEMPTS]
{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,merge_bin,get_security_info,version}

esptool.py v4.2.1 - Espressif chips ROM Bootloader Utility

positional arguments:
{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,merge_bin,get_security_info,version}

1 Like

Hmm, I didn’t know about this. Initally I was trying to execute %USERPROFILE%\.platformio\packages\framework-arduinoespressif8266\tools\esptool\esptool.py, and making it dynamic with the envar.

How would you add this pio command to the python script?

Teh API platform.get_package_dir() can be used, see here and here.

Ok, so for those who didn’t get this, first you have to define platform variable, which is platform = env.PioPlatform(), then you can get you can access more APIs, one of them can provide the location of esptool.py with this: print(platform.get_package_dir("tool-esptoolpy") or "", "esptool.py").

I still don’t know how to use this extra information. @maxgerhardt if you could make an example by modifying my function would be a HUGE help, thank you.