odroid-m1:application_note:gpio:can-bus_rk

How to configure and use CAN bus

This page explains how to enable the CAN bus on ODROID-M1.
Detail instruction to communicate data with a SN65HVD230 CAN transceiver.
can_bus

target
root@odroid:~# apt update && apt upgrade

Apply Device tree overlay

Add the text “can0”-can(zero)- on the Device Tree Overlay section in your file located at /boot/config.ini.

target
; Device Tree Overlay
overlay_resize=16384
overlay_profile=
overlays="spi0 i2c0 i2c1 uart0 can0"

Then reboot to apply the changes. You can check if the modules loaded.

target
root@odroid:~# lsmod | grep can
rockchip_can   20480  0
can_dev        24576  0
root@odroid:~#
Verify the CAN host driver is registered correctly
target
root@odroid:~# ls /sys/class/net/
can0  eth0  lo
root@odroid:~# ifconfig can0
can0: flags=128<NOARP>  mtu 16
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 10  (UNSPEC)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 55
 
root@odroid:~# 
Power on CAN hardware

Set the bitrate before all operations Example: Set the bitrate of the can0 interface to 125kbps:

target
root@odroid:~# ip link set can0 type can bitrate 125000 triple-sampling on
root@odroid:~# ifconfig can0 up
root@odroid:~# ifconfig
can0: flags=193<UP,RUNNING,NOARP>  mtu 16
        unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 10  (UNSPEC)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 55
 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.8  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::e160:7710:5360:f82a  prefixlen 64  scopeid 0x20<link>
        ether 02:00:00:0d:1d:01  txqueuelen 1000  (Ethernet)
        RX packets 24  bytes 6066 (6.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 54  bytes 6420 (6.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 45  
 
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 129  bytes 10117 (10.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 129  bytes 10117 (10.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
root@odroid:~#
Installing SocketCAN utils

can-utils package is a collection of CAN drivers and networking tools for Linux. It allows interfacing with CAN bus devices in a similar fashion as other network devices.

sudo apt install can-utils
Loopback test on a single CAN port

loopback mode on can0

ifconfig can0 down
ip link set can0 type can bitrate 125000 loopback on
ifconfig can0 up
ip -details link show can0
target
root@odroid:~# ifconfig can0 down
root@odroid:~# ip link set can0 type can bitrate 125000 loopback on
root@odroid:~# ifconfig can0 up
root@odroid:~# ip -details link show can0
3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc fq_codel state UNKNOWN mode DEFAULT group default qlen 10
    link/can  promiscuity 0 
    can <LOOPBACK,TRIPLE-SAMPLING> state ERROR-ACTIVE restart-ms 0 
          bitrate 125000 sample-point 0.850 
          tq 400 prop-seg 8 phase-seg1 8 phase-seg2 3 sjw 1
          mcp251x: tseg1 3..16 tseg2 2..8 sjw 1..4 brp 1..64 brp-inc 1
          clock 5000000numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
root@odroid:~# 

The following command shows the received message from the CAN bus

candump can0

On second terminal, The following command sends 3 bytes on the bus (0x11, 0x22, 0x33) with the identifier 500.

cansend can0 500#11.22.33

J2 Header Pin # CAN Module Pin # Role
28 4 CAN Tx
27 3 CAN Rx
9 2 GND
1 1 3.3V

mcp2515 refer: https://wiki.odroid.com/common/application_note/gpio/can-bus#hw_connection

Power-up both boards Type the following into the shell of both boards for configuration the CAN bus device:

ip link set can0 type can bitrate 125000 triple-sampling on
ifconfig can0 up

Type the following to the shell of board 1 (which is used for testing receiving over can0 device):

candump can0

Type the following to the shell of board 2 (which is used for testing sending data packets over can0 device):

cansend can0 500#11.22.33

At this point, board 1 will receive the data packet sent from board 2:

target
root@odroid:~# candump can0
  can0  500   [3]  11 22 33
  can0  500   [3]  11 22 33
  • odroid-m1/application_note/gpio/can-bus_rk.txt
  • Last modified: 2022/09/01 12:40
  • by steve.jeong