Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[drivers][serial_v2]允许阻塞接收超过rx缓冲区大小的数据、增加超时时间、flush、获取缓冲区数据长度命令、数据溢出逻… #9389

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

Ryan-CW-Code
Copy link
Contributor

…辑修复、稳定性细节优化、添加更多serial_v2测试用例

当前只在STM32平台测试过,别的平台只是将rt_ringbuffer_putchar替换为rt_ringbuffer_putchar_force理论上不会出问题,可以再检查一下

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

优化serial_v2驱动

你的解决方案是什么 (what is your solution)

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
    STM32F407-lckfb-skystar进行utest、echo等测试
    STM32F401RCT6进行at_socket测试
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification

@CLAassistant
Copy link

CLAassistant commented Sep 4, 2024

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added BSP: STM32 BSP related with ST/STM32 BSP BSP: Renesas BSP related with Renesas BSP: GD32 BSP related with GD32 testcase BSP: AT32 BSP related with AT32 Component labels Sep 4, 2024
@Rbb666
Copy link
Member

Rbb666 commented Sep 4, 2024

@Ryan-CW-Code 感谢PR,但CI的代码格式化检查没有通过,麻烦使用工具:https://github.com/mysterywolf/formatting 格式化下再提交下~

@Ryan-CW-Code
Copy link
Contributor Author

Ryan-CW-Code commented Sep 4, 2024

utest和at_socket都使用dma和int方式分别进行测试。
echo测试代码如下,之前跑了两天数据没有丢包

大家可以再测试测试,看看有什么问题是没发现的

#include <board.h>
#include <rtthread.h>
#include <rtdevice.h>

#define echo_test_buffer_size (1024)

// 测试环境
// 串口2、3开启dma, 5、6不开启
// 缓冲区大小都为 128 字节

// echo test
// 3tx - 6rx
// 3rx - 6tx

// self test
// 2tx - 2rx
// 5tx - 5rx

static rt_device_t u2serial;
static rt_device_t u3serial;
static rt_device_t u5serial;
static rt_device_t u6serial;

static rt_uint32_t u2rx_length = 0;
static rt_uint32_t u2tx_length = 0;

static rt_uint32_t u3rx_length = 0;
static rt_uint32_t u3tx_length = 0;

static rt_uint32_t u5rx_length = 0;
static rt_uint32_t u5tx_length = 0;

static rt_uint32_t u6rx_length = 0;
static rt_uint32_t u6tx_length = 0;

static void echo_test_u3_thread_entry(void *parameter)
{
    char *uart_name = "uart3";

    rt_err_t result;
    static char rx_buffer[echo_test_buffer_size + 1];

    char str[] = "hello RT-Thread!";

    /* 查找串口设备 */
    u3serial = rt_device_find(uart_name);
    if (!u3serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return;
    }

    rt_device_open(u3serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);

    rt_ssize_t buf_datalen = 0;
    while (1)
    {
        rt_device_control(u3serial, RT_SERIAL_CTRL_GET_UNREAD_BYTES_COUNT, (void *)&buf_datalen);
        int32_t recbLen = rt_device_read(u3serial, 0, rx_buffer, buf_datalen > 0 ? buf_datalen : 1);
        if (recbLen > 0)
        {
            u3rx_length += recbLen;

            u3tx_length += rt_device_write(u3serial, 0, rx_buffer, recbLen);
        }
    }
}

static void echo_test_u6_thread_entry(void *parameter)
{
    static char rx_buffer[echo_test_buffer_size + 1];
    rt_ssize_t buf_datalen = 0;
    while (1)
    {
        rt_device_control(u6serial, RT_SERIAL_CTRL_GET_UNREAD_BYTES_COUNT, (void *)&buf_datalen);
        int32_t recbLen = rt_device_read(u6serial, 0, rx_buffer, buf_datalen > 0 ? buf_datalen : 1);
        if (recbLen > 0)
        {
            u6rx_length += recbLen;
        }
    }
}

