diff --git a/platformio/boards/arduino.json b/platformio/boards/arduino.json index 84fa14ff..f3e6e5ec 100644 --- a/platformio/boards/arduino.json +++ b/platformio/boards/arduino.json @@ -550,7 +550,7 @@ "ldscript": "sam3x8e.ld" }, "name": "Arduino Due (Programming Port)", - "platform": "sam", + "platform": "atmelsam", "upload": { "disable_flushing": true, "maximum_ram_size": 524288, diff --git a/platformio/boards/teensy.json b/platformio/boards/teensy.json new file mode 100644 index 00000000..b1882bc9 --- /dev/null +++ b/platformio/boards/teensy.json @@ -0,0 +1,61 @@ +{ + "teensy20": { + "build": { + "core": "teensy", + "f_cpu": "16000000L", + "mcu": "atmega32u4" + }, + "name": "Teensy 2.0", + "platform": "teensy", + "upload": { + "maximum_ram_size": 2560, + "maximum_size": 32256 + } + }, + + "teensy20pp": { + "build": { + "core": "teensy", + "f_cpu": "16000000L", + "mcu": "at90usb1286" + }, + "name": "Teensy++ 2.0", + "platform": "teensy", + "upload": { + "maximum_ram_size": 8192, + "maximum_size": 130048 + } + }, + + "teensy30": { + "build": { + "core": "teensy3", + "extra_flags": "-D__MK20DX128__", + "f_cpu": "48000000L", + "ldscript": "mk20dx128.ld", + "mcu": "mk20dx128" + }, + "name": "Teensy 3.0", + "platform": "teensy", + "upload": { + "maximum_ram_size": 16384, + "maximum_size": 131072 + } + }, + + "teensy31": { + "build": { + "core": "teensy3", + "extra_flags": "-D__MK20DX256__", + "f_cpu": "72000000L", + "ldscript": "mk20dx256.ld", + "mcu": "mk20dx256" + }, + "name": "Teensy 3.1", + "platform": "teensy", + "upload": { + "maximum_ram_size": 65536, + "maximum_size": 262144 + } + } +} diff --git a/platformio/builder/scripts/sam.py b/platformio/builder/scripts/atmelsam.py similarity index 100% rename from platformio/builder/scripts/sam.py rename to platformio/builder/scripts/atmelsam.py diff --git a/platformio/builder/scripts/frameworks/arduino.py b/platformio/builder/scripts/frameworks/arduino.py index aa353081..446d6ed0 100644 --- a/platformio/builder/scripts/frameworks/arduino.py +++ b/platformio/builder/scripts/frameworks/arduino.py @@ -5,51 +5,21 @@ Build script for Arduino Framework (based on Wiring). """ -from os.path import join +from os import listdir +from os.path import isfile, join from SCons.Script import Import, Return env = None Import("env") +BOARD_BUILDOPTS = env.get("BOARD_OPTIONS", {}).get("build", {}) -ARDUINO_VERSION = int( - open(join(env.subst("$PLATFORMFW_DIR"), - "version.txt")).read().replace(".", "").strip()) -# usb flags -ARDUINO_USBDEFINES = [] -if "usb_product" in env.subst("${BOARD_OPTIONS['build']}"): - ARDUINO_USBDEFINES = [ - "USB_VID=${BOARD_OPTIONS['build']['vid']}", - "USB_PID=${BOARD_OPTIONS['build']['pid']}", - 'USB_PRODUCT=\\"%s\\"' % (env.subst( - "${BOARD_OPTIONS['build']['usb_product']}").replace('"', "")) - ] +# +# Atmel SAM platform +# -env.Append( - CPPDEFINES=[ - "ARDUINO=%d" % ARDUINO_VERSION - ] + ARDUINO_USBDEFINES, - - CPPPATH=[ - join("$BUILD_DIR", "FrameworkArduino") - ] -) - -# include board variant -if "variant" in env.get("BOARD_OPTIONS", {}).get("build", {}): - env.VariantDir( - join("$BUILD_DIR", "FrameworkArduinoVariant"), - join("$PLATFORMFW_DIR", "variants", - "${BOARD_OPTIONS['build']['variant']}") - ) - env.Append( - CPPPATH=[ - join("$BUILD_DIR", "FrameworkArduinoVariant") - ] - ) - -if env.get("BOARD_OPTIONS", {}).get("platform", None) == "sam": +if env.get("BOARD_OPTIONS", {}).get("platform", None) == "atmelsam": env.VariantDir( join("$BUILD_DIR", "FrameworkCMSISInc"), join("$PLATFORMFW_DIR", "system", "CMSIS", "CMSIS", "include") @@ -76,6 +46,77 @@ if env.get("BOARD_OPTIONS", {}).get("platform", None) == "sam": ] ) +# +# 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()) + +# usb flags +ARDUINO_USBDEFINES = [] +if "usb_product" in BOARD_BUILDOPTS: + ARDUINO_USBDEFINES = [ + "USB_VID=${BOARD_OPTIONS['build']['vid']}", + "USB_PID=${BOARD_OPTIONS['build']['pid']}", + 'USB_PRODUCT=\\"%s\\"' % (env.subst( + "${BOARD_OPTIONS['build']['usb_product']}").replace('"', "")) + ] + +if env.get("BOARD_OPTIONS", {}).get("platform", None) == "teensy": + ARDUINO_USBDEFINES += [ + "ARDUINO=106", + "TEENSYDUINO=%d" % ARDUINO_VERSION + ] +else: + ARDUINO_USBDEFINES += ["ARDUINO=%d" % ARDUINO_VERSION] + +env.Append( + CPPDEFINES=ARDUINO_USBDEFINES, + + CPPPATH=[ + join("$BUILD_DIR", "FrameworkArduino") + ] +) + +if "variant" in BOARD_BUILDOPTS: + env.VariantDir( + join("$BUILD_DIR", "FrameworkArduinoVariant"), + join("$PLATFORMFW_DIR", "variants", + "${BOARD_OPTIONS['build']['variant']}") + ) + env.Append( + CPPPATH=[ + join("$BUILD_DIR", "FrameworkArduinoVariant") + ] + ) + # # Target: Build Core Library # diff --git a/platformio/builder/scripts/frameworks/cmsis.py b/platformio/builder/scripts/frameworks/cmsis.py index a1c0c9a6..4f194034 100644 --- a/platformio/builder/scripts/frameworks/cmsis.py +++ b/platformio/builder/scripts/frameworks/cmsis.py @@ -9,7 +9,6 @@ from os.path import join from SCons.Script import Import, Return - env = None Import("env") diff --git a/platformio/builder/scripts/frameworks/opencm3.py b/platformio/builder/scripts/frameworks/opencm3.py index d07357c4..324d7bee 100644 --- a/platformio/builder/scripts/frameworks/opencm3.py +++ b/platformio/builder/scripts/frameworks/opencm3.py @@ -13,7 +13,6 @@ from SCons.Script import Import, Return from platformio.util import exec_command - env = None Import("env") BOARD_BUILDOPTS = env.get("BOARD_OPTIONS", {}).get("build", {}) diff --git a/platformio/builder/scripts/frameworks/spl.py b/platformio/builder/scripts/frameworks/spl.py index 62801bfd..39cdb4ec 100644 --- a/platformio/builder/scripts/frameworks/spl.py +++ b/platformio/builder/scripts/frameworks/spl.py @@ -9,7 +9,6 @@ from os.path import join from SCons.Script import Import, Return - env = None Import("env") diff --git a/platformio/builder/scripts/teensy.py b/platformio/builder/scripts/teensy.py new file mode 100644 index 00000000..11d391e5 --- /dev/null +++ b/platformio/builder/scripts/teensy.py @@ -0,0 +1,189 @@ +# Copyright (C) Ivan Kravets +# See LICENSE for details. + +""" + Builder for Teensy boards +""" + +import time +from os.path import isfile, join +from random import randint + +from SCons.Script import (COMMAND_LINE_TARGETS, AlwaysBuild, Builder, Default, + DefaultEnvironment) + +env = DefaultEnvironment() + +if env.get("BOARD_OPTIONS", {}).get("build", {}).get("core") == "teensy": + env.Replace( + AR="avr-ar", + AS="avr-gcc", + CC="avr-gcc", + CXX="avr-g++", + OBJCOPY="avr-objcopy", + RANLIB="avr-ranlib", + + ARFLAGS=["rcs"], + + CXXFLAGS=[ + "-std=c++0x" + ], + + CPPFLAGS=[ + "-mmcu=$BOARD_MCU" + ], + + CPPDEFINES=[ + "SERIALNUM=-%d" % randint(1000000000, 2000000000) + ], + + LINKFLAGS=[ + "-mmcu=$BOARD_MCU" + ] + ) + +elif env.get("BOARD_OPTIONS", {}).get("build", {}).get("core") == "teensy3": + 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=[ + "-mcpu=cortex-m4", + "-mthumb", + # "-nostdlib" + ], + + CXXFLAGS=[ + "-std=gnu++0x", + "-fno-rtti", + ], + + CPPFLAGS=[ + "-mcpu=cortex-m4", + "-mthumb", + "-ffunction-sections", # place each function in its own section + "-fdata-sections", + # "-nostdlib" + ], + + CPPDEFINES=[ + "TIME_T=%d" % time.time() + ], + + LINKFLAGS=[ + "-mcpu=cortex-m4", + "-mthumb", + "-Wl,--gc-sections", + # "-nostartfiles", + # "-nostdlib", + ] + ) + +env.Append( + BUILDERS=dict( + ElfToHex=Builder( + action=" ".join([ + "$OBJCOPY", + "-O", + "ihex", + "-R", + ".eeprom", + "$SOURCES", + "$TARGET"]), + suffix=".hex" + ) + ), + + ASFLAGS=[ + "-c", + "-g", # include debugging info (so errors include line numbers) + "-x", "assembler-with-cpp", + "-Wall" + ], + + CPPFLAGS=[ + "-g", # include debugging info (so errors include line numbers) + "-Os", # optimize for size + "-fdata-sections", + "-ffunction-sections", # place each function in its own section + "-Wall", + "-MMD" # output dependancy info + ], + + CPPDEFINES=[ + "F_CPU=$BOARD_F_CPU", + "USB_PID=null", + "USB_VID=null", + "USB_SERIAL", + "LAYOUT_US_ENGLISH" + ], + + CXXFLAGS=[ + "-felide-constructors", + "-fno-exceptions" + ], + + LINKFLAGS=[ + "-Os" + ] +) + +if isfile(env.subst(join( + "$PIOPACKAGES_DIR", "tool-teensy", "teensy_loader_cli"))): + env.Append( + UPLOADER=join("$PIOPACKAGES_DIR", "tool-teensy", "teensy_loader_cli"), + UPLOADERFLAGS=[ + "-mmcu=$BOARD_MCU", + "-w", # wait for device to apear + "-r", # hard reboot if device not online + "-v" # verbose output + ], + UPLOADHEXCMD='"$UPLOADER" $UPLOADERFLAGS $SOURCES' + ) +else: + env.Append( + UPLOADER=join( + "$PIOPACKAGES_DIR", "tool-teensy", "teensy_post_compile"), + UPLOADERFLAGS=[ + "-file=firmware", + '-path="$BUILD_DIR"', + '-tools="%s"' % join("$PIOPACKAGES_DIR", "tool-teensy") + ], + UPLOADHEXCMD='"$UPLOADER" $UPLOADERFLAGS' + ) + +CORELIBS = env.ProcessGeneral() + +# +# Target: Build executable and linkable firmware +# + +target_elf = env.BuildFirmware(CORELIBS + ["m"]) + +# +# Target: Build the .hex file +# + +if "uploadlazy" in COMMAND_LINE_TARGETS: + target_hex = join("$BUILD_DIR", "firmware.hex") +else: + target_hex = env.ElfToHex(join("$BUILD_DIR", "firmware"), target_elf) + +# +# Target: Upload by default .hex file +# + +upload = env.Alias(["upload", "uploadlazy"], target_hex, ("$UPLOADHEXCMD")) +AlwaysBuild(upload) + +# +# Target: Define targets +# + +Default(target_hex) diff --git a/platformio/platforms/sam.py b/platformio/platforms/atmelsam.py similarity index 94% rename from platformio/platforms/sam.py rename to platformio/platforms/atmelsam.py index 3bc6ac9b..9d0aaa4a 100644 --- a/platformio/platforms/sam.py +++ b/platformio/platforms/atmelsam.py @@ -4,7 +4,7 @@ from platformio.platforms.base import BasePlatform -class SamPlatform(BasePlatform): +class AtmelsamPlatform(BasePlatform): """ An embedded platform for Atmel SAM microcontrollers diff --git a/platformio/platforms/teensy.py b/platformio/platforms/teensy.py new file mode 100644 index 00000000..4a45a2ca --- /dev/null +++ b/platformio/platforms/teensy.py @@ -0,0 +1,47 @@ +# Copyright (C) Ivan Kravets +# See LICENSE for details. + +from platformio.platforms.base import BasePlatform +from platformio.util import get_boards + + +class TeensyPlatform(BasePlatform): + + """ + An embedded platform for Teensy boards + (with Arduino Framework) + """ + + PACKAGES = { + + "toolchain-atmelavr": { + "default": True + }, + + "toolchain-gccarmnoneeabi": { + "default": True + }, + + "ldscripts": { + "default": True + }, + + "framework-arduinoteensy": { + "alias": "framework", + "default": True + } + } + + def run(self, variables, targets): + for v in variables: + if "BOARD=" not in v: + continue + _, board = v.split("=") + bdata = get_boards(board) + if bdata['build']['core'] == "teensy": + tpackage = "toolchain-atmelavr" + else: + tpackage = "toolchain-gccarmnoneeabi" + self.PACKAGES[tpackage]['alias'] = "toolchain" + break + return BasePlatform.run(self, variables, targets)