PlaformIO support for Mikroe STM32, compilation problem

Hello,

I have a MikroE board with STM32f4 and I’m trying to setup PlaftormIO to work with it.

There is a open-source tool developed to upload code to this board, and it works: GitHub - thotypous/mikroe-uhb: USB HID Bootloader programming tool for devices manufactured by MikroElektronika

I have already setted up the board json file, but I’m having problems to generate the .hex to upload. This board/uploader needs a .hex, not a .elf.

This is the json board I have:

{
“build”: {
“core”: “stm32”,
“cpu”: “cortex-m4”,
“extra_flags”: “-DSTM32F4 -DSTM32F407xx -DSTM32F40_41xxx”,
“f_cpu”: “168000000L”,
“ldscript”: “stm32f405x6.ld”,
“mcu”: “stm32f407vgt6”,
“variant”: “stm32f407xx”
},
“frameworks”: [
“libopencm3”,
“cmsis”
],
“name”: “Mikromedia for STM32 M4”,
“upload”: {
“maximum_ram_size”: 131072,
“maximum_size”: 1048576,
“protocol”: “mikroe-uhb”
},
“url”: “MikroElektronika”,
“vendor”: “mikromedia for STM32 M4 - ARM Cortex-M4 Development Board
}

I tried to modify ststm32/builder/main.py to check for the upload protocol “mikroe-uhb” and build the .hex instead of a .elf but I get an error:

*** Multiple ways to build the same target were specified for: /home/h/projects/STM32F4_test/.pioenvs/mikroe/firmware.hex (from [‘/home/h/projects/STM32F4_test/.pioenvs/mikroe/src/main.o’] and from [‘/home/h/projects/STM32F4_test/.pioenvs/mikroe/firmware.hex’])
File “/home/h/.platformio/platforms/ststm32/builder/main.py”, line 174, in

This is the file:

# Copyright 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from os.path import basename, isfile, join

from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Builder, Default,
                          DefaultEnvironment)


env = DefaultEnvironment()

env.Replace(
    AR="arm-none-eabi-ar",
    AS="arm-none-eabi-as",
    CC="arm-none-eabi-gcc",
    CXX="arm-none-eabi-g++",
    OBJCOPY="arm-none-eabi-objcopy",
    RANLIB="arm-none-eabi-ranlib",
    SIZETOOL="arm-none-eabi-size",

    ARFLAGS=["rcs"],

    ASFLAGS=["-x", "assembler-with-cpp"],

    CCFLAGS=[
        "-g",   # include debugging info (so errors include line numbers)
        "-Os",  # optimize for size
        "-ffunction-sections",  # place each function in its own section
        "-fdata-sections",
        "-Wall",
        "-mthumb",
        "-nostdlib"
    ],

    CXXFLAGS=[
        "-fno-rtti",
        "-fno-exceptions"
    ],

    CPPDEFINES=[
        ("F_CPU", "$BOARD_F_CPU")
    ],

    LINKFLAGS=[
        "-Os",
        "-Wl,--gc-sections,--relax",
        "-mthumb",
        "-nostartfiles",
        "-nostdlib"
    ],

    LIBS=["c", "gcc", "m", "stdc++", "nosys"],

    UPLOADER="st-flash",
    UPLOADERFLAGS=[
        "write",        # write in flash
        "$SOURCES",     # firmware path to flash
        "0x08000000"    # flash start adress
    ],
    UPLOADCMD='$UPLOADER $UPLOADERFLAGS',

    SIZEPRINTCMD='$SIZETOOL -B -d $SOURCES',

    PROGNAME="firmware",
    PROGSUFFIX=".elf"
)

if "BOARD" in env:
    env.Append(
        CCFLAGS=[
            "-mcpu=%s" % env.BoardConfig().get("build.cpu")
        ],
        CPPDEFINES=[
            env.BoardConfig().get("build.variant", "").upper()
        ],
        LINKFLAGS=[
            "-mcpu=%s" % env.BoardConfig().get("build.cpu")
        ]
    )

env.Append(
    ASFLAGS=env.get("CCFLAGS", [])[:],

    BUILDERS=dict(
        ElfToBin=Builder(
            action=env.VerboseAction(" ".join([
                "$OBJCOPY",
                "-O",
                "binary",
                "$SOURCES",
                "$TARGET"
            ]), "Building $TARGET"),
            suffix=".bin"
        ),
        ElfToHex=Builder(
            action=env.VerboseAction(" ".join([
                "$OBJCOPY",
                "-O",
                "ihex",
                "-R",
                ".eeprom",
                "$SOURCES",
                "$TARGET"
            ]), "Building $TARGET"),
            suffix=".hex"
        )
    )
)

