UDPI using upload_command with multiple lines

I’m using this command in platform.ini to program UDPI and it works.

upload_command = pymcuprog $UPLOAD_FLAGS write --filename $SOURCE --verify

I need to do a few lines of pymcuprog, but upload_command is only a single line.
For example, I’d like to run these 3 lines

  1. pymcuprog $UPLOAD_FLAGS erase -chip-erase-locked-device
  2. pymcuprog $UPLOAD_FLAGS write --filename $SOURCE --verify
  3. pymcuprog write -m lockbits -l 0

I tried creating one long upload_command using &&, but PYMCUPROG is not handling it very well. It might need a delay between lines.

Seems like you want one pre-action before uploading (erasing locked device) and one post-action after uploading (locking device again). Hook the before + after events for the upload task and execute your pymcuprog commands there. See

https://docs.platformio.org/en/latest/scripting/actions.html

You can arbitrarily time.sleep() after the commands you’ve executed.

Thanks. After looking at that, not sure what to do. There are no examples on my machine.

do I need to create 2 scripts here? And do I need to write them in Python?

Well but I’ve just linked you to two examples.

You can write one python script, that is being referenced in the platformio.ini, that will register these actions.

Import("env")

def before_upload(source, target, env):
    print("before_upload")
    # execute pymcuprog $UPLOAD_FLAGS erase -chip-erase-locked-device
    # e.g. env.Execute()

def after_upload(source, target, env):
    print("after_upload")
    # execute pymcuprog write -m lockbits -l 0 
    # e.g. env.Execute()

env.AddPreAction("upload", before_upload)
env.AddPostAction("upload", after_upload)

That’s great, thank you so much. Got it working.

Yeah, I couldn’t find any upload commands or pymcuprog examples.