Klipper on Duet3HC without USB

I want to flash Duet3HC with Klipper. The problem is that there is no USB port on this board.
I tried to download the firmware via CAN port, but as far as I understand it can’t be done without USB port (even with USB-CAN adapter). Please advise me if I am wrong.
The second option is to use SWD pins for flashing. I tried using OpenOCD by connecting Raspberry Pi 4 via SWD (GPIO25 - SWDCLK, GPIO24 - SWDIO, GPIO18 - nRST). As I understand it, the boot is successful, however, when subsequently connecting to CANbus I do not see this board via ~/klippy-env/bin/python ~/klipper/scripts/canbus_query.py can0. Also, the DIAG LED that shows CAN synchronization between boards (when working with RRF) is not lit. My openocd.cfg:

source [find interface/raspberrypi-native.cfg]
adapter driver bcm2835gpio
bcm2835gpio peripheral_base 0xFE000000
bcm2835gpio speed_coeffs 236181 60
adapter driver linuxgpiod
adapter gpio swclk 25 -chip 4
adapter gpio swdio 24 -chip 4
adapter gpio srst 18
transport select swd
source [find target/atsame5x.cfg]
reset_config srst_nogate
adapter_nsrst_delay 100
adapter_nsrst_assert_width 100
init;
targets
reset halt
atsame5 bootloader 0
program ../klipper/out/klipper.elf verify

Also the output when running sudo openocd:

pi@klid3:~/bootloader $ sudo openocd
[sudo] password for pi:
Open On-Chip Debugger 0.12.0+dev-00731-g1173473f6 (2024-10-04-14:18)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Warn : TMS/SWDIO moved to GPIO 8 (pin 24). Check the wiring please!
Warn : Interface already configured, ignoring
Warn : Interface already configured, ignoring
DEPRECATED! use 'adapter srst delay' not 'adapter_nsrst_delay'
DEPRECATED! use 'adapter srst pulse_width' not 'adapter_nsrst_assert_width'
Info : BCM2835 GPIO JTAG/SWD bitbang driver
Info : clock speed 1985 kHz
Info : SWD DPIDR 0x2ba01477
Info : [atsame5.cpu] Cortex-M4 r0p1 processor detected
Info : [atsame5.cpu] target has 6 breakpoints, 4 watchpoints
Info : [atsame5.cpu] Examination succeed
Info : [atsame5.cpu] starting gdb server on 3333
Info : Listening on port 3333 for gdb connections
[atsame5.cpu] halted due to breakpoint, current mode: Thread
xPSR: 0x01000000 pc: 0x00010100 msp: 0x464c457c
[atsame5.cpu] halted due to breakpoint, current mode: Thread
xPSR: 0x01000000 pc: 0x00010100 msp: 0x464c457c
** Programming Started **
Info : SAM MCU: SAME51N19A (512KB Flash, 192KB RAM)
** Programming Finished **
** Verify Started **
** Verified OK **
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections

Configuring klipper:

Processor model (SAME51J19)
Bootloader offset (16KiB bootloader(Arduino M0, Duet3 Bootloader))
Clock Reference (25MHz (standard))
Communication interface (CAN bus (on PB13/PB12))

At first glance it seems that Klipper does not natively support this MCU (SAME51N19A)

I thought it’s possible to select firmware for SAME51J19 because this MCU is in the same family. https://www.microchip.com/en-us/product/ATSAME51N19A

Not an expert on these chips, but typically there is rarely a thing like “family compatibility”. E.g. on the STM side of life, a STM32H723xxx and STM32H743xxx needs different settings.

So, it could work, but I’d be surprised.

There are no differences in the datasheet that are present in the Kconfig file. Perhaps somewhere else in the Klipper files the difference in MCU is taken into account, if so please advise where. The datasheet shows that the microcontrollers differ only in the number of pins and available peripherals.
Datasheet: https://ww1.microchip.com/downloads/aemDocuments/documents/MCU32/ProductDocuments/DataSheets/SAM-D5x-E5x-Family-Data-Sheet-DS60001507.pdf
Kconfig: klipper/src/atsamd/Kconfig at master · Klipper3d/klipper · GitHub

Again without being the authority, but this is exactly what needs to be reflected in the settings that are behind the make menuconfig choices.
There might not be much missing, but I cannot offer this guidance.

@koconnor or @Arksine can you help?

I don’t have much familiarity with the Atmel ARM platforms, so I couldn’t tell you anything specific about the N variant’s requirements or setup in Klipper and whether or not selecting the J variant will work.

Just glancing at your openocd configuration it doesn’t look like you specified a programming address. So it would start at memory address 0, overwriting the duet bootloader. You could try configuring the klipper build with no bootloader and reprogramming.

FWIW, any “sync” or “diag” leds that are toggled in Duet’s firmware when CANbus sync was achieved will not do the same for Klipper.

@Arksine I’ve tried to change src/atsamd/Kconfig file to add SAMEN19 in the appropriate sections:

choice
	prompt "Processor model"
	config MACH_SAME51N19
	  bool "SAME51N19"
	  select MACH_SAME51
config MCU
	default "same51n19a" if MACH_SAME51N19
config FLASH_SIZE
	hex
  default 0x80000 if MACH_SAMD51G19 || MACH_SAMD51J19 || MACH_SAMD51N19 || MACH_SAME51J19 || MACH_SAME51N19
