Hello,
I have implemented a quick feature to allow obtaining a device’s chip id / serial number, and would appreciate feedack. get_chip_id
is modeled on get_canbus_id
, and returns “chip_id id=%.*s”.
The implementation is simple, and I felt it was better to copy/paste a few lines rather than try for another layer of abstraction.
Reasoning
- This is something that can be shown in places like Mainsail’s “System Loads” section.
- It was useful to me when debugging / flashing to make sure I was getting the correct data from the serial port, and when writing the Python code below.
- It’s a useful double check to make sure I’m flashing the correct chip.
- Future utility. Katapult currently cannot flash a chip running in USB-CAN mode without running multiple commands. It needs a way to determine the device’s serial number. Either by using a regexp of all available adapters and the code below, or querying the chip directly.
Testers Needed
My printer only has STM32 and RP2040 chips. If anyone has an “atsam” or “atsamd” chip, I would appreciate them testing if this works on those platforms.
Question
Is this the right way to do this?
It feels like I could instead remove the if statement around usb_fill_serial
, then use that data structure instead. The struct is already in memory, so there’s no space difference. I also think it should be an additional return item from get_canbus_id
, with that re-worked so it’s always available. Even if using USB.
Related Python Code
Use C code to derive a canbus uuid from a chip’s serial number. Easier than re-writing fasthash64 in Python.
# Requires cc -fPIC -shared -o /home/pi/klipper/lib/fast-hash/fasthash.so /home/pi/klipper/lib/fast-hash/fasthash.c
serial_number = '<a_string>'
#chip_id_bytes = bytes.fromhex(serial_number)
chip_id_bytes = <return value from get_chip_id>
hasher = ctypes.CDLL('/home/pi/klipper/lib/fast-hash/fasthash.so')
hasher.fasthash64.restype = ctypes.c_uint64
hasher.fasthash64.argtypes = (ctypes.c_void_p, ctypes.c_uint32, ctypes.c_uint64)
hash = hasher.fasthash64(chip_id_bytes, len(chip_id_bytes), 0xA16231A7)
uuid = hash.to_bytes(8, byteorder='little')[0:6]
return uuid.hex()