spi或gpiospi驱动CAN芯片mcp2515说明文档(Hi3531A)

dtb文件16进制以D0 0D FE ED开头
#从内核中获取dtb文件(海思dtb附在内核后面)
dd if=uImage of=hisi.dtb skip=3678808 bs=1 count=20k
#dtb转dts
dtc -I dtb -O dts -o hisi.dts hisi.dtb
中断设置两种方式,其中 interrupt-controller;#interrupt-cells = <0x2>;表示其他节点引用时用两个参数
1、interrupts = <0x0 0x39 0x4>;   //三个参数,共享中断类型、中断号、中断触发方式高电平    中断号配置时减32
2、interrupt-parent = <&gpio_chip0>; 
      interrupts = <4 0x2>;  //两个参数   中断gpio号、中断触发下降沿
配置can实例,已验证
1、使用spi0.3配置CAN节点
先配置管脚复用:can芯片中断引脚配置gpio模式、spi引脚配置spi模式(如果中断gpio没有复用正确,则只能发送一帧
mw 0x120F01E4 0 (gpio0_4)
mw 0x120F0148 1 (sck)
mw 0x120F014C 1 (sdo)
mw 0x120F0150 1 (sdi)
内核menuconfig增加gpio支持(class、pl061)
海思HI3531A dts部分配置,增加gpio配置、can节点配置中断gpio0_4、net->can->mcp2515
海思HI3531A
hi3531a-demb.dts文件增加
&spi_bus0{
    status = "okay";
    num-cs = <4>;
    spidev@0 {
        compatible = "rohm,dh2228fv";
        reg = <0>;
        pl022,interface = <0>;
        pl022,com-mode = <0>;
        spi-max-frequency = <31250000>;
    };
    can0: can@10 {
        compatible = "microchip,mcp2515";
        reg = <3>;
        clocks = <&clk16m>;
        spi-max-frequency = <2000000>;
        interrupt-parent = <&gpio_chip0>;
        interrupts = <4 0x2>;
        status = "okay";
    };
};
hi3531a.dtsi文件
根目录下:
  aliases {
        fmc = &fmc;
        serial0 = &uart0;
        serial1 = &uart1;
        serial2 = &uart2;
        serial3 = &uart3;
        i2c0 = &i2c_bus0;
        i2c1 = &i2c_bus1;
        spi0 = &spi_bus0;
        gpio0 = &gpio_chip0;
    };
  clk16m:clk16m {
        compatible = "fixed-clock";
        #clock-cells = <0x0>;
        clock-frequency = <16000000>;
    };
soc目录下:
 gpio_chip0:gpio_chip@12150000 {
            compatible = "arm,pl061", "arm,primecell";
            reg = <0x12150000 0x10000>;
            interrupts = <0x0 0x39 0x4>;
            clocks = <&clock HI3531A_PERIAXI_CLK>;
            clock-names = "apb_pclk";
            gpio-controller;
            #gpio-cells = <0x2>;
            interrupt-controller;
            #interrupt-cells = <0x2>;
            status = "okay";
        };
2、使用gpio模拟spi配置CAN节点
gpio模拟spi,内核配置开启spi->GPIO-based bitbanging SPI Master
配置管脚复用(UBOOT命令)
mw 0x120F02F4 0(gpio15_0)
mw 0x120F02F8 0(gpio15_1)
mw 0x120F02FC 0(gpio15_2)
mw 0x120F01E0 0 (gpio0_3)
dts配置
 spi_bus1: spi{
            compatible = "spi-gpio";
            #address-cells = <1>;
            #size-cells = <0>;
            gpio-sck = <&gpio_chip15 0 0>;
            gpio-mosi = <&gpio_chip15 1 0>;
            gpio-miso = <&gpio_chip15 2 0>;
            cs-gpios = <&gpio_chip20 4 0>;  //cs-gpios = <&gpio_chip20 4 0>, <&gpio_chip20 3 0>, <&gpio_chip20 2 0>, <&gpio_chip20 0 0>;
            num-chipselects = <1>; //num-chipselects = <4>;
        };
&spi_bus1{
    status = "okay";
    can0: can@10 {
        compatible = "microchip,mcp2515";
        reg = <0>;
        clocks = <&clk16m>;
        spi-max-frequency = <2000000>;
        interrupt-parent = <&gpio_chip0>;
        interrupts = <3 0x2>;
        status = "okay";
    };
};
启动can0接口(ip link set can0 up)时,有报错(RTNETLINK answers: No sucrh device),mcp251x_read_reg/mcp251x_write_reg加打印后,发现是mcp251x_open函数内mcp251x_hw_reset出错了,读寄存器值为0。
原因:
1、进入应用后,gpio引脚被其他应用重新配置了,屏蔽即可
2、还有可能原因(网上):尝试将mcp251x_hw_reset函数内的延时增大后,就解决了。问题原因是驱动发送第一个reset指令时,芯片还未初始化完成,读到的状态不对,加大延时就解决了。
#define MCP251X_OST_DELAY_MS (20)//(5)

你可能感兴趣的:(音视频,c语言,驱动开发)