RT-Thread RTOS
An open source embedded real-time operating system
+ Collaboration diagram for Serial v2:

Data Structures

struct  rt_serial_rx_fifo
 
struct  rt_serial_tx_fifo
 
struct  rt_serial_device
 
struct  rt_uart_ops
 

Typedefs

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

Functions

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)
 

Detailed Description

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_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
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);
#define RT_IPC_FLAG_FIFO
Definition: rtdef.h:972
#define RT_WAITING_FOREVER
Definition: rtdef.h:980
rt_err_t rt_thread_startup(rt_thread_t thread)
This function will start a thread and put it to system ready queue.
Definition: thread.c:393
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.
Definition: thread.c:523
#define MSH_CMD_EXPORT(...)
Definition: finsh.h:142
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.
Definition: 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...
Definition: ipc.c:3616
Definition: rtdef.h:1370
Definition: rtdef.h:1109
Definition: rtdef.h:852

Typedef Documentation

◆ 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.

Function Documentation

◆ rt_hw_serial_isr()

void rt_hw_serial_isr ( struct rt_serial_device serial,
int  event 
)

Serial interrupt service routine.

Parameters
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.

Parameters
serialserial device
namedevice name
flagdevice flag
datadevice private data
Returns
rt_err_t error code
Note
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

Parameters
serialserial device
Returns
rt_err_t error code