第一步:必须保证你的接线正确
xavier agx 像上图一样需要外接CAN收发器,同时xavier agx与usbcan(分析仪)都需要接120欧电阻(CAN总线的要求)
第二步用命令行初始化CAN(FD)
# 安装busybox, 需要里面的devmem工具
sudo apt install busybox
# 检查当前的寄存器值
sudo busybox devmem 0x0c303000 # 0x0000C055
sudo busybox devmem 0x0c303008 # 0x0000C055
sudo busybox devmem 0x0c303010 # 0x0000C059
sudo busybox devmem 0x0c303018 # 0x0000C059
# 用devmem修改寄存器
sudo busybox devmem 0x0c303000 32 0x0000C400
sudo busybox devmem 0x0c303008 32 0x0000C458
sudo busybox devmem 0x0c303010 32 0x0000C400
sudo busybox devmem 0x0c303018 32 0x0000C458
# 改完后检查
sudo busybox devmem 0x0c303000 # 0x0000C400
sudo busybox devmem 0x0c303008 # 0x0000C458
sudo busybox devmem 0x0c303010 # 0x0000C400
sudo busybox devmem 0x0c303018 # 0x0000C458
#寄存器改好了, 该挂载了, 使用modprobe:
sudo modprobe can
sudo modprobe can_raw
sudo modprobe mttcan
# 检查挂载
lsmod
#下面是挂载后, 用lsmod检查的情况:
xavier@xavier-c:~$ lsmod
Module Size Used by
mttcan 66187 0
can_dev 13306 1 mttcan
can_raw 10388 3
can 46600 1 can_raw
fuse 103841 3
zram 26166 7
overlay 48691 0
hid_logitech_hidpp 22721 0
hid_logitech_dj 13813 0
nvgpu 1569917 20
bluedroid_pm 13912 0
ip_tables 19441 0
x_tables 28951 1 ip_tables
#挂载好了, 还需要配置一些波特率之类的参数:
#配置为1Mbps的标准CAN
sudo ip link set can0 type can bitrate 1000000
sudo ip link set can1 type can bitrate 1000000
#或者配置为仲裁段500k, 数据段2M的的CANFD:
sudo ip link set can0 type can bitrate 500000 dbitrate 2000000 berr-reporting on fd on
sudo ip link set can1 type can bitrate 500000 dbitrate 2000000 berr-reporting on fd on
#打开CAN控制器:
sudo ip link set up can0
sudo ip link set up can1
# 检查
ifconfig
#关闭CAN控制器:
sudo ip link set down can0
sudo ip link set down can1
# 检查
ifconfig # 关闭的话里面就没有can0, can1了
第三步:CAN(FD)的收发
# 123是十六进制帧ID, #后面是8字节十六进制数, 可以用.相隔也可以不用
cansend can0 123#99.95.42.07.2B.96.66.6E
cansend can1 123#99.95.42.07.2B.96.66.6E
# 随机发送
cangen -v can0
cangen -v can1
发送数据的格式(均为十六进制, 3字节是标准帧, 8字节是扩展帧, #跟标准数据帧, #R跟遥控帧, ##跟CANFD帧
):
<can_frame>:
<can_id>#{R|data} for CAN 2.0 frames
<can_id>##<flags>{data} for CAN FD frames
<can_id>:
can have 3 (SFF) or 8 (EFF) hex chars
{data}:
has 0..8 (0..64 CAN FD) ASCII hex-values (optionally separated by '.')
<flags>:
a single ASCII Hex value (0 .. F) which defines canfd_frame.flags
Examples:
5A1#11.2233.44556677.88 / 123#DEADBEEF / 5AA# / 123##1 / 213##311
1F334455#1122334455667788 / 123#R for remote transmission request.
第四步:CAN接收
candump can0
candump can1