From b006b3c355616c89d66084880b5a5a87f0241009 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 9 Oct 2021 23:41:40 +0200 Subject: Basic build environment for Teensy 4.0 programs --- Makefile.lib | 20 ++++++++++++++++++++ core.nix | 42 ++++++++++++++++++++++++++++++++++++++++++ flake.lock | 27 +++++++++++++++++++++++++++ flake.nix | 19 +++++++++++++++++++ test.nix | 28 ++++++++++++++++++++++++++++ test/Makefile | 26 ++++++++++++++++++++++++++ test/blink.cpp | 13 +++++++++++++ test/counter.cpp | 19 +++++++++++++++++++ 8 files changed, 194 insertions(+) create mode 100644 Makefile.lib create mode 100644 core.nix create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 test.nix create mode 100644 test/Makefile create mode 100644 test/blink.cpp create mode 100644 test/counter.cpp diff --git a/Makefile.lib b/Makefile.lib new file mode 100644 index 0000000..ce5787c --- /dev/null +++ b/Makefile.lib @@ -0,0 +1,20 @@ +MCU = IMXRT1062 +MCU_DEF = ARDUINO_TEENSY40 + +OPTIONS = -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH +OPTIONS += -D__$(MCU)__ -DARDUINO=10813 -DTEENSYDUINO=154 -D$(MCU_DEF) + +CPU_OPTIONS = -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -mthumb + +CPPFLAGS = -Wall -g -O2 $(CPU_OPTIONS) -MMD $(OPTIONS) -I. -ffunction-sections -fdata-sections -I. +CXXFLAGS = -std=gnu++14 -felide-constructors -fno-exceptions -fpermissive -fno-rtti -Wno-error=narrowing + +C_FILES := $(wildcard *.c) +CPP_FILES := $(wildcard *.cpp) +OBJS := $(C_FILES:.c=.o) $(CPP_FILES:.cpp=.o) + +C_FILES := $(wildcard *.c) +CPP_FILES := $(wildcard *.cpp) +OBJS := $(C_FILES:.c=.o) $(CPP_FILES:.cpp=.o) + +all: $(OBJS) \ No newline at end of file diff --git a/core.nix b/core.nix new file mode 100644 index 0000000..6a98e55 --- /dev/null +++ b/core.nix @@ -0,0 +1,42 @@ +{ pkgs, ... }: + +pkgs.stdenvNoCC.mkDerivation rec { + name = "teensy-core"; + version = "1.54"; + + src = pkgs.fetchFromGitHub { + owner = "PaulStoffregen"; + repo = "cores"; + rev = "${version}"; + sha256 = "6IXKPVc06WCjJDOnDFOOqjPZSt4UhRW7LHyblEaZ7fw="; + }; + + buildInputs = with pkgs; [ + binutils + gcc-arm-embedded + ]; + + buildPhase = '' + export CC=arm-none-eabi-gcc + export CXX=arm-none-eabi-g++ + + pushd teensy4 + rm main.cpp + cp ${./Makefile.lib} Makefile + make + ar rvs libteensy-core.a *.o + popd + ''; + + installPhase = '' + mkdir -p $out/{include,lib} + pushd teensy4 + cp -r *.h $out/include/ + cp -r avr $out/include/ + cp -r debug $out/include/ + cp -r util $out/include/ + cp libteensy-core.a $out/lib/ + cp imxrt1062.ld $out/lib/ + popd + ''; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..b87a192 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1633709683, + "narHash": "sha256-KVJEToVd6SuUqc2XFIpfSjns07orqEWzIq9AcF/SIf8=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "ce7a1190a0fa4ba3465b5f5471b08567060ca14c", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-21.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..03109f4 --- /dev/null +++ b/flake.nix @@ -0,0 +1,19 @@ +{ + description = "Build environment for Teensy 4.0"; + + inputs = { + nixpkgs.url = github:NixOS/nixpkgs/nixos-21.05; + }; + + outputs = { self, nixpkgs, ... }: let + system = "x86_64-linux"; + pkgs = import nixpkgs { inherit system; }; + + teensy-core = import ./core.nix { inherit pkgs; }; + teensy-test = import ./test.nix { inherit pkgs teensy-core; }; + in { + packages.${system} = { + inherit teensy-core teensy-test; + }; + }; +} diff --git a/test.nix b/test.nix new file mode 100644 index 0000000..e6329a8 --- /dev/null +++ b/test.nix @@ -0,0 +1,28 @@ +{ pkgs, teensy-core, ... }: + +pkgs.stdenv.mkDerivation rec { + name = "teensy-test"; + + src = ./test; + + buildInputs = with pkgs; [ + gcc-arm-embedded + teensy-core + ]; + + buildPhase = '' + export CC=arm-none-eabi-gcc + export CXX=arm-none-eabi-g++ + export OBJCOPY=arm-none-eabi-objcopy + export SIZE=arm-none-eabi-size + + export LDFLAGS="-T${teensy-core}/lib/imxrt1062.ld -L${teensy-core}/lib" + + make + ''; + + installPhase = '' + mkdir $out + cp *.hex $out/ + ''; +} diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..0891b70 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,26 @@ +MCU = IMXRT1062 +MCU_DEF = ARDUINO_TEENSY40 + +OPTIONS = -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH +OPTIONS += -D__$(MCU)__ -DARDUINO=10813 -DTEENSYDUINO=154 -D$(MCU_DEF) + +CPU_OPTIONS = -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -mthumb + +CPPFLAGS = $(NIX_CFLAGS_COMPILE) -Wall -g -O2 $(CPU_OPTIONS) -MMD $(OPTIONS) -I. -ffunction-sections -fdata-sections +CXXFLAGS = -std=gnu++20 -felide-constructors -fno-exceptions -fpermissive -fno-rtti -Wno-error=narrowing + +LDFLAGS += -Os -Wl,--gc-sections,--relax $(CPU_OPTIONS) + +LIBS = -lm -lstdc++ -lteensy-core + +CPP_FILES := $(wildcard *.cpp) +TARGETS := $(CPP_FILES:.cpp=.hex) + +%.elf: %.o + $(CC) $(LDFLAGS) -o $@ $< $(LIBS) + +%.hex: %.elf + $(SIZE) $< + $(OBJCOPY) -O ihex -R .eeprom $< $@ + +all: $(TARGETS) diff --git a/test/blink.cpp b/test/blink.cpp new file mode 100644 index 0000000..ec7b3bd --- /dev/null +++ b/test/blink.cpp @@ -0,0 +1,13 @@ +#include + +extern "C" int main(void) { + pinMode(13, OUTPUT); + + while (true) { + digitalWrite(13, HIGH); + delay(500); + digitalWrite(13, LOW); + delay(500); + } +} + diff --git a/test/counter.cpp b/test/counter.cpp new file mode 100644 index 0000000..680e233 --- /dev/null +++ b/test/counter.cpp @@ -0,0 +1,19 @@ +#include + +extern "C" int main(void) { + for (unsigned iPin=16; iPin <= 20; ++iPin) { + pinMode(iPin, OUTPUT); + } + + std::size_t i = 0; + + while (true) { + digitalWrite(16, (i % 2) > 0); + digitalWrite(17, (i % 4) > 1); + digitalWrite(18, (i % 8) > 3); + digitalWrite(19, (i % 16) > 7); + digitalWrite(20, (i % 32) > 15); + delay(analogRead(7)); + i++; + } +} -- cgit v1.2.3