2014-05-18 23:38:59 +03:00
|
|
|
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
|
|
|
# See LICENSE for details.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Builder for Atmel AVR series of microcontrollers
|
|
|
|
"""
|
|
|
|
|
|
|
|
from os.path import join
|
2014-08-01 16:19:54 +03:00
|
|
|
from time import sleep
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
from SCons.Script import (AlwaysBuild, Builder, COMMAND_LINE_TARGETS, Default,
|
2014-07-27 22:42:04 +03:00
|
|
|
DefaultEnvironment, Exit)
|
|
|
|
|
2014-10-03 19:47:34 +03:00
|
|
|
from platformio.util import get_serialports, reset_serialport
|
2014-07-27 22:42:04 +03:00
|
|
|
|
2014-05-18 23:38:59 +03:00
|
|
|
env = DefaultEnvironment()
|
|
|
|
|
|
|
|
env.Replace(
|
|
|
|
AR="avr-ar",
|
2014-06-11 21:31:36 +03:00
|
|
|
AS="avr-gcc",
|
2014-05-18 23:38:59 +03:00
|
|
|
CC="avr-gcc",
|
|
|
|
CXX="avr-g++",
|
|
|
|
OBJCOPY="avr-objcopy",
|
|
|
|
RANLIB="avr-ranlib",
|
|
|
|
|
|
|
|
ARFLAGS=["rcs"],
|
|
|
|
|
|
|
|
ASFLAGS=[
|
|
|
|
"-g", # include debugging info (so errors include line numbers)
|
|
|
|
"-x", "assembler-with-cpp",
|
2014-06-21 22:27:58 +03:00
|
|
|
"-mmcu=$BOARD_MCU"
|
2014-05-18 23:38:59 +03:00
|
|
|
],
|
2014-06-21 22:27:58 +03:00
|
|
|
|
2014-05-18 23:38:59 +03:00
|
|
|
CCFLAGS=[
|
|
|
|
"-g", # include debugging info (so errors include line numbers)
|
|
|
|
"-Os", # optimize for size
|
2014-08-02 13:23:42 +03:00
|
|
|
# "-Wall", # show warnings
|
2014-05-18 23:38:59 +03:00
|
|
|
"-ffunction-sections", # place each function in its own section
|
|
|
|
"-fdata-sections",
|
2014-08-02 13:23:42 +03:00
|
|
|
"-MMD", # output dependency info
|
2014-06-21 22:27:58 +03:00
|
|
|
"-mmcu=$BOARD_MCU"
|
2014-05-18 23:38:59 +03:00
|
|
|
],
|
2014-06-21 22:27:58 +03:00
|
|
|
|
2014-06-02 20:53:36 +03:00
|
|
|
CXXFLAGS=["-fno-exceptions"],
|
2014-05-18 23:38:59 +03:00
|
|
|
|
2014-06-21 22:27:58 +03:00
|
|
|
CPPDEFINES=[
|
|
|
|
"F_CPU=$BOARD_F_CPU"
|
|
|
|
],
|
|
|
|
|
2014-05-18 23:38:59 +03:00
|
|
|
LINKFLAGS=[
|
|
|
|
"-Os",
|
2014-06-02 20:53:36 +03:00
|
|
|
"-Wl,--gc-sections",
|
|
|
|
"-mmcu=$BOARD_MCU"
|
2014-05-18 23:38:59 +03:00
|
|
|
],
|
|
|
|
|
2014-06-07 13:30:00 +03:00
|
|
|
UPLOADER=join("$PLATFORMTOOLS_DIR", "avrdude", "avrdude"),
|
2014-05-18 23:38:59 +03:00
|
|
|
UPLOADERFLAGS=[
|
|
|
|
"-V", # do not verify
|
|
|
|
"-q", # suppress progress output
|
|
|
|
"-D", # disable auto erase for flash memory
|
|
|
|
"-p", "$BOARD_MCU",
|
2014-06-07 13:30:00 +03:00
|
|
|
"-C", join("$PLATFORMTOOLS_DIR", "avrdude", "avrdude.conf"),
|
2014-06-02 20:53:36 +03:00
|
|
|
"-c", "$UPLOAD_PROTOCOL",
|
|
|
|
"-b", "$UPLOAD_SPEED",
|
|
|
|
"-P", "$UPLOAD_PORT"
|
2014-05-18 23:38:59 +03:00
|
|
|
],
|
|
|
|
UPLOADHEXCMD="$UPLOADER $UPLOADERFLAGS -U flash:w:$SOURCES:i",
|
|
|
|
UPLOADEEPCMD="$UPLOADER $UPLOADERFLAGS -U eeprom:w:$SOURCES:i"
|
|
|
|
)
|
|
|
|
|
|
|
|
env.Append(
|
|
|
|
BUILDERS=dict(
|
|
|
|
ElfToEep=Builder(
|
|
|
|
action=" ".join([
|
|
|
|
"$OBJCOPY",
|
|
|
|
"-O",
|
|
|
|
"ihex",
|
|
|
|
"-j",
|
|
|
|
".eeprom",
|
|
|
|
'--set-section-flags=.eeprom="alloc,load"',
|
|
|
|
"--no-change-warnings",
|
|
|
|
"--change-section-lma",
|
|
|
|
".eeprom=0",
|
|
|
|
"$SOURCES",
|
|
|
|
"$TARGET"]),
|
|
|
|
suffix=".eep"
|
|
|
|
),
|
|
|
|
|
|
|
|
ElfToHex=Builder(
|
|
|
|
action=" ".join([
|
|
|
|
"$OBJCOPY",
|
|
|
|
"-O",
|
|
|
|
"ihex",
|
|
|
|
"-R",
|
|
|
|
".eeprom",
|
|
|
|
"$SOURCES",
|
|
|
|
"$TARGET"]),
|
|
|
|
suffix=".hex"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-08-01 16:19:54 +03:00
|
|
|
|
|
|
|
def reset_device():
|
|
|
|
|
|
|
|
def rpi_sysgpio(path, value):
|
|
|
|
with open(path, "w") as f:
|
|
|
|
f.write(str(value))
|
|
|
|
|
|
|
|
if env.subst("$BOARD") == "raspduino":
|
|
|
|
rpi_sysgpio("/sys/class/gpio/export", 18)
|
|
|
|
rpi_sysgpio("/sys/class/gpio/gpio18/direction", "out")
|
|
|
|
rpi_sysgpio("/sys/class/gpio/gpio18/value", 1)
|
|
|
|
sleep(0.1)
|
|
|
|
rpi_sysgpio("/sys/class/gpio/gpio18/value", 0)
|
|
|
|
rpi_sysgpio("/sys/class/gpio/unexport", 18)
|
|
|
|
else:
|
|
|
|
return reset_serialport(env.subst("$UPLOAD_PORT"))
|
|
|
|
|
|
|
|
|
2014-08-02 20:47:05 +03:00
|
|
|
CORELIBS = env.ProcessGeneral()
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Target: Build executable and linkable firmware
|
|
|
|
#
|
|
|
|
|
2014-08-02 20:47:05 +03:00
|
|
|
target_elf = env.BuildFirmware(CORELIBS + ["m"])
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Target: Extract EEPROM data (from EEMEM directive) to .eep file
|
|
|
|
#
|
|
|
|
|
2014-07-27 22:42:04 +03:00
|
|
|
target_eep = env.Alias("eep", env.ElfToEep(join("$BUILD_DIR", "firmware"),
|
|
|
|
target_elf))
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Target: Build the .hex file
|
|
|
|
#
|
|
|
|
|
2014-07-27 22:42:04 +03:00
|
|
|
if "uploadlazy" in COMMAND_LINE_TARGETS:
|
|
|
|
target_hex = join("$BUILD_DIR", "firmware.hex")
|
|
|
|
else:
|
|
|
|
target_hex = env.ElfToHex(join("$BUILD_DIR", "firmware"), target_elf)
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
#
|
2014-07-27 22:42:04 +03:00
|
|
|
# Target: Upload by default .hex file
|
2014-05-18 23:38:59 +03:00
|
|
|
#
|
|
|
|
|
2014-07-27 22:42:04 +03:00
|
|
|
upload = env.Alias(["upload", "uploadlazy"], target_hex, [
|
2014-08-01 16:19:54 +03:00
|
|
|
lambda target, source, env: reset_device(), "$UPLOADHEXCMD"])
|
2014-07-27 22:42:04 +03:00
|
|
|
AlwaysBuild(upload)
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
#
|
2014-07-27 22:42:04 +03:00
|
|
|
# Target: Upload .eep file
|
2014-05-18 23:38:59 +03:00
|
|
|
#
|
|
|
|
|
2014-07-27 22:42:04 +03:00
|
|
|
uploadeep = env.Alias("uploadeep", target_eep, [
|
2014-08-01 16:19:54 +03:00
|
|
|
lambda target, source, env: reset_device(), "$UPLOADEEPCMD"])
|
2014-07-27 22:42:04 +03:00
|
|
|
AlwaysBuild(uploadeep)
|
2014-05-18 23:38:59 +03:00
|
|
|
|
|
|
|
#
|
2014-07-27 22:42:04 +03:00
|
|
|
# Check for $UPLOAD_PORT variable
|
2014-05-18 23:38:59 +03:00
|
|
|
#
|
|
|
|
|
2014-07-27 22:42:04 +03:00
|
|
|
is_uptarget = (set(["upload", "uploadlazy", "uploadeep"]) &
|
|
|
|
set(COMMAND_LINE_TARGETS))
|
2014-10-03 19:47:34 +03:00
|
|
|
|
|
|
|
if is_uptarget:
|
|
|
|
# try autodetect upload port
|
|
|
|
if "UPLOAD_PORT" not in env:
|
|
|
|
for item in get_serialports():
|
|
|
|
if "VID:PID" in item['hwid']:
|
|
|
|
print ("Auto-detected UPLOAD_PORT: %s" % item['port'])
|
|
|
|
env['UPLOAD_PORT'] = item['port']
|
|
|
|
break
|
|
|
|
|
|
|
|
if "UPLOAD_PORT" not in env:
|
|
|
|
Exit("Please specify environment 'upload_port' or use global "
|
|
|
|
"--upload-port option.")
|
2014-07-27 22:42:04 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Setup default targets
|
|
|
|
#
|
|
|
|
|
|
|
|
Default(target_hex)
|