klipper-firmware: init at klipper.version

- Add `klipper-genconf` helper to generate firmware configs
- Add `klipper-flash` helper to flash firmware to microcontroller

By default, klipper-firmware is built with a simulator config. Override
`firmwareConfig` with a generated config from `klipper-genconf`. To
flash the firmware, override `flashDevice` in `klipper-flash`.
This commit is contained in:
Van Tuan Vo 2022-03-21 17:50:08 +01:00
parent 6a2d6ee7fb
commit fd1a869603
No known key found for this signature in database
GPG Key ID: 7C96B1B77958A566
5 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,66 @@
{ stdenv
, lib
, pkg-config
, pkgsCross
, bintools-unwrapped
, libffi
, libusb1
, wxGTK
, python2
, python3
, gcc-arm-embedded
, klipper
, avrdude
, stm32flash
, mcu ? "mcu"
, firmwareConfig ? ./simulator.cfg
, flashDevice ? null
}: stdenv.mkDerivation rec {
name = "klipper-firmware-${mcu}-${version}";
version = klipper.version;
src = klipper.src;
nativeBuildInputs = [
python2
python3
pkgsCross.avr.stdenv.cc
gcc-arm-embedded
bintools-unwrapped
libffi
libusb1
avrdude
stm32flash
pkg-config
wxGTK # Required for bossac
];
preBuild = "cp ${firmwareConfig} ./.config";
postPatch = ''
patchShebangs .
'';
makeFlags = [
"V=1"
"KCONFIG_CONFIG=${firmwareConfig}"
];
installPhase = ''
mkdir -p $out
cp ./.config $out/config
cp -r out/* $out
if ${lib.boolToString (!isNull flashDevice)}; then
make FLASH_DEVICE=${toString flashDevice} OUT=$out/ KCONFIG_CONFIG=$out/config flash
fi
'';
dontFixup = true;
meta = {
inherit (klipper.meta) homepage license;
description = "Firmware part of Klipper";
maintainers = with lib.maintainers; [ vtuan10 ];
platforms = lib.platforms.linux;
};
}

View File

@ -0,0 +1,42 @@
{ lib
, writeShellApplication
, gnumake
, pkgsCross
, klipper
, klipper-firmware
, python2
, avrdude
, stm32flash
, mcu ? "mcu"
, flashDevice ? "/dev/null"
}:
let
firmwareConfig = builtins.readFile "${klipper-firmware}/config";
isNotSupported = with builtins; isNull (match ''^.*CONFIG_BOARD_DIRECTORY="(avr|stm32|lpc176x)".*$'' firmwareConfig);
isNotStm = with builtins; isNull (match ''^.*CONFIG_BOARD_DIRECTORY="(stm32)".*$'' firmwareConfig);
in
writeShellApplication {
name = "klipper-flash-${mcu}";
runtimeInputs = [
python2
avrdude
stm32flash
pkgsCross.avr.stdenv.cc
];
text = ''
NOT_SUPPORTED=${lib.boolToString isNotSupported}
NOT_STM=${lib.boolToString isNotStm}
if $NOT_SUPPORTED; then
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
pushd ${klipper.src}
if $NOT_STM; then
${gnumake}/bin/make FLASH_DEVICE="${toString flashDevice}" OUT="${klipper-firmware}/" KCONFIG_CONFIG="${klipper-firmware}/config" flash
else
${gnumake}/bin/make FLASH_DEVICE="${toString flashDevice}" OUT="${klipper-firmware}/" KCONFIG_CONFIG="${klipper-firmware}/config" serialflash
fi
popd
'';
}

View File

@ -0,0 +1,22 @@
{ writeShellApplication
, klipper
, python2
, gnumake
, pkgsCross
}: writeShellApplication {
name = "klipper-genconf";
runtimeInputs = [
python2
pkgsCross.avr.stdenv.cc
];
text = ''
CURRENT_DIR=$(pwd)
TMP=$(mktemp -d)
pushd ${klipper.src}
${gnumake}/bin/make OUT="$TMP" KCONFIG_CONFIG="$CURRENT_DIR/config" menuconfig
popd
rm -rf "$TMP" config.old
printf "\nYour firmware configuration for klipper:\n\n"
cat config
'';
}

View File

@ -0,0 +1,23 @@
# CONFIG_LOW_LEVEL_OPTIONS is not set
# CONFIG_MACH_AVR is not set
# CONFIG_MACH_ATSAM is not set
# CONFIG_MACH_ATSAMD is not set
# CONFIG_MACH_LPC176X is not set
# CONFIG_MACH_STM32 is not set
# CONFIG_MACH_RP2040 is not set
# CONFIG_MACH_PRU is not set
# CONFIG_MACH_LINUX is not set
CONFIG_MACH_SIMU=y
CONFIG_BOARD_DIRECTORY="simulator"
CONFIG_CLOCK_FREQ=20000000
CONFIG_SERIAL=y
CONFIG_SIMULATOR_SELECT=y
CONFIG_SERIAL_BAUD=250000
CONFIG_USB_VENDOR_ID=0x1d50
CONFIG_USB_DEVICE_ID=0x614e
CONFIG_USB_SERIAL_NUMBER="12345"
CONFIG_HAVE_GPIO=y
CONFIG_HAVE_GPIO_ADC=y
CONFIG_HAVE_GPIO_SPI=y
CONFIG_HAVE_GPIO_HARD_PWM=y
CONFIG_INLINE_STEPPER_HACK=y

View File

@ -3422,6 +3422,16 @@ with pkgs;
klipper = callPackage ../servers/klipper { };
klipper-firmware = callPackage ../servers/klipper/klipper-firmware.nix {
inherit libusb1;
};
klipper-flash = callPackage ../servers/klipper/klipper-flash.nix {
inherit klipper-firmware;
};
klipper-genconf = callPackage ../servers/klipper/klipper-genconf.nix { };
klog = qt5.callPackage ../applications/radio/klog { };
krapslog = callPackage ../tools/misc/krapslog { };