RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
+ Serial v2 的协作图:

结构体

struct  serial_configure
 
struct  rt_serial_rx_fifo
 
struct  rt_serial_tx_fifo
 
struct  rt_serial_device
 
struct  rt_uart_ops
 

宏定义

#define BAUD_RATE_2400   2400
 
#define BAUD_RATE_4800   4800
 
#define BAUD_RATE_9600   9600
 
#define BAUD_RATE_19200   19200
 
#define BAUD_RATE_38400   38400
 
#define BAUD_RATE_57600   57600
 
#define BAUD_RATE_115200   115200
 
#define BAUD_RATE_230400   230400
 
#define BAUD_RATE_460800   460800
 
#define BAUD_RATE_500000   500000
 
#define BAUD_RATE_921600   921600
 
#define BAUD_RATE_2000000   2000000
 
#define BAUD_RATE_2500000   2500000
 
#define BAUD_RATE_3000000   3000000
 
#define DATA_BITS_5   5
 
#define DATA_BITS_6   6
 
#define DATA_BITS_7   7
 
#define DATA_BITS_8   8
 
#define DATA_BITS_9   9
 
#define STOP_BITS_1   0
 
#define STOP_BITS_2   1
 
#define STOP_BITS_3   2
 
#define STOP_BITS_4   3
 
#define PARITY_NONE   0
 
#define PARITY_ODD   1
 
#define PARITY_EVEN   2
 
#define BIT_ORDER_LSB   0
 
#define BIT_ORDER_MSB   1
 
#define NRZ_NORMAL   0 /* Non Return to Zero : normal mode */
 
#define NRZ_INVERTED   1 /* Non Return to Zero : inverted mode */
 
#define RT_DEVICE_FLAG_RX_BLOCKING   0x1000
 
#define RT_DEVICE_FLAG_RX_NON_BLOCKING   0x2000
 
#define RT_DEVICE_FLAG_TX_BLOCKING   0x4000
 
#define RT_DEVICE_FLAG_TX_NON_BLOCKING   0x8000
 
#define RT_SERIAL_RX_BLOCKING   RT_DEVICE_FLAG_RX_BLOCKING
 
#define RT_SERIAL_RX_NON_BLOCKING   RT_DEVICE_FLAG_RX_NON_BLOCKING
 
#define RT_SERIAL_TX_BLOCKING   RT_DEVICE_FLAG_TX_BLOCKING
 
#define RT_SERIAL_TX_NON_BLOCKING   RT_DEVICE_FLAG_TX_NON_BLOCKING
 
#define RT_DEVICE_CHECK_OPTMODE   0x20
 
#define RT_SERIAL_EVENT_RX_IND   0x01 /* Rx indication */
 
#define RT_SERIAL_EVENT_TX_DONE   0x02 /* Tx complete */
 
#define RT_SERIAL_EVENT_RX_DMADONE   0x03 /* Rx DMA transfer done */
 
#define RT_SERIAL_EVENT_TX_DMADONE   0x04 /* Tx DMA transfer done */
 
#define RT_SERIAL_EVENT_RX_TIMEOUT   0x05 /* Rx timeout */
 
#define RT_SERIAL_ERR_OVERRUN   0x01
 
#define RT_SERIAL_ERR_FRAMING   0x02
 
#define RT_SERIAL_ERR_PARITY   0x03
 
#define RT_SERIAL_TX_DATAQUEUE_SIZE   2048
 
#define RT_SERIAL_TX_DATAQUEUE_LWM   30
 
#define RT_SERIAL_RX_MINBUFSZ   64
 
#define RT_SERIAL_TX_MINBUFSZ   64
 
#define RT_SERIAL_TX_BLOCKING_BUFFER   1
 
#define RT_SERIAL_TX_BLOCKING_NO_BUFFER   0
 
#define RT_SERIAL_FLOWCONTROL_CTSRTS   1
 
#define RT_SERIAL_FLOWCONTROL_NONE   0
 
#define RT_SERIAL_CONFIG_DEFAULT
 

