Add nix flakes support (#2)

Reviewed-on: M-Labs/humpback-dds#2
Co-authored-by: mwojcik <mw@m-labs.hk>
Co-committed-by: mwojcik <mw@m-labs.hk>
This commit is contained in:
mwojcik 2022-01-25 10:13:35 +08:00 committed by sb10q
parent 54b87d0b36
commit aaa29a73ba
12 changed files with 217 additions and 20478 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ target/
**/build
**/__pycache__
itm.fifo
result

View File

@ -2,14 +2,20 @@
MQTT-controlled 4-channel DDS signal generator using Urukul, Humpback and STM32 NUCLEO.
- [Continuous Integration](https://nixbld.m-labs.hk/job/stm32/stm32/humpback-dds)
- [Download latest firmware build](https://nixbld.m-labs.hk/job/stm32/stm32/humpback-dds/latest/download-by-type/file/binary-dist)
- [Continuous Integration](https://nixbld.m-labs.hk/job/mcu/humpback-dds/humpback-dds)
- [Download latest firmware build](https://nixbld.m-labs.hk/job/mcu/humpback-dds/humpback-dds/latest/download-by-type/file/binary-dist)
## Nix commands
Start nix shell before anything.
Humpback-DDS firmware is packaged using the [Nix](https://nixos.org) Flakes system. Install Nix 2.4+ and enable flakes by adding ``experimental-features = nix-command flakes`` to ``nix.conf`` (e.g. ``~/.config/nix/nix.conf``).
Once you have Flakes enabled, you can use ``nix build`` to build the firmware.
Alternatively, you can develop and build it within Nix shell:
```shell
nix-shell
nix develop
python fpga/fpga_config.py
cargo build --release
```
**(For users who had completed the [networking setup](##networking-setup-for-first-time-user))** Flash firmware onto STM32 NUCLEO-H743ZI2 using OpenOCD.

View File

@ -1 +0,0 @@
"05lq7c5yz320gasp5q4g76lbj8s1hv1ypgjfg9iqb6jiryy4kv75"

61
flake.lock generated Normal file
View File

@ -0,0 +1,61 @@
{
"nodes": {
"mozilla-overlay": {
"flake": false,
"locked": {
"lastModified": 1638887313,
"narHash": "sha256-FMYV6rVtvSIfthgC1sK1xugh3y7muoQcvduMdriz4ag=",
"owner": "mozilla",
"repo": "nixpkgs-mozilla",
"rev": "7c1e8b1dd6ed0043fb4ee0b12b815256b0b9de6f",
"type": "github"
},
"original": {
"owner": "mozilla",
"repo": "nixpkgs-mozilla",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1642348879,
"narHash": "sha256-ReoDCqqqGEQBmQHlQAXSLSk4LGO96HhBtxsF1TpOnLU=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "8a70a6808c884282161bd77706927caeac0c11e8",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-21.11",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"mozilla-overlay": "mozilla-overlay",
"nixpkgs": "nixpkgs",
"src-migen": "src-migen"
}
},
"src-migen": {
"flake": false,
"locked": {
"lastModified": 1639659493,
"narHash": "sha256-qpVj/yJf4hDDc99XXpVPH4EbLC8aCmEtACn5qNc3DGI=",
"owner": "m-labs",
"repo": "migen",
"rev": "ac703010eaa06ac9b6e32f97c6fa98b15de22b31",
"type": "github"
},
"original": {
"owner": "m-labs",
"repo": "migen",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

145
flake.nix Normal file
View File

@ -0,0 +1,145 @@
{
description = "Firmware for MQTT-controlled 4-channel DDS signal generator using Urukul, Humpback and STM32 NUCLEO.";
inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-21.11;
inputs.mozilla-overlay = { url = github:mozilla/nixpkgs-mozilla; flake = false; };
inputs.src-migen = { url = github:m-labs/migen; flake = false; };
outputs = { self, nixpkgs, mozilla-overlay, src-migen }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import mozilla-overlay) ]; };
rustManifest = pkgs.fetchurl {
url = "https://static.rust-lang.org/dist/2020-10-30/channel-rust-nightly.toml";
sha256 = "0iygcwzh8s0lfdghj5809krvzifc1ii1wm4sd3qqn7s0rz1s14hi";
};
migen = pkgs.python3Packages.buildPythonPackage rec {
name = "migen";
src = src-migen;
propagatedBuildInputs = [ pkgs.python3Packages.colorama ];
};
targets = [
"thumbv7em-none-eabihf"
];
rustChannelOfTargets = _channel: _date: targets:
(pkgs.lib.rustLib.fromManifestFile rustManifest {
inherit (pkgs) stdenv lib fetchurl patchelf;
}).rust.override {
inherit targets;
extensions = ["rust-src"];
};
rust = rustChannelOfTargets "nightly" null targets;
rustPlatform = pkgs.recurseIntoAttrs (pkgs.makeRustPlatform {
rustc = rust;
cargo = rust;
});
itm = rustPlatform.buildRustPackage rec {
version = "0.3.1";
pname = "itm";
src = pkgs.fetchFromGitHub {
owner = "rust-embedded";
repo = "itm";
rev = "v${version}";
sha256 = "sha256-UmWI3NOE8Lf8ICHOR8nNpbCP9+g3R8XHRX+nUJsH6pY=";
};
cargoPatches = [ ./itm-cargo-lock.patch ];
cargoSha256 = "sha256-3odQabrzjFm5rTkeqZWDFLnculwGeB3gG71jNuCtqIo=";
nativeBuildInputs = [ pkgs.pkgconfig ];
doCheck = false;
};
runOpenOcdBlock = pkgs.writeShellScriptBin "run-openocd-block" ''
openocd -f openocd/openocd.cfg
'';
openocdFlash = pkgs.writeShellScriptBin "openocd-flash" ''
openocd -f openocd/openocd.cfg -f openocd/main.cfg
'';
publishMqtt = pkgs.writeShellScriptBin "publish-mqtt" ''
mosquitto_pub -h localhost -t $1 -m "$2" -d
'';
openOCDFlashCustomised = pkgs.writeShellScriptBin "openocd-flash-customised" ''
python3 flash.py $@
openocd -f openocd/openocd.cfg \
-c "init
reset init
halt
stm32h7x mass_erase 1
flash write_image erase target/thumbv7em-none-eabihf/release/humpback-dds
flash write_image flash_config.bin 0x081e0000 bin
reset run
shutdown"
'';
humpback-dds = rustPlatform.buildRustPackage rec {
name = "humpback-dds";
version = "0.0.0";
src = self;
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = {
"SaiTLS-0.1.0" = "sha256-T3hyASQGZAXGLKfOB3mh33VrvRlYSMc1CJdX4XvDFrQ=";
"rsa-0.3.0" = "sha256-9X2kDAOu0HG94HhwYoUtX/ezq99w7u95XMzn3h/JUwk=";
"minimq-0.1.0" = "sha256-yPqMAwyusZZW7571Jn3QVYK2jqmuSMNU6LiZXRYAGGM=";
"smoltcp-0.6.0" = "sha256-BCqcphWF3AotQfuIFYhDiWIMhh2eUCuqD4MPz4dASQ4=";
};
};
nativeBuildInputs = [
pkgs.llvm
pkgs.yosys
pkgs.nextpnr
pkgs.icestorm
(pkgs.python3.withPackages(ps: [ migen ]))
];
buildPhase = ''
cargo build --release --bin humpback-dds
'';
installPhase = ''
mkdir -p $out $out/nix-support
cp target/thumbv7em-none-eabihf/release/humpback-dds $out/humpback-dds.elf
echo file binary-dist $out/humpback-dds.elf >> $out/nix-support/hydra-build-products
llvm-objcopy -O binary target/thumbv7em-none-eabihf/release/humpback-dds $out/humpback-dds.bin
echo file binary-dist $out/humpback-dds.bin >> $out/nix-support/hydra-build-products
'';
doCheck = false;
dontFixup = true;
};
in {
packages.x86_64-linux = {
inherit humpback-dds;
};
hydraJobs = {
inherit humpback-dds;
};
devShell.x86_64-linux = pkgs.mkShell {
name = "humpback-dds-dev-shell";
buildInputs = with pkgs; [
rustPlatform.rust.rustc
rustPlatform.rust.cargo
openocd dfu-util
yosys nextpnr icestorm
gdb mosquitto
itm runOpenOcdBlock
openocdFlash publishMqtt
openOCDFlashCustomised
] ++ (with python3Packages; [
numpy matplotlib migen
]);
};
defaultPackage.x86_64-linux = humpback-dds;
};
}

File diff suppressed because it is too large Load Diff

View File

@ -1,21 +0,0 @@
{ stdenv, fetchFromGitHub, rustPlatform, pkgconfig }:
rustPlatform.buildRustPackage rec {
version = "0.3.1";
pname = "itm";
src = fetchFromGitHub {
owner = "rust-embedded";
repo = "itm";
rev = "v${version}";
sha256 = "15pa0ydm19vz8p3wairpx3vqzc55rp4lgki143ybgw44sgf8hraj";
};
cargoPatches = [ ./itm-cargo-lock.patch ];
cargoSha256 = "1x2pagfxwhgxliygw7325qsg3ccn276f4slg9cql0sf1s304qj4g";
nativeBuildInputs = [ pkgconfig ];
doCheck = false;
}

View File

@ -1,22 +0,0 @@
{ stdenv, fetchFromGitHub, python3Packages }:
python3Packages.buildPythonPackage rec {
name = "migen";
src = fetchFromGitHub {
owner = "m-labs";
repo = "migen";
rev = "7bc4eb1387b39159a74c1dbd1b820728e0bfbbaa";
sha256 = "039jk8y7f0vhr32svg3nd23i88c0bhws8ngxwk9bdznfxvhiy1h6";
fetchSubmodules = true;
};
propagatedBuildInputs = with python3Packages; [ colorama ];
meta = with stdenv.lib; {
description = "A refreshed Python toolbox for building complex digital hardware";
homepage = "https://m-labs.hk";
license = licenses.bsd2;
maintainers = [ maintainers.sb0 ];
};
}

View File

@ -1,66 +0,0 @@
{ stdenv, lib, fetchgit, autoreconfHook,fetchpatch, libftdi1, libusb1, pkgconfig, hidapi }:
stdenv.mkDerivation rec {
pname = "openocd";
version = "0.10.0-dev";
src = fetchgit {
url = "https://git.code.sf.net/p/openocd/code";
rev = "7c88e76a76588fa0e3ab645adfc46e8baff6a3e4";
sha256 = "04ia0rjyil5353dw4mmrmwpald6lqqliaypadp467421dvp0xv97";
fetchSubmodules = true;
};
nativeBuildInputs = [ pkgconfig autoreconfHook];
buildInputs = [ libftdi1 libusb1 hidapi];
configureFlags = [
"--enable-jtag_vpi"
"--enable-usb_blaster_libftdi"
(lib.enableFeature (! stdenv.isDarwin) "amtjtagaccel")
(lib.enableFeature (! stdenv.isDarwin) "gw16012")
"--enable-presto_libftdi"
"--enable-openjtag_ftdi"
(lib.enableFeature (! stdenv.isDarwin) "oocd_trace")
"--enable-buspirate"
(lib.enableFeature stdenv.isLinux "sysfsgpio")
"--enable-remote-bitbang"
];
NIX_CFLAGS_COMPILE = toString (lib.optionals stdenv.cc.isGNU [
"-Wno-implicit-fallthrough"
"-Wno-format-truncation"
"-Wno-format-overflow"
"-Wno-error=tautological-compare"
"-Wno-error=array-bounds"
"-Wno-error=cpp"
]);
postInstall = lib.optionalString stdenv.isLinux ''
mkdir -p "$out/etc/udev/rules.d"
rules="$out/share/openocd/contrib/60-openocd.rules"
if [ ! -f "$rules" ]; then
echo "$rules is missing, must update the Nix file."
exit 1
fi
ln -s "$rules" "$out/etc/udev/rules.d/"
'';
meta = with lib; {
description = "Free and Open On-Chip Debugging, In-System Programming and Boundary-Scan Testing";
longDescription = ''
OpenOCD provides on-chip programming and debugging support with a layered
architecture of JTAG interface and TAP support, debug target support
(e.g. ARM, MIPS), and flash chip drivers (e.g. CFI, NAND, etc.). Several
network interfaces are available for interactiving with OpenOCD: HTTP,
telnet, TCL, and GDB. The GDB server enables OpenOCD to function as a
"remote target" for source-level debugging of embedded systems using the
GNU GDB program.
'';
homepage = "http://openocd.sourceforge.net/";
license = licenses.gpl2Plus;
maintainers = with maintainers; [ bjornfor ];
platforms = platforms.unix;
};
}

View File

@ -1,24 +0,0 @@
{ recurseIntoAttrs, stdenv, lib,
makeRustPlatform, defaultCrateOverrides,
fetchurl, patchelf,
rustManifest ? ./channel-rust-nightly.toml
}:
let
targets = [
"thumbv7em-none-eabihf" # For ARM Cortex-M4 or M7 w/ FPU support
];
rustChannel =
lib.rustLib.fromManifestFile rustManifest {
inherit stdenv fetchurl patchelf;
};
rust =
rustChannel.rust.override {
inherit targets;
};
in
makeRustPlatform {
rustc = rust;
cargo = rust;
}

View File

@ -1,55 +0,0 @@
let
mozillaOverlay = import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz);
pkgs = import <nixpkgs> {overlays = [mozillaOverlay];};
in with pkgs;
let
migen = callPackage ./nix/migen.nix {};
openocd = callPackage ./nix/openocd.nix {};
rustPlatform = callPackage ./nix/rustPlatform.nix {};
itm = callPackage ./nix/itm.nix {inherit rustPlatform;};
runOpenOcdBlock = writeShellScriptBin "run-openocd-block" ''
openocd -f openocd/openocd.cfg
'';
openocdFlash = writeShellScriptBin "openocd-flash" ''
openocd -f openocd/openocd.cfg -f openocd/main.cfg
'';
publishMqtt = writeShellScriptBin "publish-mqtt" ''
mosquitto_pub -h localhost -t $1 -m "$2" -d
'';
openOCDFlashCustomised = writeShellScriptBin "openocd-flash-customised" ''
python3 flash.py $1 $2 $3 $4
openocd -f openocd/openocd.cfg \
-c "init
reset init
halt
stm32h7x mass_erase 1
flash write_image erase target/thumbv7em-none-eabihf/release/humpback-dds
flash write_image flash_config.bin 0x081e0000 bin
reset run
shutdown"
'';
in
stdenv.mkDerivation {
name = "Humpback-DDS";
buildInputs = with rustPlatform.rust; [
(pkgs.python3.withPackages(ps: [ migen ]))
pkgs.yosys
pkgs.nextpnr
pkgs.icestorm
pkgs.gdb
pkgs.mosquitto
openocd
rustc
cargo
itm
runOpenOcdBlock
openocdFlash
publishMqtt
openOCDFlashCustomised
];
}