GUF-Yocto-jethro-9.0-r7707-0
i.MX6
User Manual
6.8
CAN
CAN bus devices are controlled through the SocketCAN framework in the Linux kernel. As a consequence, CAN
interfaces are network interfaces. Applications receive and transmit CAN messages via the BSD Socket API.
CAN interfaces are configured via the netlink protocol. Additionally, Garz & Fricke Linux systems are shipped
with the
canutils
package to control and test the CAN bus from the command line.
On i.MX6 the CAN bus is physically available on connector X39.
Example 1
shows how a CAN bus interface can be set up properly for 125 kBit/s from a Linux console:
root@santaro:~# canconfig can0 bitrate 125000
root@santaro:~# ifconfig can0 up
Note:
Due to the use of the busybox version of the
ip
tool the following sequence does
NOT
work on
Garz & Fricke Linux systems:
root@santaro:~# ip link set can0 type can bitrate 125000
root@santaro:~# ifconfig can0 up
As already stated above, CAN messages can be sent through the BSD Socket API. The structure for a CAN
message is defined in the kernel header
include/linux/can.h
:
struct can_frame {
u32 can_id; /* 29 bit flags */
u8 can_dlc; /* data length code: 0 .. 8 */
u8 data[8];
};
Example 2
shows how to initialize SocketCAN from a C program:
int iSock;
struct sockaddr_can addr;
iSock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
addr.can_family = AF_CAN;
addr.can_ifindex = if_nametoindex("can0");
bind(iSock, (struct sockaddr *)&addr,
sizeof(addr);
Example 3
shows how a CAN message is sent from a C program:
struct can_frame frame;
frame.can_id = 0x123;
frame.can_dlc = 1;
frame.data[0] = 0xAB;
nbytes = write(iSock, &frame,
sizeof(frame));
Example 4
shows how a CAN message is received from a C program:
struct can_frame frame;
nbytes = read(iSock, &frame, sizeof(frame));
if (nbytes > 0) {
printf("ID=0x%X DLC=%d data[0]=0x%X\n",
frame.can_id,
40