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

结构体

struct  serial_configure
 
struct  rt_serial_rx_fifo
 
struct  rt_serial_tx_fifo
 
struct  rt_serial_rx_dma
 
struct  rt_serial_tx_dma
 
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_576000   576000
 
#define BAUD_RATE_921600   921600
 
#define BAUD_RATE_1000000   1000000
 
#define BAUD_RATE_1152000   1152000
 
#define BAUD_RATE_1500000   1500000
 
#define BAUD_RATE_2000000   2000000
 
#define BAUD_RATE_2500000   2500000
 
#define BAUD_RATE_3000000   3000000
 
#define BAUD_RATE_3500000   3500000
 
#define BAUD_RATE_4000000   4000000
 
#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_SERIAL_RB_BUFSZ   64
 
#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_DMA_RX   0x01
 
#define RT_SERIAL_DMA_TX   0x02
 
#define RT_SERIAL_RX_INT   0x01
 
#define RT_SERIAL_TX_INT   0x02
 
#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_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)
 
typedef struct rt_serial_device rt_serial_t
 

函数

 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 driver api

Example

#include <rtthread.h>
#define SAMPLE_UART_NAME "uart2"
static struct rt_semaphore rx_sem;
static rt_device_t serial;
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&rx_sem);
return RT_EOK;
}
static void serial_thread_entry(void *parameter)
{
char ch;
while (1)
{
while (rt_device_read(serial, -1, &ch, 1) != 1)
{
}
ch = ch + 1;
rt_device_write(serial, 0, &ch, 1);
}
}
static int uart_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
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_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
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_sample, uart device 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))
#define RT_DEVICE_FLAG_INT_RX
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(...)
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_sem_init(rt_sem_t sem, const char *name, rt_uint32_t value, rt_uint8_t flag)
This function will initialize a static semaphore object.
定义 ipc.c:376
rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
定义 ipc.c:644
rt_err_t rt_sem_release(rt_sem_t sem)
This function will release a semaphore. If there is thread suspended on the semaphore,...
定义 ipc.c:695
rt_base_t rt_err_t
rt_ubase_t rt_size_t
#define RT_NULL

宏定义说明

◆ BAUD_RATE_2400

#define BAUD_RATE_2400   2400

在文件 dev_serial.h117 行定义.

◆ BAUD_RATE_4800

#define BAUD_RATE_4800   4800

在文件 dev_serial.h118 行定义.

◆ BAUD_RATE_9600

#define BAUD_RATE_9600   9600

在文件 dev_serial.h119 行定义.

◆ BAUD_RATE_19200

#define BAUD_RATE_19200   19200

在文件 dev_serial.h120 行定义.

◆ BAUD_RATE_38400

#define BAUD_RATE_38400   38400

在文件 dev_serial.h121 行定义.

◆ BAUD_RATE_57600

#define BAUD_RATE_57600   57600

在文件 dev_serial.h122 行定义.

◆ BAUD_RATE_115200

#define BAUD_RATE_115200   115200

在文件 dev_serial.h123 行定义.

◆ BAUD_RATE_230400

#define BAUD_RATE_230400   230400

在文件 dev_serial.h124 行定义.

◆ BAUD_RATE_460800

#define BAUD_RATE_460800   460800

在文件 dev_serial.h125 行定义.

◆ BAUD_RATE_500000

#define BAUD_RATE_500000   500000

在文件 dev_serial.h126 行定义.

◆ BAUD_RATE_576000

#define BAUD_RATE_576000   576000

在文件 dev_serial.h127 行定义.

◆ BAUD_RATE_921600

#define BAUD_RATE_921600   921600

在文件 dev_serial.h128 行定义.

◆ BAUD_RATE_1000000

#define BAUD_RATE_1000000   1000000

在文件 dev_serial.h129 行定义.

◆ BAUD_RATE_1152000

#define BAUD_RATE_1152000   1152000

在文件 dev_serial.h130 行定义.

◆ BAUD_RATE_1500000

#define BAUD_RATE_1500000   1500000

在文件 dev_serial.h131 行定义.

◆ BAUD_RATE_2000000

#define BAUD_RATE_2000000   2000000

在文件 dev_serial.h132 行定义.

◆ BAUD_RATE_2500000

#define BAUD_RATE_2500000   2500000

在文件 dev_serial.h133 行定义.

◆ BAUD_RATE_3000000

#define BAUD_RATE_3000000   3000000

在文件 dev_serial.h134 行定义.

◆ BAUD_RATE_3500000

#define BAUD_RATE_3500000   3500000

在文件 dev_serial.h135 行定义.

◆ BAUD_RATE_4000000

#define BAUD_RATE_4000000   4000000

在文件 dev_serial.h136 行定义.

◆ DATA_BITS_5

#define DATA_BITS_5   5

在文件 dev_serial.h138 行定义.

◆ DATA_BITS_6

#define DATA_BITS_6   6

在文件 dev_serial.h139 行定义.

◆ DATA_BITS_7

#define DATA_BITS_7   7

在文件 dev_serial.h140 行定义.

◆ DATA_BITS_8

#define DATA_BITS_8   8

在文件 dev_serial.h141 行定义.

◆ DATA_BITS_9

#define DATA_BITS_9   9

在文件 dev_serial.h142 行定义.

◆ STOP_BITS_1

