|
rt_err_t | rt_spi_bus_register (struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops) |
|
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_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) |
|
rt_err_t | rt_spi_bus_configure (struct rt_spi_device *device) |
|
rt_err_t | rt_spi_take_bus (struct rt_spi_device *device) |
|
rt_err_t | rt_spi_release_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_configure (struct rt_spi_device *device, struct rt_spi_configuration *cfg) |
|
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_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_ssize_t | rt_spi_transfer (struct rt_spi_device *device, const void *send_buf, void *recv_buf, rt_size_t length) |
|
rt_err_t | rt_spi_sendrecv8 (struct rt_spi_device *device, rt_uint8_t senddata, rt_uint8_t *recvdata) |
|
rt_err_t | rt_spi_sendrecv16 (struct rt_spi_device *device, rt_uint16_t senddata, rt_uint16_t *recvdata) |
|
struct rt_spi_message * | rt_spi_transfer_message (struct rt_spi_device *device, struct rt_spi_message *message) |
|
rt_inline rt_size_t | rt_spi_recv (struct rt_spi_device *device, void *recv_buf, rt_size_t length) |
|
rt_inline rt_size_t | rt_spi_send (struct rt_spi_device *device, const void *send_buf, rt_size_t length) |
|
rt_inline void | rt_spi_message_append (struct rt_spi_message *list, struct rt_spi_message *message) |
|
rt_err_t | rt_qspi_configure (struct rt_qspi_device *device, struct rt_qspi_configuration *cfg) |
|
rt_err_t | rt_qspi_bus_register (struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops) |
|
rt_size_t | rt_qspi_transfer_message (struct rt_qspi_device *device, struct rt_qspi_message *message) |
|
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) |
|
rt_err_t | rt_qspi_send (struct rt_qspi_device *device, const void *send_buf, rt_size_t length) |
|
SPI driver api
Example
#include <rtdevice.h>
#define W25Q_SPI_DEVICE_NAME "qspi10"
static void spi_w25q_sample(int argc, char *argv[])
{
char name[RT_NAME_MAX];
if (argc == 2)
{
rt_strncpy(name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(name, W25Q_SPI_DEVICE_NAME, RT_NAME_MAX);
}
if (!spi_dev_w25q)
{
rt_kprintf(
"spi sample run failed! can't find %s device!\n", name);
}
else
{
rt_kprintf(
"use rt_spi_send_then_recv() read w25q ID is:%x%x\n",
id[3],
id[4]);
msg1.length = 1;
msg1.cs_take = 1;
msg1.cs_release = 0;
msg1.next = &msg2;
msg2.recv_buf = id;
msg2.length = 5;
msg2.cs_take = 0;
msg2.cs_release = 1;
rt_kprintf(
"use rt_spi_transfer_message() read w25q ID is:%x%x\n",
id[3],
id[4]);
}
}
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)
This function transfers a message list to the SPI device.
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)
This function can send data then receive data from SPI device.
#define MSH_CMD_EXPORT(...)
SPI Virtual BUS, one device must connected to a virtual BUS
◆ RT_SPI_CPHA
#define RT_SPI_CPHA (1<<0) |
At CPOL=0 the base value of the clock is zero
- For CPHA=0, data are captured on the clock's rising edge (low->high transition) and data are propagated on a falling edge (high->low clock transition).
- For CPHA=1, data are captured on the clock's falling edge and data are propagated on a rising edge. At CPOL=1 the base value of the clock is one (inversion of CPOL=0)
- For CPHA=0, data are captured on clock's falling edge and data are propagated on a rising edge.
- For CPHA=1, data are captured on clock's rising edge and data are propagated on a falling edge. bit[0]:CPHA, clock phase
在文件 dev_spi.h 第 111 行定义.
◆ RT_SPI_CPOL
#define RT_SPI_CPOL (1<<1) |
◆ RT_SPI_LSB
#define RT_SPI_LSB (0<<2) |
◆ RT_SPI_MSB
#define RT_SPI_MSB (1<<2) |
◆ RT_SPI_MASTER
#define RT_SPI_MASTER (0<<3) |
◆ RT_SPI_SLAVE
#define RT_SPI_SLAVE (1<<3) |
◆ RT_SPI_CS_HIGH
#define RT_SPI_CS_HIGH (1<<4) |
◆ RT_SPI_NO_CS
#define RT_SPI_NO_CS (1<<5) |
◆ RT_SPI_3WIRE
#define RT_SPI_3WIRE (1<<6) |
◆ RT_SPI_READY
#define RT_SPI_READY (1<<7) |
◆ RT_SPI_MODE_MASK
◆ RT_SPI_MODE_0
#define RT_SPI_MODE_0 (0 | 0) |
◆ RT_SPI_MODE_1
◆ RT_SPI_MODE_2
◆ RT_SPI_MODE_3
◆ RT_SPI_BUS_MODE_SPI
#define RT_SPI_BUS_MODE_SPI (1<<0) |
◆ RT_SPI_BUS_MODE_QSPI
#define RT_SPI_BUS_MODE_QSPI (1<<1) |
◆ SPI_DEVICE
#define SPI_DEVICE |
( |
| dev | ) |
|
◆ rt_spi_bus_register()
register a SPI bus
- 参数
-
bus | the SPI bus |
name | the name of SPI bus |
ops | the operations of SPI bus |
- 返回
- rt_err_t error code
◆ rt_spi_bus_attach_device()
rt_err_t rt_spi_bus_attach_device |
( |
struct rt_spi_device * | device, |
|
|
const char * | name, |
|
|
const char * | bus_name, |
|
|
void * | user_data ) |
attach a device on SPI bus
- 参数
-
device | the SPI device |
name | the name of SPI device |
bus_name | the name of SPI bus |
user_data | the user data of SPI device |
- 返回
- rt_err_t error code
◆ rt_spi_bus_attach_device_cspin()
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 ) |
attach a device on SPI bus with CS pin
- 参数
-
device | the SPI device |
name | the name of SPI device |
bus_name | the name of SPI bus |
cs_pin | the CS pin of SPI device |
user_data | the user data of SPI device |
- 返回
- rt_err_t error code
◆ rt_spi_bus_configure()
Reconfigure the SPI bus for the specified device.
- 参数
-
device | Pointer to the SPI device attached to the SPI bus. |
- 返回值
-
RT_EOK | if the SPI device was successfully released and the bus was configured. RT_EBUSY if the SPI bus is currently in use; the new configuration will take effect once the device releases the bus. Other return values indicate failure to configure the SPI bus due to various reasons. |
- 注解
- If the configuration of the SPI device has been updated and requires bus re-initialization, call this function directly. This function will reconfigure the SPI bus for the specified device. If this is the first time to initialize the SPI device, please call rt_spi_configure or rt_qspi_configure. This function is used to reconfigure the SPI bus when the SPI device is already in use. For further details, refer to: https://github.com/RT-Thread/rt-thread/pull/8528
◆ rt_spi_take_bus()
This function takes SPI bus.
- 参数
-
device | the SPI device attached to SPI bus |
- 返回
- RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
◆ rt_spi_release_bus()
This function releases SPI bus.
- 参数
-
device | the SPI device attached to SPI bus |
- 返回
- RT_EOK on release SPI bus successfully.
◆ rt_spi_take()
This function take SPI device (takes CS of SPI device).
- 参数
-
device | the SPI device attached to SPI bus |
- 返回
- RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
◆ rt_spi_release()
This function releases SPI device (releases CS of SPI device).
- 参数
-
device | the SPI device attached to SPI bus |
- 返回
- RT_EOK on release SPI device successfully.
◆ rt_spi_configure()
This function can set configuration on SPI device.
- 参数
-
device | the SPI device attached to SPI bus |
cfg | the configuration pointer. |
- 返回值
-
RT_EOK | on release SPI device successfully. RT_EBUSY is not an error condition and the configuration will take effect once the device has the bus others on taken SPI bus failed. |
◆ rt_spi_send_then_recv()
This function can send data then receive data from SPI device.
- 参数
-
device | the SPI device attached to SPI bus |
send_buf | the buffer to be transmitted to SPI device. |
send_length | the number of data to be transmitted. |
recv_buf | the buffer to be recivied from SPI device. |
recv_length | the data to be recivied. |
- 返回
- rt_err_t error code
◆ rt_spi_send_then_send()
This function can send data then send data from SPI device.
- 参数
-
device | the SPI device attached to SPI bus |
send_buf1 | the buffer to be transmitted to SPI device. |
send_length1 | the number of data to be transmitted. |
send_buf2 | the buffer to be transmitted to SPI device. |
send_length2 | the number of data to be transmitted. |
- 返回
- the status of transmit.
◆ rt_spi_transfer()
This function transmits data to SPI device.
- 参数
-
device | the SPI device attached to SPI bus |
send_buf | the buffer to be transmitted to SPI device. |
recv_buf | the buffer to save received data from SPI device. |
length | the length of transmitted data. |
- 返回
- the actual length of transmitted.
◆ rt_spi_sendrecv8()
The SPI device transmits 8 bytes of data
- 参数
-
device | the SPI device attached to SPI bus |
senddata | send data buffer |
recvdata | receive data buffer |
- 返回
- rt_err_t error code
◆ rt_spi_sendrecv16()
The SPI device transmits 16 bytes of data
- 参数
-
device | the SPI device attached to SPI bus |
senddata | send data buffer |
recvdata | receive data buffer |
- 返回
- rt_err_t error code
◆ rt_spi_transfer_message()
This function transfers a message list to the SPI device.
- 参数
-
device | the SPI device attached to SPI bus |
message | the message list to be transmitted to SPI device |
- 返回
- RT_NULL if transmits message list successfully, SPI message which be transmitted failed.
◆ rt_spi_recv()
This function receives data from SPI device.
- 参数
-
device | the SPI device attached to SPI bus |
recv_buf | the buffer to be recivied from SPI device. |
length | the data to be recivied. |
- 返回
- the actual length of received.
在文件 dev_spi.h 第 527 行定义.
530{
532}
rt_ssize_t rt_spi_transfer(struct rt_spi_device *device, const void *send_buf, void *recv_buf, rt_size_t length)
This function transmits data to SPI device.
引用了 rt_spi_message::length, rt_spi_message::recv_buf, RT_NULL , 以及 rt_spi_transfer().
◆ rt_spi_send()
◆ rt_spi_message_append()
This function appends a message to the SPI message list.
- 参数
-
list | the SPI message list header. |
message | the message pointer to be appended to the message list. |
在文件 dev_spi.h 第 556 行定义.
558{
561 return;
562
564 {
566 }
567
568 list->
next = message;
570}
struct rt_spi_message * next
引用了 rt_spi_message::next, RT_ASSERT , 以及 RT_NULL.
◆ rt_qspi_configure()
This function can set configuration on QSPI device.
- 参数
-
device | the QSPI device attached to QSPI bus. |
cfg | the configuration pointer. |
- 返回
- the actual length of transmitted.
◆ rt_qspi_bus_register()
This function can register a SPI bus for QSPI mode.
- 参数
-
bus | the SPI bus for QSPI mode. |
name | The name of the spi bus. |
ops | the SPI bus instance to be registered. |
- 返回
- the actual length of transmitted.
◆ rt_qspi_transfer_message()
This function transmits data to QSPI device.
- 参数
-
device | the QSPI device attached to QSPI bus. |
message | the message pointer. |
- 返回
- the actual length of transmitted.
◆ rt_qspi_send_then_recv()
This function can send data then receive data from QSPI device
- 参数
-
device | the QSPI device attached to QSPI bus. |
send_buf | the buffer to be transmitted to QSPI device. |
send_length | the number of data to be transmitted. |
recv_buf | the buffer to be recivied from QSPI device. |
recv_length | the data to be recivied. |
- 返回
- the status of transmit.
◆ rt_qspi_send()
This function can send data to QSPI device
- 参数
-
device | the QSPI device attached to QSPI bus. |
send_buf | the buffer to be transmitted to QSPI device. |
length | the number of data to be transmitted. |
- 返回
- the status of transmit.