diff --git a/flake.nix b/flake.nix index 93ee5e8..13bafa8 100644 --- a/flake.nix +++ b/flake.nix @@ -9,7 +9,7 @@ let version = builtins.string 0 8 self.lastModifiedDate; supportedSystems = [ "x86_64-linux" "avr-none" ]; - cflags = { mcu, chip_select, f_cpu }: ''-O -finline-functions -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Werror -Wstrict-prototypes -DF_CPU=${f_cpu} -std=gnu99 -D__${chip_select}__=1 -mmcu=${mcu}''; + mcu_cflags = { mcu, chip_select, f_cpu }: ''-O -finline-functions -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Werror -Wstrict-prototypes -DF_CPU=${f_cpu} -std=gnu99 -D__${chip_select}__=1 -mmcu=${mcu}''; attiny85 = { mcu = "attiny85"; chip_select = "AVR_ATtiny85"; f_cpu = "8000000"; }; atmega32u4 = { mcu = "atmega32u4"; chip_select = "AVR_ATmega32u4"; f_cpu = "8000000"; }; @@ -29,13 +29,12 @@ dontConfigure = true; buildPhase = '' - set -x for source in ${src}/*.c; do if [ -e $source ]; then - ${gcc}/bin/gcc ${cflags} ${include_dirs} -c $source + ${gcc} ${cflags} ${include_dirs} -c $source fi done - cp ${src}/*.h . + cp ${src}/*.h . || true ''; installPhase = '' @@ -45,23 +44,52 @@ ''; }; + mkProgram = { pkgs, gcc, cflags, pname, psrc, pbuildInputs ? [] }: + pkgs.stdenv.mkDerivation rec { + name = pname; + src = psrc; + + buildInputs = pbuildInputs; + + include_dirs = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}/include") buildInputs); + object_files = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/lib/*.o") buildInputs); + + dontUnpack = true; + dontConfigure = true; + + buildPhase = '' + set -x + for source in ${src}/*.c; do + if [ -e $source ]; then + ${gcc} ${cflags} ${include_dirs} -c $source + fi + done + + ${gcc} -o ${name} ${cflags} ${object_files} *.o + ''; + + installPhase = '' + mkdir -p $out/bin + cp ${name} $out/bin + ''; + }; + in rec { defaultPackage."x86_64-linux" = let pkgs = import nixpkgs { system = "x86_64-linux"; }; + avr = pkgs.pkgsCross.avr.buildPackages; in pkgs.symlinkJoin { name = "avr-monorepo"; paths = [ - (packages."x86_64-linux"."flame" processor) - (packages."x86_64-linux"."prime-tx" processor) - (packages."x86_64-linux"."radio-rx" processor) - (packages."x86_64-linux"."interrupts" processor) + (packages."x86_64-linux"."prime-tx" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; }) + (packages."x86_64-linux"."radio-rx" { gcc = "${avr.gcc}/bin/avr-gcc"; cflags = mcu_cflags atmega32u4; }) ]; }; - packages."x86_64-linux"."dio-attiny85" = (packages."x86_64-linux"."dio" attiny85); + /* packages."x86_64-linux"."dio-attiny85" = (packages."x86_64-linux"."dio" attiny85); */ /* packages."x86_64-linux"."dio" = @@ -74,77 +102,158 @@ name = */ - - /* packages."x86_64-linux"."dio" = - { mcu, chip_select, f_cpu }: - let - pkgs = import nixpkgs { system = "x86_64-linux"; }; - avr = pkgs.pkgsCross.avr.buildPackages; - in pkgs.stdenv.mkDerivation rec { - name = "dio"; - src = ./.; - - CFLAGS = cflags { inherit mcu chip_select f_cpu; }; - - installPhase = '' - mkdir $out - cp dio/dio.h $out/ - ''; - }; - */ - - packages."x86_64-linux"."rng" = + { gcc, cflags }: let pkgs = import nixpkgs { system = "x86_64-linux"; }; in mkLibrary { pkgs = pkgs; - gcc = pkgs.gcc; - cflags = "-Werror -Wall"; + gcc = gcc; + cflags = cflags; + pname = "dio"; + psrc = ./dio; + }; + + packages."x86_64-linux"."rng" = + { gcc, cflags }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkLibrary { + pkgs = pkgs; + gcc = gcc; + cflags = cflags; pname = "rng"; psrc = ./rng; }; - /* - packages."x86_64-linux"."rng" = - { mcu, chip_select, f_cpu }: - let - pkgs = import nixpkgs { system = "x86_64-linux"; }; - avr = pkgs.pkgsCross.avr.buildPackages; - in pkgs.stdenv.mkDerivation rec { - name = "rng"; - src = ./.; - - buildInputs = [ - (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) - ]; - - CFLAGS = cflags { inherit mcu chip_select f_cpu; }; - INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs); - OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs); - - buildPhase = '' - ${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o rng.o -c ${src}/rng/rng.c - ''; - - installPhase = '' - mkdir $out - cp rng/rng.h rng.o $out/ - ''; - }; - */ - packages."x86_64-linux"."animation" = + { gcc, cflags }: let pkgs = import nixpkgs { system = "x86_64-linux"; }; in mkLibrary { pkgs = pkgs; - gcc = pkgs.gcc; - cflags = "-Werror -Wall"; + gcc = gcc; + cflags = cflags; pname = "animation"; psrc = ./animation; }; + packages."x86_64-linux"."sk9822" = + { gcc, cflags }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkLibrary { + pkgs = pkgs; + gcc = gcc; + cflags = cflags; + pname = "sk9822"; + psrc = ./sk9822; + pbuildInputs = [ + (packages."x86_64-linux"."dio" { inherit gcc cflags; }) + ]; + }; + + packages."x86_64-linux"."shift-register" = + { gcc, cflags }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkLibrary { + pkgs = pkgs; + gcc = gcc; + cflags = cflags; + pname = "shift-register"; + psrc = ./shift-register; + pbuildInputs = [ + (packages."x86_64-linux"."dio" { inherit gcc cflags; }) + ]; + }; + + packages."x86_64-linux"."spi" = + { gcc, cflags }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkLibrary { + pkgs = pkgs; + gcc = gcc; + cflags = cflags; + pname = "spi"; + psrc = ./spi; + pbuildInputs = [ + (packages."x86_64-linux"."dio" { inherit gcc cflags; }) + ]; + }; + + packages."x86_64-linux"."rfm" = + { gcc, cflags }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkLibrary { + pkgs = pkgs; + gcc = gcc; + cflags = cflags; + pname = "rfm"; + psrc = ./rfm69hcw; + pbuildInputs = [ + (packages."x86_64-linux"."dio" { inherit gcc cflags; }) + (packages."x86_64-linux"."spi" { inherit gcc cflags; }) + ]; + }; + + packages."x86_64-linux"."display" = + { gcc, cflags }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkLibrary { + pkgs = pkgs; + gcc = gcc; + cflags = cflags; + pname = "display"; + psrc = ./display; + pbuildInputs = [ + (packages."x86_64-linux"."dio" { inherit gcc cflags; }) + (packages."x86_64-linux"."shift-register" { inherit gcc cflags; }) + ]; + }; + + packages."x86_64-linux"."prime-tx" = + { gcc, cflags }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkProgram { + pkgs = pkgs; + gcc = gcc; + cflags = cflags; + pname = "prime-tx"; + psrc = ./prime-tx; + pbuildInputs = [ + (packages."x86_64-linux"."dio" { inherit gcc cflags; }) + (packages."x86_64-linux"."spi" { inherit gcc cflags; }) + (packages."x86_64-linux"."shift-register" { inherit gcc cflags; }) + (packages."x86_64-linux"."rfm" { inherit gcc cflags; }) + (packages."x86_64-linux"."display" { inherit gcc cflags; }) + ]; + }; + + packages."x86_64-linux"."radio-rx" = + { gcc, cflags }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + in mkProgram { + pkgs = pkgs; + gcc = gcc; + cflags = cflags; + pname = "radio-rx"; + psrc = ./radio-rx; + pbuildInputs = [ + (packages."x86_64-linux"."dio" { inherit gcc cflags; }) + (packages."x86_64-linux"."spi" { inherit gcc cflags; }) + (packages."x86_64-linux"."shift-register" { inherit gcc cflags; }) + (packages."x86_64-linux"."rfm" { inherit gcc cflags; }) + (packages."x86_64-linux"."display" { inherit gcc cflags; }) + ]; + }; + + /* packages."x86_64-linux"."animation-test" = let pkgs = import nixpkgs { system = "x86_64-linux"; }; @@ -171,138 +280,6 @@ ''; }; - packages."x86_64-linux"."sk9822" = - { mcu, chip_select, f_cpu }: - let - pkgs = import nixpkgs { system = "x86_64-linux"; }; - avr = pkgs.pkgsCross.avr.buildPackages; - in pkgs.stdenv.mkDerivation rec { - name = "sk9822"; - src = ./.; - - buildInputs = [ - (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) - ]; - - CFLAGS = cflags { inherit mcu chip_select f_cpu; }; - INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs); - OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs); - - buildPhase = '' - ${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o sk9822.o -c ${src}/sk9822/sk9822.c - ''; - installPhase = '' - mkdir $out - cp sk9822/sk9822.h sk9822.o $out/ - ''; - }; - - packages."x86_64-linux"."shift-register" = - { mcu, chip_select, f_cpu }: - let - pkgs = import nixpkgs { system = "x86_64-linux"; }; - avr = pkgs.pkgsCross.avr.buildPackages; - in pkgs.stdenv.mkDerivation rec { - name = "shift-register"; - src = ./.; - - buildInputs = [ - (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) - ]; - - CFLAGS = cflags { inherit mcu chip_select f_cpu; }; - INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs); - OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs); - - buildPhase = '' - ${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o shift_register.o -c ${src}/shift_register/shift_register.c - ''; - installPhase = '' - mkdir $out - cp shift_register/shift_register.h shift_register.o $out/ - ''; - }; - - packages."x86_64-linux"."spi" = - { mcu, chip_select, f_cpu }: - let - pkgs = import nixpkgs { system = "x86_64-linux"; }; - avr = pkgs.pkgsCross.avr.buildPackages; - in pkgs.stdenv.mkDerivation rec { - name = "spi"; - src = ./.; - - buildInputs = [ - (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) - ]; - - CFLAGS = cflags { inherit mcu chip_select f_cpu; }; - INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs); - OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs); - - buildPhase = '' - ${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o spi.o -c ${src}/spi/spi.c - ''; - installPhase = '' - mkdir $out - cp spi/spi.h spi.o $out/ - ''; - }; - - packages."x86_64-linux"."rfm" = - { mcu, chip_select, f_cpu }: - let - pkgs = import nixpkgs { system = "x86_64-linux"; }; - avr = pkgs.pkgsCross.avr.buildPackages; - in pkgs.stdenv.mkDerivation rec { - name = "rfm"; - src = ./.; - - buildInputs = [ - (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) - (packages."x86_64-linux"."spi" { inherit mcu chip_select f_cpu; }) - ]; - - CFLAGS = cflags { inherit mcu chip_select f_cpu; }; - INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs); - OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs); - - buildPhase = '' - ${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o rfm.o -c ${src}/rfm69hcw/rfm.c - ''; - installPhase = '' - mkdir $out - cp rfm69hcw/rfm.h rfm.o $out/ - ''; - }; - - packages."x86_64-linux"."display" = - { mcu, chip_select, f_cpu }: - let - pkgs = import nixpkgs { system = "x86_64-linux"; }; - avr = pkgs.pkgsCross.avr.buildPackages; - in pkgs.stdenv.mkDerivation rec { - name = "display"; - src = ./.; - - buildInputs = [ - (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) - (packages."x86_64-linux"."shift-register" { inherit mcu chip_select f_cpu; }) - ]; - - CFLAGS = cflags { inherit mcu chip_select f_cpu; }; - INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs); - OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs); - - buildPhase = '' - ${avr.gcc}/bin/avr-gcc ${CFLAGS} ${INCLUDE_DIRS} -o display.o -c ${src}/display/display.c - ''; - installPhase = '' - mkdir $out - cp display/display.h display.o $out/ - ''; - }; - packages."x86_64-linux"."flame" = { mcu, chip_select, f_cpu }: let @@ -321,7 +298,7 @@ gcc = "${avr.gcc}/bin/avr-gcc"; }) ]; - CFLAGS = cflags { inherit mcu chip_select f_cpu; }; + CFLAGS = mcu_cflags { inherit mcu chip_select f_cpu; }; INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs); OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs); @@ -354,7 +331,7 @@ (packages."x86_64-linux"."display" { inherit mcu chip_select f_cpu; }) ]; - CFLAGS = cflags { inherit mcu chip_select f_cpu; }; + CFLAGS = mcu_cflags { inherit mcu chip_select f_cpu; }; INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs); OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs); @@ -389,7 +366,7 @@ (packages."x86_64-linux"."display" { inherit mcu chip_select f_cpu; }) ]; - CFLAGS = cflags { inherit mcu chip_select f_cpu; }; + CFLAGS = mcu_cflags { inherit mcu chip_select f_cpu; }; INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs); OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs); @@ -422,7 +399,7 @@ (packages."x86_64-linux"."display" { inherit mcu chip_select f_cpu; }) ]; - CFLAGS = cflags { inherit mcu chip_select f_cpu; }; + CFLAGS = mcu_cflags { inherit mcu chip_select f_cpu; }; INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs); OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs); @@ -453,7 +430,7 @@ (packages."x86_64-linux"."dio" { inherit mcu chip_select f_cpu; }) ]; - CFLAGS = cflags { inherit mcu chip_select f_cpu; }; + CFLAGS = mcu_cflags { inherit mcu chip_select f_cpu; }; INCLUDE_DIRS = pkgs.lib.concatStringsSep " " (map (dir: "-I${dir}") buildInputs); OBJECT_FILES = pkgs.lib.concatStringsSep " " (map (dir: "${dir}/*.o") buildInputs); @@ -467,6 +444,7 @@ cp power-management.elf power-management.hex $out ''; }; + */ devShell."x86_64-linux" = let diff --git a/shift_register/shift_register.c b/shift-register/shift_register.c similarity index 100% rename from shift_register/shift_register.c rename to shift-register/shift_register.c diff --git a/shift_register/shift_register.h b/shift-register/shift_register.h similarity index 100% rename from shift_register/shift_register.h rename to shift-register/shift_register.h