本文章主要是记录如何利用 RT-Thread Studio 从 0 开始使用 RT-Thread 的 SPI 设备,详细的 API 可见 SPI 设备文档 。
首先创建一个空的 RT-Thread 项目。
创建项目
然后开启 SPI 组件和 SFUD(串行 SPI Flash 通用驱动库)组件,因为后续的例子会使用到 W25Q64 Flash,因此需要开启 SFUD。
然后进入 board.h ,找到下述段落
>/*-------------------------- 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*/
>
>/*-------------------------- SPI CONFIG END --------------------------*/
按照上述 STEP 1 ~ STEP 4 的要求:
- 在 RT-Thread 设置文件打开 spi 驱动框架。
- 先定义使用的 spi bus,此处我使用的是 SPI1,于是
#define BSP_USING_SPI1
。 - 使用 CubeMX 工具生成相应 SPI 的初始化文件,具体模式不重要,只用到了引脚初始化。将生成的
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
函数和void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
函数复制到 board.c 文件中。 - 在 stm32xxxx_hal_config.h 文件中(此处我的是 stm32f1xx_hal_config.h)定义
#define HAL_SPI_MODULE_ENABLED
。
以上就是 SPI Flash 使用时的环境配置,下面介绍怎么使用。
在使用 SPI 设备之前需要将其挂载到 SPI 总线上去,参考下述代码:
// 注册 spi 设备
static int rt_hw_spi_flash_init(void)
{
__HAL_RCC_GPIOC_CLK_ENABLE();
rt_hw_spi_device_attach("spi1", "spi10", GPIOC, GPIO_PIN_0);
if (RT_NULL == rt_sfud_flash_probe("W25Q64", "spi10"))
{
return -RT_ERROR;
};
return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);
下述语句是必须要有的,否则 Flash 无法正常访问。
if (RT_NULL == rt_sfud_flash_probe("W25Q64", "spi10"))
{
return -RT_ERROR;
};
这样就将 W25Q64 这个设备挂在到 spi1 总线上去了,设备名为 spi10。
环境配置好后使用就简单多了,毕竟是一个成熟的框架,参考文档里的示例代码:
#define W25Q_SPI_DEVICE_NAME "spi10"
static void spi_w25q_sample(int argc, char *argv[])
{
struct rt_spi_device *spi_dev_w25q;
char name[RT_NAME_MAX];
rt_uint8_t w25x_read_id = 0x90;
rt_uint8_t id[5] = {0};
if (argc == 2)
{
rt_strncpy(name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(name, W25Q_SPI_DEVICE_NAME, RT_NAME_MAX);
}
/* 查找 spi 设备获取设备句柄 */
spi_dev_w25q = (struct rt_spi_device *)rt_device_find(name);
if (!spi_dev_w25q)
{
rt_kprintf("spi sample run failed! can't find %s device!\n", name);
}
else
{
/* 方式1:使用 rt_spi_send_then_recv()发送命令读取ID */
rt_spi_send_then_recv(spi_dev_w25q, &w25x_read_id, 1, id, 5);
rt_kprintf("use rt_spi_send_then_recv() read w25q ID is:%x%x\n", id[3], id[4]);
/* 方式2:使用 rt_spi_transfer_message()发送命令读取ID */
struct rt_spi_message msg1, msg2;
msg1.send_buf = &w25x_read_id;
msg1.recv_buf = RT_NULL;
msg1.length = 1;
msg1.cs_take = 1;
msg1.cs_release = 0;
msg1.next = &msg2;
msg2.send_buf = RT_NULL;
msg2.recv_buf = id;
msg2.length = 5;
msg2.cs_take = 0;
msg2.cs_release = 1;
msg2.next = RT_NULL;
rt_spi_transfer_message(spi_dev_w25q, &msg1);
rt_kprintf("use rt_spi_transfer_message() read w25q ID is:%x%x\n", id[3], id[4]);
}
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(spi_w25q_sample, spi w25q sample);
本文章主要记录使用 SPI 时的环境配置,具体使用方法建议参考官方文档,写的十分详细。