2020-12-10 14:50:56 +08:00
|
|
|
/*
|
2021-03-14 15:33:55 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2020-12-10 14:50:56 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2020-12-10 thread-liu first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <board.h>
|
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
#if defined(BSP_UART3_RX_USING_DMA) || defined(BSP_USING_UART3)
|
2020-12-10 14:50:56 +08:00
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
#include <rtthread.h>
|
2020-12-10 14:50:56 +08:00
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
#define SAMPLE_UART_NAME "uart3" /* serial device name */
|
2020-12-10 14:50:56 +08:00
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
struct rx_msg
|
2020-12-10 14:50:56 +08:00
|
|
|
{
|
2021-01-20 15:22:23 +08:00
|
|
|
rt_device_t dev;
|
|
|
|
rt_size_t size;
|
2020-12-10 14:50:56 +08:00
|
|
|
};
|
2021-01-20 15:22:23 +08:00
|
|
|
static rt_device_t serial;
|
|
|
|
static struct rt_messagequeue rx_mq;
|
2020-12-10 14:50:56 +08:00
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
|
2020-12-10 14:50:56 +08:00
|
|
|
{
|
2021-01-20 15:22:23 +08:00
|
|
|
struct rx_msg msg;
|
|
|
|
rt_err_t result;
|
|
|
|
msg.dev = dev;
|
|
|
|
msg.size = size;
|
2020-12-10 14:50:56 +08:00
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
|
|
|
|
if ( result == -RT_EFULL)
|
2020-12-10 14:50:56 +08:00
|
|
|
{
|
2021-12-30 11:27:06 +08:00
|
|
|
rt_kprintf("message queue full!\n");
|
2020-12-10 14:50:56 +08:00
|
|
|
}
|
2021-01-20 15:22:23 +08:00
|
|
|
return result;
|
2020-12-10 14:50:56 +08:00
|
|
|
}
|
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
static void serial_thread_entry(void *parameter)
|
2020-12-10 14:50:56 +08:00
|
|
|
{
|
2021-01-20 15:22:23 +08:00
|
|
|
struct rx_msg msg;
|
|
|
|
rt_err_t result;
|
|
|
|
rt_uint32_t rx_length;
|
|
|
|
static char rx_buffer[RT_SERIAL_RB_BUFSZ + 1];
|
2020-12-10 14:50:56 +08:00
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
while (1)
|
2020-12-10 14:50:56 +08:00
|
|
|
{
|
2021-01-20 15:22:23 +08:00
|
|
|
rt_memset(&msg, 0, sizeof(msg));
|
|
|
|
result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
|
|
|
|
if (result == RT_EOK)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2020-12-10 14:50:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
static int uart_dma_sample(int argc, char *argv[])
|
2020-12-10 14:50:56 +08:00
|
|
|
{
|
2021-01-20 15:22:23 +08:00
|
|
|
rt_err_t ret = RT_EOK;
|
|
|
|
char uart_name[RT_NAME_MAX];
|
|
|
|
static char msg_pool[256];
|
|
|
|
char str[] = "hello RT-Thread!\r\n";
|
2020-12-10 14:50:56 +08:00
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
|
|
|
|
}
|
2020-12-10 14:50:56 +08:00
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
serial = rt_device_find(uart_name);
|
|
|
|
if (!serial)
|
2020-12-10 14:50:56 +08:00
|
|
|
{
|
2021-01-20 15:22:23 +08:00
|
|
|
rt_kprintf("find %s failed!\n", uart_name);
|
|
|
|
return RT_ERROR;
|
2020-12-10 14:50:56 +08:00
|
|
|
}
|
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
rt_mq_init(&rx_mq, "rx_mq",
|
2021-03-14 15:33:55 +08:00
|
|
|
msg_pool,
|
|
|
|
sizeof(struct rx_msg),
|
|
|
|
sizeof(msg_pool),
|
2021-01-20 15:22:23 +08:00
|
|
|
RT_IPC_FLAG_FIFO);
|
2020-12-10 14:50:56 +08:00
|
|
|
|
2021-01-21 09:46:00 +08:00
|
|
|
ret = rt_device_open(serial, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_DMA_RX);
|
|
|
|
if (ret != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_kprintf("serial device open fail!.\n");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2021-01-21 09:46:00 +08:00
|
|
|
ret = rt_device_set_rx_indicate(serial, uart_input);
|
|
|
|
if (ret != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_kprintf("set rx indicate fail!.\n");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2021-03-14 15:33:55 +08:00
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
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)
|
2020-12-10 14:50:56 +08:00
|
|
|
{
|
2021-01-20 15:22:23 +08:00
|
|
|
rt_thread_startup(thread);
|
2020-12-10 14:50:56 +08:00
|
|
|
}
|
2021-01-20 15:22:23 +08:00
|
|
|
else
|
2020-12-10 14:50:56 +08:00
|
|
|
{
|
2021-01-20 15:22:23 +08:00
|
|
|
ret = RT_ERROR;
|
2020-12-10 14:50:56 +08:00
|
|
|
}
|
2021-01-20 15:22:23 +08:00
|
|
|
|
|
|
|
return ret;
|
2020-12-10 14:50:56 +08:00
|
|
|
}
|
2021-01-20 15:22:23 +08:00
|
|
|
MSH_CMD_EXPORT(uart_dma_sample, uart device dma sample);
|
2020-12-10 14:50:56 +08:00
|
|
|
|
2021-01-20 15:22:23 +08:00
|
|
|
#endif /* BSP_USING_DMA */
|