Difference between revisions of "SPI/zh"
(updated by API) |
(→其他信息) |
||
(16 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | [[SPI | + | [[SPI|English]] |
+ | |||
+ | ==RK3399== | ||
+ | RK3399下,SPI由于与UART4共用引脚,所以需要修改内核DTS文件。 | ||
+ | ===Linux下启用SPI=== | ||
+ | 下载内核源代码,编辑内核目录下的dts文件 arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | git clone https://github.com/friendlyarm/kernel-rockchip --depth 1 -b nanopi4-linux-v4.4.y kernel-rockchip | ||
+ | cd kernel-rockchip | ||
+ | vim arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi | ||
+ | </syntaxhighlight> | ||
+ | 首先找到 spi1 的定义: | ||
+ | <syntaxhighlight lang="c"> | ||
+ | &spi1 { | ||
+ | status = "disabled"; // 将其中的 disabled 改为 okay | ||
+ | </syntaxhighlight> | ||
+ | 然后在 rk3399-nanopi4-common.dtsi 文件到找到 uart4 的定义: | ||
+ | <syntaxhighlight lang="c"> | ||
+ | &uart4 { | ||
+ | status = "okay"; // 将其中的 okay 改为 disabled | ||
+ | </syntaxhighlight> | ||
+ | 最后重新编译内核 (编译器: [https://github.com/friendlyarm/prebuilts/blob/master/gcc-x64/aarch64-cortexa53-linux-gnu-6.4.tar.xz gcc6.4]): | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | make ARCH=arm64 nanopi4_linux_defconfig | ||
+ | export PATH=/opt/FriendlyARM/toolchain/6.4-aarch64/bin/:$PATH | ||
+ | make ARCH=arm64 nanopi4-images | ||
+ | </syntaxhighlight> | ||
+ | 编译完成后会生成 kernel.img resource.img 这两个文件,将其拷贝到eflasher系统的SD卡覆盖旧文件即可, 假设SD卡的FRIENDLYARM分区挂载到FRIENDLYARM目录,更新命令如下: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | # for Lubuntu | ||
+ | cp kernel.img resource.img /media/FRIENDLYARM/lubuntu/ | ||
+ | |||
+ | # for FriendlyCore | ||
+ | cp kernel.img resource.img /media/FRIENDLYARM/friendlycore-arm64/ | ||
+ | |||
+ | # for FriendlyDesktop | ||
+ | cp kernel.img resource.img /media/FRIENDLYARM/friendlydesktop-arm64/ | ||
+ | </syntaxhighlight> | ||
+ | 也可以用线刷工具来更新。 | ||
+ | |||
+ | ===Android下启用SPI=== | ||
+ | Android下修改方法如下: <br /> | ||
+ | 编辑内核目录下的dts文件 arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi: | ||
+ | <syntaxhighlight lang="c"> | ||
+ | cd ANDROID_SOURCE/kernel | ||
+ | vim arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi | ||
+ | </syntaxhighlight> | ||
+ | 其中,ANDROID_SOURCE请替换为真实的 Android7或Android8源代码目录。 | ||
+ | |||
+ | 首先找到 spi1 的定义: | ||
+ | <syntaxhighlight lang="c"> | ||
+ | &spi1 { | ||
+ | status = "disabled"; // 将其中的 disabled 改为 okay | ||
+ | </syntaxhighlight> | ||
+ | 然后再在 rk3399-nanopi4-common.dtsi 文件到找到 uart4 的定义: | ||
+ | <syntaxhighlight lang="c"> | ||
+ | &uart4 { | ||
+ | status = "okay"; // 将其中的 okay 改为 disabled | ||
+ | </syntaxhighlight> | ||
+ | 最后重新编译内核: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | cd ANDROID_SOURCE/ | ||
+ | ./build-nanopc-t4.sh -K -M | ||
+ | </syntaxhighlight> | ||
+ | 更新 rockdev/Image-nanopc_t4/resource.img 到开发板即可,可以将resource.img拷贝到安装有 eflasher 系统的TF卡中,位置是FriendlyARM分区的androidX 目录,替换里面的resource.img文件,然后用TF卡重刷系统。 | ||
+ | ===RK3399 SPI的其他信息=== | ||
+ | * 目前spidev0 配置为最高 10mhz | ||
+ | * 驱动是 drivers/spi/spi-rockchip.c | ||
==S5P4418== | ==S5P4418== | ||
Line 8: | Line 75: | ||
==H5== | ==H5== | ||
+ | ===使用/dev/spidev0.0=== | ||
+ | 在板子上执行下面的命令: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ wget https://github.com/friendlyarm/linux/blob/sunxi-4.14.y/tools/spi/spidev_test.c | ||
+ | $ gcc spidev_test.c -o spidev_test | ||
+ | $ ./spidev_test -D /dev/spidev0.0 -S 1024 -I 1000 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ===使能SPI1=== | ||
+ | H5的SPI1和UART3的引脚是复用的,我们默认提供的配置是使能UART3,禁用SPI1,内核设备树的配置如下:<br> | ||
+ | linux/arch/arm64/boot/dts/allwinner/sun50i-h5-nanopi.dtsi | ||
+ | <syntaxhighlight lang="text"> | ||
+ | &spi1 { | ||
+ | // against uart3_rts_cts_pins | ||
+ | status = "disable"; | ||
+ | spidev1: spi@1 { | ||
+ | compatible = "nanopi,spidev"; | ||
+ | reg = <0>; | ||
+ | spi-max-frequency = <10000000>; | ||
+ | }; | ||
+ | }; | ||
+ | |||
+ | &uart3 { | ||
+ | pinctrl-names = "default"; | ||
+ | pinctrl-0 = <&uart3_pins>, <&uart3_rts_cts_pins>; | ||
+ | status = "okay"; | ||
+ | }; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 如果想使能SPI1,需要修改设备树如下: | ||
+ | <syntaxhighlight lang="text"> | ||
+ | &spi1 { | ||
+ | // against uart3_rts_cts_pins | ||
+ | status = "okay"; | ||
+ | spidev1: spi@1 { | ||
+ | compatible = "nanopi,spidev"; | ||
+ | reg = <0>; | ||
+ | spi-max-frequency = <10000000>; | ||
+ | }; | ||
+ | }; | ||
+ | |||
+ | &uart3 { | ||
+ | pinctrl-names = "default"; | ||
+ | pinctrl-0 = <&uart3_pins>, <&uart3_rts_cts_pins>; | ||
+ | status = "disable"; | ||
+ | }; | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 另外,使用[[npi-config/zh]]也可以实现上述目的。 | ||
+ | |||
===SPI Flash=== | ===SPI Flash=== | ||
FriendlyElec H5的Linux-4.14内核集成了"mxicy,mx25l12805d"的SPI Flash驱动,使用的是SPI0,但是默认并没有使能,相关的dts配置如下: | FriendlyElec H5的Linux-4.14内核集成了"mxicy,mx25l12805d"的SPI Flash驱动,使用的是SPI0,但是默认并没有使能,相关的dts配置如下: | ||
<syntaxhighlight lang="text"> | <syntaxhighlight lang="text"> | ||
− | $ | + | $ cat linux/arch/arm64/boot/dts/allwinner/sun50i-h5-nanopi.dtsi |
&spi0 { | &spi0 { | ||
... | ... | ||
Line 19: | Line 136: | ||
compatible = "mxicy,mx25l12805d"; | compatible = "mxicy,mx25l12805d"; | ||
reg = <0>; | reg = <0>; | ||
− | status = "disabled"; | + | status = "disabled"; // disabled表示禁用该驱动 |
spi-max-frequency = <50000000>; | spi-max-frequency = <50000000>; | ||
Line 32: | Line 149: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 可以在[[npi-config/zh|npi-config]]里将其使能,如下: | |
<syntaxhighlight lang="text"> | <syntaxhighlight lang="text"> | ||
$ npi-config | $ npi-config | ||
Line 42: | Line 159: | ||
spi-flash| SPI0 flash(X) | spi-flash| SPI0 flash(X) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 设置完毕后,dtb里spi flash的status = "okay",需要重启系统才能生效。<br> | |
− | {{Linux-SPIFlash/zh}} | + | {{Linux-SPIFlash/zh}} |
− | + | ||
==H3/H2+== | ==H3/H2+== | ||
+ | ===使用/dev/spidev0.0=== | ||
+ | 在板子上执行下面的命令: | ||
+ | <syntaxhighlight lang="text"> | ||
+ | $ wget https://github.com/friendlyarm/linux/blob/sunxi-4.14.y/tools/spi/spidev_test.c | ||
+ | $ gcc spidev_test.c -o spidev_test | ||
+ | $ ./spidev_test -D /dev/spidev0.0 -S 1024 -I 1000 | ||
+ | </syntaxhighlight> | ||
+ | |||
===SPI Flash=== | ===SPI Flash=== | ||
FriendlyElec H3/H2+的Linux-4.14内核集成了"mxicy,mx25l12805d"的SPI Flash驱动,使用的是SPI0,但是默认并没有使能,相关的dts配置如下: | FriendlyElec H3/H2+的Linux-4.14内核集成了"mxicy,mx25l12805d"的SPI Flash驱动,使用的是SPI0,但是默认并没有使能,相关的dts配置如下: | ||
<syntaxhighlight lang="text"> | <syntaxhighlight lang="text"> | ||
− | $ | + | $ cat linux/arch/arm/boot/dts/sun8i-h3-nanopi.dtsi |
&spi0 { | &spi0 { | ||
... | ... | ||
Line 58: | Line 182: | ||
compatible = "mxicy,mx25l12805d"; | compatible = "mxicy,mx25l12805d"; | ||
reg = <0>; | reg = <0>; | ||
− | status = "disabled"; | + | status = "disabled"; // disabled表示禁用该驱动 |
spi-max-frequency = <50000000>; | spi-max-frequency = <50000000>; | ||
Line 71: | Line 195: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 可以在[[npi-config/zh|npi-config]]里将其使能,如下: | |
<syntaxhighlight lang="text"> | <syntaxhighlight lang="text"> | ||
$ npi-config | $ npi-config | ||
Line 81: | Line 205: | ||
spi-flash| SPI0 flash(X) | spi-flash| SPI0 flash(X) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 设置完毕后,dtb里spi flash的status = "okay",需要重启系统才能生效。<br> | |
{{Linux-SPIFlash/zh}} | {{Linux-SPIFlash/zh}} |
Latest revision as of 08:07, 13 July 2020
Contents
1 RK3399
RK3399下,SPI由于与UART4共用引脚,所以需要修改内核DTS文件。
1.1 Linux下启用SPI
下载内核源代码,编辑内核目录下的dts文件 arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi:
git clone https://github.com/friendlyarm/kernel-rockchip --depth 1 -b nanopi4-linux-v4.4.y kernel-rockchip cd kernel-rockchip vim arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi
首先找到 spi1 的定义:
&spi1 { status = "disabled"; // 将其中的 disabled 改为 okay
然后在 rk3399-nanopi4-common.dtsi 文件到找到 uart4 的定义:
&uart4 { status = "okay"; // 将其中的 okay 改为 disabled
最后重新编译内核 (编译器: gcc6.4):
make ARCH=arm64 nanopi4_linux_defconfig export PATH=/opt/FriendlyARM/toolchain/6.4-aarch64/bin/:$PATH make ARCH=arm64 nanopi4-images
编译完成后会生成 kernel.img resource.img 这两个文件,将其拷贝到eflasher系统的SD卡覆盖旧文件即可, 假设SD卡的FRIENDLYARM分区挂载到FRIENDLYARM目录,更新命令如下:
# for Lubuntu cp kernel.img resource.img /media/FRIENDLYARM/lubuntu/ # for FriendlyCore cp kernel.img resource.img /media/FRIENDLYARM/friendlycore-arm64/ # for FriendlyDesktop cp kernel.img resource.img /media/FRIENDLYARM/friendlydesktop-arm64/
也可以用线刷工具来更新。
1.2 Android下启用SPI
Android下修改方法如下:
编辑内核目录下的dts文件 arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi:
cd ANDROID_SOURCE/kernel vim arch/arm64/boot/dts/rockchip/rk3399-nanopi4-common.dtsi
其中,ANDROID_SOURCE请替换为真实的 Android7或Android8源代码目录。
首先找到 spi1 的定义:
&spi1 { status = "disabled"; // 将其中的 disabled 改为 okay
然后再在 rk3399-nanopi4-common.dtsi 文件到找到 uart4 的定义:
&uart4 { status = "okay"; // 将其中的 okay 改为 disabled
最后重新编译内核:
cd ANDROID_SOURCE/ ./build-nanopc-t4.sh -K -M
更新 rockdev/Image-nanopc_t4/resource.img 到开发板即可,可以将resource.img拷贝到安装有 eflasher 系统的TF卡中,位置是FriendlyARM分区的androidX 目录,替换里面的resource.img文件,然后用TF卡重刷系统。
1.3 RK3399 SPI的其他信息
- 目前spidev0 配置为最高 10mhz
- 驱动是 drivers/spi/spi-rockchip.c
2 S5P4418
2.1 Q: S5P4418的/dev目录下只有一个spidev0.0这个设备节点, 怎么添加spidev1.0
对于3.4内核来说,4418 需要修改 arch/arm/plat-s5p4418/nanopi2/device.c;
想要增加一路的话,可先搜索 CONFIG_SPI_SPIDEV , 然后参考已有代码增加另一路的支持;
另外,arch/arm/plat-s5p4418/asb/device.c 有 spi2 的支持,修改时可参考;
3 H5
3.1 使用/dev/spidev0.0
在板子上执行下面的命令:
$ wget https://github.com/friendlyarm/linux/blob/sunxi-4.14.y/tools/spi/spidev_test.c $ gcc spidev_test.c -o spidev_test $ ./spidev_test -D /dev/spidev0.0 -S 1024 -I 1000
3.2 使能SPI1
H5的SPI1和UART3的引脚是复用的,我们默认提供的配置是使能UART3,禁用SPI1,内核设备树的配置如下:
linux/arch/arm64/boot/dts/allwinner/sun50i-h5-nanopi.dtsi
&spi1 { // against uart3_rts_cts_pins status = "disable"; spidev1: spi@1 { compatible = "nanopi,spidev"; reg = <0>; spi-max-frequency = <10000000>; }; }; &uart3 { pinctrl-names = "default"; pinctrl-0 = <&uart3_pins>, <&uart3_rts_cts_pins>; status = "okay"; };
如果想使能SPI1,需要修改设备树如下:
&spi1 { // against uart3_rts_cts_pins status = "okay"; spidev1: spi@1 { compatible = "nanopi,spidev"; reg = <0>; spi-max-frequency = <10000000>; }; }; &uart3 { pinctrl-names = "default"; pinctrl-0 = <&uart3_pins>, <&uart3_rts_cts_pins>; status = "disable"; };
另外,使用npi-config/zh也可以实现上述目的。
3.3 SPI Flash
FriendlyElec H5的Linux-4.14内核集成了"mxicy,mx25l12805d"的SPI Flash驱动,使用的是SPI0,但是默认并没有使能,相关的dts配置如下:
$ cat linux/arch/arm64/boot/dts/allwinner/sun50i-h5-nanopi.dtsi &spi0 { ... spiflash: spiflash@0 { #address-cells = <1>; #size-cells = <1>; compatible = "mxicy,mx25l12805d"; reg = <0>; status = "disabled"; // disabled表示禁用该驱动 spi-max-frequency = <50000000>; mode = <0>; partition@0 { reg = <0x0 0x1000000>; label = "spi-flash"; }; }; ...
可以在npi-config里将其使能,如下:
$ npi-config 6 Advanced Options| Configure advanced setting A5 SPI| Enable/Disable SPI spi0 Enable/Disable spi0[enabled] spidev0.0| SPI0 char device spi-2.8'tft| Matrix-2'8_SPI_Key_TFT spi-flash| SPI0 flash(X)
设置完毕后,dtb里spi flash的status = "okay",需要重启系统才能生效。
启动Linux系统时,看到类似下面的启动信息则说明SPI Flash被成功检测到了:
m25p80 spi0.0: mx25l12805d (16384 Kbytes) 1 ofpart partitions found on MTD device spi0.0 Creating 1 MTD partitions on "spi0.0": 0x000000000000-0x000001000000 : "spi-flash"
SPI Flash在Linux系统下的使用方法如下:
$ cat /sys/class/mtd/mtd0/size # 以byte为单位 $ cat /sys/class/block/mtdblock0/size # 以sector(512 byte)为单位 $ apt-get install mtd-utils $ flash_eraseall -j /dev/mtd0 # 格式成jffs2的格式 $ mount -t jffs2 /dev/mtdblock0 /mnt $ mkfs.ext4 /dev/mtdblock0 # 也格式化为ext4 $ mount -t ext4 /dev/mtdblock0 /mnt
4 H3/H2+
4.1 使用/dev/spidev0.0
在板子上执行下面的命令:
$ wget https://github.com/friendlyarm/linux/blob/sunxi-4.14.y/tools/spi/spidev_test.c $ gcc spidev_test.c -o spidev_test $ ./spidev_test -D /dev/spidev0.0 -S 1024 -I 1000
4.2 SPI Flash
FriendlyElec H3/H2+的Linux-4.14内核集成了"mxicy,mx25l12805d"的SPI Flash驱动,使用的是SPI0,但是默认并没有使能,相关的dts配置如下:
$ cat linux/arch/arm/boot/dts/sun8i-h3-nanopi.dtsi &spi0 { ... spiflash: spiflash@0 { #address-cells = <1>; #size-cells = <1>; compatible = "mxicy,mx25l12805d"; reg = <0>; status = "disabled"; // disabled表示禁用该驱动 spi-max-frequency = <50000000>; mode = <0>; partition@0 { reg = <0x0 0x1000000>; label = "spi-flash"; }; }; ...
可以在npi-config里将其使能,如下:
$ npi-config 6 Advanced Options| Configure advanced setting A5 SPI| Enable/Disable SPI spi0 Enable/Disable spi0[enabled] spidev0.0| SPI0 char device spi-2.8'tft| Matrix-2'8_SPI_Key_TFT spi-flash| SPI0 flash(X)
设置完毕后,dtb里spi flash的status = "okay",需要重启系统才能生效。
启动Linux系统时,看到类似下面的启动信息则说明SPI Flash被成功检测到了:
m25p80 spi0.0: mx25l12805d (16384 Kbytes) 1 ofpart partitions found on MTD device spi0.0 Creating 1 MTD partitions on "spi0.0": 0x000000000000-0x000001000000 : "spi-flash"
SPI Flash在Linux系统下的使用方法如下:
$ cat /sys/class/mtd/mtd0/size # 以byte为单位 $ cat /sys/class/block/mtdblock0/size # 以sector(512 byte)为单位 $ apt-get install mtd-utils $ flash_eraseall -j /dev/mtd0 # 格式成jffs2的格式 $ mount -t jffs2 /dev/mtdblock0 /mnt $ mkfs.ext4 /dev/mtdblock0 # 也格式化为ext4 $ mount -t ext4 /dev/mtdblock0 /mnt