2022-03-21 17:50:08 +01:00
|
|
|
{ lib
|
|
|
|
, writeShellApplication
|
|
|
|
, gnumake
|
|
|
|
, pkgsCross
|
|
|
|
, klipper
|
|
|
|
, klipper-firmware
|
2022-11-26 01:41:16 +01:00
|
|
|
, python3
|
2022-03-21 17:50:08 +01:00
|
|
|
, avrdude
|
|
|
|
, stm32flash
|
|
|
|
, mcu ? "mcu"
|
|
|
|
, flashDevice ? "/dev/null"
|
2022-03-21 21:04:23 +01:00
|
|
|
, firmwareConfig ? ./simulator.cfg
|
2022-03-21 17:50:08 +01:00
|
|
|
}:
|
|
|
|
let
|
2022-03-22 01:48:17 +01:00
|
|
|
supportedArches = [ "avr" "stm32" "lpc176x" ];
|
|
|
|
matchBoard = with builtins; match ''^.*CONFIG_BOARD_DIRECTORY="([a-zA-Z0-9_]+)".*$'' (readFile firmwareConfig);
|
|
|
|
boardArch = if matchBoard == null then null else builtins.head matchBoard;
|
2022-03-21 17:50:08 +01:00
|
|
|
in
|
|
|
|
writeShellApplication {
|
|
|
|
name = "klipper-flash-${mcu}";
|
|
|
|
runtimeInputs = [
|
2022-11-26 01:41:16 +01:00
|
|
|
python3
|
2022-03-21 17:50:08 +01:00
|
|
|
pkgsCross.avr.stdenv.cc
|
2022-03-21 21:04:23 +01:00
|
|
|
gnumake
|
2022-03-22 01:48:17 +01:00
|
|
|
] ++ lib.optionals (boardArch == "avr") [ avrdude ] ++ lib.optionals (boardArch == "stm32") [ stm32flash ];
|
2022-03-21 17:50:08 +01:00
|
|
|
text = ''
|
2022-03-22 01:48:17 +01:00
|
|
|
if ${lib.boolToString (!builtins.elem boardArch supportedArches)}; then
|
2022-03-21 17:50:08 +01:00
|
|
|
printf "Flashing Klipper firmware to your board is not supported yet.\n"
|
|
|
|
printf "Please use the compiled firmware at ${klipper-firmware} and flash it using the tools provided for your microcontroller."
|
|
|
|
exit 1
|
|
|
|
fi
|
2022-03-22 01:48:17 +01:00
|
|
|
if ${lib.boolToString (boardArch == "stm32")}; then
|
2022-03-21 21:04:23 +01:00
|
|
|
make -C ${klipper.src} FLASH_DEVICE="${toString flashDevice}" OUT="${klipper-firmware}/" KCONFIG_CONFIG="${klipper-firmware}/config" serialflash
|
2022-03-22 01:48:17 +01:00
|
|
|
else
|
|
|
|
make -C ${klipper.src} FLASH_DEVICE="${toString flashDevice}" OUT="${klipper-firmware}/" KCONFIG_CONFIG="${klipper-firmware}/config" flash
|
2022-03-21 17:50:08 +01:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
}
|