Skip to content

Commit

Permalink
[serial] add bypass testcase in utest
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuzhuzhus committed Nov 27, 2024
1 parent db8e2e4 commit 754888d
Show file tree
Hide file tree
Showing 10 changed files with 605 additions and 20 deletions.
2 changes: 1 addition & 1 deletion components/drivers/serial/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ menuconfig RT_USING_SERIAL
default 64
config RT_USING_SERIAL_BYPASS
bool "Using serial bypass"
default y
default n
endif
7 changes: 4 additions & 3 deletions components/drivers/serial/dev_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -1428,14 +1428,14 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event)

while (1)
{
rt_bool_t skip = RT_FALSE;
ch = serial->ops->getc(serial);
if (ch == -1) break;

/* disable interrupt */
#ifdef RT_USING_SERIAL_BYPASS
if (serial->bypass && serial->bypass->upper_h && (serial->bypass->upper_h->head.next != &serial->bypass->upper_h->head))
{
rt_bool_t skip = RT_FALSE;
char buf = (char)ch;
int ret;
rt_list_t* node = serial->bypass->upper_h->head.next;
Expand All @@ -1449,10 +1449,11 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
}
node = node->next;
} while (node != &serial->bypass->upper_h->head);

if (skip)
continue;
}

if (skip)
continue;
#endif
level = rt_spin_lock_irqsave(&(serial->spinlock));
rx_fifo->buffer[rx_fifo->put_index] = ch;
Expand Down
34 changes: 18 additions & 16 deletions components/drivers/serial/serial_tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,22 @@ static void _setup_debug_rxind_hook(void)

#endif /* LWP_DEBUG_INIT */

static void _tty_rx_notify(struct rt_device *device)
#ifdef RT_USING_SERIAL_BYPASS
static rt_err_t _serial_ty_bypass(struct rt_serial_device* serial, char ch,void *data)
{
lwp_tty_t tp;
tp = (lwp_tty_t)data;

tty_lock(tp);
ttydisc_rint(tp, ch, 0);
ttydisc_rint_done(tp);
tty_unlock(tp);

return RT_EOK;

}
#else
static void _tty_rx_notify(struct rt_device* device)
{
lwp_tty_t tp;
struct serial_tty_context *softc;
Expand All @@ -127,8 +142,9 @@ static void _tty_rx_notify(struct rt_device *device)
if (_ttyworkq)
rt_workqueue_submit_work(_ttyworkq, &softc->work, 0);
}
#endif