static void echo_test(void *parameter)
{
    static char tx_buffer[echo_test_buffer_size];

    for (uint32_t i = 0; i < sizeof(tx_buffer); i++)
        tx_buffer[i] = i;

    char *uart_name = "uart6";
    u6serial = rt_device_find(uart_name);
    if (!u6serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return;
    }

    rt_device_open(u6serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);

    rt_thread_startup(rt_thread_create("serial3", echo_test_u3_thread_entry, RT_NULL, 2048, 7, 5));
    rt_thread_startup(rt_thread_create("serial6", echo_test_u6_thread_entry, RT_NULL, 2048, 7, 5));

    uint32_t count = 0;
    while (1)
    {
        // 不定长发送数据
        for (uint32_t i = sizeof(tx_buffer) / 10; i < sizeof(tx_buffer); i++)
        {
            count++;

            u6tx_length += rt_device_write(u6serial, 0, tx_buffer, i);
            rt_thread_mdelay(15);

            if (count % 100 == 0)
            {
                rt_kprintf("echo, uart3: tx: %ld, rx: %ld\r\n", u3tx_length, u3rx_length);
                rt_kprintf("echo, uart6: tx: %ld, rx: %ld\r\n", u6tx_length, u6rx_length);
                if (u3tx_length != u3rx_length || u6tx_length != u6rx_length || u3tx_length != u6tx_length)
                {
                    rt_kprintf("echo test error!!!\r\n");
                    return;
                }
            }
        }
    }
}

static void self_tx_rx_u2_thread_entry(void *parameter)
{
    static char rx_buffer[1024 + 1];

    while (1)
    {
        /* 从串口读取数据 */
        u2rx_length += rt_device_read(u2serial, 0, rx_buffer, 1000);
    }
}

static void self_tx_rx_u5_thread_entry(void *parameter)
{
    static char rx_buffer[1024 + 1];

    while (1)
    {
        /* 从串口读取数据 */
        u5rx_length += rt_device_read(u5serial, 0, rx_buffer, 500);
    }
}

static void self_tx_rx_test(void *parameter)
{
    static char tx_buffer[1024 + 1];
    for (uint32_t i = 0; i < sizeof(tx_buffer); i++)
        tx_buffer[i] = i;

    char *uart_name = "uart2";
    u2serial = rt_device_find(uart_name);
    if (!u2serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return;
    }
    rt_device_open(u2serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);

    char *uart5_name = "uart5";
    u5serial = rt_device_find(uart5_name);
    if (!u5serial)
    {
        rt_kprintf("find %s failed!\n", uart5_name);
        return;
    }
    rt_device_open(u5serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);

    rt_thread_startup(rt_thread_create("serial2", self_tx_rx_u2_thread_entry, RT_NULL, 2048, 6, 5));
    rt_thread_startup(rt_thread_create("serial5", self_tx_rx_u5_thread_entry, RT_NULL, 2048, 6, 5));

    for (uint32_t i = 0;; i++)
    {

        u2tx_length += rt_device_write(u2serial, 0, tx_buffer, 1000);
        u5tx_length += rt_device_write(u5serial, 0, tx_buffer, 500);
        rt_thread_mdelay(10);

        if (i % 100 == 0)
        {
            rt_kprintf("self_test, uart2: tx: %ld, rx: %ld\r\n", u2tx_length, u2rx_length);
            rt_kprintf("self_test, uart5: tx: %ld, rx: %ld\r\n", u5tx_length, u5rx_length);
            if (u2tx_length != u2rx_length || u5tx_length != u5rx_length)
            {
                rt_kprintf("self test error!!!\r\n");
                return;
            }
        }
    }
}

static int uart_test(void)
{
    rt_thread_startup(rt_thread_create("echo_test", echo_test, RT_NULL, 2048, 10, 5));
    rt_thread_startup(rt_thread_create("self_test", self_tx_rx_test, RT_NULL, 2048, 10, 5));

    return 0;
}

MSH_CMD_EXPORT_ALIAS(uart_test, uart_test, );

@Ryan-CW-Code
Copy link
Contributor Author

Ryan-CW-Code commented Sep 10, 2024

at_client我们配合at_socket用了快一个月。
at_server我们没有产品在使用,用命令行简单的测试了一下

@Rbb666
Copy link
Member

Rbb666 commented Sep 10, 2024

utest和at_socket都使用dma和int方式分别进行测试。 echo测试代码如下,之前跑了两天数据没有丢包

大家可以再测试测试,看看有什么问题是没发现的

#include <board.h>
#include <rtthread.h>
#include <rtdevice.h>

#define echo_test_buffer_size (1024)