类型定义

typedef void(* rt_hw_serial_rxind_hookproto_t) (rt_device_t dev, rt_size_t size)
 

函数

 RT_OBJECT_HOOKLIST_DECLARE (rt_hw_serial_rxind_hookproto_t, rt_hw_serial_rxind)
 
void rt_hw_serial_isr (struct rt_serial_device *serial, int event)
 
rt_err_t rt_hw_serial_register (struct rt_serial_device *serial, const char *name, rt_uint32_t flag, void *data)
 
rt_err_t rt_hw_serial_register_tty (struct rt_serial_device *serial)
 

详细描述

Serial v2 driver api

Example

#include <rtthread.h>
#include <rtdevice.h>
#define SAMPLE_UART_NAME "uart1"
struct rx_msg
{
rt_size_t size;
};
static rt_device_t serial;
static struct rt_messagequeue rx_mq;
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
struct rx_msg msg;
rt_err_t result;
msg.dev = dev;
msg.size = size;
result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
if (result == -RT_EFULL)
{
rt_kprintf("message queue full!\n");
}
return result;
}
static void serial_thread_entry(void *parameter)
{
struct rx_msg msg;
rt_err_t result;
rt_uint32_t rx_length;
static char rx_buffer[BSP_UART1_RX_BUFSIZE + 1];
while (1)
{
rt_memset(&msg, 0, sizeof(msg));
result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
if (result > 0)
{
rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
rx_buffer[rx_length] = '\0';
rt_device_write(serial, 0, rx_buffer, rx_length);
rt_kprintf("%s\n",rx_buffer);
}
}
}
static int uart_dma_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
static char msg_pool[256];
char str[] = "hello RT-Thread!\r\n";
if (argc == 2)
{
rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
}
serial = rt_device_find(uart_name);
if (!serial)
{
rt_kprintf("find %s failed!\n", uart_name);
return RT_ERROR;
}
rt_mq_init(&rx_mq, "rx_mq",
msg_pool,
sizeof(struct rx_msg),
sizeof(msg_pool),
rt_device_set_rx_indicate(serial, uart_input);
rt_device_write(serial, 0, str, (sizeof(str) - 1));
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
if (thread != RT_NULL)
{
}
else
{
ret = RT_ERROR;
}
return ret;
}
MSH_CMD_EXPORT(uart_dma_sample, uart device dma sample);
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_ssize_t rt_device_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
rt_ssize_t rt_device_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
rt_device_t rt_device_find(const char *name)
struct rt_device * rt_device_t
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
#define RT_IPC_FLAG_FIFO
#define RT_WAITING_FOREVER
#define rt_kprintf(...)
#define RT_DEVICE_FLAG_TX_BLOCKING
#define RT_DEVICE_FLAG_RX_NON_BLOCKING
rt_err_t rt_thread_startup(rt_thread_t thread)
This function will start a thread and put it to system ready queue.
rt_thread_t rt_thread_create(const char *name, void(*entry)(void *parameter), void *parameter, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick)
This function will create a thread object and allocate thread object memory. and stack.
struct rt_thread * rt_thread_t
#define MSH_CMD_EXPORT(...)
rt_err_t rt_mq_init(rt_mq_t mq, const char *name, void *msgpool, rt_size_t msg_size, rt_size_t pool_size, rt_uint8_t flag)
Initialize a static messagequeue object.
定义 ipc.c:3092
rt_err_t rt_mq_send(rt_mq_t mq, const void *buffer, rt_size_t size)
This function will send a message to the messagequeue object. If there is a thread suspended on the m...
定义 ipc.c:3616
rt_ssize_t rt_mq_recv(rt_mq_t mq, void *buffer, rt_size_t size, rt_int32_t timeout)
定义 ipc.c:3920
rt_base_t rt_err_t
rt_ubase_t rt_size_t
unsigned int rt_uint32_t
#define RT_NULL

宏定义说明

◆ BAUD_RATE_2400

#define BAUD_RATE_2400   2400

