TX2平台CAN总线收发功能的测试

前言

项目实现过程中需要将获取的数据信息通过CAN总线传输到控制规划模块,本文主要介绍如何在TX2平台测试CAN总线的收发功能。

TX2是英伟达旗下为嵌入式平台人工智能应用开发出的一个硬件平台,TX1没有CAN bus功能,TX2有。

CAN bus作为TX2的外设,一般需要驱动,也有一些功能函数,可以用这些函数进行应用编程。Linux下的canbus是通过socket进行can传输的。

大致实现步骤是:安装驱动 -> 安装开源canbus通信的user app -> 连接CAN接口以及canbus收发器 -> 测试CAN总线收发功能;

实现方法

参考英伟达官方论坛here,可以找到官方的回复,步骤如下:

These are the steps can be followed to enable and check CAN on TX2.
1. Enable kernel support for mttcan(Other required modules are already supported)
CONFIG_MTTCAN = m (Here mttcan is compiled as a module)

使能canbus需要在tegra18_defconfig配置文件中配置CONFIG_MTTCAN=m之后重新编译内核才能使用。

本人拿到的板子这一步已经完成,直接进行之后的步骤;


2. Insert CAN BUS subsystem support module.
modprobe can

3. Insert Raw CAN protocol module (CAN-ID filtering)
modprobe can_raw
4. Real CAN interface support (for our case, it is: mttcan)
modprobe mttcan (dependent module is can_dev: can driver with netlink support)

modprobe是在linux的shell运行的命令行,用于安装canbus模块;

Linux将不常用的模块做成.ko文件,在需要使用时可以将其进行载入内核,降低系统的开销;


5. CAN interface settings for both the controllers
ip link set can0 type can bitrate 500000 dbitrate 2000000 berr-reporting on fd on
ip link set up can0
ip link set can1 type can bitrate 500000 dbitrate 2000000 berr-reporting on fd on
ip link set up can1

CAN interfaces are up now. Use ifconfig to list all the interfaces which are up.

配置canbus的属性(命令行配置),和串口的波特率设置类似,同驱动编程中的ioctl.


6. Installation of user app to check CAN communication
sudo apt-get install can-utils

安装第三方开源app can-utils测试canbus的收发功能;


7. Commands to run to check CAN packet send/receive
broadcasting a can data packet:(发送程序)
cansend <can_interface> <can_frame>
e.g. cansend can0 123#abcdabcd

Receiving a can data packet:(接收程序)
candump can_interface
e.g. candump can1
Different tools (i.e. cangen, cangw etc) can be used for various filtering options.

命令行使用应用程序测试canbus的收发功能;


8. To check the interface statistics
ip -details -statistics link show can0
ip -details -statistics link show can1

检测canbus的状态;

以上完成在TX2平台使用命令行测试CAN通讯,有机会会介绍canbus的API和应用开发函数,具体可参看github上can-utils的开源程序here.

完整的操作过程

These are the steps can be followed to enable and check CAN on TX2.

1. Enable kernel support for mttcan(Other required modules are already supported)
​ CONFIG_MTTCAN = m (Here mttcan is compiled as a module)

2. Insert CAN BUS subsystem support module.
modprobe can

3. Insert Raw CAN protocol module (CAN-ID filtering)
modprobe can_raw

4. Real CAN interface support (for our case, it is: mttcan)
modprobe mttcan (dependent module is can_dev: can driver with netlink support)

5. CAN interface settings for both the controllers
ip link set can0 type can bitrate 500000 dbitrate 2000000 berr-reporting on fd on
ip link set up can0
ip link set can1 type can bitrate 500000 dbitrate 2000000 berr-reporting on fd on
ip link set up can1

CAN interfaces are up now. Use ifconfig to list all the interfaces which are up.

6. Installation of user app to check CAN communication
sudo apt-get install can-utils

7. Commands to run to check CAN packet send/receive
broadcasting a can data packet:
cansend <can_interface> <can_frame>
e.g. cansend can0 123#abcdabcd

Receiving a can data packet:
candump can_interface
e.g. candump can1

Different tools (i.e. cangen, cangw etc) can be used for various filtering options.

8. To check the interface statistics
ip -details -statistics link show can0
ip -details -statistics link show can1
View Code

 

问题

1.使用CAN分析仪测试CAN总线的收发功能,打开can分析仪的应用工具CANPro,连接TX2平台can接口、CAN分析仪和应用工具主机,使用命令行测试can口的收发功能;

2.测试成功,但是每次重启或者开机需要重新配置CAN的环境,解决方法是在/etc/rc.local中添加canbus模块和属性(2-5步骤)即可;

modprobe can
modprobe can_raw
modprobe mttcan
ip link set can0 type can bitrate 500000 dbitrate 2000000 berr-reporting on fd on
ip link set can0 type can tq 25 prop-seg 34 phase-seg1 35 phase-seg2 10 sjw 20
ip link set up can0
ip link set can1 type can bitrate 500000 dbitrate 2000000 berr-reporting on fd on
ip link set can1 type can tq 25 prop-seg 34 phase-seg1 35 phase-seg2 10 sjw 20
ip link set up can1
View Code

3.若各个设备连接没有问题,且相关配置也已完成,但是不能正常收发,需要查看板子的can接口硬件部分PIN脚是否连接正确;(重要!!!) 博主这个问题就浪费了一些时间,经过硬件工程师查看,发现是板子can接口硬件连接有问题;

 4. 在TX2上可以生成模拟CAN设备vcan0,但是cansend和candump结果如下:

write: Network is down

还有,对于can0和can1,cansend应该是完成发送了,但是candump看不到数据,感觉一直在运行;

猜想,应该是TX2板子没有CAN收发器,需要借助CAN分析仪等工具收发数据;

 

 

参考

1.英伟达官网论坛

2.CSDN系列博客

3.can-utils开源程序

posted on 2018-04-11 17:01  鹅要长大  阅读(2540)  评论(0编辑  收藏  举报

导航