PlatformIO Community

How can I make the "PlatformIO:Upload" button a "Program" button?


#1

I’m currently programming AVRs und using an usbasp as programmer for that. If I hit the “Upload” button the hex file gets uploaded, but the verification fails, because the flash is not erased when using the “upload” target. Using the “program” target does erase the flash and the verification is OK.
How can I make the “Upload” button be a “Program” button for AVRs? I don’t know, why I’d ever use the “upload” target with an AVR under normal circumstances. I tried specifying “targets” in the platformio.ini, but that doesn’t seem to do anything.


#2

You can try to add upload_flags = -e or even override upload command https://docs.platformio.org/en/latest/projectconf/advanced_scripting.html#custom-upload-tool


#3

The -e flag erases the EEPROM. I don’t want that. For testing I added this to my platformio.ini

extra_scripts = 
  pre:extra_script.py
  post:extra_script.py

with this being my extra_script.py:

from pprint import pprint
Import("env")

print "Current build targets", map(str, BUILD_TARGETS)

def before(source, target, env):
    print env.Dump('UPLOADERFLAGS')

def after(source, target, env):
    print env.Dump('UPLOADERFLAGS')

env.AddPreAction("buildprog", before)
env.AddPostAction("buildprog", after)

env.AddPreAction("upload", before)
env.AddPostAction("upload", after)

env.AddPreAction("program", before)
env.AddPostAction("program", after)

UPLOADERFLAGS looks like this for before upload, before program and after program:

[ '$UPLOAD_FLAGS',
'-p',
'$BOARD_MCU',
'-C',
'C:\\Users\\xxxxx\\.platformio\\packages\\tool-avrdude\\avrdude.conf',
'-c',
'$UPLOAD_PROTOCOL']

But for after upload it also contains the -D flag. So how do I remove that flag if it’s only there AFTER the upload has already been tried?


#4

It’s not just the -D,

before(["upload"], [".pioenvs/uno/firmware.hex"])
[ '-p',
'$BOARD_MCU',
'-C',
'/home/pfeerick/.platformio/packages/tool-avrdude/avrdude.conf',
'-c',
'$UPLOAD_PROTOCOL']

vs

after(["upload"], [".pioenvs/uno/firmware.hex"])
[ '-p',
'$BOARD_MCU',
'-C',
'/home/pfeerick/.platformio/packages/tool-avrdude/avrdude.conf',
'-c',
'$UPLOAD_PROTOCOL',
'-b',
'$UPLOAD_SPEED',
'-D',
'-P',
'"$UPLOAD_PORT"']

But the question should be more… does it matter… what is the actual command run? I’m pretty sure it’s the contents of UPLOADHEXCMD, being by default $UPLOADER $UPLOADERFLAGS -U flash:w:$SOURCES:i

Thus, personally out of sheer lazyness, I would just replace UPLOADHEXCMD in it’s entirety… as that is the command line for uploading the hex binary… (UPLOADEEPCMD is the EEPROM counterpart)… as opposed to fighting with the fact that the pre script runs before main script of the platform, which is probably adding the extra parameters (via the UPLOADERFLAGS variable)… i.e. -D is done here, -P [port] is done here.

i.e. this is what I have for uploading with my Atmel ICE programmer… and I ignore the ‘programmer’ option entirely and use ‘upload’

Import("env")
env.Replace(UPLOADHEXCMD='$UPLOADER -p atmega328p -C /home/pfeerick/.platformio/packages/tool-avrdude/avrdude.conf -c atmelice_isp -U flash:w:$SOURCES:i')

with this in the platformio.ini

extra_scripts = upload_atmel-ice.py
upload_protocol = custom

In your case, you could capture the normal command line for uploading with verbose upload and then use that instead, with alterations as needed… i.e.

env.Replace(UPLOADHEXCMD='$UPLOADER -p atmega328p -C /home/pfeerick/.platformio/packages/tool-avrdude/avrdude.conf -c arduino -b 115200 -P "/dev/ttyUSB0" -U flash:w:$SOURCES:i')

Also do PROGRAMHEXCMD if you want both to react exactly the same :wink: