platform-ststm32/builder/main.py

185 lines
4.3 KiB
Python

# Copyright 2014-present Ivan Kravets <me@ikravets.com>
#
# 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.
"""
Builder for ST STM32 Series ARM microcontrollers.
"""
from os.path import 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=" ".join([
"$OBJCOPY",
"-O",
"binary",
"$SOURCES",
"$TARGET"]),
suffix=".bin"
),
ElfToHex=Builder(
action=" ".join([
"$OBJCOPY",
"-O",
"ihex",
"-R",
".eeprom",
"$SOURCES",
"$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",
'"%s"' % join("$PROJECT_DIR", "upload.gdb")
],
UPLOADCMD='$UPLOADER $UPLOADERFLAGS'
)
#
# Target: Build executable and linkable firmware
#
target_elf = env.BuildProgram()
#
# Target: Build the .bin file
#
if "uploadlazy" in COMMAND_LINE_TARGETS:
target_firm = join("$BUILD_DIR", "firmware.bin")
else:
target_firm = env.ElfToBin(join("$BUILD_DIR", "firmware"), target_elf)
#
# Target: Print binary size
#
target_size = env.Alias("size", target_elf, "$SIZEPRINTCMD")
AlwaysBuild(target_size)
#
# Target: Upload by default .bin file
#
if "mbed" in env.subst("$FRAMEWORK") and not env.subst("$UPLOAD_PROTOCOL"):
upload = env.Alias(["upload", "uploadlazy"],
target_firm, env.UploadToDisk)
else:
upload = env.Alias(["upload", "uploadlazy"], target_firm, "$UPLOADCMD")
AlwaysBuild(upload)
#
# Target: Unit Testing
#
AlwaysBuild(env.Alias("test", [target_firm, target_size]))
#
# Target: Define targets
#
Default([target_firm, target_size])