CAN bus

From Take-Air Wiki
Jump to navigation Jump to search

The following instructions assume that the user is connected over SSH or use a terminal in a remote desktop.

Enable CAN

  • Check if drivers iniatialized properly using dmesg | grep can. Results should include:
$ dmesg | grep can
[    1.651756] mcp251x spi0.0 can0: MCP2515 successfully initialized.
[    1.898432] can: controller area network core (rev 20120528 abi 9)
[    1.898528] can: raw protocol (rev 20120528)
  • To turn on the can0 interface:
sudo ip link set can0 type can bitrate 1000000
sudo ip link set can0 up
  • Check that the interrupt got enabled using sudo cat /proc/interrupts | grep mcp (interrupt does not show up until the link is up):
$ sudo cat /proc/interrupts | grep mcp
133:          2          0          0          0      GPIO  34 Edge      mcp251x

Loopback test

  • Loopback mode allows to test if CAN is working properly. To enable it:
sudo ip link set can0 down
sudo ip link set can0 type can loopback on
sudo ip link set can0 type can bitrate 1000000
sudo ip link set can0 up
  • Two terminals are required to test:
    • First terminal:
    cansend can0 001#1122334455667788
    • Second terminal:
    candump can0
  • If the loopback test is successful, it should show:
candump can0
can0  001   [8]  11 22 33 44 55 66 77 88
can0  001   [8]  11 22 33 44 55 66 77 88
  • Turn off the loopback:
sudo ip link set can0 down
sudo ip link set can0 type can loopback off
sudo ip link set can0 type can bitrate 1000000

Send and receive data

  • To receive data:
candump can0
  • To send single frame:
cansend can0 001#1122334455667788
  • To send extended frame (used by ArduPilot):
cansend can0 00100000#1122334455667788