Serial driver api.
#include <rtthread.h>
#define SAMPLE_UART_NAME "uart2"
static rt_err_t uart_input(
rt_device_t dev, rt_size_t size)
{
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_device_set_rx_indicate(serial, uart_input);
rt_device_write(serial, 0, str, (sizeof(str) - 1));
if (thread != RT_NULL)
{
}
else
{
ret = -RT_ERROR;
}
return ret;
}
#define RT_DEVICE_FLAG_INT_RX
Definition: rtdef.h:1297
#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_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.
Definition: ipc.c:376
rt_err_t rt_sem_release(rt_sem_t sem)
This function will release a semaphore. If there is thread suspended on the semaphore,...
Definition: ipc.c:695