OPENWRT lede MT7628dan 增加SPI接口

时间:2024-04-04 14:58:58

SPI是可以全双工通信的一种串行总线,两个设备之间双向通信的话一般使用3根线:SCLK,MISO,MOSI,多个设备之间双向通信的话,每个设备还需要再加上一根地址线CSn。相比之下I2C只能半双工,而且一般需要上拉电阻,但无论几个设备,都只需要2根线。更多基础知识请谷歌百度。

MT7628DAN芯片只有一个主控制器,但是有两个片选信号,可以接2个设备,其中一个已经被SPI FLASH占用,所以需要启用另外一个设备。

1、修改内核配置文件 

make menuconfig

OPENWRT lede MT7628dan 增加SPI接口

配置完成后退出保存。

1、修改文件mt7628an.dtsi

OPENWRT lede MT7628dan 增加SPI接口

spi0: [email protected] {
compatible = "ralink,mt7621-spi";
reg = <0xb00 0x100>;
resets = <&rstctrl 18>;
reset-names = "spi";
#address-cells = <1>;
#size-cells = <0>;
pinctrl-names = "default";
pinctrl-0 = <&spi_pins>,
<&spi_cs1_pins>;
status = "disabled";

};

[email protected]代表一个spi控制器,是一个platform device,compatible = "ralink,mt7621-spi";和  platform driver 中的of_match_table 对应(如果要支持片选1,还得修改num_cs=2,前提是mt7628的spi控制器本来就支持两个片选).相同就会进入到probe函数中,再调用spi_register_master()注册一个spi主控制器.<&spi_cs1_pins>是新增片选引脚,文件mt7628an.dtsi中有定义:

spi_pins: spi {
spi {
ralink,group = "spi";
ralink,function = "spi";
};
};


spi_cs1_pins: spi_cs1 {
spi_cs1 {
ralink,group = "spi cs1";
ralink,function = "spi cs1";
};
};

3、修改MT7628.dts 文件


OPENWRT lede MT7628dan 增加SPI接口

/dts-v1/;

#include "mt7628an.dtsi"


/ {
compatible = "mediatek,mt7628an-eval-board", "mediatek,mt7628an-soc";
model = "Mediatek MT7628AN evaluation board";


[email protected] {
device_type = "memory";
reg = <0x0 0x2000000>;
};
};


&pinctrl {
state_default: pinctrl0 {
gpio {
ralink,group = "i2c";
ralink,function = "gpio";
};
};
};


&wmac {
status = "okay";
};


&spi0 {
status = "okay";


[email protected] {
#address-cells = <1>;
#size-cells = <1>;
compatible = "jedec,spi-nor";
reg = <0>;
spi-max-frequency = <10000000>;
m25p,chunked-io = <32>;


[email protected] {
label = "u-boot";
reg = <0x0 0x30000>;
read-only;
};


[email protected] {
label = "u-boot-env";
reg = <0x30000 0x10000>;
read-only;
};


factory: [email protected] {
label = "factory";
reg = <0x40000 0x10000>;
read-only;
};


[email protected] {
label = "firmware";
reg = <0x50000 0x7b0000>;
};
};
[email protected] {
                 compatible = "rohm,dh2228fv";
                 reg = <1 0>;
                 spi-max-frequency = <1000000>;
        };

};


&wmac {
status = "okay";

};


 spi_register_master注册spi主控制器时就会扫描这些设备,并注册这些设备。   status = “okay”表示选中,否则不能编译进内核. [email protected]表示在spi片选0下挂了一个m25p80的设备,reg=<0,0>表示片选0,compatible = "jedec,spi-nor"; 与驱动文件匹配.如果要在spi控制器的片选1上挂一个设备,就要修改dts文件,修改如下:

[email protected] {
                 compatible = "rohm,dh2228fv";
                 reg = <1 0>;
                 spi-max-frequency = <1000000>;

        };

 compatible = "rohm,dh2228fv";与 spidev.c文件中compatible 一致。

OPENWRT lede MT7628dan 增加SPI接口


