Skip to content

Instantly share code, notes, and snippets.

@CRTified
Last active March 26, 2024 18:45
Show Gist options
  • Save CRTified/15074a52de236b96b23d86bc9acc68ae to your computer and use it in GitHub Desktop.
Save CRTified/15074a52de236b96b23d86bc9acc68ae to your computer and use it in GitHub Desktop.
Klipper hostmcu config on NixOS
# Relevant part from my klipper config
[mcu host]
serial: /tmp/klipper_host_mcu
[temperature_sensor Host_CPU]
sensor_type: temperature_host
sensor_path: /sys/class/thermal/thermal_zone0/temp
min_temp: 0
max_temp: 90
[temperature_sensor Host_GPU]
sensor_type: temperature_host
sensor_path: /sys/class/thermal/thermal_zone1/temp
min_temp: 0
max_temp: 90
[temperature_sensor Chamber]
sensor_type: BME280
i2c_address: 118
i2c_mcu: host
i2c_bus: i2c.7
[gcode_macro QUERY_BME280]
gcode:
{% set sensor = printer["bme280 Chamber"] %}
{action_respond_info(
"Temperature: %.2f C\n"
"Pressure: %.2f hPa\n"
"Humidity: %.2f%%" % (
sensor.temperature,
sensor.pressure,
sensor.humidity))}
self: super: {
klipper-hostmcu = super.callPackage
({ klipper, python3, pkg-config, git, glibc, bintools-unwrapped }:
super.stdenv.mkDerivation rec {
name = "klipper-host-mcu-${version}";
version = klipper.version;
src = klipper.src;
nativeBuildInputs = [
python3
bintools-unwrapped
pkg-config
glibc # Required for linux process MCUs
];
patches = [
# It would be better to fetch this with pkgs.fetchurl or similar
# URL would be https://github.com/Klipper3d/klipper/pull/6493.patch :)
"${super.writeText "6493.patch" ''
From 1a1284d7356183b773ddcdaf9298bd44394ccdcb Mon Sep 17 00:00:00 2001
From: Michael 'ASAP' Weinrich <[email protected]>
Date: Thu, 15 Feb 2024 20:11:47 -0800
Subject: [PATCH 1/2] makefile: Replace CFLAGS -I with -iquote
The -iquote tells GCC to only search that path when resolving a quoted
"include" (vs <angle brackets>) which by convention imples a include from
the projects own soruce tree. This prevents a conflict between Klippers
"sched.h" and "gpio.h" and <linux/gpio.h> and glibc <sched.h>.
Signed-off-by: Michael 'ASAP' Weinrich <[email protected]>
---
Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
index 106157265478..a6b1d1af76d5 100644
--- a/Makefile
+++ b/Makefile
@@ -29,7 +29,7 @@ dirs-y = src
cc-option=$(shell if test -z "`$(1) $(2) -S -o /dev/null -xc /dev/null 2>&1`" \
; then echo "$(2)"; else echo "$(3)"; fi ;)
-CFLAGS := -I$(OUT) -Isrc -I$(OUT)board-generic/ -std=gnu11 -O2 -MD \
+CFLAGS := -iquote $(OUT) -iquote src -iquote $(OUT)board-generic/ -std=gnu11 -O2 -MD \
-Wall -Wold-style-definition $(call cc-option,$(CC),-Wtype-limits,) \
-ffunction-sections -fdata-sections -fno-delete-null-pointer-checks
CFLAGS += -flto -fwhole-program -fno-use-linker-plugin -ggdb3
From d5947161100aa60b0b324097a38cd0266a7a7d1c Mon Sep 17 00:00:00 2001
From: Michael 'ASAP' Weinrich <[email protected]>
Date: Thu, 15 Feb 2024 20:17:08 -0800
Subject: [PATCH 2/2] linux: Don't use absolute paths for include
Not all systems (i.e. Nix) repect the standard Linux filesystem hierarchy,
instead relative paths should be used and allowing GCC to rely on it's
builtin search paths.
Signed-off-by: Michael 'ASAP' Weinrich <[email protected]>
---
src/linux/gpio.c | 2 +-
src/linux/main.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/linux/gpio.c b/src/linux/gpio.c
index bb07f5a07a9f..c7f4c5bf66c2 100644
--- a/src/linux/gpio.c
+++ b/src/linux/gpio.c
@@ -10,7 +10,7 @@
#include <string.h> // memset
#include <sys/ioctl.h> // ioctl
#include <unistd.h> // close
-#include </usr/include/linux/gpio.h> // GPIOHANDLE_REQUEST_OUTPUT
+#include <linux/gpio.h> // GPIOHANDLE_REQUEST_OUTPUT
#include "command.h" // shutdown
#include "gpio.h" // gpio_out_write
#include "internal.h" // report_errno
diff --git a/src/linux/main.c b/src/linux/main.c
index f9ea3f6daaa8..b260f162b800 100644
--- a/src/linux/main.c
+++ b/src/linux/main.c
@@ -4,7 +4,7 @@
//
// This file may be distributed under the terms of the GNU GPLv3 license.
-#include </usr/include/sched.h> // sched_setscheduler sched_get_priority_max
+#include <sched.h> // sched_setscheduler sched_get_priority_max
#include <stdio.h> // fprintf
#include <string.h> // memset
#include <unistd.h> // getopt
''}"
];
postPatch = ''
patchShebangs .
'';
makeFlags = [
"out/klipper.elf"
"V=1"
"KCONFIG_CONFIG=${
super.writeText "host.config" ''
CONFIG_MACH_LINUX=y
CONFIG_BOARD_DIRECTORY="linux"
CONFIG_CLOCK_FREQ=50000000
CONFIG_LINUX_SELECT=y
CONFIG_USB_VENDOR_ID=0x1d50
CONFIG_USB_DEVICE_ID=0x614e
CONFIG_USB_SERIAL_NUMBER="12345"
CONFIG_WANT_GPIO_BITBANGING=y
CONFIG_WANT_DISPLAYS=y
CONFIG_WANT_SENSORS=y
CONFIG_WANT_LIS2DW=y
CONFIG_WANT_SOFTWARE_I2C=y
CONFIG_WANT_SOFTWARE_SPI=y
CONFIG_CANBUS_FREQUENCY=1000000
CONFIG_HAVE_GPIO=y
CONFIG_HAVE_GPIO_ADC=y
CONFIG_HAVE_GPIO_SPI=y
CONFIG_HAVE_GPIO_I2C=y
CONFIG_HAVE_GPIO_HARD_PWM=y
CONFIG_INLINE_STEPPER_HACK=y
CONFIG_NEED_SENSOR_BULK=y
''
}"
"WXVERSION=3.2"
];
installPhase = ''
mkdir -p $out/bin
cp out/klipper.elf $out/bin/klipper-mcu
'';
dontFixup = true;
meta = with super.lib; {
inherit (klipper.meta) homepage license;
description = "Host MCU part of Klipper";
platforms = platforms.linux;
};
}) { };
}
{
# Untested, but something like this should work
# My personal setup has a whole directory where I put overlays into
nixpkgs.overlays = [ (import ./klipper-overlay.nix )];
systemd.services.klipper-mcu = {
description = "Klipper 3D Printer MCU process";
wantedBy = [ "klipper.service" "multi-user.target" ];
before = [ "klipper.service" ];
partOf = [ "klipper.service" ];
serviceConfig = (lib.filterAttrs (name: _:
lib.elem name [
"CPUSchedulingPolicy"
"CPUSchedulingPriority"
"DynamicUser"
"User"
"Group"
"IOSchedulingClass"
"IOSchedulingPriority"
"OOMScoreAdjust"
"RuntimeDirectory"
"StateDirectory"
]) config.systemd.services.klipper.serviceConfig) // {
ExecStart = let args = "-I /tmp/klipper_host_mcu -r";
in "${pkgs.klipper-hostmcu}/bin/klipper-mcu ${args}";
ExecStop =
[ "echo 'FORCE_SHUTDOWN' > /tmp/klipper_host_mcu" "sleep 1" ];
SupplementaryGroups = [ "gpio" "i2c" ];
RestrictRealtime = false;
AmbientCapabilities = [ "CAP_SYS_NICE" ];
};
};
services.klipper = {
enable = true;
# Somehow wasn't able to access the host mcu char device
# with systemd DynamicUser
user = "klipper";
group = "klipper";
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment