PlatformIO Community

New target to upload bootloader

I am still quite new in the IOT world and Arduino. I had to upload few bootloader for Arduino pro or Atmel 328p.

Why not having a target to make this upload using the data given in the board json ? Do I miss something.

Thanks for your input.

This is one-time operation and you can use Arduino IDE for that. If you want to have custom target for that, please check

One time per board. But ok I understand. I already read this thread and end up with this code:

import sys
Import("env")

print env.Dump()

def miss(variable):
    sys.stderr.write(
        "Error: Please specify `%s` in the board config json " % variable
    )
    exit(1)

if "BOARD" in env:
    if "bootloader.low_fuses" in env.BoardConfig():
        env.Replace(
            LOW_FUSES = env.BoardConfig().get("bootloader.low_fuses")
        )
    else:
        miss('low_fuses')
    if "bootloader.high_fuses" in env.BoardConfig():
        env.Replace(
            HIGH_FUSES = env.BoardConfig().get("bootloader.high_fuses")
        )
    else:
        miss('high_fuses')
    if "bootloader.extended_fuses" in env.BoardConfig():
        env.Replace(
            EXTENDED_FUSES = env.BoardConfig().get("bootloader.extended_fuses")
        )
    else:
        miss('extended_fuses')
    if "bootloader.unlock_bits" in env.BoardConfig():
        env.Replace(
            UNLOCK_BITS = env.BoardConfig().get("bootloader.unlock_bits")
        )
    else:
        miss('unlock_bits')
    if "bootloader.lock_bits" in env.BoardConfig():
        env.Replace(
            LOCK_BITS = env.BoardConfig().get("bootloader.lock_bits")
        )
    else:
        miss('lock_bits')
    if "bootloader.file" in env.BoardConfig():
        env.Replace(
            FILE = env.BoardConfig().get("bootloader.file")
        )
    else:
        miss('file')
    if "build.mcu" in env.BoardConfig():
        env.Replace(
            MCU = env.BoardConfig().get("build.mcu")
        )
    else:
        miss('build.mcu')
else:
    sys.stderr.write(
        "Error: Please specify in platform ini which board"
    )
    env.Exit(1)

env.Replace(
    SETFUSECMD='$UPLOADER $UPLOADERFLAGS -v -p$MCU -cstk500v1 -Ulock:w:$UNLOCK_BITS:m -Uefuse:w:$EXTENDED_FUSES:m -Uhfuse:w:$HIGH_FUSES:m -Ulfuse:w:$LOW_FUSES:m',
    UPLOADBOOTCMD='$UPLOADER $UPLOADERFLAGS -v -p$MCU -cstk500v1 -Uflash:w:$SOURCES:i -Ulock:w:$LOCK_BITS:m',
)

uploadboot = env.Alias(
    "bootloader", './$FILE',
    [
        env.VerboseAction(env.AutodetectUploadPort, "Looking for upload port..."),
        env.VerboseAction("$SETFUSECMD", "Setting fuses"),
        env.VerboseAction("$UPLOADBOOTCMD", "Uploading bootloader $SOURCE")
    ]
)

AlwaysBuild(uploadboot)

1 Like

Does it work? :slight_smile:

If I use the right upload speed yes :slight_smile:… I mean I have success… Well when it fails, it fail on Arduino IDE. I guess I need an external clock.

I like this implementation :slight_smile: Please tune it and I’ll put to our docs.Thanks!

1 Like

Thanks a lot. I will try my best. But I cannot anymore tonight ^^ And I still don’t know how Arduino IDE decide on the speed. It is not the one defined in the board.txt

Of course board file would need to be update to integrate all this infos.

Nice solution, do you have an example of this script in action?

Update, I’ve found this thread. This explains how it works:

We have related issue for that

Done! See http://docs.platformio.org/en/latest/platforms/atmelavr.html#bootloader-programming

1 Like