SPI的玩法

来自WRTnode Wiki
跳转至: 导航搜索
此页面有未被标记进行翻译的更改

其他语言:
English • ‎中文(中国大陆)‎


注意!!!!!!!这个是给WRTnode1用的SPI驱动,WRTnode2系列直接选kmod-spi-dev驱动,然后更新固件就可以使用spi,不需要以下更改

一:修改spi控制器驱动,实现对spi cs1的支持
我们假定$(TOPDIR)为OpenWrt的编译目录。
打开spi-rt2880.c

vim $(TOPDIR)/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_mt7620n/linux-3.10.49/drivers/spi/spi-rt2880.c

查找 struct rt2880_spi_ops 然后设置spi_ops[0].num_cs = 2

static struct rt2880_spi_ops spi_ops[] = {
   {
        .init_hw = rt2880_spi_reset,
        .set_cs = rt2880_spi_set_cs,
        .baudrate_set = rt2880_spi_baudrate_set,
        .write_read = rt2880_spi_write_read,
        .num_cs = 2,  //这里改为 .num_cs = 2,原本是 .num_cs = 1,
    }, {
        .init_hw = rt5350_spi_reset,
        .set_cs = rt2880_spi_set_cs,
        .baudrate_set = rt2880_spi_baudrate_set,
        .write_read = rt2880_spi_write_read,
        .num_cs = 2,
    }, {
        .init_hw = mt7621_spi_reset,
        .set_cs = mt7621_spi_set_cs,
        .baudrate_set = mt7621_spi_baudrate_set,
        .write_read = mt7621_spi_write_read,
        .num_cs = 1,
    },
};

二:在dts里完成对spidev设备的添加

--- a/target/linux/ramips/dts/WRTNODE.dts
+++ b/target/linux/ramips/dts/WRTNODE.dts
		spi@b00 {
			status = "okay";

			m25p80@0 {
				#address-cells = <1>;
				#size-cells = <1>;
				compatible = "mx25l12805d";
				reg = <0 0>;
				linux,modalias = "m25p80", "w25q128";
				spi-max-frequency = <10000000>;

				partition@0 {
					label = "u-boot";
					reg = <0x0 0x30000>;
					read-only;
				};

				partition@30000 {
					label = "u-boot-env";
					reg = <0x30000 0x10000>;
					read-only;
				};

				factory: partition@40000 {
					label = "factory";
					reg = <0x40000 0x10000>;
					read-only;
				};

				partition@50000 {
					label = "firmware";
					reg = <0x50000 0xfb0000>;
				};
			};

+			spidev@1 {
+				#address-cells = <1>;
+				#size-cells = <1>;
+				compatible = "spidev";
+				reg = <1 0>;
+				linux,modalias = "spidev", "spidev";
+				spi-max-frequency = <10000000>;
+
+			};
		};

三:spidev的内核驱动

make menuconfig
Kernel modules  --->    SPI Support  --->    <*> kmod-spi-dev

四:编译、刷新固件
将新固件重新烧写到WRTnode上,在/dev下会出现相应的spidev设备:

ls /dev

Spi6.png
这是一个测试程序,你可以用这个程序来测试spi是否工作
也可以在package中选上[spidev-test]进行测试