0

My pi3 has 2 mcp25625 devices connected. The first is connected to spidev0.0, the second to spidev1.2. They both work correctly, after making an overlay (based on https://github.com/raspberrypi/linux/issues/1804) and setting the right /boot/config.txt parameters:

dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=20000000,interrupt=5

dtoverlay=spi1-3cs,cs0_spidev=disabled,cs1_spidev=disabled
dtoverlay=mcp2515-can2,oscillator=20000000,interrupt=6
dtoverlay=spi-bcm2835

This works, but sometimes the can0 and can1 devices are mixed up:

[    3.830843] mcp251x spi0.0 can0: MCP2515 successfully initialized.
[    3.842469] mcp251x spi1.2 can1: MCP2515 successfully initialized.

Versus:

[    3.799918] mcp251x spi1.2 can0: MCP2515 successfully initialized.
[    3.811296] mcp251x spi0.0 can1: MCP2515 successfully initialized.

I was thinking that I might be able to utilize udev for this, but as far as I know that requires an address for the can devices, which I don't seem to have. Both /sys/class/net/can0/address and /sys/class/net/can1/address are empty.

Is there a way to connect the correct spidev with the correct can name at all boots?

The /sys/class/net/can{0,1}/uevent might help, there are no /dev/can{0,1} files:

root@canpi:~# ls -al /dev/can{0,1}
ls: cannot access '/dev/can0': No such file or directory
ls: cannot access '/dev/can1': No such file or directory
root@canpi:~# cat /sys/class/net/can{0,1}/uevent
INTERFACE=can0
IFINDEX=3
INTERFACE=can1
IFINDEX=4
root@canpi:~#

The output of udevadm info /sys/class/net/can0 is even more promising:

$ udevadm info /sys/class/net/can0
P: /devices/platform/soc/3f215080.spi/spi_master/spi1/spi1.2/net/can0
E: DEVPATH=/devices/platform/soc/3f215080.spi/spi_master/spi1/spi1.2/net/can0
E: ID_NET_DRIVER=mcp251x
E: ID_PATH=platform-3f215080.spi
E: ID_PATH_TAG=platform-3f215080_spi
E: IFINDEX=4
E: INTERFACE=can0
E: SUBSYSTEM=net
E: SYSTEMD_ALIAS=/sys/subsystem/net/devices/can0
E: TAGS=:systemd:
E: USEC_INITIALIZED=3889130
  • 1
    If the devices can be hotplugged, did you try using udev monitor to see if there is any information to hook into when they are added? Presuming canX refers to /dev entries you could also check udevadm -a -p /dev/whatever. – goldilocks Dec 21 '17 at 19:15
  • They cannot be live plugged (as they are on a hat). There are no /dev/can{0,1} files on the pi. So I am afraid that the udev tools don't work. The /sys/class/net/can{0,1}/uevent files do exist and contain info. I'll add the output to the question. – steviethecat Dec 21 '17 at 21:11
0

After searching further, I decided to make my own udev rule in order to fix this:

$ cat /etc/udev/rules.d/75-can.rules
SUBSYSTEM!="net", GOTO="my_can_end"
ACTION!="add", GOTO="my_can_end"

DEVPATH=="/devices/platform/soc/3f204000.spi/spi_master/spi0/spi0.0/net/can?", ATTR{id}="SPI0", ENV{SPI}="0", NAME="can0"
DEVPATH=="/devices/platform/soc/3f215080.spi/spi_master/spi1/spi1.2/net/can?", ATTR{id}="SPI1", ENV{SPI}="1", NAME="can1"

LABEL="my_can_end"

This makes sure that additions to the subsystem net are being parsed for matches on device path. As the device path contains the spi bus, the name can be set based on the bus.

pi@canpi:~ $ dmesg | grep can[0-1]
[    3.875577] mcp251x spi1.2 can0: MCP2515 successfully initialized.
[    3.893817] mcp251x spi0.0 can1: MCP2515 successfully initialized.
[    3.915604] mcp251x spi1.2 rename3: renamed from can0
[    3.965174] mcp251x spi0.0 can0: renamed from can1
[    4.040393] mcp251x spi1.2 can1: renamed from rename3
[    7.859240] IPv6: ADDRCONF(NETDEV_CHANGE): can0: link becomes ready
[    8.143414] IPv6: ADDRCONF(NETDEV_CHANGE): can1: link becomes ready
pi@canpi:~ $

And during other boots:

pi@canpi:~ $ dmesg | grep can[0-1]
[    3.825023] mcp251x spi0.0 can0: MCP2515 successfully initialized.
[    3.836400] mcp251x spi1.2 can1: MCP2515 successfully initialized.
[    7.314775] IPv6: ADDRCONF(NETDEV_CHANGE): can1: link becomes ready
[    7.335641] IPv6: ADDRCONF(NETDEV_CHANGE): can0: link becomes ready
pi@canpi:~ $

I also set the environment variable "SPI" to contain the SPI number:

$ udevadm info /sys/class/net/can0
P: /devices/platform/soc/3f204000.spi/spi_master/spi0/spi0.0/net/can0
E: DEVPATH=/devices/platform/soc/3f204000.spi/spi_master/spi0/spi0.0/net/can0
E: ID_NET_DRIVER=mcp251x
E: ID_PATH=platform-3f204000.spi
E: ID_PATH_TAG=platform-3f204000_spi
E: IFINDEX=3
E: INTERFACE=can0
E: SPI=0
E: SUBSYSTEM=net
E: SYSTEMD_ALIAS=/sys/subsystem/net/devices/can0
E: TAGS=:systemd:
E: USEC_INITIALIZED=3831919

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.