CAN

The Pi-Tron has a CAN interface connected to SPI0. In order to use SPI0 and the CAN bus the following adjustments have to be made.

1. Update Raspberry Pi OS

pi@raspberry:~ $ sudo apt-get update
pi@raspberry:~ $ sudo apt-get dist-upgrade

2. Edit the config.txt

pi@raspberry:~ $ sudo nano /boot/config.txt

Enter the following at the end of the file:

### CAN Interface
dtparam=spi=on
device_tree_param=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=42
dtoverlay=spi-bcm2835-overlay
dtoverlay=spi-gpio35-39

3. Install CAN tools

pi@raspberry:~ $ sudo apt-get install can-utils

4. Reboot

(optional) Check if the CAN interface is available

The listing below shows that the spi0.0 device, which is SPI0 with CE0, has a can0 folder under the net subfolder. This means that the CAN interface should work fine:

pi@raspberry:~ $ ls /sys/bus/spi/devices/spi0.0
driver  modalias  net  of_node  subsystem  uevent
pi@raspberry:~ $ ls /sys/bus/spi/devices/spi0.0/net
can0
pi@raspberry:~ $ ls /sys/bus/spi/devices/spi0.0
driver  modalias  net  of_node  subsystem  uevent
pi@raspberry:~ $ ls /sys/bus/spi/devices/spi0.0/net/
can0
pi@raspberry:~ $ ls /sys/bus/spi/devices/spi0.0/net/can0/
addr_assign_type  carrier_changes  duplex             iflink            operstate       speed         uevent
address           device           flags              link_mode         phys_port_id    statistics
addr_len          dev_id           gro_flush_timeout  mtu               phys_port_name  subsystem
broadcast         dev_port         ifalias            name_assign_type  phys_switch_id  tx_queue_len
carrier           dormant          ifindex            netdev_group      queues          type

5. Setup the CAN interface with 1M baudrate

pi@raspberry:~ $ sudo ip link set can0 up type can bitrate 1000000

If it doesn't work, you have to login with sudo su as root and run the command again. You can then log out with exit and continue working as a normal user.

You can use the ifconfig command to display further information about the can0 device:

pi@raspberry:~ $ sudo ifconfig
can0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
          UP RUNNING NOARP  MTU:16  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:10
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
...

6. Send data

pi@raspberry:~ $ cansend can0 123#12345678

7. Display received data

pi@raspberry:~ $ candump can0
  can0  001   [8]  11 22 33 44 55 66 77 88
  can0  00003456   [8]  EF FE DD AD CB 67 98 AA
  can0  00000FE6   [8]  EF FE DD AD CB 67 98 AA
  can0       0CC   [8]  EF FE DD AD CB 67 98 AA
  can0       0CC   [8]  EF FE DD AD CB 67 98 AA
^Cpi@raspberry:~ $