CAN 使用
1. CAN 簡(jiǎn)介
CAN (controller Area Network):控制器局域網(wǎng)絡(luò)總線,是一種有效支持分布式控制或?qū)崟r(shí)控制的串行通信網(wǎng)絡(luò)。
- 目前世界上絕大多數(shù)汽車制造廠商都采用CAN總線來實(shí)現(xiàn)汽車內(nèi)部控制系統(tǒng)之間的數(shù)據(jù)通信。
- RK3568/RK3588的CAN驅(qū)動(dòng)文件:drivers/net/can/rockchip/rockchip_canfd.c
- 在ArmSoM-Sige7中,CAN集成在40PIN中,可供用戶復(fù)用為CAN相關(guān)引腳。
2. 原理圖
CAN在40PIN中的位置: CAN_TX對(duì)應(yīng)40PIN中的第35編號(hào),CAN_RX對(duì)應(yīng)40PIN中的第12編號(hào)
3. 硬件連接
CAN模塊之間接線:CAN_TX接CAN_TX,CAN_RX接CAN_RX。
4. 內(nèi)核配置
- rockchip_linux_defconfig配置:
CONFIG_CAN=y
CONFIG_CAN_DEV=y
CONFIG_CAN_ROCKCHIP=y
CONFIG_CANFD_ROCKCHIP=y
- 內(nèi)核配置:
cd kernel
make ARCH=arm64 menuconfig
make savedefconfig
- 選擇:Networking support ---> CAN bus subsystem support ( )--->CAN Device Drivers( ) ---> Platform CAN drivers with Netlink support(*)
5. DTS 節(jié)點(diǎn)配置
5.1 主要參數(shù):
- interrupts = ;
轉(zhuǎn)換完成,產(chǎn)生中斷信號(hào)。 - clock
時(shí)鐘屬性,用于驅(qū)動(dòng)開關(guān)clk,reset屬性,用于每次復(fù)位總線。 - pinctrl
配置CAN相關(guān)的引腳信息,功能復(fù)用
5.2 芯片級(jí)公共配置
kernel-5.10/arch/arm64/boot/dts/rockchip/rk3588s.dtsi
can0: can@fea50000 {
compatible = "rockchip,can-2.0";
reg = < 0x0 0xfea50000 0x0 0x1000 >;
interrupts = < GIC_SPI 341 IRQ_TYPE_LEVEL_HIGH >;
clocks = < &cru CLK_CAN0 >, < &cru PCLK_CAN0 >;
clock-names = "baudclk", "apb_pclk";
resets = < &cru SRST_CAN0 >, < &cru SRST_P_CAN0 >;
reset-names = "can", "can-apb";
pinctrl-names = "default";
pinctrl-0 = < &can0m0_pins >;
tx-fifo-depth = < 1 >;
rx-fifo-depth = < 6 >;
status = "disabled";
};
can1: can@fea60000 {
compatible = "rockchip,can-2.0";
reg = < 0x0 0xfea60000 0x0 0x1000 >;
interrupts = < GIC_SPI 342 IRQ_TYPE_LEVEL_HIGH >;
clocks = < &cru CLK_CAN1 >, < &cru PCLK_CAN1 >;
clock-names = "baudclk", "apb_pclk";
resets = < &cru SRST_CAN1 >, < &cru SRST_P_CAN1 >;
reset-names = "can", "can-apb";
pinctrl-names = "default";
pinctrl-0 = < &can1m0_pins >;
tx-fifo-depth = < 1 >;
rx-fifo-depth = < 6 >;
status = "disabled";
};
can2: can@fea70000 {
compatible = "rockchip,can-2.0";
reg = < 0x0 0xfea70000 0x0 0x1000 >;
interrupts = < GIC_SPI 343 IRQ_TYPE_LEVEL_HIGH >;
clocks = < &cru CLK_CAN2 >, < &cru PCLK_CAN2 >;
clock-names = "baudclk", "apb_pclk";
resets = < &cru SRST_CAN2 >, < &cru SRST_P_CAN2 >;
reset-names = "can", "can-apb";
pinctrl-names = "default";
pinctrl-0 = < &can2m0_pins >;
tx-fifo-depth = < 1 >;
rx-fifo-depth = < 6 >;
status = "disabled";
};
- compatible = “rockchip,can-1.0” ,rockchip,can-1.0用來匹配can控制器驅(qū)動(dòng)。
- compatible = “rockchip,can-2.0” ,rockchip,can-2.0用來匹配canfd控制器驅(qū)動(dòng)。
- assigned-clock-rates用來配置can的始終頻率,如果CAN的比特率低于等于3M建議修改CAN時(shí)鐘到100M,信號(hào)更穩(wěn)定。高于3M比特率的,時(shí)鐘設(shè)置200M就可以。
- pinctrl配置:根據(jù)實(shí)際板卡連接情況配置can_h和can_l的iomux作為can功能使用。
5.3 板級(jí)配置
kernel-5.10/arch/arm64/boot/dts/rockchip/rk3588-armsom-w3.dts
/* can1 */
&can1 {
status = "okay";
assigned-clocks = < &cru CLK_CAN1 >;
assigned-clock-rates = < 200000000 >;
pinctrl-names = "default";
pinctrl-0 = < &can1m1_pins >; #根據(jù)原理圖進(jìn)行配置
};
- 由于系統(tǒng)根據(jù)上述dts節(jié)點(diǎn)創(chuàng)建的CAN設(shè)備只有一個(gè),而第一個(gè)創(chuàng)建的設(shè)備為CAN0
6. CAN通信測(cè)試
- 查詢當(dāng)前?絡(luò)設(shè)備:
ifconfig -a
- CAN啟動(dòng)
ip link set can0 down //關(guān)閉CAN
ip link set can0 type can bitrate 500000 #設(shè)置?特率500KHz
ip -details -statistics link show can0 #打印can0信息
ip link set can0 up //啟動(dòng)CAN
- CAN發(fā)送
cansend can0 123#DEADBEEF #發(fā)送(標(biāo)準(zhǔn)幀,數(shù)據(jù)幀,ID:123,date:DEADBEEF)
cansend can0 123#R #發(fā)送(標(biāo)準(zhǔn)幀,遠(yuǎn)程幀,ID:123)
cansend can0 00000123#12345678 #發(fā)送(擴(kuò)展幀,數(shù)據(jù)幀,ID:00000123,date:DEADBEEF)
cansend can0 00000123#R #發(fā)送(擴(kuò)展幀,遠(yuǎn)程幀,ID:00000123)
- CAN接收
candump can0 //candump can0
審核編輯 黃宇
-
CAN
+關(guān)注
關(guān)注
57文章
2756瀏覽量
463873 -
Linux
+關(guān)注
關(guān)注
87文章
11314瀏覽量
209777 -
總線
+關(guān)注
關(guān)注
10文章
2890瀏覽量
88155 -
Rockchip
+關(guān)注
關(guān)注
0文章
72瀏覽量
18590
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論