Using CAN in Linux

Many of our Trizeps modules got an integrated CAN controller.
On others, this function may be added through additional chips like the Microchip MCP2518FD.

Detect & Init

See if CAN-adapter got registered:

root@imx8mmevk:~# ifconfig -a can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 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) eth0 Link encap:Ethernet HWaddr 8a:7b:51:54:3a:65 UP BROADCAST MULTICAST MTU:1500 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:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) eth1 Link encap:Ethernet HWaddr 0a:ea:42:e6:ee:90 UP BROADCAST MULTICAST MTU:1500 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:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:90 errors:0 dropped:0 overruns:0 frame:0 TX packets:90 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:6708 (6.5 KiB) TX bytes:6708 (6.5 KiB)

Activate Interface:

root@imx8mmevk:~# sudo ip link set can0 up type can bitrate 1000000 [ 68.603002] IPv6: ADDRCONF(NETDEV_CHANGE): can0: link becomes ready root@imx8mmevk:~# sudo ifconfig can0 txqueuelen 65536

On Debian and similar systems you can run CAN at startup by adding the following lines to your /etc/network/interfaces:

auto can0 iface can0 can static bitrate 1000000 txqueuelen 65536

Testing CAN

The following tools are contained in the package can-utils. On Debian and it's relatives you can install it with:

cangen

Use cangen to generate CAN-packets to send over the interface. See 'cangen –help' for more information.
Example:

candump

Use candump to show received CAN-packets.