1.添加驱动
①点开设置界面
②勾选看门 SPI 驱动
③点击保存
④查看添加的驱动文件 drv_spi.c

2.打开驱动头文件定义
①打开配置文件
②打开定义

3.打开需要开启的SPI总线
打开 drivers 目录下的 board.h 用SPI搜索,找到如下文字,打开对应的宏。
cpp
/*-------------------------- SPI CONFIG BEGIN --------------------------*/
/** if you want to use spi bus you can use the following instructions.
*
* STEP 1, open spi driver framework support in the RT-Thread Settings file
*
* STEP 2, define macro related to the spi bus
* such as #define BSP_USING_SPI1
*
* STEP 3, copy your spi init function from stm32xxxx_hal_msp.c generated by stm32cubemx to the end of board.c file
* such as void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
*
* STEP 4, modify your stm32xxxx_hal_config.h file to support spi peripherals. define macro related to the peripherals
* such as #define HAL_SPI_MODULE_ENABLED
*/
/*#define BSP_USING_SPI1*/
#define BSP_USING_SPI2
/*#define BSP_USING_SPI3*/
4.查看设备
添加好后,把程序编译,并下载到mcu,在系统启动到输出 mch 时,通过串口发送命令 list_device,系统会回复 设备清单,如下图 sp1,sp2就是新增的设备。

5.打开STM32CubeMX配置初始化代码
①在时钟已经配好的情况下进行如下操作
②根据硬件接口选择SPI对应的管脚


③配置SPI

输出项目设置

代码生成器 设置

生成代码.

打开文件夹找到代码生成目录:
需要的代码在如下②,③文件,也有可能因为选择项的不同生成的文件是独立 spi.c