if env.subst("$UPLOAD_PROTOCOL") == "gdb":
    if not isfile(join(env.subst("$PROJECT_DIR"), "upload.gdb")):
        env.Exit(
            "Error: You are using GDB as firmware uploader. "
            "Please specify upload commands in upload.gdb "
            "file in project directory!"
        )
    env.Replace(
        UPLOADER="arm-none-eabi-gdb",
        UPLOADERFLAGS=[
            join("$BUILD_DIR", "firmware.elf"),
            "-batch",
            "-x",
            join("$PROJECT_DIR", "upload.gdb")
        ],

        UPLOADCMD='$UPLOADER $UPLOADERFLAGS'
    )

elif env.subst("$UPLOAD_PROTOCOL") in ("serial", "dfu") \
        and "arduino" in env.subst("$PIOFRAMEWORK"):
    _upload_tool = "serial_upload"
    _upload_flags = ["{upload.altID}", "{upload.usbID}"]
    if env.subst("$UPLOAD_PROTOCOL") == "dfu":
        _upload_tool = "maple_upload"
        _usbids = env.BoardConfig().get("build.hwids")
        _upload_flags = [env.BoardConfig().get("upload.boot_version", 2),
                         "%s:%s" % (_usbids[0][0][2:], _usbids[0][1][2:])]

    env.Replace(
        UPLOADER=_upload_tool,
        UPLOADERFLAGS=["$UPLOAD_PORT"] + _upload_flags,
        UPLOADCMD=(
            '$UPLOADER $UPLOADERFLAGS $PROJECT_DIR/$SOURCES'))
############### I changed here ##############
elif env.subst("$UPLOAD_PROTOCOL") == "mikroe-uhb":
    env.Replace(
        PROGSUFFIX=".hex",
        UPLOADER="mikroe-uhb",
        UPLOADERFLAGS=[ "-v", "$PROJECT_DIR/$SOURCES" ],
        UPLOADCMD='$UPLOADER $UPLOADERFLAGS,'
    )
#
# Target: Build executable and linkable firmware
#

target_elf = None
if "nobuild" in COMMAND_LINE_TARGETS:
    target_firm = join("$BUILD_DIR", "firmware.bin")
else:
    target_elf = env.BuildProgram()
    ##### and here #####
    if env.subst("$PROGSUFFIX") == ".hex":
        target_firm = env.ElfToHex(join("$BUILD_DIR", "firmware"), target_elf) # this is the line with error
    else:
        target_firm = env.ElfToBin(join("$BUILD_DIR", "firmware"), target_elf)

AlwaysBuild(env.Alias("nobuild", target_firm))
target_buildprog = env.Alias("buildprog", target_firm, target_firm)

#
# Target: Print binary size
#

target_size = env.Alias(
    "size", target_elf,
    env.VerboseAction("$SIZEPRINTCMD", "Calculating size $SOURCE"))
AlwaysBuild(target_size)

#
# Target: Upload by default .bin file
#

if "mbed" in env.subst("$PIOFRAMEWORK") and not env.subst("$UPLOAD_PROTOCOL"):
    target_upload = env.Alias(
        "upload", target_firm,
        [env.VerboseAction(env.AutodetectUploadPort,
                           "Looking for upload disk..."),
         env.VerboseAction(env.UploadToDisk, "Uploading $SOURCE")])
elif "arduino" in env.subst("$PIOFRAMEWORK"):

    def BeforeUpload(target, source, env):
        env.AutodetectUploadPort()
        env.Replace(UPLOAD_PORT=basename(env.subst("$UPLOAD_PORT")))

    target_upload = env.Alias(
        "upload", target_firm,
        [env.VerboseAction(BeforeUpload,
                           "Looking for upload disk..."),
         env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")])
#elif env.subst("$UPLOAD_PROTOCOL") == "mikroe-uhb":
#    print "mikroe"
else:
    target_upload = env.Alias(
        "upload", target_firm,
        env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE"))
AlwaysBuild(target_upload)

#
# Default targets
#

Default([target_buildprog, target_size])

Any ideas how to solve this?
I decided to not create a new platform because this board use the same STM32 as other vendors, the difference is the uploading protocol.
I still need to find out how to create the uploader tool without installing it manually.

Thank you in advance!