嵌入式爱好者

8mp使用2515芯片实现SPI转CAN

2023-6-27 14:47| 发布者: wjy| 查看: 185| 评论: 0

类目: 知识库  >  i.MX8MP系列产品     文档编号: 1028

文档编号:911 移植的是2518 SPI转CAN FD芯片,本文移植的是2515 SPI转CAN芯片,只满足普通CAN功能,不支持CAN FD。

说明:OKMX8MP开发板内核源码中已经包含了2515的驱动,直接修改设备树及驱动配置即可。

驱动路径:OK8MP-linux-sdk/OK8MP-linux-kernel/drivers/net/can/spi/mcp251x.c

1.1.1添加驱动配置

forlinx@ubuntu:~/OK8MP-linux-sdk/OK8MP-linux-kernel/$ vi arch/arm64/configs/OK8MP-C_\

defconfig

在任意位置添加:

CONFIG_CAN_MCP251X=y

1.1.2修改设备树

forlinx@ubuntu:~/OK8MP-linux-sdk/OK8MP-linux-kernel/$ vi arch/arm64/boot/dts/freescale/\

OK8MP-C.dts

1. 添加时钟节点:

[……]

/ {

        model = "Forlinx OK8MPlus-C board";

compatible = "forlinx,ok8mp-c", "fsl,imx8mp-evk", "fsl,imx8mp";

 

        clocks {                                                //添加时钟节点

                mcp251x_clock: mcp251x_clock{

                        compatible = "fixed-clock";

                        #clock-cells = <0>;

                        clock-frequency = <8000000>;           //对应模块上晶振的频率

                };

        };

 

        reserved-memory {

                #address-cells = <2>;

[……]

2. ecspi2节点下屏蔽之前的子节点spidev1,添加2515的子节点:

[……]

&ecspi2 {

        #address-cells = <1>;

        #size-cells = <0>;

        fsl,spi-num-chipselects = <1>;

        pinctrl-names = "default";

        pinctrl-0 = <&pinctrl_ecspi2 &pinctrl_ecspi2_cs>;

        cs-gpios = <&gpio5 13 GPIO_ACTIVE_LOW>;

        status = "okay";

/*

        spidev1: spi@0 {

                reg = <0>;

                compatible = "rohm,dh2228fv";

                spi-max-frequency = <500000>;

        };

*/

        can0: mcp2515@0 {

                compatible = "microchip,mcp2515";

                reg = <0>;                           //地址从0开始

                spi-max-frequency = <10000000>;      //spi最大频率设置,此处为1MHz

                clocks = <&mcp251x_clock>;                       //使用刚刚写的时钟

                interrupt-parent = <&gpio4>;   //中断引脚配置,中断引脚是GPIO4_IO21

                interrupts = <21 0x2>;     //中断引脚配置,参数0x2代表触发方式

        };

};

[……]

3. 添加中断引脚配置:

[……]

pinctrl_ecspi2: ecspi2grp {

fsl,pins = <

MX8MP_IOMUXC_ECSPI2_SCLK__ECSPI2_SCLK 0x82

MX8MP_IOMUXC_ECSPI2_MOSI__ECSPI2_MOSI 0x82

MX8MP_IOMUXC_ECSPI2_MISO__ECSPI2_MISO 0x82

MX8MP_IOMUXC_SAI2_RXFS__GPIO4_IO21 0x40000

>;

};

[……]

4. 编译源码并烧写。

1.1.3开机验证

1. 在开发板上SPI2接口处接上2515转接板,启动开发板。

2. 查看驱动是否成功挂载:

root@OK8MP:~# lsmod

Module Size Used by

can_raw 20480 1

can 28672 1 can_raw

vvcam_video 32768 0

moal 716800 1

mlan 499712 1 moal

cdc_acm 32768 0

xr_usb_serial_common 45056 0

vvcam_isp 69632 1

vvcam_dwe 20480 1

imx8_media_dev 20480 0

basler_camera_driver 16384 0

crct10dif_ce 16384 1

edt_ft5x06 24576 0

basler_camera_driver_vvcam 16384 0

goodix 24576 0

mcp251x 24576 0

flexcan 32768 0

can_dev 36864 2 mcp251x,flexcan

3. 查看是否生成节点:

root@OK8MP:~# ifconfig -a

can0 Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  

UP RUNNING NOARP  MTU:16  Metric:1

RX packets:2 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:16 (16.0 B)  TX bytes:0 (0.0 B)

Interrupt:31 

 

can1 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)

 

can2 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:32 

[……]

 注意:

使用SPICAN芯片转出的can节点号不确定,可通过排除法确定哪个是SPICAN的节点;

原生CAN节点有中断“Interrupt”,原生CAN1中断值为31,原生CAN2中断值为32,没有中断的即为2515 SPICAN

上述结果中:SPICAN对应软件节点:can1

CAN1对应软件节点:can0

CAN2对应软件节点:can2

1.1.4测试SPICAN的功能

SPICAN与原生CAN1短接进行测试,接线方法如下:

CAN1 H 端子与 SPICAN 设备 H 端子连接;

CAN1 L 端子与 SPICAN 设备 L 端子连接。

1. 设置 CAN 设备 can0 can1 波特率为 500 KHz500000)。

root@OK8MP:~# ip link set can0 type can bitrate 500000 triple-sampling on 

root@OK8MP:~# ip link set can1 type can bitrate 500000 triple-sampling on 

2. 打开 CAN 设备 can0 can1

root@OK8MP:~# ifconfig can0 up                                         //打开 can0

root@OK8MP:~# ifconfig can1 up                                         //打开 can1

3. 配置 CAN 设备 can0 为接收端、can1 为发送端:

root@OK8MP:~# candump can0 &                               //设置 can0 为接收端

root@OK8MP:~# cansend can1 1F334455#1122334455667788     //设置 can1 发送数据

can0 1F334455 [8] 11 22 33 44 55 66 77 88                    //can0 端接收到数据

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


已解决

未解决

只是看看

最新评论

QQ|小黑屋| 飞凌嵌入式 ( 冀ICP备12004394号-1 )

GMT+8, 2024-11-22 09:47

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

返回顶部