RTT设备与驱动之串口

有一般收发、中断方式、DMA方式:

rt_device_t rt_device_find(const char* name);
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflags); oflags =RT_DEVICE_FLAG_RDWR|RT_DEVICE_FLAG_INT_RX
#define RT_DEVICE_FLAG_RDWR         0x003     /* 可读写模式 */
#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);

应用举例:
#define SAMPLE_UART_NAME       "uart2"  /* 串口设备名称 */
static rt_device_t serial;              /* 串口设备句柄 */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 配置参数 */
/* 查找串口设备 */
serial = rt_device_find(SAMPLE_UART_NAME);

/* 以读写及中断接收方式打开串口设备,若要修改缓冲区大小需在open前用rt_device_control修改;其它参数可以在open后修改*/ 
rt_device_open(serial, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX); config.baud_rate = BAUD_RATE_115200; config.data_bits = DATA_BITS_8; config.stop_bits = STOP_BITS_2; config.parity = PARITY_NONE; /* 打开设备后才可修改串口配置参数 */ rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
相关读写:
rt_err_t rt_device_close(rt_device_t dev);
rt_size_t rt_device_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size);
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_rx_indicate(rt_device_t dev, rt_err_t (*rx_ind)(rt_device_t dev,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));//发送完成回调

 
 
原文地址:https://www.cnblogs.com/jieruishu/p/10270836.html