4、保存修改文件,执行make 生产升级固件

lede-snapshot-r7346-7b74b40-ramips-mt76x8-mt7628-squashfs-sysupgrade.bin

5、烧写固件查看设备文件:

OPENWRT lede MT7628dan 增加SPI接口

6、好不好用还没有测试。

7、编译spi-test进行spi测试

OPENWRT lede MT7628dan 增加SPI接口

8、执行

[email protected]:/# spidev_test -D /dev/spidev0.1

出现如下一连串错误

spi mode: 0x0[ 196.130000] ------------[ cut here ]------------

bits per word:[ 196.130000] WARNING: CPU: 0 PID: 161 at drivers/spi/spi-mt7621.c:137 mt7621_spi_transfer_one_message+0x158/0x360()

8

max speed: 5[ 196.140000] Modules linked in:00000 Hz (500 KH qcserialz)

pppoe ppp_async option iptable_nat usb_wwan sierra pppox ppp_generic nf_nat_ipv4 nf_conntrack_ipv6 nf_conntrack_ipv4 ipt_REJECT ipt_MASQUERADE xt_time xt_tcpudp xt_state xt_nat xt_multiport xt_mark xt_mac xt_limit xt_id xt_conntrack xt_comment xt_TCPMSS xt_REDIRECT xt_LOG xt_CT usbserial spidev slhc nf_reject_ipv4 nf_nat_masquerade_ipv4 nf_nat_ftp nf_nat nf_log_ipv4 nf_defrag_ipv6 nf_defrag_ipv4 nf_conntrack_rtcache nf_conntrack_ftp nf_conntrack iptable_raw iptable_mangle iptable_filter ip_tables crc_ccitt i2c_gpio i2c_algo_bit i2c_dev i2c_core mt76x8 ralink_eeprom_api ledtrig_usbdev ip6t_REJECT nf_reject_ipv6 nf_log_ipv6 nf_log_common ip6table_raw ip6table_mangle ip6table_filter ip6_tables x_tables ipv6 mmc_block mmc_core leds_gpio ohci_platform ohci_hcd ehci_platform ehci_hcd gpio_button_hotplug usbcore nls_base usb_common

[ 196.220000] CPU: 0 PID: 161 Comm: spi32766 Tainted: G W 3.18.20 #8
[ 196.230000] Stack : 00000000 00000000 00000000 00000000 803441f2 00000042 00000000 801ae398

00000001 8f8c7b88 802a53d0 802fc9c3 000000a1 8034341c 8f900bf8 8f8c7b88
00010000 80309578 00000000 80047400 00000003 80024170 00000089 8f8c7b88
802a88d4 8f979d74 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
...

[ 196.270000] Call Trace:
[ 196.270000] [<800140b4>] show_stack+0x48/0x70
[ 196.270000] [<800242ec>] warn_slowpath_common+0x84/0xb4
[ 196.280000] [<800243a4>] warn_slowpath_null+0x18/0x24
[ 196.290000] [<801ae398>] mt7621_spi_transfer_one_message+0x158/0x360
[ 196.290000] [<801ad4e8>] spi_pump_messages+0x3cc/0x438
[ 196.300000] [<80039b9c>] kthread_worker_fn+0xa8/0xf4
[ 196.300000] [<80039cc0>] kthread+0xd8/0xe4
[ 196.310000] [<80004878>] ret_from_kernel_thread+0x14/0x1c
[ 196.310000]
[ 196.310000] ---[ end trace 828b306131dd7246 ]---
can't send spi message: Input/output error
Aborted

查了好几天问题,没明白找到了一个帖子

http://dev.archive.openwrt.org/ticket/20521?action=new&attachfilebutton=Attach+file#no1

原因是

The spidev_test is transmitting a 38 bytes array, that is too large. The spi-mt7621.c will reject when Tx length > 16 (full duplex).

我真是菜鸟一个,

然后修改mt7621.c文件,把全双工注释掉可正常运行spi-test。

OPENWRT lede MT7628dan 增加SPI接口

OPENWRT lede MT7628dan 增加SPI接口