【RT-Thread Studio】W25Q128配置
配置步骤
/** 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*/
STEP 1
RT-Thread Settings打开SPI
SFUD
STEP 2
drivers\board.h
#define BSP_USING_SPI1
STEP 3
代码参考连接bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Src/stm32f4xx_hal_msp.c
board.c
/**
* @brief SPI MSP Initialization
* This function configures the hardware resources used in this example
* @param hspi: SPI handle pointer
* @retval None
*/
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{GPIO_InitTypeDef GPIO_InitStruct = {0};if(hspi->Instance==SPI1){/* USER CODE BEGIN SPI1_MspInit 0 *//* USER CODE END SPI1_MspInit 0 *//* Peripheral clock enable */__HAL_RCC_SPI1_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();/**SPI1 GPIO ConfigurationPB3 ------> SPI1_SCKPB4 ------> SPI1_MISOPB5 ------> SPI1_MOSI*/GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|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_AF5_SPI1;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);/* SPI1 interrupt Init */HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);HAL_NVIC_EnableIRQ(SPI1_IRQn);/* USER CODE BEGIN SPI1_MspInit 1 *//* USER CODE END SPI1_MspInit 1 */}else if(hspi->Instance==SPI2){/* USER CODE BEGIN SPI2_MspInit 0 *//* USER CODE END SPI2_MspInit 0 *//* Peripheral clock enable */__HAL_RCC_SPI2_CLK_ENABLE();__HAL_RCC_GPIOC_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();/**SPI2 GPIO ConfigurationPC2 ------> SPI2_MISOPC3 ------> SPI2_MOSIPB13 ------> SPI2_SCK*/GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);GPIO_InitStruct.Pin = GPIO_PIN_13;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);/* USER CODE BEGIN SPI2_MspInit 1 *//* USER CODE END SPI2_MspInit 1 */}}
如果不加这段代码,rtthread将会使用库自带的弱定义函数:
__weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)`
STEP 4
drivers\stm32f4xx_hal_conf.h
#define HAL_SPI_MODULE_ENABLED
测试代码
/*-------------------------- HEADER FILES BEGIN --------------------------*/
#include "spi_flash_sfud.h"
#include "drv_spi.h"/*-------------------------- MACRO DEFINITIONS BEGIN ----------------------*/
#define W25Q_SPI_BUS_NAME "spi1"
#define W25Q_SPI_DEVICE_NAME "spi10"/*-------------------------- TYPE DEFINITIONS BEGIN -----------------------*//*-------------------------- FUNCTION DECLARATIONS BEGIN ------------------*/
/*-------------------------- FUNCTION DEFINITIONS BEGIN -------------------*/static int _spi_flash_init(void)
{rt_hw_spi_device_attach(W25Q_SPI_BUS_NAME, W25Q_SPI_DEVICE_NAME, GPIOB, GPIO_PIN_14);if (RT_NULL == rt_sfud_flash_probe("W25Q128", W25Q_SPI_DEVICE_NAME)){rt_kprintf("rt_sfud_flash_probe failed!\n");return -RT_ERROR;};return RT_EOK;
}static int _spi_flash_read_id(void)
{struct rt_spi_device *spi_dev_w25q;rt_uint8_t w25x_read_id = 0x90;rt_uint8_t id[5] = {0};/* 查找 spi 设备获取设备句柄 */spi_dev_w25q = (struct rt_spi_device *)rt_device_find(W25Q_SPI_DEVICE_NAME);if (!spi_dev_w25q){rt_kprintf("failed! can't find %s device!\n", W25Q_SPI_DEVICE_NAME);return -RT_ERROR;}else{rt_spi_send_then_recv(spi_dev_w25q, &w25x_read_id, 1, id, 5);rt_kprintf("read w25q ID is:%x%x\n", id[3], id[4]);}return RT_EOK;
}int dev_spi_flash_init(void)
{_spi_flash_init();_spi_flash_read_id();return RT_EOK;
}INIT_COMPONENT_EXPORT(dev_spi_flash_init);
查看设备
msh >list_device
device type ref count
-------- -------------------- ----------
W25Q128 Block Device 0
spi10 SPI Device 0
spi1 SPI Bus 0