Difference between revisions of "CAN bus Linux driver"

From ArmadeusWiki
Jump to: navigation, search
(Usage)
 
(35 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
The current CAN bus driver uses SocketCAN API which is now the official CAN API for Linux.
 
The current CAN bus driver uses SocketCAN API which is now the official CAN API for Linux.
 
SocketCAN is based on the Linux socket. Further details can be found on the links at the bottom of this page.
 
SocketCAN is based on the Linux socket. Further details can be found on the links at the bottom of this page.
 +
 +
{{Warning|Please ensure that you use a recent version of armadeus BSP (kernel version more than 2.6.38 is required) before trying the instructions described on this page! With older kernel versions you may not be able to change the bitrate.}}
  
 
==Driver installation==
 
==Driver installation==
 +
* Nothing has to be done for the [[APF51Dev]], the [[APF28Dev]], [[APF6Dev]] and the [[OPOS6ULDev]].
 +
* For the [[APF27Dev|APF27DevFull]]:
 
<pre class="host">
 
<pre class="host">
  $ make linux26-menuconfig
+
  $ make linux-menuconfig
 
</pre>
 
</pre>
  
Line 15: Line 19:
 
               CAN Device Drivers  --->
 
               CAN Device Drivers  --->
 
                   <M> Virtual Local CAN Interface (vcan)
 
                   <M> Virtual Local CAN Interface (vcan)
 +
                  <M> Platform CAN drivers with Netlink support
 +
                  [*]  CAN bit-timing calculation 
 
                   <M> Microchip 251x series SPI CAN Controller
 
                   <M> Microchip 251x series SPI CAN Controller
 
</pre>
 
</pre>
  
 
<pre class="host">
 
<pre class="host">
 +
$ make linux
 
  $ make
 
  $ make
 
</pre>
 
</pre>
Line 24: Line 31:
  
 
==Usage==
 
==Usage==
* Load all the needed drivers:
+
* Load all the required drivers (not needed on [[OPOS6UL]]):
 
<pre class="apf">
 
<pre class="apf">
modprobe can
+
# modprobe can
modprobe can-dev # if > armadeus-3.4
+
# modprobe can-dev
modprobe candev  # if <= armadeus-3.4
+
# modprobe can-raw
modprobe can-raw  
+
modprobe mcp251x
+
 
</pre>
 
</pre>
* Set the bitrate before all operations (if > armadeus-3.4 only)
+
{| border="1" cellpadding="10" cellspacing="0" summary="CAN bus modules depending on board" class="wikitable"
 +
|- style="background:#efefef;" align="center"
 +
! APF27 & APF51 || APF6
 +
|---
 +
|<pre class="apf">
 +
# modprobe mcp251x         
 +
</pre>
 +
|<pre class="apf">
 +
# modprobe flexcan       
 +
</pre>
 +
|}
 +
* Set the bitrate before all operations
 
Example:
 
Example:
Set the bitrate of the can interface can0 on 125000kbps
+
Set the bitrate of the can0 interface to 125kbps:
 
<pre class="apf">
 
<pre class="apf">
ip link set can0 up type can bitrate 125000
+
# ip link set can0 up type can bitrate 125000
 
</pre>
 
</pre>
 +
 +
{{Note| An error occurs when you try to set the bitrate with an old Linux kernel. In that case, ''echo 125000 > /sys/devices/platform/FlexCAN.0/bitrate'' was reported to be a good alternative.}}
 +
 +
* If the following error occurs when you do the last instruction :
 +
<pre class="apf">
 +
ip: either "dev" is duplicate, or "type" is garbage
 +
</pre>
 +
 +
check that this command:
 +
 +
<pre class="apf">
 +
# which ip
 +
</pre>
 +
 +
return this message:
 +
 +
<pre class="apf">
 +
/sbin/ip
 +
</pre>
 +
 +
and not this one :
 +
 +
<pre class="apf">
 +
/bin/ip
 +
</pre>
 +
 +
If the binary is installed in ''/bin'' instead of ''/sbin'', the executable file is a link to busybox and the command to set the bitrate doesn't work on busybox, so try the following instructions:
 +
 +
<pre class="host">
 +
$ make busybox-clean
 +
$ make busybox-dirclean
 +
$ make menuconfig
 +
</pre>
 +
 +
<pre class="config">
 +
Package Selection for the target  --->
 +
    Networking applications  --->
 +
        [*] iproute2
 +
</pre>
 +
 +
<pre class="host">
 +
$ make
 +
</pre>
 +
 +
Then, reflash your rootfs.
  
 
==Quick test==
 
==Quick test==
Once the driver installed, the CAN interface has to be started like a standard net interface
+
* Once the driver is installed and the bitrate is set, the CAN interface has to be started like a standard net interface:
 
<pre class="apf">
 
<pre class="apf">
 
  # ifconfig can0 up
 
  # ifconfig can0 up
 
</pre>
 
</pre>
and can be stopped like that:
+
* and can be stopped like that:
 
<pre class="apf">
 
<pre class="apf">
 
  # ifconfig can0 down
 
  # ifconfig can0 down
 
</pre>
 
</pre>
The socketCAN version can be retrieved like that:
+
* The socketCAN version can be retrieved this way:
 
<pre class="apf">
 
<pre class="apf">
 
  # cat /proc/net/can/version
 
  # cat /proc/net/can/version
 
</pre>
 
</pre>
The socketCAN statistics can be retrieved like that:
+
* The socketCAN statistics can be retrieved this way:
 
<pre class="apf">
 
<pre class="apf">
 
  # cat /proc/net/can/stats
 
  # cat /proc/net/can/stats
 
</pre>
 
</pre>
The default baud rate is 250kbauds
 
  
 
==Userspace tools==
 
==Userspace tools==
*Several tools are provided by socketCAN:
+
Several tools are provided by socketCAN:
**'''candump''': dump traffic on a CAN network
+
*'''candump''': dump traffic on a CAN network
**'''cansend''': simple command line tool to send CAN-frames via CAN_RAW sockets
+
The following command shows the received message from the CAN bus
**'''cangen''': CAN frames generator for testing purpose
+
<pre class="apf">
**'''canplayer''': send CAN frames from a file to a CAN interface
+
candump can0
 +
</pre>
 +
*'''cansend''': simple command line tool to send CAN-frames via CAN_RAW sockets  
 +
exemple :
 +
The following command sends 3 bytes on the bus (0x1E, 0x10, 0x10) with the identifier 500.
 +
<pre class="apf">
 +
cansend can0 500#1E.10.10
 +
</pre>
 +
You can send a remote request message
 +
<pre class="apf">
 +
cansend can0 500#R
 +
</pre>
 +
The information with the identifier 500 will be available on the bus when the device receive the remote request message
 +
*'''cangen''': CAN frames generator for testing purpose
 +
*'''canplayer''': send CAN frames from a file to a CAN interface
  
*These tools can be compiled and installed on the target by means of the Buildroot menuconfig:
+
These tools can be compiled and installed on the target by means of the Buildroot menuconfig:
 
<pre class="host">
 
<pre class="host">
 
$ make menuconfig
 
$ make menuconfig
Line 73: Line 147:
 
     Networking  --->
 
     Networking  --->
 
         [*]  Socket CAN
 
         [*]  Socket CAN
        [*]    install test tools
 
 
</pre>
 
</pre>
 
<pre class="host">
 
<pre class="host">
Line 79: Line 152:
 
</pre>
 
</pre>
 
then, reflash your rootfs.
 
then, reflash your rootfs.
 +
 +
==Automatically launch CAN interface at startup==
 +
* Add the following lines to ''/etc/network/interfaces'' configuration file:
 +
<pre class="APF">
 +
auto can0
 +
iface can0 inet manual
 +
        pre-up /sbin/ip link set $IFACE type can bitrate 125000 on
 +
        up /sbin/ifconfig $IFACE up
 +
        down /sbin/ifconfig $IFACE down
 +
</pre>
  
 
==Links==
 
==Links==
Line 85: Line 168:
 
* http://ww1.microchip.com/downloads/en/DeviceDoc/21801e.pdf (MCP2515 datasheet)
 
* http://ww1.microchip.com/downloads/en/DeviceDoc/21801e.pdf (MCP2515 datasheet)
 
* http://www.kvaser.com/can/protocol/index.htm (CAN introduction)
 
* http://www.kvaser.com/can/protocol/index.htm (CAN introduction)
 +
* [[User:KevinJ#Communication_with_the_CAN_Bus_of_a_car|Communication with a car's CAN bus]]
  
 
[[Category:Linux drivers]]
 
[[Category:Linux drivers]]
 
[[Category:CAN bus]]
 
[[Category:CAN bus]]

Latest revision as of 10:10, 31 July 2018

The current CAN bus driver uses SocketCAN API which is now the official CAN API for Linux. SocketCAN is based on the Linux socket. Further details can be found on the links at the bottom of this page.

Warning Warning: Please ensure that you use a recent version of armadeus BSP (kernel version more than 2.6.38 is required) before trying the instructions described on this page! With older kernel versions you may not be able to change the bitrate.


Driver installation

 $ make linux-menuconfig
Networking support  --->
    <M>   CAN bus subsystem support ---> 
        --- CAN bus subsystem support
        <M>   Raw CAN Protocol (raw access with CAN-ID filtering)
        <M>   Broadcast Manager CAN Protocol (with content filtering)
              CAN Device Drivers  --->
                  <M> Virtual Local CAN Interface (vcan)
                  <M> Platform CAN drivers with Netlink support
                  [*]   CAN bit-timing calculation   
                  <M> Microchip 251x series SPI CAN Controller
 $ make linux
 $ make
  • Reflash kernel and rootfs

Usage

  • Load all the required drivers (not needed on OPOS6UL):
# modprobe can
# modprobe can-dev
# modprobe can-raw
APF27 & APF51 APF6
# modprobe mcp251x          
# modprobe flexcan         
  • Set the bitrate before all operations

Example: Set the bitrate of the can0 interface to 125kbps:

# ip link set can0 up type can bitrate 125000
Note Note: An error occurs when you try to set the bitrate with an old Linux kernel. In that case, echo 125000 > /sys/devices/platform/FlexCAN.0/bitrate was reported to be a good alternative.


  • If the following error occurs when you do the last instruction :
ip: either "dev" is duplicate, or "type" is garbage

check that this command:

# which ip

return this message:

/sbin/ip

and not this one :

/bin/ip

If the binary is installed in /bin instead of /sbin, the executable file is a link to busybox and the command to set the bitrate doesn't work on busybox, so try the following instructions:

$ make busybox-clean
$ make busybox-dirclean
$ make menuconfig
Package Selection for the target  --->
    Networking applications  --->
        [*] iproute2
$ make 

Then, reflash your rootfs.

Quick test

  • Once the driver is installed and the bitrate is set, the CAN interface has to be started like a standard net interface:
 # ifconfig can0 up
  • and can be stopped like that:
 # ifconfig can0 down
  • The socketCAN version can be retrieved this way:
 # cat /proc/net/can/version
  • The socketCAN statistics can be retrieved this way:
 # cat /proc/net/can/stats

Userspace tools

Several tools are provided by socketCAN:

  • candump: dump traffic on a CAN network

The following command shows the received message from the CAN bus

candump can0
  • cansend: simple command line tool to send CAN-frames via CAN_RAW sockets

exemple : The following command sends 3 bytes on the bus (0x1E, 0x10, 0x10) with the identifier 500.

cansend can0 500#1E.10.10

You can send a remote request message

cansend can0 500#R

The information with the identifier 500 will be available on the bus when the device receive the remote request message

  • cangen: CAN frames generator for testing purpose
  • canplayer: send CAN frames from a file to a CAN interface

These tools can be compiled and installed on the target by means of the Buildroot menuconfig:

$ make menuconfig
Package Selection for the target  --->
    Networking  --->
        [*]   Socket CAN
$ make

then, reflash your rootfs.

Automatically launch CAN interface at startup

  • Add the following lines to /etc/network/interfaces configuration file:
auto can0
iface can0 inet manual
        pre-up /sbin/ip link set $IFACE type can bitrate 125000 on
        up /sbin/ifconfig $IFACE up
        down /sbin/ifconfig $IFACE down

Links