系列文章目录
前言
适用于 Jetson AGX Orin、Jetson Orin NX、Jetson Orin Nano、Jetson Xavier NX 系列和 Jetson AGX Xavier 系列。
本主题介绍英伟达™(NVIDIA®)SoC "始终开启 "模块上的时间触发 CAN (TTCAN) 控制器,以及如何在用户空间中使用该控制器。
一、重要功能
时间触发 CAN 控制器有几个重要功能:
- 它支持标准和扩展帧传输。
- CAN 总线比特率可配置为 10 kbps 至 1 Mbps。
- 它支持 CAN FD 模式,最大数据比特率为 15 Mbps。所有类型的收发器都能达到 5 Mbps 的数据比特率。
- 如果通过用户空间 sysfs 节点配置 TDCR(传输延迟补偿寄存器),它可以提供更高的数据比特率。
二、Jetson 平台详情
下表列出了各支持平台 CAN 实施的详细信息。
Property | Jetson Orin NX and Nano | Jetson AGX Orin | Jetson Xavier NX series | Jetson AGX Xavier series |
Number of controllers | 1 | 2 | 1 | 2 |
Controller base address | mttcan@c310000 |
mttcan@c310000 mttcan@c320000 |
mttcan@c310000 |
mttcan@c310000 mttcan@c320000 |
Pins on Jetson carrier board | J17:CAN_RX : [1]CAN_TX : [2] |
J30 (40-pin header):CAN0_DIN : [29]CAN0_DOUT : [31]CAN1_DIN : [37]CAN1_DOUT : [33] |
J17:CAN_RX : [1]CAN_TX : [2] |
J30 (40-pin header):CAN0_DIN : [29]CAN0_DOUT : [31]CAN1_DIN : [37]CAN1_DOUT : [33] |
Pinmux can0_din |
Addr: 0x0c303018 Value: 0xc458 |
Addr: 0x0c303018 Value: 0xc458 |
Addr: 0x0c303018 Value: 0xc458 |
Addr: 0x0c303018 Value: 0xc458 |
Pinmux can0_dout |
Addr: 0x0c303010 Value: 0xc400 |
Addr: 0x0c303010 Value: 0xc400 |
Addr: 0x0c303010 Value: 0xc400 |
Addr: 0x0c303010 Value: 0xc400 |
Pinmux can1_din |
n/a | Addr: 0x0c303008 Value: 0xc458 |
n/a | Addr: 0x0c303008 Value: 0xc458 |
Pinmux can1_dout |
n/a | Addr: 0x0c303000 Value: 0xc400 |
n/a | Addr: 0x0c303000 Value: 0xc400 |
Default pin configuration | SFIO: CAN functionality | GPIO | SFIO: CAN functionality | GPIO |
三、启用 CAN
NVIDIA® Jetson™ 设备与 CAN 的接口要求收发器至少支持 3.3 V 电压和基于 Jetson 平台规格的传输速率。有关详细信息,请参阅 "重要特性 "部分。
NVIDIA 推荐将 WaveShare SN65HVD230 CAN 板用于开发系统。对于生产设备,收发器的选择取决于您的应用要求。
从收发器到 Jetson 载板的连接如下:
- 收发器 Rx 至 Jetson CAN_RX
- 收发器 Tx 至 Jetson CAN_TX
- 收发器 VCC 至 Jetson 3.3V 引脚
- 收发器 GND 至 Jetson GND 引脚
该图显示了一个正确连接的示例:
3.1 内核 DTB
默认情况下,内核设备树中启用了 "+"节点:
mttcan@c310000 { status = "okay"; }; mttcan@c320000 { status = "okay"; };
您也可以从用户空间检查该字段:
cat /proc/device-tree/mttcan\@c310000/status
3.2 Pinmux
确保 pinmux 寄存器设置已应用,如 Jetson 平台详情一节中的表格所示。
可以通过多种方式更新 Pinmux:
- 使用 BusyBox devmem 工具写入硬件寄存器。更改只会持续到系统重启。
输入此命令安装 BusyBox:
sudo apt-get install busybox
本例将数值 0x458 写入寄存器地址 0x0c303020:
busybox devmem 0x0c303020 w 0x458
- 更新各平台 pinmux 配置文件并闪存更新后的文件。
- 有关如何启用引脚的信息,请参阅主题 "配置 Jetson 扩展头 "中的 "运行 JetsonIO"。
四、内核驱动程序
按所示顺序加载所有必要的 CAN 内核驱动程序。
4.1 加载 CAN 内核驱动程序
- 安装 CAN BUS 子系统支持模块:
sudo modprobe can
- 或者
insmod /lib/modules/5.x.xxx-tegra/kernel/net/can/can.ko
- 插入原始 CAN 协议模块(CAN-ID 过滤):
sudo modprobe can_raw
- 或者
insmod /lib/modules/5.x.xxx-tegra/kernel/net/can/can-raw.ko
- 添加真正的 CAN 接口支持(针对 Jetson、mttcan):
sudo modprobe mttcan
- 或者
insmod /lib/modules/5.x.xxx-tegra/kernel/drivers/net/can/mttcan/native/mttcan.ko
编写为 Python 程序 setup.py
# I Terminal # sudo python3 /home/subsea/CanTesting/startup.py #"sudo busybox devmem 0x0c303018 w 0xc458", #"sudo busybox devmem 0x0c303010 w 0xc400", # Kjører for hver komando # Sende can 2.0 melding "sudo cansend can0 444#04" # Sende canFD melding "sudo cansend can0 144##0.04" #"sudo ip link set can0 up type can bitrate 500000 dbitrate 2000000 berr-reporting on fd on", import os commands = [ "sudo ip link set can0 down", "sudo modprobe can", "sudo modprobe can_raw", "sudo modprobe mttcan", "sudo busybox devmem 0x0c303018 w 0xc458", "sudo busybox devmem 0x0c303010 w 0xc400", "sudo ip link set can0 up type can bitrate 1000000 dbitrate 2500000 berr-reporting on fd on" #"sudo ip link set can0 up type can bitrate 1000000 dbitrate 2500000 fd on" ] for command in commands: os.system(command)
/etc/rc.local
#!/bin/bash # Load CAN kernel drivers modprobe can modprobe can_raw modprobe mttcan # Check if the modules are loaded successfully if lsmod | grep -q "^can" && lsmod | grep -q "^can_raw" && lsmod | grep -q "^mttcan"; then echo "CAN kernel drivers loaded successfully." else echo "Failed to load CAN kernel drivers." fi # Set up pinmux configurations busybox devmem 0x0c303018 w 0xc458 busybox devmem 0x0c303010 w 0xc400 # Check pinmux configurations pinmux_can0_din=$(busybox devmem 0x0c303018) pinmux_can0_dout=$(busybox devmem 0x0c303010) if [ "$pinmux_can0_din" = "0xc458" ] && [ "$pinmux_can0_dout" = "0xc400" ]; then echo "Pinmux configurations applied successfully." else echo "Failed to apply pinmux configurations." fi
sudo chmod +x /etc/rc.local
sudo systemctl enable rc-local
sudo systemctl start rc-local.service sudo systemctl status rc-local.service
To load it automatically, you could configure the following line in kernel/kernel-5.10/arch/arm64/configs/defconfig and re-build kernel image.
- CONFIG_MTTCAN=m
+ CONFIG_MTTCAN=y
五、管理网络
要使用网络,首先必须在网络上调用 CAN 设备,并安装一组 CAN 实用程序用于测试。然后就可以传输数据包,必要时还可以调试网络接口。
5.1 设置接口属性
这些示例命令将网络接口设置为使用 FD(灵活数据)模式,总线比特率为 500 kbps,数据比特率为 1 Mbps:
sudo ip link set can0 up type can bitrate 500000 dbitrate 1000000 berr-reporting on fd on sudo ip link set can1 up type can bitrate 500000 dbitrate 1000000 berr-reporting on fd on
5.2 安装 CAN 实用程序
输入命令
sudo apt-get install can-utils
5.3 传输数据包
- 此示例命令向 can0 传输 CAN ID 为 123、数据为 "abcdabcd "的数据包:
cansend can0 123#abcdabcd
- 此示例命令从连接到总线的任何 CAN 节点接收数据包:
candump -x any &
- 该示例命令向 can0 传输一个 FD 帧,CAN ID 为 123,标志位为 1,数据为 "abcdabcd":
cansend can0 123##1abcdabcd
有关使用 cansend 的更多信息,请输入此命令:
cansend --help
您可以使用其他工具(如 cangen)来获得不同的过滤选项。
六、调试方法
本节讨论 CAN 网络问题的调试方法。
6.1 环回测试
您可以执行环回测试,以确定控制器是否正常工作。
执行环回测试
- 短接 Jetson 载板 CAN0 的 Tx 和 Rx 引脚。
- 启用 CAN 驱动程序。(更多信息,请参阅 "内核驱动程序")。
sudo ip link set can0 type can bitrate 500000 loopback on sudo ip link set can0 up candump can0 & cansend can0 123#abcdabcd
如果环回测试成功,最后一条命令将显示此信息:
can0 123 [4] AB CD AB CD can0 123 [4] AB CD AB CD
6.2 其他方法
在适当情况下,其他几种调试技术也可能有用:
- 如果环回测试表明控制器工作正常,但仍无法发送或接收数据包,请尝试重新连接收发器,并确认连接正确无误。
- 检查是否正确执行了启用 CAN 的所有必要步骤。
- 连接示波器,查看总线是否正常。
- 如果设备在发送过程中记录了 "No buffer space available"(无可用缓冲空间)信息,请输入此命令以使用轮询机制:
cangen -L 8 can0 -p 1000
七、获得更高的比特率
通过用户空间 sysfs 节点配置 TDCR(传输延迟补偿寄存器),可以获得更高的数据比特率。
请确保所用收发器能够支持更高的比特率。
7.1 为更高比特率配置 TDCR
- 输入命令
echo 0x600 > /sys/devices/c320000.mttcan/net/can1/tdcr
- 在网络上调用 CAN 接口并传输数据包。更多信息,请参阅管理网络。
八、在 T194 平台上更改 CAN 父时钟
适用于 仅适用于 Jetson Xavier NX 系列和 Jetson AGX Xavier 系列设备
在基于 NVIDIA T194 SoC 的设备上,CAN 父时钟是 PLL_C。PLL_C 时钟的核心时钟频率设置为 34 MHz。
要设置更高的时钟频率或获得精确的 1 Mbps 比特率,请启用 PLLAON 时钟并将其作为 MTTCAN 的父时钟。
有关将 PLLAON 作为 MTTCAN 的父时钟的信息,请参阅时钟主题中的配置时钟。
九、更改 CAN 时钟速率
mttcan 内核驱动程序设置 CAN 时钟速率。按照 "内核定制 "主题中的 "使用 Git 获取内核源代码 "中的说明获取内核源代码。更新 mttcan 驱动程序中的时钟频率,如代码片段所示:
.set_can_core_clk = true, .can_core_clk_rate = 50000000, // modify here (in Hz) .can_clk_rate = 200000000, // four times of core clock rate
默认 CAN 时钟频率为
- NVIDIA® Jetson Xavier™ NX 系列: 50 MHz
- NVIDIA® Jetson AGX Xavier™ 系列: 50 MHz
要通过用户空间 sysfs 节点检查软件中设置的时钟频率,请输入命令:
cat /sys/kernel/debug/bpmp/debug/clk/can0/rate
要通过用户空间 sysfs 节点检查硬件中设置的实际时钟频率,请输入命令:
cat /sys/kernel/debug/bpmp/debug/clk/can0/pto_counter