zynq linux内核驱动编写,Zynq-Linux移植学习笔记之20-Zynq linux can驱动开发

1、  硬件配置

在vivado中选择启用ps端的can控制器,如下图

d20fce43f27606b392bc2551977ee8e8.png

设置can总线的主频

65822de079a1e43672c5373c82818622.png

2、  devicetree配置

在devicetree中需要增加can的配置信息,如下:

can@e0008000 {

compatible= "xlnx,zynq-can-1.0";

status= "okay";

clocks= <0x1 0x13 0x1 0x24>;

clock-names= "ref_clk", "aper_clk";

reg = <0xe00080000x1000>;

interrupts= <0x0 0x1c 0x4>;

interrupt-parent= <0x3>;

tx-fifo-depth= <0x40>;

rx-fifo-depth= <0x40>;

};

实际场景中只使用了can0,故devicetree中只配置can0即可。

3、  kernel配置

在kernel中需要增加can驱动的支持

75512692bedc102e4776757e8dbf2336.png

4f6fb2f30a2ac64c874832794354c35a.png

af3ef4891006c71a94ce5bead23d18e4.png

编译内核即可。

4、  测试

加载镜像后,通过ifconfig –a能够看到can0的打印信息,如下:

f2fd3cc4d0391e5afe4cac8be439c59c.png

设置can0的波特率,这里设置的是100k

>ip link set can0 type can bitrate100000

启用can0

>ip link set can0 up

显示can0状态信息

>ip -d -s link show can0

adcadcf6a7f9bd1ae196f7959dd202cf.png

可以通过Cantest测试工具连接开发板进行发送包测试

be74e029ad8aa695239f773e47b7f41c.png

选择与开发板can总线相同速率的波特率100k

d391e2eba02d51734f5c86a984038826.png

发送报文测试,发送成功

c92037b27e87fd9f5fa1b228a42c78ea.png

开发板上RX有相应的计数

开发板使用cansend函数进行数据发送

742167e50a50c66f1a2e00830783c9fe.png

c567b829d2a25613af19e96ddd92c68a.png

收发正常,至此测试完成。

5、  相关函数

ifconfig -a

可以查到当前can网络can0 can1,包括收发包数量、是否有错误等等

//ip link set can0 type can--help

ip link set can0 up type canbitrate 800000

设置can0的波特率为800kbps,CAN网络波特率最大值为1Mbps

ip link set can0 up type canbitrate 800000 loopback on

设置回环模式,自发自收,用于测试是硬件是否正常,loopback不一定支持

ip link set can0 down

关闭can0 网络

cansend can0 0x11 0x22 0x33 0x440x55 0x66 0x77 0x88

发送默认ID为0x1的can标准帧,数据为0x11 22 33 44 55 66 77 88

每次最大8个byte

cansend can0 -i 0x800 0x11 0x220x33 0x44 0x55 0x66 0x77 0x88 -e

-e 表示扩展帧,CAN_ID最大29bit,标准帧CAN_ID最大11bit

-i表示CAN_ID

cansend can0 -i 0x02 0x11 0x12--loop=20

--loop 表示发送20个包

candump can0

接收CAN0数据

附:Xilinx官网上can操作命令

Usage For CAN

1. Set bit-timing

Can supports bitrates upto 1Mb/s. Xilinx CAN h/w and driver supports these bitrates

Note: Triple sampling is not supported by Xilinx CAN H/W.

$ ./ip link set can0 type can bitrate 200000

or

$ ./canconfig can0 bitrate 200000

2. Bring up the device

$ ./ip link set can0 up

or

$ ./canconfig can0 start

3. Bring down the device

$ ./ip link set can0 down

or

$ ./canconfig can0 stop

4. Transmit and receive packetswith standard id number

$ ./cansend can0 -i 0x14

$ ./candump can0

5. Transmit and receive packetswith extended id number (--loop argument here)

$ ./cansend can0 -i 0x333

$ ./candump can0

6. Loopback mode

$ ./canconfig can0 ctrlmode loopback on

7. Checking link state (checkingbitrate/errors/packets)

$ ./ip -d -s link show can0

8. Checking net device state

$ ifconfig can0

9. Getting statistics via proc

$ cat /proc/net/can/stats

10. Socket CAN core uses severalfilter lists to deliver received CAN frames to CAN protocol modules. These receivelists, their filters and the count of filter matches can be checked in theappropriate receive list. All entries

contain the device and a protocol moduleidentifier:

$ cat /proc/net/can/rcvlist_all

rcvlist_all - list for unfiltered entries (no filter operations)

rcvlist_eff - list for single extended frame (EFF) entries

rcvlist_err - list for error message frames masks

rcvlist_fil - list for mask/value filters

rcvlist_inv - list for mask/value filters (inverse semantic)

rcvlist_sff - list for single standard frame (SFF) entries

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值