diff --git a/rt-thread-version/rt-thread-standard/programming-manual/device/spi/spi.md b/rt-thread-version/rt-thread-standard/programming-manual/device/spi/spi.md index 8139c6612480e02160c9eb366fd54b52496cb1d6..01873bf2bff8cb5cbc486e9caffb6290a45f7977 100644 --- a/rt-thread-version/rt-thread-standard/programming-manual/device/spi/spi.md +++ b/rt-thread-version/rt-thread-standard/programming-manual/device/spi/spi.md @@ -38,6 +38,46 @@ SPI 以主从方式工作,通常有一个主设备和一个或多个从设备 SPI 驱动会注册 SPI 总线,SPI 设备需要挂载到已经注册好的 SPI 总线上。 +```c +rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device, + const char *name, + const char *bus_name, + rt_base_t cs_pin, + void *user_data) +``` + +| **参数** | **描述** | +| ---------- | --------------------------- | +| device | SPI 设备句柄 | +| name | SPI 设备名称 | +| bus_name | SPI 总线名称 | +| cs_pin | SPI 片选引脚号(基于PIN框架) | +| user_data | 用户数据指针 | +| **返回** | —— | +| RT_EOK | 成功 | +| 其他错误码 | 失败 | + +此函数用于挂载一个 SPI 设备到指定的 SPI 总线,并向内核注册 SPI 设备。并且可以依赖RT-Thread的PIN框架来绑定SPI的片选引脚(cs_pin),避免了不同bsp的上层应用对片选引脚操作不统一的问题。 + +一般 SPI 总线命名原则为 spix, SPI 设备命名原则为 spixy ,如 spi10 表示挂载在 spi1 总线上的 0 号设备。cs_pin可以通过PIN框架`rt_pin_get`函数来获取,也可以使用BSP级提供的`GET_PIN`宏定义来获取。user_data 在用户使用不到的情况下可以设置为`RT_NULL`。 + +使用示例: + +```c +struct rt_spi_device *spi_device; +spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); +/* BSP级 GET_PIN 宏定义方式 */ +rt_hw_spi_device_attach(spi_device, "spi1", "spi10", GET_PIN(B, 14), RT_NULL); +/* PIN框架级 rt_pin_get api方式 */ +rt_hw_spi_device_attach(spi_device, "spi1", "spi10", rt_pin_get("PB.14"), RT_NULL); +``` + +> [!NOTE] +> +> 此函数是RT-Thread 5.0.0 添加的新函数,如果低于5.0.0版本不支持这个函数。 + +为了兼容RT-Thread 5.0.0 版本前的SPI设备片选引脚通过user_data挂载的方式,我们保留了`rt_spi_bus_attach_device`这个api,但是希望大家在今后使用的时候,尽量使用`rt_spi_bus_attach_device_cspin`这个新特性api。 + ```c rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device, const char *name, @@ -59,27 +99,33 @@ rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device, 一般 SPI 总线命名原则为 spix, SPI 设备命名原则为 spixy ,如 spi10 表示挂载在 spi1 总线上的 0 号设备。user_data 一般为 SPI 设备的 CS 引脚指针,进行数据传输时 SPI 控制器会操作此引脚进行片选。 -若使用 rt-thread/bsp/stm32 目录下的 BSP 则可以使用下面的函数挂载 SPI 设备到总线: - -```c -rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, GPIO_TypeDef* cs_gpiox, uint16_t cs_gpio_pin); -``` - 下面的示例代码挂载 SPI FLASH W25Q128 到 SPI 总线: ```c static int rt_hw_spi_flash_init(void) { - __HAL_RCC_GPIOB_CLK_ENABLE(); - rt_hw_spi_device_attach("spi1", "spi10", GPIOB, GPIO_PIN_14); - + struct rt_spi_device *spi_device = RT_NULL; + + spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); + if(RT_NULL == spi_device) + { + LOG_E("Failed to malloc the spi device."); + return -RT_ENOMEM; + } + if (RT_EOK != rt_spi_bus_attach_device_cspin(spi_device, "spi1", "spi10",GET_PIN(B, 14), RT_NULL)) + { + LOG_E("Failed to attach the spi device."); + return -RT_ERROR; + } if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi10")) { + LOG_E("Failed to probe the W25Q128."); return -RT_ERROR; }; return RT_EOK; } + /* 导出到自动初始化 */ INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init); ```