7.SPI主从数据传输测试
该教程演示如何通过两块开发板实现SPI主从数据传输测试
一、准备工作
- 准备2块BearPi-Pico H2821开发板
二、编译代码
在Windows下编译操作
点击左侧的“KConfig”,打开配置界面.
选择Support SPI Sample.
[*] Enable Sample. [*] Enable the Sample of peripheral. [ ] Enable all the sample of peripheral, it's just for build. [ ] Support ADC Sample. [ ] Support BLINKY Sample. [ ] Support BUTTON Sample. [ ] Support DMA Sample. [ ] Support I2C Sample. [ ] Support I2S Sample. [ ] Support KEYSCAN Sample. [ ] Support PINCTRL Sample. [ ] Support PWM Sample. [ ] Support QDEC Sample. [ ] Support RTC Sample. [*] Support SPI Sample. SPI Sample Configuration --->
设置SPI主设备的SPI id和GPIO,id可设置为0、1、2,GPIO可设置任意引脚。
SPI Sample Configuration ---> [*] Support SPI Master Sample. (1) Choose SPI master bus id. (NEW) (12) Choose SPI DI master pin. (NEW) (13) Choose SPI DO master pin. (NEW) (14) Choose SPI CLK master pin. (NEW) (15) Choose SPI CS master pin. (NEW)
按下"Save"键保存配置。
编译烧录固件
参考环境搭建教程编译烧录代码
设置SPI从设备的SPI id和GPIO,id可设置为0、1、2,GPIO可设置任意引脚。
SPI Sample Configuration ---> [*] Support SPI Slave Sample. (2) Choose SPI slave bus id. (NEW) (12) Choose SPI DI slave pin. (NEW) (13) Choose SPI DO slave pin. (NEW) (14) Choose SPI CLK slave pin. (NEW) (15) Choose SPI CS slave pin. (NEW)
按下"Save"键保存配置。
编译烧录固件
参考环境搭建教程编译烧录代码
在Ubuntu下编译操作
在MobaXterm中输入:
./build.py menuconfig
选择Support SPI Sample.
[*] Enable Sample. [*] Enable the Sample of peripheral. [ ] Enable all the sample of peripheral, it's just for build. [ ] Support ADC Sample. [ ] Support BLINKY Sample. [ ] Support BUTTON Sample. [ ] Support DMA Sample. [ ] Support I2C Sample. [ ] Support I2S Sample. [ ] Support KEYSCAN Sample. [ ] Support PINCTRL Sample. [ ] Support PWM Sample. [ ] Support QDEC Sample. [ ] Support RTC Sample. [*] Support SPI Sample. SPI Sample Configuration --->
设置SPI主设备的SPI id和GPIO,id可设置为0、1、2,GPIO可设置任意引脚。
SPI Sample Configuration ---> [*] Support SPI Master Sample. (1) Choose SPI master bus id. (NEW) (12) Choose SPI DI master pin. (NEW) (13) Choose SPI DO master pin. (NEW) (14) Choose SPI CLK master pin. (NEW) (15) Choose SPI CS master pin. (NEW)
按下"ESC"键退出并保存配置。
编译烧录固件
参考环境搭建教程编译烧录代码
设置SPI从设备的SPI id和GPIO,id可设置为0、1、2,GPIO可设置任意引脚。
SPI Sample Configuration ---> [*] Support SPI Slave Sample. (2) Choose SPI slave bus id. (NEW) (12) Choose SPI DI slave pin. (NEW) (13) Choose SPI DO slave pin. (NEW) (14) Choose SPI CLK slave pin. (NEW) (15) Choose SPI CS slave pin. (NEW)
按下"ESC"键退出并保存配置。
编译烧录固件
参考环境搭建教程编译烧录代码
三、测试
烧录固件后按下开发的复位按键,将两块开发板设置为SPI CLK的引脚接一起;将两块开发板设置为SPI CS的引脚接一起;将一块开发板设置为SPI DO的引脚与另外一块开发板设置为SPI DI的引脚接一起;将一块开发板设置为SPI DI的引脚与另外一块开发板设置为SPI DO的引脚接一起;将两块开发板GND引脚接一起;观察两块开发板的运行日志。
主设备端日志:
spi1 master send start!
spi1 master send succ!
spi1 master receive start!
spi1 master receive data is 0
spi1 master receive data is 0
spi1 master receive data is 0
spi1 master receive data is 0
spi1 master receive data is 0
spi1 master receive data is 0
spi1 master receive data is 0
spi1 master receive data is 0
spi1 master receive succ!
从设备端日志:
spi2 slave send start!
spi2 slave send succ!
spi2 slave receive start!
spi2 slave receive data is 0
spi2 slave receive data is 1
spi2 slave receive data is 2
spi2 slave receive data is 3
spi2 slave receive data is 4
spi2 slave receive data is 5
spi2 slave receive data is 6
spi2 slave receive data is 7
spi2 slave receive succ!