Difference between revisions of "CAN bus"

From Take-Air Wiki
Jump to navigation Jump to search
(Created page with "== Enable CAN == * Check if drivers iniatialized properly using <code>lsmod | grep can</code> and <code>dmesg | grep can</code>. Results should be: : <syntaxhighlight lang="ba...")
 
Line 1: Line 1:
 
== Enable CAN ==
 
== Enable CAN ==
 +
* Enable required modules (not persistent):
 +
: <syntaxhighlight lang="bash">sudo modprobe can
 +
sudo modprobe mcp251x
 +
</syntaxhighlight>
 
* Check if drivers iniatialized properly using <code>lsmod | grep can</code> and <code>dmesg | grep can</code>. Results should be:
 
* Check if drivers iniatialized properly using <code>lsmod | grep can</code> and <code>dmesg | grep can</code>. Results should be:
 
: <syntaxhighlight lang="bash">$ lsmod | grep can
 
: <syntaxhighlight lang="bash">$ lsmod | grep can

Revision as of 10:36, 27 October 2021

Enable CAN

  • Enable required modules (not persistent):
sudo modprobe can
sudo modprobe mcp251x
  • Check if drivers iniatialized properly using lsmod | grep can and dmesg | grep can. Results should be:
$ lsmod | grep can
can                    46600  0
can_dev                13528  1 mcp251x
$ dmesg | grep can
[   13.950602] mcp251x spi0.0 can0: MCP2515 successfully initialized.
[  120.394646] can: controller area network core (rev 20120528 abi 9)
  • 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