Search code examples
visual-studio-codefirmwareplatformio

PlatformIO will not create firmware.bin in VS Code


I downloaded a updated version of some firmware, and need to compile it into a firmware.bin file, (firmware.hex will not work). Most of the tutorials online said to use PlatformIO for this, but whenever I build the firmware, everything goes as planed, it compiles without any errors and says that it succeeded. However, on most youtube videos or online documents, the last few lines include something like

Archiving .pio\build\mega2560\libFrameworkArduino.a
Archiving .pio\build\mega2560\lib9e6\libU8glib-HAL_ID1932.a
Linking .pio\build\mega2560\firmware.elf
Building .pio\build\mega2560\firmware.hex
Checking size .pio\build\mega2560\firmware.elf
Building .pio\build\mega2560\firmware.bin
RAM:   [======    ]  60.6% (used 4963 bytes from 8192 bytes)
Flash: [======    ]  63.7% (used 161682 bytes from 253952 bytes)
================================================ [SUCCESS] Took 222.23 seconds ================================================

While mine looks like this:

Archiving .pio\build\mega2560\libFrameworkArduino.a
Archiving .pio\build\mega2560\lib9e6\libU8glib-HAL_ID1932.a
Linking .pio\build\mega2560\firmware.elf
Building .pio\build\mega2560\firmware.hex
Checking size .pio\build\mega2560\firmware.elf
RAM:   [======    ]  60.6% (used 4963 bytes from 8192 bytes)
Flash: [======    ]  63.7% (used 161682 bytes from 253952 bytes)
================================================ [SUCCESS] Took 222.23 seconds ================================================

And sure enough, there is a firmware.elf and firmware.hex, but no firmware.bin. Many other places say that platformio creates a .bin file by default, so I don't think it was something I forgot to do, but then what was it? Do I need to edit my Platformio.ini file, or something else?


Solution

  • I had the same issue when building for an arduino. The solution I found was to add an extra_script.py file in the workplace to compile the bin after.

    references

    Basically you want to add the extra_scripts line to your platformio.ini file

    ; you env might be different to might platform, board and framework be
    [env:nanoatmega328]
    platform = atmelavr
    board = nanoatmega328
    framework = arduino
    
    extra_scripts = post:extra_script.py
    

    The extra_script.py file resides in the same folder as platformio.ini and is

    from os.path import join
    Import("env", "projenv")
    
    # Custom BIN from ELF
    env.AddPostAction(
        "$BUILD_DIR/${PROGNAME}.elf",
        env.VerboseAction(" ".join([
                    "$OBJCOPY",
                    "-O",
                    "binary",
                    "$TARGET",
                    "$BUILD_DIR/${PROGNAME}.bin"
                ]), "Building $TARGET"))