在文件 dev_serial_v2.h132 行定义.

◆ BAUD_RATE_4800

#define BAUD_RATE_4800   4800

在文件 dev_serial_v2.h133 行定义.

◆ BAUD_RATE_9600

#define BAUD_RATE_9600   9600

在文件 dev_serial_v2.h134 行定义.

◆ BAUD_RATE_19200

#define BAUD_RATE_19200   19200

在文件 dev_serial_v2.h135 行定义.

◆ BAUD_RATE_38400

#define BAUD_RATE_38400   38400

在文件 dev_serial_v2.h136 行定义.

◆ BAUD_RATE_57600

#define BAUD_RATE_57600   57600

在文件 dev_serial_v2.h137 行定义.

◆ BAUD_RATE_115200

#define BAUD_RATE_115200   115200

在文件 dev_serial_v2.h138 行定义.

◆ BAUD_RATE_230400

#define BAUD_RATE_230400   230400

在文件 dev_serial_v2.h139 行定义.

◆ BAUD_RATE_460800

#define BAUD_RATE_460800   460800

在文件 dev_serial_v2.h140 行定义.

◆ BAUD_RATE_500000

#define BAUD_RATE_500000   500000

在文件 dev_serial_v2.h141 行定义.

◆ BAUD_RATE_921600

#define BAUD_RATE_921600   921600

在文件 dev_serial_v2.h142 行定义.

◆ BAUD_RATE_2000000

#define BAUD_RATE_2000000   2000000

在文件 dev_serial_v2.h143 行定义.

◆ BAUD_RATE_2500000

#define BAUD_RATE_2500000   2500000

在文件 dev_serial_v2.h144 行定义.

◆ BAUD_RATE_3000000

#define BAUD_RATE_3000000   3000000

在文件 dev_serial_v2.h145 行定义.

◆ DATA_BITS_5

#define DATA_BITS_5   5

在文件 dev_serial_v2.h147 行定义.

◆ DATA_BITS_6

#define DATA_BITS_6   6

在文件 dev_serial_v2.h148 行定义.

◆ DATA_BITS_7

#define DATA_BITS_7   7

在文件 dev_serial_v2.h149 行定义.

◆ DATA_BITS_8

#define DATA_BITS_8   8

在文件 dev_serial_v2.h150 行定义.

◆ DATA_BITS_9

#define DATA_BITS_9   9

在文件 dev_serial_v2.h151 行定义.

◆ STOP_BITS_1

#define STOP_BITS_1   0

在文件 dev_serial_v2.h153 行定义.

◆ STOP_BITS_2

#define STOP_BITS_2   1

在文件 dev_serial_v2.h154 行定义.

◆ STOP_BITS_3

#define STOP_BITS_3   2

在文件 dev_serial_v2.h155 行定义.

◆ STOP_BITS_4

#define STOP_BITS_4   3

在文件 dev_serial_v2.h156 行定义.

◆ PARITY_NONE

#define PARITY_NONE   0

在文件 dev_serial_v2.h161 行定义.

◆ PARITY_ODD

#define PARITY_ODD   1

在文件 dev_serial_v2.h162 行定义.

◆ PARITY_EVEN

#define PARITY_EVEN   2

在文件 dev_serial_v2.h163 行定义.

◆ BIT_ORDER_LSB

#define BIT_ORDER_LSB   0

在文件 dev_serial_v2.h166 行定义.

◆ BIT_ORDER_MSB

#define BIT_ORDER_MSB   1

在文件 dev_serial_v2.h167 行定义.

◆ NRZ_NORMAL

#define NRZ_NORMAL   0 /* Non Return to Zero : normal mode */

在文件 dev_serial_v2.h169 行定义.

◆ NRZ_INVERTED

#define NRZ_INVERTED   1 /* Non Return to Zero : inverted mode */

在文件 dev_serial_v2.h170 行定义.

◆ RT_DEVICE_FLAG_RX_BLOCKING

#define RT_DEVICE_FLAG_RX_BLOCKING   0x1000

