0

I am using a Raspberry Pi 4b with Raspberry Pi OS (kernel 5.15) and a custom CAN HAT with 3xMCP2517FD CAN controllers connected to SPI0/1/4 of the Raspberry Pi to communicate to other CAN nodes on these 3 CAN buses.

To be able to use the 3 SPI interfaces, I removed all but the first CS pin from the SPI0 overlay as the 2nd CS pin interferes with the SPI4 SCLK:

/dts-v1/;
/plugin/;
  
/ {
        compatible = "brcm,bcm2835";
  
        fragment@0 {
                target = <&spi0_cs_pins>;
                frag0: __overlay__ {
                        brcm,pins = <8>;
                };
        };
  
        fragment@1 {
                target = <&spi0>;
                frag1: __overlay__ {
                        cs-gpios = <&gpio 8 1>;
                        status = "okay";
                };
        };
  
        __overrides__ {
                cs0_pin  = <&frag0>,"brcm,pins:0",
                           <&frag1>,"cs-gpios:4";
        };
};

The other 2 SPI overlays remain untouched. I also edited the MCP21xFD overlay to replace all SPI2 appearances with SPI4. I then moved both compiled overlays into the /boot/overlay folder and appended the following code to /boot/config.txt:

#dtparam=spi=on
dtoverlay=spi0-1cs
dtoverlay=spi1-1cs
dtoverlay=spi4-1cs
dtoverlay=mcp251xfd,spi0-0,oscillator=40000000,interrupt=25
dtoverlay=mcp251xfd,spi1-0,oscillator=40000000,interrupt=26
dtoverlay=mcp251xfd,spi4-0,oscillator=40000000,interrupt=16

To start the 3 CAN interfaces I execute the following script:

# 1MHz 60% / 8MHz 60%
sudo ip link set can0 up type can fd on \
tq 25 prop-seg 11 phase-seg1 12 phase-seg2 16 sjw 1 \
dtq 50 dprop-seg 1 dphase-seg1 1 dphase-seg2 2 dsjw 1

sudo ifconfig can0 txqueuelen 200

sudo ip link set can1 up type can fd on \
tq 25 prop-seg 11 phase-seg1 12 phase-seg2 16 sjw 1 \
dtq 50 dprop-seg 1 dphase-seg1 1 dphase-seg2 2 dsjw 1

sudo ifconfig can1 txqueuelen 200

sudo ip link set can2 up type can fd on \
tq 25 prop-seg 11 phase-seg1 12 phase-seg2 16 sjw 1 \
dtq 50 dprop-seg 1 dphase-seg1 1 dphase-seg2 2 dsjw 1

sudo ifconfig can2 txqueuelen 200

This gets all 3 interfaces up and running and I can successfully use them to communicate to other nodes.

I now want to link a specific SPI bus (i.e. CAN controller) to a SocketCAN interface, as the 3 CAN buses have different purposes. It seems, though, that the order is quite random and changes sometimes on reboots. I have not found a way to specify the SPI interface (e.g. by using the ip link command). This means I either have to rewire the CAN buses or specify a different SocketCAN interface (can0/can1/can2) to the application, that is handling the communication, potentially on every reboot.

Why is that? Is there no way the user can explicitly say which SPI bus to use for the SocketCAN interface?

0 Answers0