【国民技术N32G457评测】UART—DMA不定长丢包测试
<p><span style="font-size: 16px;">目的:测试开发板串口在各个波特率的接收与发送稳定性</span></p><p><span style="font-size: 16px;">评测环境:1、开发板:国民技术N32G457XVL-STB V1.1</span></p>
<p><span style="font-size: 16px;"> 2、USB—TTL :CP2102</span></p>
<p><span style="font-size: 16px;"> 3、串口助手:SSCM V5.13.1</span></p>
<p><span style="font-size: 16px;"> 4、开发软件:RT-Thread studio 2.1.5</span></p>
<p><span style="font-size: 16px;">串口接收与发送程序:</span></p>
<pre>
<code>/*
* 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;
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 = '\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;
static char msg_pool;
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;
}
</code></pre>
<p>测试方法为:用串口助手的定时发送64个字符,记录下发送与接收的差值,测试时间为30秒钟左右。</p>
<p>测试结果如下表:</p>
<table align="center" border="1" cellpadding="1" cellspacing="1">
<tbody>
<tr>
<td>波特率</td>
<td>发送间隔时间</td>
<td>发送数据</td>
<td>接收数据</td>
<td>差值</td>
</tr>
<tr>
<td>921600</td>
<td>1ms</td>
<td>123200</td>
<td>123200</td>
<td>0</td>
</tr>
<tr>
<td>115200</td>
<td>1ms</td>
<td>122624</td>
<td>122624</td>
<td>0</td>
</tr>
<tr>
<td>9600</td>
<td>1ms</td>
<td>28864</td>
<td>24128</td>
<td>4376</td>
</tr>
<tr>
<td>9600</td>
<td>50ms</td>
<td>30848</td>
<td>25792</td>
<td>5056</td>
</tr>
<tr>
<td>9600</td>
<td>100ms</td>
<td>17216</td>
<td>17216</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>在高波特率的情况下,串口处理数据及时未发生丢包现象,9600的波特率因为发送是阻塞发送所以处理不过来而引起的丢包。从测试结果来看这个芯片的串口处理数据还是可以称赞的,特别是DMA的接收处理能力。</p>
<p>921600:</p>
<p> 115200</p>
<p> 9600</p>
<p> 9600间隔50ms:</p>
<p> 9600 间隔100ms</p>
<p> </p>
页:
[1]