在文件 dev_serial_v2.h172 行定义.

◆ RT_DEVICE_FLAG_RX_NON_BLOCKING

#define RT_DEVICE_FLAG_RX_NON_BLOCKING   0x2000

在文件 dev_serial_v2.h173 行定义.

◆ RT_DEVICE_FLAG_TX_BLOCKING

#define RT_DEVICE_FLAG_TX_BLOCKING   0x4000

在文件 dev_serial_v2.h175 行定义.

◆ RT_DEVICE_FLAG_TX_NON_BLOCKING

#define RT_DEVICE_FLAG_TX_NON_BLOCKING   0x8000

在文件 dev_serial_v2.h176 行定义.

◆ RT_SERIAL_RX_BLOCKING

#define RT_SERIAL_RX_BLOCKING   RT_DEVICE_FLAG_RX_BLOCKING

在文件 dev_serial_v2.h178 行定义.

◆ RT_SERIAL_RX_NON_BLOCKING

#define RT_SERIAL_RX_NON_BLOCKING   RT_DEVICE_FLAG_RX_NON_BLOCKING

在文件 dev_serial_v2.h179 行定义.

◆ RT_SERIAL_TX_BLOCKING

#define RT_SERIAL_TX_BLOCKING   RT_DEVICE_FLAG_TX_BLOCKING

在文件 dev_serial_v2.h180 行定义.

◆ RT_SERIAL_TX_NON_BLOCKING

#define RT_SERIAL_TX_NON_BLOCKING   RT_DEVICE_FLAG_TX_NON_BLOCKING

在文件 dev_serial_v2.h181 行定义.

◆ RT_DEVICE_CHECK_OPTMODE

#define RT_DEVICE_CHECK_OPTMODE   0x20

在文件 dev_serial_v2.h183 行定义.

◆ RT_SERIAL_EVENT_RX_IND

#define RT_SERIAL_EVENT_RX_IND   0x01 /* Rx indication */

在文件 dev_serial_v2.h185 行定义.

◆ RT_SERIAL_EVENT_TX_DONE

#define RT_SERIAL_EVENT_TX_DONE   0x02 /* Tx complete */

在文件 dev_serial_v2.h186 行定义.

◆ RT_SERIAL_EVENT_RX_DMADONE

#define RT_SERIAL_EVENT_RX_DMADONE   0x03 /* Rx DMA transfer done */

在文件 dev_serial_v2.h187 行定义.

◆ RT_SERIAL_EVENT_TX_DMADONE

#define RT_SERIAL_EVENT_TX_DMADONE   0x04 /* Tx DMA transfer done */

在文件 dev_serial_v2.h188 行定义.

◆ RT_SERIAL_EVENT_RX_TIMEOUT

#define RT_SERIAL_EVENT_RX_TIMEOUT   0x05 /* Rx timeout */

在文件 dev_serial_v2.h189 行定义.

◆ RT_SERIAL_ERR_OVERRUN

#define RT_SERIAL_ERR_OVERRUN   0x01

在文件 dev_serial_v2.h191 行定义.

◆ RT_SERIAL_ERR_FRAMING

#define RT_SERIAL_ERR_FRAMING   0x02

在文件 dev_serial_v2.h192 行定义.

◆ RT_SERIAL_ERR_PARITY

#define RT_SERIAL_ERR_PARITY   0x03

在文件 dev_serial_v2.h193 行定义.

◆ RT_SERIAL_TX_DATAQUEUE_SIZE

#define RT_SERIAL_TX_DATAQUEUE_SIZE   2048

在文件 dev_serial_v2.h195 行定义.

◆ RT_SERIAL_TX_DATAQUEUE_LWM

#define RT_SERIAL_TX_DATAQUEUE_LWM   30

在文件 dev_serial_v2.h196 行定义.

◆ RT_SERIAL_RX_MINBUFSZ

#define RT_SERIAL_RX_MINBUFSZ   64

在文件 dev_serial_v2.h198 行定义.

