diff --git a/platformio/boards/arduino.json b/platformio/boards/arduino.json index deba1b1b..84fa14ff 100644 --- a/platformio/boards/arduino.json +++ b/platformio/boards/arduino.json @@ -536,5 +536,29 @@ "via_ssh": true, "wait_for_upload_port": true } - } + }, + "due": { + "build": { + "core": "arduino", + "extra_flags": "-D__SAM3X8E__ -DARDUINO_SAM_DUE -DARDUINO_ARCH_SAM", + "f_cpu": "84000000L", + "mcu": "cortex-m3", + "pid": "0x003e", + "usb_product": "Arduino Due", + "variant": "arduino_due_x", + "vid": "0x2341", + "ldscript": "sam3x8e.ld" + }, + "name": "Arduino Due (Programming Port)", + "platform": "sam", + "upload": { + "disable_flushing": true, + "maximum_ram_size": 524288, + "maximum_size": 28672, + "protocol": "sam-ba", + "speed": 57600, + "use_1200bps_touch": true, + "wait_for_upload_port": false + } + } } diff --git a/platformio/builder/scripts/frameworks/arduino.py b/platformio/builder/scripts/frameworks/arduino.py index c99ae9a6..141901c4 100644 --- a/platformio/builder/scripts/frameworks/arduino.py +++ b/platformio/builder/scripts/frameworks/arduino.py @@ -14,6 +14,67 @@ env = None Import("env") BOARD_BUILDOPTS = env.get("BOARD_OPTIONS", {}).get("build", {}) + +# +# Atmel SAM platform +# + +if env.get("BOARD_OPTIONS", {}).get("platform", None) == "sam": + env.VariantDir( + join("$BUILD_DIR", "FrameworkCMSISInc"), + join("$PLATFORMFW_DIR", "system", "CMSIS", "CMSIS", "include") + ) + env.VariantDir( + join("$BUILD_DIR", "FrameworkLibSamInc"), + join("$PLATFORMFW_DIR", "system", "libsam") + ) + env.VariantDir( + join("$BUILD_DIR", "FrameworkDeviceInc"), + join("$PLATFORMFW_DIR", "system", "CMSIS", "Device", "ATMEL") + ) + env.Append( + CPPPATH=[ + join("$BUILD_DIR", "FrameworkCMSISInc"), + join("$BUILD_DIR", "FrameworkLibSamInc"), + join("$BUILD_DIR", "FrameworkDeviceInc") + ] + ) + env.Append( + LINKFLAGS=[ + "-T", join("$PIOHOME_DIR", "packages", "ldscripts", + "${BOARD_OPTIONS['build']['ldscript']}") + ] + ) + +# +# Teensy platform +# + +# Teensy 2.x Core +if BOARD_BUILDOPTS.get("core", None) == "teensy": + # search relative includes in teensy directories + core_dir = join(env.get("PIOHOME_DIR"), "packages", + "framework-arduinoteensy", "cores", "teensy") + for item in listdir(core_dir): + file_path = join(core_dir, item) + if not isfile(file_path): + continue + content = None + content_changed = False + with open(file_path) as fp: + content = fp.read() + if '#include "../' in content: + content_changed = True + content = content.replace('#include "../', '#include "') + if not content_changed: + continue + with open(file_path, "w") as fp: + fp.write(content) + +# +# Miscellaneous +# + ARDUINO_VERSION = int( open(join(env.subst("$PLATFORMFW_DIR"), "version.txt")).read().replace(".", "").strip()) @@ -56,26 +117,6 @@ if "variant" in BOARD_BUILDOPTS: ] ) -if BOARD_BUILDOPTS.get("core", None) == "teensy": - # search relative includes in teensy directories - core_dir = join(env.get("PIOHOME_DIR"), "packages", - "framework-arduinoteensy", "cores", "teensy") - for item in listdir(core_dir): - file_path = join(core_dir, item) - if not isfile(file_path): - continue - content = None - content_changed = False - with open(file_path) as fp: - content = fp.read() - if '#include "../' in content: - content_changed = True - content = content.replace('#include "../', '#include "') - if not content_changed: - continue - with open(file_path, "w") as fp: - fp.write(content) - # # Target: Build Core Library # diff --git a/platformio/builder/scripts/sam.py b/platformio/builder/scripts/sam.py new file mode 100644 index 00000000..a8c98183 --- /dev/null +++ b/platformio/builder/scripts/sam.py @@ -0,0 +1,137 @@ +# Copyright (C) Ivan Kravets +# See LICENSE for details. + +""" + Builder for Atmel SAM series of microcontrollers +""" + +from os.path import join + +from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Builder, Default, + DefaultEnvironment) + +from platformio.util import get_serialports + +env = DefaultEnvironment() + +env.Replace( + AR="arm-none-eabi-ar", + AS="arm-none-eabi-gcc", + CC="arm-none-eabi-gcc", + CXX="arm-none-eabi-g++", + OBJCOPY="arm-none-eabi-objcopy", + RANLIB="arm-none-eabi-ranlib", + + ARFLAGS=["rcs"], + + ASFLAGS=[ + "-c", + "-g", # include debugging info (so errors include line numbers) + "-x", "assembler-with-cpp", + "-Wall", + "-mthumb", + "-mcpu=${BOARD_OPTIONS['build']['mcu']}" + ], + + CCFLAGS=[ + "-g", # include debugging info (so errors include line numbers) + "-Os", # optimize for size + "-Wall", # show warnings + "-ffunction-sections", # place each function in its own section + "-fdata-sections", + "-MMD", # output dependency info + "-mcpu=${BOARD_OPTIONS['build']['mcu']}", + "-mthumb" + ], + + CXXFLAGS=[ + "-fno-rtti", + "-fno-exceptions" + ], + + CPPDEFINES=[ + "F_CPU=$BOARD_F_CPU" + ], + + LINKFLAGS=[ + "-Os", + "-Wl,--gc-sections", + "-mcpu=${BOARD_OPTIONS['build']['mcu']}", + "-mthumb" + ], + + UPLOADER=join("$PIOPACKAGES_DIR", "$PIOPACKAGE_UPLOADER", "bossac"), + UPLOADERFLAGS=[ + "--info", + "--debug", + "--port", "$UPLOAD_PORT", + "--erase", + "--write", + "--verify", + "--boot" + ], + UPLOADBINCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCES' +) + +env.Append( + BUILDERS=dict( + ElfToBin=Builder( + action=" ".join([ + "$OBJCOPY", + "-O", + "binary", + "$SOURCES", + "$TARGET"]), + suffix=".bin" + ) + ) +) + +CORELIBS = env.ProcessGeneral() + +# +# Target: Build executable and linkable firmware +# + +target_elf = env.BuildFirmware(CORELIBS + ["m", "gcc"]) + +# +# Target: Build the .bin file +# + +if "uploadlazy" in COMMAND_LINE_TARGETS: + target_bin = join("$BUILD_DIR", "firmware.bin") +else: + target_bin = env.ElfToBin(join("$BUILD_DIR", "firmware"), target_elf) + +# +# Target: Upload by default .bin file +# + +upload = env.Alias(["upload", "uploadlazy"], target_bin, ("$UPLOADBINCMD")) +AlwaysBuild(upload) + +# +# Check for $UPLOAD_PORT variable +# + +is_uptarget = (set(["upload", "uploadlazy"]) & set(COMMAND_LINE_TARGETS)) + +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.Replace(UPLOAD_PORT=item['port']) + break + + if "UPLOAD_PORT" not in env: + print("WARNING!!! Please specify environment 'upload_port' or use " + "global --upload-port option.\n") + +# +# Setup default targets +# + +Default(target_bin) diff --git a/platformio/platforms/sam.py b/platformio/platforms/sam.py new file mode 100644 index 00000000..3bc6ac9b --- /dev/null +++ b/platformio/platforms/sam.py @@ -0,0 +1,34 @@ +# Copyright (C) Ivan Kravets +# See LICENSE for details. + +from platformio.platforms.base import BasePlatform + + +class SamPlatform(BasePlatform): + + """ + An embedded platform for Atmel SAM microcontrollers + (with Arduino Framework) + """ + + PACKAGES = { + + "toolchain-gccarmnoneeabi": { + "alias": "toolchain", + "default": True + }, + + "ldscripts": { + "default": True + }, + + "framework-arduinosam": { + "alias": "framework", + "default": True + }, + + "tool-bossac": { + "alias": "uploader", + "default": True + } + }