SPI全双工设备的操作分为主设备和从设备(可以多个,多线程下从设备访问主设备要先获得总线控制权)
rt_device_t rt_device_find(const char* name);
查找设备
struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,struct rt_spi_message *message);
发送消息,以单向链表形式
void rt_spi_message_append(struct rt_spi_message *list,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);
发送2次
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_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)
释放总线
特殊使用场景
在一些特殊的使用场景,某个设备希望独占总线一段时间,且期间要保持片选一直有效,期间数据传输可能是间断的,则可以按照如所示步骤使用相关接口。传输数据函数必须使用 rt_spi_transfer_message()
,并且此函数每个待传输消息的片选控制域 cs_take 和 cs_release 都要设置为 0 值,因为片选已经使用了其他接口控制,不需要在数据传输的时候控制