◆ RT_SERIAL_TX_MINBUFSZ

#define RT_SERIAL_TX_MINBUFSZ   64

在文件 dev_serial_v2.h199 行定义.

◆ RT_SERIAL_TX_BLOCKING_BUFFER

#define RT_SERIAL_TX_BLOCKING_BUFFER   1

在文件 dev_serial_v2.h201 行定义.

◆ RT_SERIAL_TX_BLOCKING_NO_BUFFER

#define RT_SERIAL_TX_BLOCKING_NO_BUFFER   0

在文件 dev_serial_v2.h202 行定义.

◆ RT_SERIAL_FLOWCONTROL_CTSRTS

#define RT_SERIAL_FLOWCONTROL_CTSRTS   1

在文件 dev_serial_v2.h204 行定义.

◆ RT_SERIAL_FLOWCONTROL_NONE

#define RT_SERIAL_FLOWCONTROL_NONE   0

在文件 dev_serial_v2.h205 行定义.

◆ RT_SERIAL_CONFIG_DEFAULT

#define RT_SERIAL_CONFIG_DEFAULT
值:
{ \
BAUD_RATE_115200, /* 115200 bits/s */ \
DATA_BITS_8, /* 8 databits */ \
STOP_BITS_1, /* 1 stopbit */ \
PARITY_NONE, /* No parity */ \
BIT_ORDER_LSB, /* LSB first sent */ \
NRZ_NORMAL, /* Normal mode */ \
RT_SERIAL_RX_MINBUFSZ, /* rxBuf size */ \
RT_SERIAL_TX_MINBUFSZ, /* txBuf size */ \
RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
0 \
}
#define RT_SERIAL_TX_MINBUFSZ
#define RT_SERIAL_RX_MINBUFSZ
#define STOP_BITS_1
#define BAUD_RATE_115200
#define RT_SERIAL_FLOWCONTROL_NONE
#define NRZ_NORMAL
#define PARITY_NONE
#define DATA_BITS_8
#define BIT_ORDER_LSB

在文件 dev_serial_v2.h208 行定义.

208#define RT_SERIAL_CONFIG_DEFAULT \
209{ \
210 BAUD_RATE_115200, /* 115200 bits/s */ \
211 DATA_BITS_8, /* 8 databits */ \
212 STOP_BITS_1, /* 1 stopbit */ \
213 PARITY_NONE, /* No parity */ \
214 BIT_ORDER_LSB, /* LSB first sent */ \
215 NRZ_NORMAL, /* Normal mode */ \
216 RT_SERIAL_RX_MINBUFSZ, /* rxBuf size */ \
217 RT_SERIAL_TX_MINBUFSZ, /* txBuf size */ \
218 RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
219 0 \
220}

类型定义说明

◆ rt_hw_serial_rxind_hookproto_t

typedef void(* rt_hw_serial_rxind_hookproto_t) (rt_device_t dev, rt_size_t size)

Serial receive indicate hook function type

在文件 dev_serial_v2.h226 行定义.

函数说明

◆ RT_OBJECT_HOOKLIST_DECLARE()

RT_OBJECT_HOOKLIST_DECLARE ( rt_hw_serial_rxind_hookproto_t ,
rt_hw_serial_rxind  )

◆ rt_hw_serial_isr()

void rt_hw_serial_isr ( struct rt_serial_device * serial,
int event )

Serial interrupt service routine

参数
serialserial device
eventevent mask

◆ rt_hw_serial_register()

rt_err_t rt_hw_serial_register ( struct rt_serial_device * serial,
const char * name,
rt_uint32_t flag,
void * data )

Register a serial device to device list

参数
serialserial device
namedevice name
flagdevice flag
datadevice private data
返回
rt_err_t error code
注解
This function will register a serial device to system device list, and add a device object to system object list.

◆ rt_hw_serial_register_tty()

rt_err_t rt_hw_serial_register_tty ( struct rt_serial_device * serial)

register a serial device to system device list and add a device object to system object list

参数
serialserial device
返回
rt_err_t error code