CAN匯流排配置
在Linux系統中,CAN匯流排介面裝置作為網路裝置被系統進行統一管理。在控制台下, CAN匯流排的配置和乙太網路的配置使用相同的命令。
在控制台上輸入命令:
ifconfig –a
可以得到以下結果:
can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 NOARP MTU:16 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:10 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) Interrupt:18 eth0 Link encap:Ethernet HWaddr 00:50:c2:22:3b:0e UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) eth1 Link encap:Ethernet HWaddr 00:50:c2:22:3b:60 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) Interrupt:41 Base address:0xe000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:256 errors:0 dropped:0 overruns:0 frame:0 TX packets:256 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:19952 (19.9 KB) TX bytes:19952 (19.9 KB)
在上面的結果中,eth0和eth1裝置為乙太網路介面,can0裝置為CAN匯流排介面。接下來使用ip命令來配置CAN匯流排的位速率:
ip link set can0 type can tq 125 prop-seg 6phase-seg1 7 phase-seg2 2 sjw 1
也可以使用ip命令直接設定位速率:
ip link set can0 type can bitrate 125000
當設定完成後,可以通過下面的命令查詢can0裝置的參數設定:
ip -details link show can0
當設定完成後,可以使用下面的命令使能can0裝置:
ifconfig can0 up
使用下面的命令取消can0裝置使能:
ifconfig can0 down
###########################################
1、 首先配置can0 ip link set can0 type can tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1 這時dmesg可以看到sja1000_fpga_pci 0000:07:04.0: setting BTR0=0x01 BTR1=0x1c 2、 ip -details link show can0 查看一下 can0: mtu 16 qdisc pfifo_fast state UNKNOWN qlen 10 link/can can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 bitrate 500000 sample-point 0.875 tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1 sja1000: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1 clock 16000000 3、接收測試,接收測試軟體發送的幀: # ./candump can0 interface = can0, family = 29, type = 3, proto = 1 <0x00000002> [8] 70 01 02 03 04 05 06 07 <0x00000002> [8] 70 01 02 03 04 05 06 07 <0x00000002> [8] 70 01 02 03 04 05 06 07 <0x00000002> [8] 70 01 02 03 04 05 06 07 <0x00000002> [8] 70 01 02 03 04 05 06 07 <0x00000002> [8] 70 01 02 03 04 05 06 07 <0x00000002> [8] 70 01 02 03 04 05 06 07 <0x00000002> [8] 70 01 02 03 04 05 06 07 <0x00000002> [8] 70 01 02 03 04 05 06 07
4、發送測試 ./cansend can0 -e 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 interface = can0, family = 29, type = 3, proto = 1 測試軟體上能看到接收的幀
5、重啟 使用核心文檔說的ip link set can0 type can restart-ms 100 會報 RTNETLINK answers: Device or resource busy 使用ifconfig can0 down ;ip link set can0 up type can 即可
並沒有掌握sja1000傳輸速率的配置。摸索出4種常見傳輸速率: 250kbps: ip link set can0 type can tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1 125kbps: ip link set can0 type can tq 250 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1 500kbps: ip link set can0 type can tq 75 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1 1000kbps: ip link set can0 up type can bitrate 2000000 常見用法: ip -details link show can0 ifconfig can0 down ;ip link set can0 up type can ./candump can0 ./cansend can0 -e 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^6
2>. 使用loopback模式測試:
使用loopback模式可測試原生can驅動是否正常工作.
# ip link set can0 down
# ip link set can0 type can bitrate 50000 loopback on
xilinx_can e0008000.can can0: bitrate error 0.0%
# ip link set can0 up
# candump can0 &
# cansend can0 123#11223344556677