config RAM_SIZE
  hex
  default 0x30000 if MACH_SAMD51G19 || MACH_SAMD51J19 || MACH_SAMD51N19 || MACH_SAME51J19 || MACH_SAME51N19

Then I chose “SAME51N19” in make menuconfig and flash Klipper.elf. I tried “no bootloader” options too, but this doesn’t help. I found the necessary files for this microcontroller in the lib/same51/include folder, and I think they were imported when I added the SAME51N19 to Kconfig. Do I need to change something else in Klipper files? I used the commit with same51j19 to find more changes needed, but the rest is the same for the SAME51 family as I understand it. (atsamd: Add Kconfig definitions for same51j19 and same54p20 chips · Klipper3d/klipper@bdf79ca · GitHub)

Unfortunately I’m not familiar enough with the atsame implementation to provide strong guidance as far as what is necessary to bring up a new variant. At a cursory glance your change seems ok.

It looks like the diag LED is on pin PC10. You could try adding PC10 to the GPIO Pins to set at micro-controller startup selection in the menuconfig. If the LED turns on you know that Klipper is running and you have a communication issue. If it doesn’t, then there is a build or programming issue.

I debugged klipper.elf using openocd and @koconnor told me that the problem is with the external clocking source, I tried changing the Clock Reference to Internal clock and the system worked: the LED lit up and I could find the uuid on the CAN bus. Then I found in Duet3_Exp_3HC_Schematic that MCU is connected to 12MHz crystal. I changed the following block in src/atsamd/Kconfig and everything is also working. I am working with board version 1.01, if you are working with board v1.02, you will need to select 25MHz crystal in make menuconfig.

choice
	prompt "Clock Reference"
		config CLOCK_REF_X32K
	bool "32.768Khz crystal" if !MACH_SAMC21
		config CLOCK_REF_X12M
	bool "12Mhz crystal" if MACH_SAMC21 || MACH_SAMX5
		config CLOCK_REF_X25M
	bool "25Mhz crystal" if MACH_SAMC21 || MACH_SAMX5
		config CLOCK_REF_INTERNAL
	bool "Internal clock" if !MACH_SAMC21
endchoice

To flash Duet 3 Expansion 3HC you need to follow the steps below:

  1. Change the src/atsamd/Kconfig file according to the attached file (rename to Kconfig)
    Kconfig.txt (7.1 KB)

  2. Go to ~/klipper, make menuconfig. Select the following configuration and make. If you are using version 1.01, you need to select Clock Reference (12 MHz crystal).

    Micro-controller Architecture (SAMC21/SAMD21/SAMD51/SAME5x)
    Processor model (SAME51N19)
    Bootloader offset (no bootloader)
    Clock Reference (25MHz crystal)
    Processor speed (120 MHz (standard))
    Communication interface (CAN bus (on PB13/PB12))
    GPIO pins to set at microcontroller startup (PC10)
    
  3. Connect Duet6HC to Raspberry Pi via SWD: for this you need JST ZH 1.5mm 6pin connector. The connection diagram to Raspberry Pi 4: GPIO25 - SWDCLK, GPIO24 - SWDIO, GPIO18 - nRST, GND - GND, 3.3 V - 3.3 V.

  4. Next you need to install Openocd on the Raspberry: I used the guide.

  5. Create a file openocd.cfg (you can in the openocd folder or create your own). Here is my file, maybe there is something unnecessary in it (the path to the klipper.elf file may be different for you).
    openocd.cfg (383 Bytes)

  6. From the folder with the file run sudo openocd

  7. Check that there are no errors, programming and verification were successful and the DIAG_LED is on.

  8. Connect CAN to a board that is programmed as USB to CAN bus bridge (e.g. Duet6HC) and look for the uuid of the board in the web interface or with the command ~/klippy-env/bin/python ~/klipper/scripts/canbus_query.py can0

  9. Customize printer.cfg
    When I make my printer.cfg for this board, I will also post it.

2 Likes

Thanks. I created PR #6721 for atsame51n19 support: Support atsame51n19 chip (for Duet3HC) by KevinOConnor · Pull Request #6721 · Klipper3d/klipper · GitHub

-Kevin

I added the 12M crystal implementation to samd51_clock and it seems to work as it should. Here is the resulting file.
samd51_clock.c.txt (8.2 KB)

However, now I am facing another problem when connecting to the tmc5160 via SPI. The following error is displayed: “Unable to write tmc spi ‘stepper_x’ register GLOBALSCALER”. I have checked that all pins are specified correctly and tried to find a solution on the forum, but no success so far. @koconnor can you tell me if the source code supports this connection? Here are the settings from printer.cfg:

[stepper_z]
#driver0
step_pin: duet3hc:PA25
dir_pin: duet3hc:PA23 
enable_pin: ! duet3hc: PB23
microsteps: 16
rotation_distance: 32
homing_speed: 16
endstop_pin: duet3hc:PA6 #tmc5160_stepper_z:virtual_endstop
homing_retract_dist: 0
position_endstop: 0
position_max: 420
homing_positive_dir: false

[tmc5160 stepper_z]
cs_pin: duet3hc:PB22
# spi_bus: spi0
spi_software_mosi_pin: duet3hc:PB24
spi_software_miso_pin: duet3hc:PC25
spi_software_sclk_pin: duet3hc:PB25
chain_position: 1
chain_length: 3
interpolate: True
run_current: 1.1                                  
sense_resistor: 0.05                              
stealthchop_threshold: 63
diag0_pin: ^!duet3hc:PA9
driver_SGT: -64

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.