【国民技术N32G457评测】UART—DMA不定长丢包测试
[复制链接]
目的:测试开发板串口在各个波特率的接收与发送稳定性
评测环境:1、开发板:国民技术N32G457XVL-STB V1.1
2、USB—TTL :CP2102
3、串口助手:SSCM V5.13.1
4、开发软件:RT-Thread studio 2.1.5
串口接收与发送程序:
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-01-23 Administrator the first version
*/
#include <rtthread.h>
#include "uartdma.h"
#include <rtdevice.h>
#define UART2_NAME "uart2" /* 串口设备名称 */
/* 串口接收消息结构体 */
struct rx_msg
{
rt_device_t dev;
rt_size_t size;
};
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
/* 串口设备句柄 */
static rt_device_t serial2;
/* 消息队伍控制块 */
static struct rt_messagequeue rx_mq;
/*接收数据回调函数 */
static rt_err_t uart2_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!\r\n");
}
return result;
}
static void serial2_thread_entry(void *parameter)
{
struct rx_msg msg;
rt_err_t result;
rt_uint32_t rx_length;
static char rx_buffer[RT_SERIAL_RB_BUFSZ +1];
while(1)
{
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(msg.dev, 0, rx_buffer, rx_length);
/*打印数据 */
rt_kprintf("%s\n", rx_buffer);
}
}
}
int uart2_dma_sample(void)
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
static char msg_pool[256];
char str[] = "Hello RT-Thread!\r\n";
config.baud_rate = BAUD_RATE_921600;
rt_strncpy(uart_name, UART2_NAME, RT_NAME_MAX);
/* 查找串口设备 */
serial2 = rt_device_find(uart_name);
if(!serial2)
{
rt_kprintf("Find %s failed!\r\n", uart_name);
return RT_ERROR;
}
/* 初始化消息队列 */
rt_mq_init(&rx_mq,
"rx_mq",
msg_pool,
sizeof(struct rx_msg),
sizeof(msg_pool),
RT_IPC_FLAG_FIFO);
/* 以DMA接收及轮询发送方式打开串口设备 */
rt_device_control(serial2, RT_DEVICE_CTRL_CONFIG, &config);
rt_device_open(serial2, RT_DEVICE_FLAG_DMA_RX);
/* 设置接收回调函数 */
rt_device_set_rx_indicate(serial2, uart2_input);
/* 发送字符串 */
rt_device_write(serial2, 0, str, (sizeof(str) - 1));
/* 创建serial2 线程 */
rt_thread_t thread_uart2 = rt_thread_create("serial2", serial2_thread_entry, RT_NULL, 1024, 25, 10);
if(thread_uart2 != RT_NULL)
{
rt_thread_startup(thread_uart2);
}
else {
ret = RT_ERROR;
}
return ret;
}
测试方法为:用串口助手的定时发送64个字符,记录下发送与接收的差值,测试时间为30秒钟左右。
测试结果如下表:
波特率 |
发送间隔时间 |
发送数据 |
接收数据 |
差值 |
921600 |
1ms |
123200 |
123200 |
0 |
115200 |
1ms |
122624 |
122624 |
0 |
9600 |
1ms |
28864 |
24128 |
4376 |
9600 |
50ms |
30848 |
25792 |
5056 |
9600 |
100ms |
17216 |
17216 |
0 |
在高波特率的情况下,串口处理数据及时未发生丢包现象,9600的波特率因为发送是阻塞发送所以处理不过来而引起的丢包。从测试结果来看这个芯片的串口处理数据还是可以称赞的,特别是DMA的接收处理能力。
921600:
115200
9600
9600间隔50ms:
9600 间隔100ms
|