应用程序通过 RT-Thread提供的 I/O 设备管理接口来访问串口硬件,相关接口如下所示:
函数 | 描述 |
---|---|
rt_device_find() | 查找设备 |
rt_device_open() | 打开设备 |
rt_device_read() | 读取数据 |
rt_device_write() | 写入数据 |
rt_device_control() | 控制设备 |
rt_device_set_rx_indicate() | 设置接收回调函数 |
rt_device_set_tx_complete() | 设置发送完成回调函数 |
rt_device_close() | 关闭设备 |
rt_device_t rt_device_find(const char* name);
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflags);
flag取值
#define RT_DEVICE_FLAG_STREAM 0x040 /* 流模式 /
/ 接收模式参数 /
#define RT_DEVICE_FLAG_INT_RX 0x100 / 中断接收模式 /
#define RT_DEVICE_FLAG_DMA_RX 0x200 / DMA 接收模式 /
/ 发送模式参数 /
#define RT_DEVICE_FLAG_INT_TX 0x400 / 中断发送模式 /
#define RT_DEVICE_FLAG_DMA_TX 0x800 / DMA 发送模式 */
rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void* arg);//控制串口
struct serial_configure
{
rt_uint32_t baud_rate; /* 波特率 /
rt_uint32_t data_bits :4; / 数据位 /
rt_uint32_t stop_bits :2; / 停止位 /
rt_uint32_t parity :2; / 奇偶校验位 /
rt_uint32_t bit_order :1; / 高位在前或者低位在前 /
rt_uint32_t invert :1; / 模式 /
rt_uint32_t bufsz :16; / 接收数据缓冲区大小 /
rt_uint32_t reserved :4; / 保留位 /
};
rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void buffer, rt_size_t size);//发送数据
rt_err_t rt_device_set_tx_complete(rt_device_t dev, rt_err_t (*tx_done)(rt_device_t dev,void *buffer));//发送完成设置回调函数
rt_err_t rt_device_set_rx_indicate(rt_device_t dev, rt_err_t (rx_ind)(rt_device_t dev,rt_size_t size));//接收完成设置回调函数
rt_size_t rt_device_read(rt_device_t dev, rt_off_t pos, void buffer, rt_size_t size);//接收数据
rt_err_t rt_device_close(rt_device_t dev);//关闭串口设备