// 测试环境
// 串口2、3开启dma, 5、6不开启
// 缓冲区大小都为 128 字节

// echo test
// 3tx - 6rx
// 3rx - 6tx

// self test
// 2tx - 2rx
// 5tx - 5rx

static rt_device_t u2serial;
static rt_device_t u3serial;
static rt_device_t u5serial;
static rt_device_t u6serial;

static rt_uint32_t u2rx_length = 0;
static rt_uint32_t u2tx_length = 0;

static rt_uint32_t u3rx_length = 0;
static rt_uint32_t u3tx_length = 0;

static rt_uint32_t u5rx_length = 0;
static rt_uint32_t u5tx_length = 0;

static rt_uint32_t u6rx_length = 0;
static rt_uint32_t u6tx_length = 0;

static void echo_test_u3_thread_entry(void *parameter)
{
    char *uart_name = "uart3";

    rt_err_t result;
    static char rx_buffer[echo_test_buffer_size + 1];

    char str[] = "hello RT-Thread!";

    /* 查找串口设备 */
    u3serial = rt_device_find(uart_name);
    if (!u3serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return;
    }

    rt_device_open(u3serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);

    rt_ssize_t buf_datalen = 0;
    while (1)
    {
        rt_device_control(u3serial, RT_SERIAL_CTRL_GET_RX_DATA_LEN, (void *)&buf_datalen);
        int32_t recbLen = rt_device_read(u3serial, 0, rx_buffer, buf_datalen > 0 ? buf_datalen : 1);
        if (recbLen > 0)
        {
            u3rx_length += recbLen;

            u3tx_length += rt_device_write(u3serial, 0, rx_buffer, recbLen);
        }
    }
}

static void echo_test_u6_thread_entry(void *parameter)
{
    static char rx_buffer[echo_test_buffer_size + 1];
    rt_ssize_t buf_datalen = 0;
    while (1)
    {
        rt_device_control(u6serial, RT_SERIAL_CTRL_GET_RX_DATA_LEN, (void *)&buf_datalen);
        int32_t recbLen = rt_device_read(u6serial, 0, rx_buffer, buf_datalen > 0 ? buf_datalen : 1);
        if (recbLen > 0)
        {
            u6rx_length += recbLen;
        }
    }
}

static void echo_test(void *parameter)
{
    static char tx_buffer[echo_test_buffer_size];

    for (uint32_t i = 0; i < sizeof(tx_buffer); i++)
        tx_buffer[i] = i;

    char *uart_name = "uart6";
    u6serial = rt_device_find(uart_name);
    if (!u6serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return;
    }

    rt_device_open(u6serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);

    rt_thread_startup(rt_thread_create("serial3", echo_test_u3_thread_entry, RT_NULL, 2048, 7, 5));
    rt_thread_startup(rt_thread_create("serial6", echo_test_u6_thread_entry, RT_NULL, 2048, 7, 5));

    uint32_t count = 0;
    while (1)
    {
        // 不定长发送数据
        for (uint32_t i = sizeof(tx_buffer) / 10; i < sizeof(tx_buffer); i++)
        {
            count++;

            u6tx_length += rt_device_write(u6serial, 0, tx_buffer, i);
            rt_thread_mdelay(15);

            if (count % 100 == 0)
            {
                rt_kprintf("echo, uart3: tx: %ld, rx: %ld\r\n", u3tx_length, u3rx_length);
                rt_kprintf("echo, uart6: tx: %ld, rx: %ld\r\n", u6tx_length, u6rx_length);
                if (u3tx_length != u3rx_length || u6tx_length != u6rx_length || u3tx_length != u6tx_length)
                {
                    rt_kprintf("echo test error!!!\r\n");
                    return;
                }
            }
        }
    }
}

static void self_tx_rx_u2_thread_entry(void *parameter)
{
    static char rx_buffer[1024 + 1];

    while (1)
    {
        /* 从串口读取数据 */
        u2rx_length += rt_device_read(u2serial, 0, rx_buffer, 1000);
    }
}

static void self_tx_rx_u5_thread_entry(void *parameter)
{
    static char rx_buffer[1024 + 1];

    while (1)
    {
        /* 从串口读取数据 */
        u5rx_length += rt_device_read(u5serial, 0, rx_buffer, 500);
    }
}

