Files
luban-lite/bsp/examples/test-uart/test_uart.c
刘可亮 564e22b32f v0.7.5
2023-08-28 09:48:01 +08:00

91 lines
2.1 KiB
C

/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-08-15 misonyo first implementation.
* 2023-05-25 geo.dong ArtInChip
*/
#include <rtthread.h>
#include <aic_core.h>
#define SAMPLE_UART_NAME "uart4"
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;
int ret = 0;
while (1)
{
ret = rt_device_read(serial, -1, &ch, 1);
if (ret == 1) {
printf("--ch %c \n", ch);
// ch = ch + 1;
} else if (ret == 0) {
printf("--ret == 0, ch [%c] \n", ch);
rt_device_close(serial);
rt_sem_detach(&rx_sem);
return;
} else {
printf("--RT_WAITING_FOREVER \n");
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
aicos_msleep(1);
}
}
}
static int test_uart(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
char str[] = "ArtInChip Uart Test!";
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_open(serial, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
rt_device_set_rx_indicate(serial, uart_input);
rt_device_write(serial, 0, str, (sizeof(str) - 1));
// NOTE: thread stack-size at least for 1024*2 Bytes !!!
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024*2, 25, 10);
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
ret = RT_ERROR;
}
return ret;
}
MSH_CMD_EXPORT(test_uart, ArtInChip Uart Test);