#define STOP_BITS_1   0

在文件 dev_serial.h144 行定义.

◆ STOP_BITS_2

#define STOP_BITS_2   1

在文件 dev_serial.h145 行定义.

◆ STOP_BITS_3

#define STOP_BITS_3   2

在文件 dev_serial.h146 行定义.

◆ STOP_BITS_4

#define STOP_BITS_4   3

在文件 dev_serial.h147 行定义.

◆ PARITY_NONE

#define PARITY_NONE   0

在文件 dev_serial.h152 行定义.

◆ PARITY_ODD

#define PARITY_ODD   1

在文件 dev_serial.h153 行定义.

◆ PARITY_EVEN

#define PARITY_EVEN   2

在文件 dev_serial.h154 行定义.

◆ BIT_ORDER_LSB

#define BIT_ORDER_LSB   0

在文件 dev_serial.h157 行定义.

◆ BIT_ORDER_MSB

#define BIT_ORDER_MSB   1

在文件 dev_serial.h158 行定义.

◆ NRZ_NORMAL

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

在文件 dev_serial.h160 行定义.

◆ NRZ_INVERTED

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

在文件 dev_serial.h161 行定义.

◆ RT_SERIAL_RB_BUFSZ

#define RT_SERIAL_RB_BUFSZ   64

在文件 dev_serial.h164 行定义.

◆ RT_SERIAL_EVENT_RX_IND

#define RT_SERIAL_EVENT_RX_IND   0x01 /* Rx indication */

在文件 dev_serial.h167 行定义.

◆ RT_SERIAL_EVENT_TX_DONE

#define RT_SERIAL_EVENT_TX_DONE   0x02 /* Tx complete */

在文件 dev_serial.h168 行定义.

◆ RT_SERIAL_EVENT_RX_DMADONE

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

在文件 dev_serial.h169 行定义.

◆ RT_SERIAL_EVENT_TX_DMADONE

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

在文件 dev_serial.h170 行定义.

◆ RT_SERIAL_EVENT_RX_TIMEOUT

#define RT_SERIAL_EVENT_RX_TIMEOUT   0x05 /* Rx timeout */

在文件 dev_serial.h171 行定义.

◆ RT_SERIAL_DMA_RX

#define RT_SERIAL_DMA_RX   0x01

在文件 dev_serial.h173 行定义.

◆ RT_SERIAL_DMA_TX

#define RT_SERIAL_DMA_TX   0x02

在文件 dev_serial.h174 行定义.

◆ RT_SERIAL_RX_INT

#define RT_SERIAL_RX_INT   0x01

在文件 dev_serial.h176 行定义.

◆ RT_SERIAL_TX_INT

#define RT_SERIAL_TX_INT   0x02

在文件 dev_serial.h177 行定义.

◆ RT_SERIAL_ERR_OVERRUN

#define RT_SERIAL_ERR_OVERRUN   0x01

在文件 dev_serial.h179 行定义.

◆ RT_SERIAL_ERR_FRAMING

#define RT_SERIAL_ERR_FRAMING   0x02

在文件 dev_serial.h180 行定义.

◆ RT_SERIAL_ERR_PARITY

#define RT_SERIAL_ERR_PARITY   0x03

在文件 dev_serial.h181 行定义.

◆ RT_SERIAL_TX_DATAQUEUE_SIZE

#define RT_SERIAL_TX_DATAQUEUE_SIZE   2048

在文件 dev_serial.h183 行定义.

◆ RT_SERIAL_TX_DATAQUEUE_LWM

#define RT_SERIAL_TX_DATAQUEUE_LWM   30

在文件 dev_serial.h184 行定义.

◆ RT_SERIAL_FLOWCONTROL_CTSRTS

#define RT_SERIAL_FLOWCONTROL_CTSRTS   1

在文件 dev_serial.h186 行定义.

◆ RT_SERIAL_FLOWCONTROL_NONE

#define RT_SERIAL_FLOWCONTROL_NONE   0

在文件 dev_serial.h187 行定义.

◆ 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_RB_BUFSZ, /* Buffer size */ \
RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
0 \
}
#define STOP_BITS_1
#define BAUD_RATE_115200
#define RT_SERIAL_FLOWCONTROL_NONE
#define NRZ_NORMAL
#define PARITY_NONE
#define RT_SERIAL_RB_BUFSZ
#define DATA_BITS_8
#define BIT_ORDER_LSB

在文件 dev_serial.h190 行定义.

190#define RT_SERIAL_CONFIG_DEFAULT \
191{ \
192 BAUD_RATE_115200, /* 115200 bits/s */ \
193 DATA_BITS_8, /* 8 databits */ \
194 STOP_BITS_1, /* 1 stopbit */ \
195 PARITY_NONE, /* No parity */ \
196 BIT_ORDER_LSB, /* LSB first sent */ \
197 NRZ_NORMAL, /* Normal mode */ \
198 RT_SERIAL_RB_BUFSZ, /* Buffer size */ \
199 RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
200 0 \
201}

类型定义说明

◆ rt_hw_serial_rxind_hookproto_t

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

Sets a hook function when RX indicate is called

在文件 dev_serial.h207 行定义.

◆ rt_serial_t

typedef struct rt_serial_device rt_serial_t

在文件 dev_serial.h272 行定义.

函数说明

◆ 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