linux之socketcan

Socket CAN 对CAN protocol做了封装,对CAN设备的读写变成了类似tcp/ip的读写网络(socket操作)。

目标环境:linux2.6.37, i.mx27开发板带mcp2515

首先注册spi device和mcp2515 device:

 

代码
   
     
1 static const int plat27_pins[] __initconst = {
2 ...
3
4   #if (defined CONFIG_SPI_IMX || defined CONFIG_SPI_IMX_MODULE )
5 // PD27_PF_CSPI1_SS1, // use mxc_gpio_mode instead mxc_gpio_setup_multiple_pins otherwise spi-imx.c can't request it
6 // PD28_PF_CSPI1_SS0,
7   PD25_PF_CSPI1_RDY,
8 PD29_PF_CSPI1_SCLK,
9 PD30_PF_CSPI1_MISO,
10 PD31_PF_CSPI1_MOSI,
11   #endif
12 }
13
14   #if (defined CONFIG_SPI_IMX || defined CONFIG_SPI_IMX_MODULE )
15   static int cspi1_ss[] = {GPIO_PORTD + 28 }; // CSPI1 ss0=PD28, ss1=PD27
16   static const struct spi_imx_master cspi_pdata __initconst = {
17 .chipselect = cspi1_ss,
18 .num_chipselect = ARRAY_SIZE(cspi1_ss),
19 };
20
21 #if (defined CONFIG_CAN_MCP251X || defined CONFIG_CAN_MCP251X_MODULE )
22 static struct mcp251x_platform_data mcp251x_info = {
23 .oscillator_frequency = 16 * 1000 * 1000 ,
24 };
25
26 static struct spi_board_info plat27_spi_board_info[] __initdata = {
27 {
28 .modalias = " mcp2515 " , // must match with device driver id_table name
29 .bus_num = 0 , // 0 for CSPI1, 1 for CSPI2, ...
30 .chip_select = 0 , // 0 for SS0 ,1 for SS1, ....
31 .max_speed_hz = 2 * 1000 * 1000 , // maxium is 10MHz, but will lost data in my experience
32 .irq = IRQ_GPIOC( 15 ),
33 .platform_data = & mcp251x_info,
34 .mode = SPI_MODE_0, // not required exactly since mcp251x driver will set it to SPI_MODE_0
35 },
36 };
37 #endif
38 #endif
39
40 static void __init XXX_init( void )
41 {
42 ...
43
44 #if (defined CONFIG_SPI_IMX || defined CONFIG_SPI_IMX_MODULE )
45 mxc_gpio_mode(GPIO_PORTD | 28 | GPIO_GPIO | GPIO_OUT);
46 imx27_add_spi_imx0( & cspi_pdata);
47
48 #if (defined CONFIG_CAN_MCP251X || defined CONFIG_CAN_MCP251X_MODULE )
49 spi_register_board_info(plat27_spi_board_info, ARRAY_SIZE(plat27_spi_board_info));
50 #endif
51 #endif
52
53 }

make menuconfig中加入:

 

代码
   
     
1 [ * ]Networking support ->
2 <M > CAN bus subsystem support ->
3 <M > Raw CAN Protocal
4 <M > Broadcast Manage CAN Protocal
5 CAN Device Drivers ->
6 <M > Platform CAN driver with Netlink support
7 [ * ]CAN bit - timing calculation
8 <M > Microchip MCP251x SPI CAN controllers
9
10 Device drivers ->
11 [ * ]SPI support ->
12 < *> Freescale i.MX spi controllers
13
14  

 

编译安装mod:

insmod can.ko

insmod can-raw.ko

insmod can-bcm.ko

insmod can-dev.ko

insmod mcp251x.ko

 

最新版busybox的ip还是不支持socketcan,所以要自己编译一个。

http://corp-dev.linuxfoundation.org/collaborate/workgroups/networking/iproute2下载iproute2,目前版本是2.6.35。

编译iproute2的时候有很多问题,安装了bison,flex,libdb4.7-dev还是死在tc目录,后来想想只要用它的ip,于是最终修改Makefile如下:

CC = /opt/arm-2010q1/bin/arm-none-linux-gnueabi-gcc
SUBDIRS=lib ip

make通过,把ip目录中的ip(elf文件)拷到目标板中运行:

chmod 777 ip

./ip link set can0 up type can bitrate 250000
./ip -details link show can0

can0设置好后可以用以下命令修改bitrate

ifconfig can0 down 
echo 125000 > /sys/class/net/can0/can_bitrate 
ifconfig can0 up 
 

到这一步socketcan就算配置完成了,不过还是可以给它添加一些工具。

1.用socketcan里的can-utils:

sudo apt-get install subversion
svn checkout svn://svn.berlios.de/socketcan/trunk socketcan
cp -r socketcan ../Workspaces
修改socketcan/can-utils/Makefile
PREFIX=./bin
KERNELDIR = http://www.cnblogs.com/linux-2.6.37
CC=/opt/arm-2010q1/bin/arm-none-linux-gnueabi-gcc  (新增)
socketcan和linux2.6.37带的socketcan版本不同,导致有些不能编译成功,不过大部分工具都有了,剩下的如果要的话可以考虑merge linux内核----还不如自己写工具呢

2.在http://www.pengutronix.de/software/socket-can下载can-utils 4.0.6,里面有5个工具,不过搞不定它的configure,手工编译之吧:

先在include下建一个can_config.h,里面就一句话#define VERSION 20090105  (随便怎么写吧,我是看到can.ko的版本是20090105) 

然后进到src目录

/opt/arm-2010q1/bin/arm-none-linux-gnueabi-gcc  -o xxx  xxx.c -I$PWD/../include -I$PWD/***/linux-2.6.37/include  (faint,http://www.cnblogs.com/被转成www.cnblogs.com

除了canconfig.c通不过(需要libsocketcan.h,好在已经有iproute2了)其他的都成功。 感觉这个cansend比socketcan里的cansend好用。

 

 

你可能感兴趣的:(socket)