ADXL345 Raspberry pi4 multi SPI

Hi, sorry if I’m not in the right place but I didn’t know where to post.

I have installed on my bed stringer printer, 2 ADXL345.
1 for the X axis and 1 for the Y axis.
I want to connect them permanently by connecting them to a raspberry pi 4b.
So I connected the first one on SPI0 as indicated in the tutorial on resonnance.
I then activated the SPI1 with the command “dtoverlay=spi1-1cs”.
In klipper I modified the firmware to identify the adxl345:

[mcu rpi]
serial: /tmp/klipper_host_mcu

[adxl345 hotend]
cs_pin: rpi:None

[adxl345 bed]
cs_pin: rpi:gpio18
spi_bus: spidev1.0

[resonance_tester]
accel_chip_x: adxl345 hotend
accel_chip_y: adxl345 bed
probe_points:
155,155,20 # an example

I can’t get the SPI1 to work.
Can you tell me what I need to enter in [adxl345 bed]?

Sorry for my english i translate with deepl :slight_smile:

Why 2?

One ADXL345 already does 3 axes:

https://www.analog.com/en/products/adxl345.html

because I have a tray that moves back and forth and I have a fixed mount, the adxl are screwed on the printer permanently

I see. That makes sense.

I tryed same config on rasp3 (2 differenti adxl345), but seems that only One adxl can work, or both understood few about :sweat_smile:

I was starting to look into doing this until I decided to use a Pi Pico so I could move it from machine to machine (just plugging into the USB of the host Pi).

I could be totally wrong as I haven’t done this yet, but as it was explained to me, Klipper determines which SPI bus to use based on which CS pin is specified. If that’s true, you need to specify the associated CS pin for each of them. If I understand correctly, you don’t want the spi_bus line with that config as it is for software SPI (and I assume you want hardware SPI since you’re not calling out each pin).
Unfortunately, it doesn’t look like GPIO18 is related to SPI0 or SPI1.
I’d recommend using GPIO8/SPI0_CS0 for SPI0 and GPIO16/SPI1_CS0 for SPI1.
Here’s the pinout I’m referencing:

I also saw this chart but I think the site where you found it is not up to date.
On the raspberry 4 the connections are different:

in yellow you find the spi0
in red the spi1

on the site of raspberry you can find the table assigning the gpio :

we can see the spi0 and spi1 assignments :

if I remember well, I tryed to add 2 adxl, with no succes, I retry in this week end…

if I chang from rpi: None to rpi:gpio8 this message appear… so…

Pin ‘GPIO8’ is not a valid pin name on mcu ‘rpi’

Once the underlying issue is corrected, use the “RESTART”
command to reload the config and restart the host software.
Printer is halted

same for:

Pin ‘spio_cs0’ is not a valid pin name on mcu ‘rpi’

Once the underlying issue is corrected, use the “RESTART”
command to reload the config and restart the host software.
Printer is halted

if I return to rpi:None, all works well, with only one adxl345, obviously!

On the rpi machines specifying a cs_pin causes confusion. Use cs_pin: rpi:None, as the Linux kernel spi device driver will automatically drive the CS pin associated with the given SPI device.

Also, I’ve found that if you did use a config that tried to manually alter the GPIO pin, it can put the machine in a confusing state. Remove any references to GPIO18 from the config and then perform a full hardware reboot of the rpi.

Finally, I’ve not tested using SPI1 on the rpi, so I can’t say if it will work or not.

-Kevin

if I understand what you are telling me, I should use only cs_pin: rpi:None.
If I do that, and send the command TEST_RESONANCES AXIS=Y
how would the rpi know if it should use the 1st or the 2nd adxl345?
this does not seem logical to me.
I will test anyway and I will come back to you with the answer.

I finally had some luck getting two ADXL345’s detected at once on a single Raspi. A Raspberry Pi 4B to be specific.

The issues seems to be that SPI0 and SPI1 are “special” and also kind of dumb on the Raspi. SPI 0 & 1 use a separate (legacy I think) controller versus 2-6 which were added to the latter Pi’s. SPI 0 & 1 can be enabled at the same time, but SPI 1 doesn’t support the necessary signaling mode that Klipper needs, so it fails if you try to use it.

SPI 0 works, but enabling it is a trap. If SPI 0 is enabled, SPI’s 2-6 don’t work properly. I’d originally tried combinations of SPI 0 & 1 (fails) as well as 0 & 5 (fails differently). 5 uses the same pins as 1, so that was convenient (no rewiring), but 5 fails if 0 is enabled.

I finally hooked my two ADXL345’s to SPI 5 and 6, and that works! It seems Klipper has a problem finding those though, so I also needed some symlinks to pretend they’re really 0 and 1. So all in, I have:

config.txt:

dtparam=spi=off
dtoverlay=spi5-1cs
dtoverlay=spi6-1cs

printer.cfg:

[mcu rpi]
serial: /tmp/klipper_host_mcu

[adxl345 hotend]
cs_pin: rpi:None
spi_bus: spidev0.0

[adxl345 bed]
cs_pin: rpi:None
spi_bus: spidev1.0

[resonance_tester]
accel_chip_x: adxl345 hotend
accel_chip_y: adxl345 bed

And I had to create symlinks in /dev (re-created each time by klipper’s init script) so I have:

pi@octopi:~$ ls -l /dev/spidev*
lrwxrwxrwx 1 root root     14 Sep 14 06:55 /dev/spidev0.0 -> /dev/spidev5.0
lrwxrwxrwx 1 root root     14 Sep 14 06:55 /dev/spidev1.0 -> /dev/spidev6.0
crw------- 1 root root 153, 0 Jul 20 07:39 /dev/spidev5.0
crw------- 1 root root 153, 1 Jul 20 07:39 /dev/spidev6.0

If I just put 5 & 6 directly in printer.cfg, startup of the raspberry mcu device fails. I’m printing at the moment unfortunately, so I can’t reproduce that error.

With the symlinks, all is happy. I can run resonance tests on either probe without rewiring or changing any configuration.

Thanks. I updated the code to support spidev4.0 through spidev7.0 (commit 8cf1b512).

-Kevin

EDIT: Note that in order to use these new devices from Klipper, it will be necessary to update to the latest code and recompile/flash the linux mcu code.

1 Like

I tested using 5 & 6 directly without the symlinks today. All good. Thank you!