static void self_tx_rx_test(void *parameter)
{
    static char tx_buffer[1024 + 1];
    for (uint32_t i = 0; i < sizeof(tx_buffer); i++)
        tx_buffer[i] = i;

    char *uart_name = "uart2";
    u2serial = rt_device_find(uart_name);
    if (!u2serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return;
    }
    rt_device_open(u2serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);

    char *uart5_name = "uart5";
    u5serial = rt_device_find(uart5_name);
    if (!u5serial)
    {
        rt_kprintf("find %s failed!\n", uart5_name);
        return;
    }
    rt_device_open(u5serial, RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);

    rt_thread_startup(rt_thread_create("serial2", self_tx_rx_u2_thread_entry, RT_NULL, 2048, 6, 5));
    rt_thread_startup(rt_thread_create("serial5", self_tx_rx_u5_thread_entry, RT_NULL, 2048, 6, 5));

    for (uint32_t i = 0;; i++)
    {

        u2tx_length += rt_device_write(u2serial, 0, tx_buffer, 1000);
        u5tx_length += rt_device_write(u5serial, 0, tx_buffer, 500);
        rt_thread_mdelay(10);

        if (i % 100 == 0)
        {
            rt_kprintf("self_test, uart2: tx: %ld, rx: %ld\r\n", u2tx_length, u2rx_length);
            rt_kprintf("self_test, uart5: tx: %ld, rx: %ld\r\n", u5tx_length, u5rx_length);
            if (u2tx_length != u2rx_length || u5tx_length != u5rx_length)
            {
                rt_kprintf("self test error!!!\r\n");
                return;
            }
        }
    }
}

static int uart_test(void)
{
    rt_thread_startup(rt_thread_create("echo_test", echo_test, RT_NULL, 2048, 10, 5));
    rt_thread_startup(rt_thread_create("self_test", self_tx_rx_test, RT_NULL, 2048, 10, 5));

    return 0;
}

MSH_CMD_EXPORT_ALIAS(uart_test, uart_test, );

已在瑞萨RA8开发板测试,没问题


前四个测试用例的测试思路:
默认测试串口为`**uart2**`请根据需要修改宏定义
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块的格式不正确,不需要加上``

/* Reinitialize */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
config.baud_rate = BAUD_RATE_115200;
config.rx_bufsz = BSP_UART2_RX_BUFSIZE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

测试用例里面直接这么定义BSP_UART2_RX_BUFSIZE的宏是不是不太好,搞成menuconfig里面配置的宏吧,要不然板子没有串口2,还要改源码

result = rt_device_read(client->device, 0, ch, 1);
if(result <= 0)
{
result = RT_ERROR;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块是不是应该返回错误了?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

错误码应该返回负值 -RT_ERROR


old_tick = rt_tick_get();
rt_device_control(&serial->parent, RT_SERIAL_CTRL_TX_FLUSH, RT_NULL);
if (rt_tick_get() - old_tick < 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

问下这个<2的标准是什么?我测试在gd32上这个差值是2,st的是5

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
设置超时时间为1tick,让串口阻塞发送1tick,然后调用tx_flush,认为串口发送完所有数据不会低于2tick。
您说的st是5应该不太可能,115200发送1字节大约需要868ns,最低发送256个字节也需要约22ms了。

这部分测试也可以再优化优化,根据字节数需要的时间来判断会更好

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image 设置超时时间为1tick,让串口阻塞发送1tick,然后调用tx_flush,认为串口发送完所有数据不会低于2tick。 您说的st是5应该不太可能,115200发送1字节大约需要868ns,最低发送256个字节也需要约22ms了。

这部分测试也可以再优化优化,根据字节数需要的时间来判断会更好

这个是在stm32f4上的测试数据
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面把tx_bufsz设置成64大小了,中断模式会用到缓冲区发送64字节数据5.5 - 1(等待的1ms)也差不多。
只是gd32为什么是2还要在看看。

我先修改下测试用例

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我买了gd32和at32的开发板,等我测测再。
也顺便完善下posix接口

@Rbb666
Copy link
Member

Rbb666 commented Sep 12, 2024

基于最新提交gd32使能中断后测试结果:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BSP: AT32 BSP related with AT32 BSP: GD32 BSP related with GD32 BSP: Renesas BSP related with Renesas BSP: STM32 BSP related with ST/STM32 BSP Component testcase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants