MyPi Integrator Board CAN-BUS Card Configuration

The CAN-BUS controller is configured by adding a few overlay lines to /boot/config.txt and then config

# CAN-BUS Overlay
dtoverlay=spi0-on
dtoverlay=spi0-1cs,cs0_pin=8
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25

Next, save and reboot the unit

Note : If you are using the demo OS image the system has been configured using /etc/network/interfaces to automatically bring up the can0 interface at boot (if the CAN interface is enabled in config.txt )

If you do not have can0 in /etc/network/interfaces configured 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

MyPi Industrial Raspberry Pi CAN-BUS Configuration Step 2

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.

CAN-BUS Network Topology termination resistor position

The CAN interface can be controlled creating the configuration file /etc/network/interfaces.d/can 

The settings below are examples and should be altered to suit (note that "listen-only off" setting means the system can read and write to the bus) :

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:

 

 

Belden 9481 RS485/CAN-BUS Data Cable Datasheet

CAN Utils Source Code

 

CAN-BUS Operation Tutorial

CAN-BUS C Coding Tutorial

CAN-BUS Python Examples

 

MCP2515 Datasheet

Contact us now to discuss your project

Ready to order, contact us today for pricing or samples

Contact Us