static void _tty_rx_worker(struct rt_work *work, void *data)
static void _tty_rx_worker(struct rt_work* work, void* data)
{
char input;
rt_ssize_t readbytes;
Expand All @@ -154,21 +170,7 @@ static void _tty_rx_worker(struct rt_work *work, void *data)
ttydisc_rint_done(tp);
tty_unlock(tp);
}
#ifdef RT_USING_SERIAL_BYPASS
static rt_err_t _serial_ty_bypass(struct rt_serial_device* serial, char ch,void *data)
{
lwp_tty_t tp;
tp = (lwp_tty_t)data;

tty_lock(tp);
ttydisc_rint(tp, ch, 0);
ttydisc_rint_done(tp);
tty_unlock(tp);

return RT_EOK;

}
#endif
rt_inline void _setup_serial(struct rt_serial_device* serial, lwp_tty_t tp,
struct serial_tty_context *softc)
{
Expand Down
1 change: 1 addition & 0 deletions examples/utest/testcases/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ rsource "utest/Kconfig"
rsource "kernel/Kconfig"
rsource "cpp11/Kconfig"
rsource "drivers/serial_v2/Kconfig"
rsource "drivers/serial_bypass/Kconfig"
rsource "drivers/ipc/Kconfig"
rsource "posix/Kconfig"
rsource "mm/Kconfig"
Expand Down
7 changes: 7 additions & 0 deletions examples/utest/testcases/drivers/serial_bypass/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
menu "Serial-Bypass Testcase"

config UTEST_SERIAL_BYPASS
bool "Serial testcase"
default n

endmenu
11 changes: 11 additions & 0 deletions examples/utest/testcases/drivers/serial_bypass/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Import('rtconfig')
from building import *

cwd = GetCurrentDir()
src = Glob('bypass*.c')

CPPPATH = [cwd]

group = DefineGroup('utestcases', src, depend = ['UTEST_SERIAL_BYPASS'], CPPPATH = CPPPATH)

Return('group')
185 changes: 185 additions & 0 deletions examples/utest/testcases/drivers/serial_bypass/bypass_conflict.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* Copyright (c) 2006-2024 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-11-20 zhujiale the first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "utest.h"

static struct rt_serial_device* _serial0;
static struct rt_spinlock lock;
static int cnt = 0;

#define __REG32(x) (*((volatile unsigned int*)((rt_ubase_t)x)))
#define UART_FR(base) __REG32(base + 0x18)
#define UART_DR(base) __REG32(base + 0x00)
#define UARTFR_TXFF 0x20

static rt_err_t utest_get_c(struct rt_serial_device* serial, char ch, void* data)
{
rt_atomic_add(&cnt, 1);
return RT_EOK;
}

static int utest_getc(struct rt_serial_device* serial)
{
static int num = 0;

rt_spin_lock(&lock);
if (rt_atomic_load(&num) == 10)
{
rt_atomic_flag_clear(&num);
rt_spin_unlock(&lock);
return -1;
}
rt_atomic_add(&num, 1);
rt_spin_unlock(&lock);
return 'a';
}

struct hw_uart_device
{
rt_size_t hw_base;
rt_size_t irqno;
};

static int uart_putc(struct rt_serial_device* serial, char c)
{
struct hw_uart_device* uart;

RT_ASSERT(serial != RT_NULL);
uart = (struct hw_uart_device*)serial->parent.user_data;

while (UART_FR(uart->hw_base) & UARTFR_TXFF);
UART_DR(uart->hw_base) = c;

return 1;
}

static const struct rt_uart_ops _utest_ops =
{
RT_NULL,
RT_NULL,
uart_putc,
utest_getc,
};


static void thread_rx1(void* parameter)
{
for (int i = 0; i < 10; i++)
{
rt_hw_serial_isr(_serial0, RT_SERIAL_EVENT_RX_IND);
}
}

static void thread_rx2(void* parameter)
{
for (int i = 0; i < 10; i++)
{
rt_workqueue_dowork(_serial0->bypass->lower_workq, &_serial0->bypass->work);
}
}

static void thread_high_priority(void* parameter)
{
for (int i = 1; i < 10; i++)
{
rt_bypass_upper_register(_serial0, "test", i, utest_get_c, RT_NULL);
rt_bypass_upper_unregister(_serial0, i);
}
}

static void thread_low_priority(void* parameter)
{
for (int i = 0; i < 20; i++)
{
rt_hw_serial_isr(_serial0, RT_SERIAL_EVENT_RX_IND);
}
}

static void bypass_rx_stress_003(void)
{
const struct rt_uart_ops* tmp = _serial0->ops;

rt_thread_t high = rt_thread_create("high_prio", thread_high_priority, RT_NULL, 2048, 15, 10);
rt_thread_t low = rt_thread_create("low_prio", thread_low_priority, RT_NULL, 2048, 20, 10);

rt_atomic_flag_clear(&cnt);
_serial0->ops = &_utest_ops;
rt_bypass_upper_register(_serial0, "test", 0, utest_get_c, RT_NULL);

rt_thread_startup(high);
rt_thread_startup(low);

rt_thread_mdelay(1000);
_serial0->ops = tmp;
rt_bypass_upper_unregister(_serial0, 0);
uassert_true(rt_atomic_load(&cnt) == 200);
}

static void bypass_rx_stress_002(void)
{
const struct rt_uart_ops* tmp = _serial0->ops;
rt_thread_t rx2 = rt_thread_create("rx2", thread_rx1, RT_NULL, 2048, RT_THREAD_PRIORITY_MAX - 5, 10);
rt_thread_t rx3 = rt_thread_create("rx3", thread_rx2, RT_NULL, 2048, RT_THREAD_PRIORITY_MAX - 5, 10);

rt_atomic_flag_clear(&cnt);
_serial0->ops = &_utest_ops;
rt_bypass_lower_register(_serial0, "utest", 0, utest_get_c, RT_NULL);

rt_thread_startup(rx2);
rt_thread_startup(rx3);

rt_thread_mdelay(1000);

uassert_true(rt_atomic_load(&cnt) == 100);
_serial0->ops = tmp;
rt_bypass_lower_unregister(_serial0, 0);
}

static void bypass_rx_stress_001(void)
{
const struct rt_uart_ops* tmp = _serial0->ops;
rt_thread_t rx1 = rt_thread_create("rx1", thread_rx1, RT_NULL, 2048, RT_THREAD_PRIORITY_MAX - 5, 10);
rt_thread_t rx2 = rt_thread_create("rx1", thread_rx1, RT_NULL, 2048, RT_THREAD_PRIORITY_MAX - 5, 10);

cnt = 0;
_serial0->ops = &_utest_ops;
rt_bypass_upper_register(_serial0, "utest", 0, utest_get_c, RT_NULL);

rt_thread_startup(rx1);
rt_thread_startup(rx2);

rt_thread_mdelay(1000);

uassert_true(rt_atomic_load(&cnt) == 200);
_serial0->ops = tmp;
rt_bypass_upper_unregister(_serial0, 0);
}

static rt_err_t utest_tc_init(void)
{
_serial0 = (struct rt_serial_device*)rt_console_get_device();
rt_spin_lock_init(&lock);
return RT_EOK;
}

static rt_err_t utest_tc_cleanup(void)
{
return RT_EOK;
}

static void _testcase(void)
{
UTEST_UNIT_RUN(bypass_rx_stress_001);
UTEST_UNIT_RUN(bypass_rx_stress_002);
UTEST_UNIT_RUN(bypass_rx_stress_003);
}

UTEST_TC_EXPORT(_testcase, "testcase.bypass.conflict.001", utest_tc_init, utest_tc_cleanup, 10);
Loading

0 comments on commit 754888d

Please sign in to comment.