Linux CAN 编程

参考链接地址:https://my.oschina.net/u/2326703/blog/385249

一、移植 iproute2 ip命令。

STEP1:下载iproute2的最新源码 http://www.kernel.org/pub/linux/utils/net/iproute2/

   

解压iproute2-3.19.0.tar.gz,

 

(1)  进入/iproute2-3.19.0/ip 文件夹,运行:

 

arm-none-linux-gnueabi-gcc iplink_can.c -I/srv/smart_kernel/driver_test/testCAN/iproute2-3.19.0/include -fPIC -shared -o link_can.so  

 

// -I: 指定link_can.c 中include所需的头文件

 

// -shared 该选项指定生成动态连接库;
// -fPIC表示编译为位置独立(地址无关)的代码,不用此选项的话,编译后的代码是位置相关的,所以动态载入时,是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。

 

 

 

将生成的link_can.so 拷贝到开发板/usr/lib/ip/目录下(/usr/lib 无ip目录,需自己创建).

 

 

(2)修改Makefile:

 

33 #CC = gcc

 

34 CC = arm-none-linux-gnueabi-gcc

 

37 WFLAGS := -march=armv4t -Wall -Wstrict-prototypes -Werror -Wmissing-prototypes

 

 ----march=armv4t 有时必须加上,不然会报”Illegal instruction”错误;

 

(2)因为我们只需要iprout2的ip命令,所以修改Makefile的第42行。

 

42 #SUBDIRS=lib ip tc bridge misc netem genl man

 

43 SUBDIRS=lib ip

 

修改完成执行make命令,生成ip命令.

 

将生成的ip命令放在/usr/sbin 目录下,同时把/sbin/目录下ip命令改名为ip_buildroot

 

(3)设置can0:

 

ifconfig can0 down

 

ip link set can0 up type can bitrate 500000

 

 

(4)Canutils 调试工具

  参考链接地址:https://wenku.baidu.com/view/f91e5ea05fbfc77da369b15d.html

  canutils 源码下载地址:https://public.pengutronix.de/software/socket-can/canutils/v4.0/

  libsocketcan源码下载地址:http://www.pengutronix.de/software/libsocketcan/download/

 

posted @ 2021-01-05 11:27  Soul>>null  阅读(677)  评论(0编辑  收藏  举报