参考资料:
【Linux应用】CAN总线编程
NVIDAI Jetson仅包含有CAN控制器:
Xavier:2xCAN
NX : 1xCAN
TX2 : 2xCAN
Nano: 无CAN
NVIDIA Jetson 自带CAN孔子去需增加CAN收发器(市面上非常多型号)如:
Microchip - MCP2557FD/2558FD
TI - SN65HVD231DG4
NXP - TJA1043
Xavier MTTCAN控制器,相关详细的应用可参考:
https://elinux.org/Jetson/AGX_Xavier_CAN
CSDN NVIDIA Xavier CAN
NX MTTCAN控制器,相关详细的应用可参考:
How to using CAN In nx devkit
然而在很多场景下,Jetson 自带的CAN控制器是不够用的,此时就需要额外扩展CAN接口,常见的CAN扩展方式包括:PCIe\Ethernet\USB\SPI\UART等等
下面介绍的是SPI转CAN,型号包括:
Microchip MCP2515
Microchip MCP2518FD (2xCS信号,扩展2xCAN 接口)
首先是Seeed 的树莓派的CAN BUS FD Shield(MCP2518FD)在Jetson Nano、NX、Xavier上的应用,其中官方已经提供了Nano上的参考软件、硬件接口等
https://wiki.seeedstudio.com/2-Channel-CAN-BUS-FD-Shield-for-Raspberry-Pi/
Jetson Nano (官方验证)
步骤1、
git clone https://github.com/Seeed-Studio/seeed-linux-dtoverlays
步骤2、编译dtbo 和 driver
cd seeed-linux-dtoverlays
export CUSTOM_MOD_LIST="CAN-HAT"; make all_jetsonnano
步骤3、安装驱动
sudo -E make install_jetsonnano
步骤4、安装dtbo
sudo cp overlays/jetsonnano/2xMCP2518FD-spi0.dtbo /boot
sudo /opt/nvidia/jetson-io/config-by-hardware.py -n "Seeed 2xMCP2518FD"
步骤5、重启机器,查看can 设备及通讯验证
nano@jetson-nano:~$ dmesg | grep spi
[ 10.867712] mcp25xxfd spi0.0 can0: MCP2518FD rev0.0 (-RX_INT -MAB_NO_WARN +CRC_REG +CRC_RX +CRC_TX +ECC -HD m:20.00MHz r:18.50MHz e:0.00MHz) successfully initialized.
[ 10.879487] mcp25xxfd spi0.1 can1: MCP2518FD rev0.0 (-RX_INT -MAB_NO_WARN +CRC_REG +CRC_RX +CRC_TX +ECC -HD m:20.00MHz r:18.50MHz e:0.00MHz) successfully initialized.
nano@jetson-nano:~$ ifconfig -a
can0: flags=128<NOARP> mtu 16
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 10 (UNSPEC)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 112
can1: flags=128<NOARP> mtu 16
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 10 (UNSPEC)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 114
#短接CAN0-L和CAN1-L, CAN0-H和CAN1-H
#通过can0发送数据
cangen can0 -mv
#通过can1接收数据
candump can1
Jetson NX
步骤1、
git clone https://github.com/Seeed-Studio/seeed-linux-dtoverlays
步骤2、修改dts、driver并编译dtbo 和 driver
cd seeed-linux-dtoverlays
export CUSTOM_MOD_LIST="CAN-HAT"; make all_jetsonnx
步骤3、安装驱动
sudo -E make install_jetsonnx
步骤4、安装dtbo
sudo cp overlays/jetsonnX/2xMCP2518FD-spi0.dtbo /boot
sudo /opt/nvidia/jetson-io/config-by-hardware.py -n "Seeed 2xMCP2518FD"
Jetson Xavier
步骤1、
git clone https://github.com/Seeed-Studio/seeed-linux-dtoverlays
步骤2、修改dts、driver并编译dtbo 和 driver
cd seeed-linux-dtoverlays
export CUSTOM_MOD_LIST="CAN-HAT"; make all_jetsonxavier
步骤3、安装驱动
sudo -E make install_all_jetsonxavier
步骤4、安装dtbo
sudo cp overlays/jetsonxavier/2xMCP2518FD-spi0.dtbo /boot
sudo /opt/nvidia/jetson-io/config-by-hardware.py -n "Seeed 2xMCP2518FD"
步骤5、重启机器,查看can 设备及通讯验证
nvidia@xavier:~$ dmesg | grep 'spi'
[ 0.794158] iommu: Adding device 3210000.spi to group 11
[ 4589.923309] mcp25xxfd spi0.0 can0: mcp25xxfd_register_done: 0
[ 4589.923329] mcp25xxfd spi0.0 can0: MCP2518FD rev0.0 (-RX_INT -MAB_NO_WARN +CRC_REG +CRC_RX +CRC_TX +ECC -HD c:40.00MHz m:20.00MHz r:18.50MHz e:0.00MHz) successfully initialized.
[ 4589.936317] mcp25xxfd spi0.1 can1: mcp25xxfd_register_done: 0
[ 4589.936333] mcp25xxfd spi0.1 can1: MCP2518FD rev0.0 (-RX_INT -MAB_NO_WARN +CRC_REG +CRC_RX +CRC_TX +ECC -HD c:40.00MHz m:20.00MHz r:18.50MHz e:0.00MHz) successfully initialized.
Socket CAN 编程
报文发送(仅发送,不接收)
nvidia@xavier:~/$ vi socket_can0_send.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
int main()
{
int s, nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame[2] = {{0}};
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);//创建套接字
strcpy(ifr.ifr_name, "can0" );
ioctl(s, SIOCGIFINDEX, &ifr); //指定 can0 设备
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind(s, (struct sockaddr *)&addr, sizeof(addr));//将套接字与 can0 绑定
//禁用过滤规则,本进程不接收报文,只负责发送
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
//生成两个报文
frame[0].can_id = 0x11;
frame[0].can_dlc = 1;
frame[0].data[0] = 'Y';
frame[1].can_id = 0x22;
frame[1].can_dlc = 1;
frame[1].data[0] = 'N';
//循环发送两个报文
while(1)
{
nbytes = write(s, &frame[0], sizeof(frame[0])); //发送 frame[0]
if(nbytes != sizeof(frame[0]))
{
printf("Send Error frame[0]\n!");
break; //发送错误,退出
}
sleep(1);
nbytes = write(s, &frame[1], sizeof(frame[1])); //发送 frame[1]
if(nbytes != sizeof(frame[0]))
{
printf("Send Error frame[1]\n!");
break;
}
sleep(1);
}
close(s);
return 0;
}
报文接收(仅接收,不发送)
nvidia@xavier:~/$ vi socket_can0_recv.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
int main()
{
int s, nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;
struct can_filter rfilter[1];
s = socket(PF_CAN, SOCK_RAW, CAN_RAW); //创建套接字
strcpy(ifr.ifr_name, "can3" );
ioctl(s, SIOCGIFINDEX, &ifr); //指定 can3 设备
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind(s, (struct sockaddr *)&addr, sizeof(addr)); //将套接字与 can3 绑定
rfilter[0].can_id = 0x11; //定义接收规则,只接收表示符等于 0x11 的报文
rfilter[0].can_mask = CAN_SFF_MASK;
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)); //设置过滤规则
while(1)
{
nbytes = read(s, &frame, sizeof(frame)); //接收报文
if(nbytes > 0) //显示报文
{
printf("ID=0x%X DLC=%d data[0]=0x%X\n",frame.can_id,frame.can_dlc,frame.data[0]);
}
}
close(s);
return 0;
}
运行:
nvidia@xavier:~/t$ ./socket_can0_send &
nvidia@xavier:~/t$ ./socket_can0_recv
ID=0x11 DLC=1 data[0]=0x59
ID=0x11 DLC=1 data[0]=0x59
...