2016-08-03 12:30:45 -07:00
|
|
|
# Copyright 2014-present PlatformIO <contact@platformio.org>
|
2016-05-27 08:48:20 -07:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2016-10-11 15:36:48 -07:00
|
|
|
from os.path import basename, isfile, join
|
2016-05-27 08:48:20 -07:00
|
|
|
|
|
|
|
from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Builder, Default,
|
|
|
|
DefaultEnvironment)
|
|
|
|
|
2016-09-10 07:21:29 -07:00
|
|
|
|
2016-05-27 08:48:20 -07:00
|
|
|
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=[
|
2016-11-30 05:54:55 -08:00
|
|
|
("F_CPU", "$BOARD_F_CPU")
|
2016-05-27 08:48:20 -07:00
|
|
|
],
|
|
|
|
|
|
|
|
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(
|
2016-08-27 09:31:54 -07:00
|
|
|
action=env.VerboseAction(" ".join([
|
2016-05-27 08:48:20 -07:00
|
|
|
"$OBJCOPY",
|
|
|
|
"-O",
|
|
|
|
"binary",
|
|
|
|
"$SOURCES",
|
2016-08-27 09:31:54 -07:00
|
|
|
"$TARGET"
|
|
|
|
]), "Building $TARGET"),
|
2016-05-27 08:48:20 -07:00
|
|
|
suffix=".bin"
|
|
|
|
),
|
|
|
|
ElfToHex=Builder(
|
2016-08-27 09:31:54 -07:00
|
|
|
action=env.VerboseAction(" ".join([
|
2016-05-27 08:48:20 -07:00
|
|
|
"$OBJCOPY",
|
|
|
|
"-O",
|
|
|
|
"ihex",
|
|
|
|
"-R",
|
|
|
|
".eeprom",
|
|
|
|
"$SOURCES",
|
2016-08-27 09:31:54 -07:00
|
|
|
"$TARGET"
|
|
|
|
]), "Building $TARGET"),
|
2016-05-27 08:48:20 -07:00
|
|
|
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",
|
2016-09-10 09:09:07 -07:00
|
|
|
join("$PROJECT_DIR", "upload.gdb")
|
2016-05-27 08:48:20 -07:00
|
|
|
],
|
|
|
|
|
|
|
|
UPLOADCMD='$UPLOADER $UPLOADERFLAGS'
|
|
|
|
)
|
|
|
|
|
2016-10-14 02:18:14 -07:00
|
|
|
elif env.subst("$UPLOAD_PROTOCOL") in ("serial", "dfu") \
|
|
|
|
and "arduino" in env.subst("$PIOFRAMEWORK"):
|
2016-10-11 15:36:48 -07:00
|
|
|
_upload_tool = "serial_upload"
|
|
|
|
_upload_flags = ["{upload.altID}", "{upload.usbID}"]
|
2016-10-14 02:18:14 -07:00
|
|
|
if env.subst("$UPLOAD_PROTOCOL") == "dfu":
|
2016-10-11 15:36:48 -07:00
|
|
|
_upload_tool = "maple_upload"
|
|
|
|
_usbids = env.BoardConfig().get("build.hwids")
|
2016-10-12 05:47:58 -07:00
|
|
|
_upload_flags = [env.BoardConfig().get("upload.boot_version", 2),
|
|
|
|
"%s:%s" % (_usbids[0][0][2:], _usbids[0][1][2:])]
|
2016-10-11 09:33:19 -07:00
|
|
|
|
2016-09-10 07:21:29 -07:00
|
|
|
env.Replace(
|
2016-10-11 15:36:48 -07:00
|
|
|
UPLOADER=_upload_tool,
|
|
|
|
UPLOADERFLAGS=["$UPLOAD_PORT"] + _upload_flags,
|
2016-09-10 08:00:15 -07:00
|
|
|
UPLOADCMD=(
|
2016-10-11 15:36:48 -07:00
|
|
|
'$UPLOADER $UPLOADERFLAGS $PROJECT_DIR/$SOURCES'))
|
2016-09-10 07:21:29 -07:00
|
|
|
|
2016-05-27 08:48:20 -07:00
|
|
|
#
|
|
|
|
# Target: Build executable and linkable firmware
|
|
|
|
#
|
|
|
|
|
2016-10-03 07:58:40 -07:00
|
|
|
target_elf = None
|
|
|
|
if "nobuild" in COMMAND_LINE_TARGETS:
|
2016-05-27 08:48:20 -07:00
|
|
|
target_firm = join("$BUILD_DIR", "firmware.bin")
|
|
|
|
else:
|
2016-10-03 07:58:40 -07:00
|
|
|
target_elf = env.BuildProgram()
|
2016-05-27 08:48:20 -07:00
|
|
|
target_firm = env.ElfToBin(join("$BUILD_DIR", "firmware"), target_elf)
|
|
|
|
|
2016-10-03 07:58:40 -07:00
|
|
|
AlwaysBuild(env.Alias("nobuild", target_firm))
|
2016-10-27 13:20:40 -07:00
|
|
|
target_buildprog = env.Alias("buildprog", target_firm, target_firm)
|
2016-10-03 07:58:40 -07:00
|
|
|
|
2016-05-27 08:48:20 -07:00
|
|
|
#
|
|
|
|
# Target: Print binary size
|
|
|
|
#
|
|
|
|
|
2016-08-27 09:31:54 -07:00
|
|
|
target_size = env.Alias(
|
|
|
|
"size", target_elf,
|
|
|
|
env.VerboseAction("$SIZEPRINTCMD", "Calculating size $SOURCE"))
|
2016-05-27 08:48:20 -07:00
|
|
|
AlwaysBuild(target_size)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Target: Upload by default .bin file
|
|
|
|
#
|
|
|
|
|
2016-07-17 06:08:03 -07:00
|
|
|
if "mbed" in env.subst("$PIOFRAMEWORK") and not env.subst("$UPLOAD_PROTOCOL"):
|
2016-10-03 07:58:40 -07:00
|
|
|
target_upload = env.Alias(
|
|
|
|
"upload", target_firm,
|
2016-08-27 09:31:54 -07:00
|
|
|
[env.VerboseAction(env.AutodetectUploadPort,
|
|
|
|
"Looking for upload disk..."),
|
|
|
|
env.VerboseAction(env.UploadToDisk, "Uploading $SOURCE")])
|
2016-09-10 07:21:29 -07:00
|
|
|
elif "arduino" in env.subst("$PIOFRAMEWORK"):
|
2016-10-11 15:36:48 -07:00
|
|
|
|
|
|
|
def BeforeUpload(target, source, env):
|
|
|
|
env.AutodetectUploadPort()
|
|
|
|
env.Replace(UPLOAD_PORT=basename(env.subst("$UPLOAD_PORT")))
|
|
|
|
|
2016-10-03 07:58:40 -07:00
|
|
|
target_upload = env.Alias(
|
|
|
|
"upload", target_firm,
|
2016-10-11 15:36:48 -07:00
|
|
|
[env.VerboseAction(BeforeUpload,
|
2016-09-10 07:21:29 -07:00
|
|
|
"Looking for upload disk..."),
|
|
|
|
env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE")])
|
2016-05-27 08:48:20 -07:00
|
|
|
else:
|
2016-10-03 07:58:40 -07:00
|
|
|
target_upload = env.Alias(
|
|
|
|
"upload", target_firm,
|
|
|
|
env.VerboseAction("$UPLOADCMD", "Uploading $SOURCE"))
|
|
|
|
AlwaysBuild(target_upload)
|
2016-05-27 08:48:20 -07:00
|
|
|
|
|
|
|
#
|
2016-08-30 11:49:51 -07:00
|
|
|
# Default targets
|
2016-05-27 08:48:20 -07:00
|
|
|
#
|
|
|
|
|
2016-10-03 07:58:40 -07:00
|
|
|
Default([target_buildprog, target_size])
|