当前位置: 首页 > news >正文

【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

相关文章:

  • 【字符函数和字符串函数】
  • Cadence 高速系统设计流程及工具使用三
  • NX949NX952美光科技闪存NX961NX964
  • C++——继承
  • 华为云Git使用与GitCode操作指南
  • Windows报错:OSError: [WinError 1455] 页面文件太小,无法完成操作的问题
  • C. scanf 函数基础
  • Linux系统入门第十二章 --Shell编程之正则表达式
  • MySQL 从入门到精通(四):备份与恢复实战——从逻辑到物理,增量备份全解析
  • [CLS] 向量是 BERT 类模型中一个特别重要的输出向量,它代表整个句子或文本的全局语义信息
  • LeetCode 2918.数组的最小相等和:if-else
  • 物品识别 树莓派4 YOLO v11
  • 常用的Linux命令100条
  • 如何启动vue项目及vue语法组件化不同标签应对的作用说明
  • 批量图片处理的小工具
  • Paging 3.0 + Kotlin 分页加载指南
  • 计算机网络与多线程同步机制详解
  • Pytorch应用 小记 第一回:基于ResNet网络的图像定位
  • 汇编语言的温度魔法:单总线温度采集与显示的奇幻之旅
  • Python-函数
  • 美国再工业化进程需要中国的产业支持
  • 梅花奖在上海|朱洁静:穿越了人生暴风雨,舞台是最好良药
  • 重温经典|《南郭先生》:不模仿别人,不重复自己
  • 七大交响乐团在沪“神仙斗法”,时代交响奏出何等时代新声
  • 国家主席习近平同普京总统举行小范围会谈
  • 老铺黄金拟配售募资近27亿港元,用于门店拓展扩建及补充流动资金等