SPI分为主、从、设备;具体又分标准SPI/DUAL SPI/QUAD SPI(用80字节的RAMrt_err_t rt_spi_take_bus(struct rt_spi_device *device);代替收发寄存器)
从设备的操作:在多线程通讯中,从机需要先获得SPI总线、CS使能;使用完后再分别释放,从而使其它获得控制权。
rt_err_t rt_spi_take_bus(struct rt_spi_device *device);
rt_err_t rt_spi_take(struct rt_spi_device *device);
rt_err_t rt_spi_release(struct rt_spi_device *device);
rt_err_t rt_spi_release_bus(struct rt_spi_device *device);
void rt_spi_message_append(struct rt_spi_message *list, struct rt_spi_message *message);//单链表发送一条消息
二 主设备的操作:
2.1 先挂载已经注册好的SPI设备,
rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device, const char *name, const char *bus_name, void *user_data)
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);//STM32的BSP操作
2.2然后利用配置结构体进行SPI设备的配置
rt_err_t rt_spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configuration *cfg);//QSPI配置有专门的结构体。
2.3SPI设备的读写操作:
struct rt_spi_message {
const void *send_buf; /* 发送缓冲区指针 */
void *recv_buf; /* 接收缓冲区指针 */
rt_size_t length; /* 发送 / 接收 数据字节数 */
struct rt_spi_message *next; /* 指向继续发送的下一条消息的指针 */
unsigned cs_take : 1; /* 片选选中 */
unsigned cs_release : 1; /* 释放片选 */
};
函数 | 描述 |
---|---|
rt_device_t rt_device_find(const char* name); | 根据 SPI 设备名称查找设备获取设备句柄 |
struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,struct rt_spi_message *message); | 自定义传输数据,结束时释放片选 |
rt_size_t rt_spi_transfer(struct rt_spi_device *device, const void *send_buf, void *recv_buf, rt_size_t length); | 传输一次数据 |
rt_size_t rt_spi_send(struct rt_spi_device *device, const void *send_buf, rt_size_t length) | 发送一次数据 |
rt_size_t rt_spi_recv(struct rt_spi_device *device, void *recv_buf, rt_size_t length); | 接受一次数据 |
rt_err_t rt_spi_send_then_send(struct rt_spi_device *device, const void *send_buf1, rt_size_t send_length1, const void *send_buf2, rt_size_t send_length2); | 连续两次发送 |
rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device, const void *send_buf, rt_size_t send_length, void *recv_buf, rt_size_t recv_length); | 先发送后接收 |
rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message);//QSPI操作
rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length, void *recv_buf, rt_size_t recv_length);//QSPI操作
rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length);//QSPI操作
SPI 设备管理模块还提供 rt_spi_sendrecv8()
和 rt_spi_sendrecv16()
函数,这两个函数都是对原函数的封装
来源:https://www.cnblogs.com/jieruishu/p/11796394.html