RT-Thread Studio-based SPI Development Document¶
Introduction¶
The Serial Peripheral Interface is a synchronous serial communication interface specification used for short-distance communication, primarily in embedded systems. The SFUD component and RW007 WIFI module of RT-Thread are all SPI-driven. The stm32l475-atk-pandora
development board will be taken as an example to explain the development of SPI drivers based on RT-Thread Studio.
SPI device driver development can be summarized as follows:
- Create an RT-Thread Standard Version project.
- Enable the SPI device driver framework.
- Define SPI Bus-related Macros.
- Open support for SPI in HAL Library.
- Copy the SPI PIN initialization function to the project.
For more details about SPI bus configuration and addition processes please refer to the introduction of the SPI bus in the corresponding project file board.h
.
Create RT-Thread Project¶
Using RT-Thread Studio to create an RT-Thread v4.0.2 project, as shown below:
The configuration process can be summarized as follows:
- Define your own project name and the storage path for the generated project files.
- Select 'Base on MCU' to create a project and select RT-Thread version v4.0.2.
- Select the vendor and chip series.
- Configure serial information.
- Configure the debugger information.
Once the project is configured, click the 'Finish' button to create the RT-Thread project.
Enable SPI Device Driver Framework¶
Enable the SPI driver framework by using the graphical configuration tool in the RT-Thread Setting
file, as shown below:
Left-click to enable SPI
driver framework (components will also be enabled and the icon lights up), right-click SPI
there is an option to view its 'details configuration', as shown below:
RT-Thread Setting ----Components --------Device Driver Program ------------Use SPI Bus/ Device Driver Program
Configuration results are shown as below:
Define SPI Bus Related Macros¶
Define the related macros of the SPI bus on the board.h file, in this case, we are using the SPI3 bus to define the following macro:
#define BSP_USING_SPI3
Open support for SPI in HAL Library¶
Open support for SPI in stm32xxxx_hal_config.h file, which means cancel off the HAL_SPI_MODULE_ENABLED macro definition comments, as shown below:
#define HAL_SPI_MODULE_ENABLED
Initialize PINs and Clocks¶
Once the BSP_USING_SPI3
macro is defined, the drv_spi.c
file will start compiling, this file has only simply configured how the SPI works and function transfers, the initialization of the clock and PINs for SPI peripheral needs the help of the code generated in STM32CubeMx
.
For example, the SPI3
peripheral of the stm32l475-atk-pandora
development board is connected to an LCD
screen, so the initialization code for SPI3
is generated by STM32CubeMx
(typically stored in the stm32_xxxx_hal_msp.c
file) needs to be copied to the bottom of the board.c
file of your own project for compiling, as shown below:
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if(hspi->Instance == SPI3) { /* USER CODE BEGIN SPI3_MspInit 0 */ /* USER CODE END SPI3_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_SPI3_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /**SPI3 GPIO Configuration PC11 ------> SPI3_MISO PB3 (JTDO-TRACESWO) ------> SPI3_SCK PB5 ------> SPI3_MOSI */ GPIO_InitStruct.Pin = GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF6_SPI3; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF6_SPI3; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE BEGIN SPI3_MspInit 1 */ /* USER CODE END SPI3_MspInit 1 */ } }
If you need to register more SPI bus devices, please refer to the SPI-related macro definition on the board.h
file and copy the PIN initialization function.
Use SPI Bus¶
Compile and download the program, enter the list_device
command in the terminal you can observe that the SPI bus has been successfully registered in the system, as shown below: