MyPi CAN-BUS Card Configuration
The CAN-BUS controller is configured by adding a few overlay lines to /boot/config.txt and then config
### SPI ### # These appear on the default RPi ALT1 pins 7-11 # Used by ASIO and RPI-IO dtparam=spi=on # CAN-BUS Overlay dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=25 dtoverlay=spi-bcm2835-overlay
For later kernels (4.4.x onwards) where the device tree setup has changed and we're now using .dtbo overlay files in /boot/overlays you should use the syntax below :
### SPI ### # These appear on the default RPi ALT1 pins 7-11 # Used by ASIO and RPI-IO dtparam=spi=on # CAN-BUS Overlay dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25 dtoverlay=spi0-hw-cs
Next, save and reboot the unit
Once rebooted you can then initialise the CAN interface using the command :
ip link set can0 up type can bitrate 500000
Note that the bitrate here must match that of the target application so you may need to experiment a bit to find the correct setting if you do not know this.
Type ifconfig to see the link status
Only connect the CAN_H and CAN_L lines to the bus, don't inter-connect the ground wire or swap the H and L lines and only wire in the 120R termination resistor if required.
When connecting CAN controllers together then it is vital that the bus is terminated correctly or the network will not operate correctly. If 120R termination resistors are used these should be positioned between the H and L lines at the furthest ends of the bus to correctly terminate the network, the network will not operate correctly without them. A quick resistance check across the H & L lines should give approximately 60 Ohms if all is wired up correctly.
The CAN interface can be controlled via the /etc/network/interfaces configuration file, the settings below are examples and should be altered to suit :
auto can0 iface can0 inet manual pre-up ip link set $IFACE type can bitrate 500000 listen-only off up /sbin/ifconfig $IFACE up down /sbin/ifconfig $IFACE down
The ip command can give you detailed information on the network state if you use the command below:
root@raspberrypi:~# ip -details -statistics link show can0 3: can0:
mtu 16 qdisc pfifo_fast state UNKNOWN mode DEFAULT qlen 10 link/can can state ERROR-ACTIVE restart-ms 0 bitrate 500000 sample-point 0.875 tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1 mcp251x: tseg1 3..16 tseg2 2..8 sjw 1..4 brp 1..64 brp-inc 1 clock 8000000 re-started bus-errors arbit-lost error-warn error-pass bus-off 0 0 0 0 0 0 RX: bytes packets errors dropped overrun mcast 124787 21803 0 0 0 0 TX: bytes packets errors dropped carrier collsns 0 0 0 0 0 0
The items in bold are the most important:
The state is the actual MCP2515 state, so ERROR-ACTIVE is correct operation, ERROR-PASSIVE or BUS-OFF are signs something is wrong. For more information see the MCP2515 datasheet.
The clock value should be 1/2 of what the oscillator setting in config.txt is.
The SocketCAN can-utils package provides a number of programs for sending, receiving, monitoring and (selectively) logging data from the bus, as well as the ability to replay a log file:
asc2log, bcmserver, canbusload, can-calc-bit-timing, candump, canfdtest, cangen, cangw, canlogserver, canplayer, cansend, cansniffer, isotpdump, isotprecv, isotpperf, isotpsend, isotpserver, isotpsniffer, isotptun, log2asc, log2long, slcan_attach, slcand and slcanpty.
When connecting a CAN-BUS network we strongly recommend using appropriate cabling e.g. Belden 9481 120 Ohm impedance twisted-pair wire (or equivalent)
The links below contain a wealth of information on configuring and operating the CAN interface: