[serial] add bypass hook to direct processing char when uart irq coming

This commit is contained in:
zhujiale authored and Rbb666 committed 2024-12-17 11:08:14 +08:00
1 parent c1166e0bf1
commit 27cf024585
8 files changed
+503 -7

No files matched your search

@@ -0,0 +1,69 @@
/*
* 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
*/
#ifndef __RTT_BYPASS_H__
#define __RTT_BYPASS_H__
#include <rtthread.h>
#include <rttypes.h>
#include <rtdevice.h>
typedef rt_err_t(*bypass_function_t)(struct rt_serial_device* serial, char buf, void* data);
#define RT_BYPASS_LEVEL_MAX 4
#define RT_BYPASS_LEVEL_1 0
#define RT_BYPASS_LEVEL_2 1
#define RT_BYPASS_LEVEL_3 2
#define RT_BYPASS_LEVEL_4 3
#define RT_BYPASS_MAX_LEVEL 4
/*The protect level can be register but can not be unregister we should use it carefully*/
#define RT_BYPASS_PROTECT_LEVEL_1 10
#define RT_BYPASS_PROTECT_LEVEL_2 11
#define RT_BYPASS_PROTECT_LEVEL_3 12
#define RT_BYPASS_PROTECT_LEVEL_4 13
struct rt_serial_bypass_func {
/*The function pointer of the bypassed data processing*/
bypass_function_t bypass;
/*The smaller the array of levels, the higher the priority of execution*/
rt_uint8_t level;
rt_list_t node;
char name[RT_NAME_MAX];
void* data;
};
struct rt_serial_bypass_head
{
rt_list_t head;
struct rt_spinlock spinlock;
};
struct rt_serial_bypass {
struct rt_work work;
struct rt_spinlock spinlock;
struct rt_workqueue* lower_workq;
struct rt_serial_bypass_head* upper_h;
struct rt_serial_bypass_head* lower_h;
rt_mutex_t mutex;
struct rt_ringbuffer* pipe;
};
int serial_bypass_list(int argc, char** argv);
void rt_bypass_work_straight(struct rt_serial_device* serial);
void rt_bypass_putchar(struct rt_serial_device* serial, rt_uint8_t ch);
rt_size_t rt_bypass_getchar(struct rt_serial_device* serial, rt_uint8_t* ch);
rt_err_t rt_bypass_upper_unregister(struct rt_serial_device* serial, rt_uint8_t level);
rt_err_t rt_bypass_lower_unregister(struct rt_serial_device* serial, rt_uint8_t level);
rt_err_t rt_bypass_upper_register(struct rt_serial_device* serial, const char* name, rt_uint8_t level, bypass_function_t func, void* data);
rt_err_t rt_bypass_lower_register(struct rt_serial_device* serial, const char* name, rt_uint8_t level, bypass_function_t func, void* data);
#endif
@@ -76,7 +76,7 @@
* if (!serial)
* {
* rt_kprintf("find %s failed!\n", uart_name);
* return RT_ERROR;
* return -RT_ERROR;
* }
*
*
@@ -97,7 +97,7 @@
* }
* else
* {
* ret = RT_ERROR;
* ret = -RT_ERROR;
* }
*
* return ret;
@@ -264,7 +264,9 @@ struct rt_serial_device
void *serial_tx;
struct rt_spinlock spinlock;
#ifdef RT_USING_SERIAL_BYPASS
struct rt_serial_bypass* bypass;
#endif
struct rt_device_notify rx_notify;
};
typedef struct rt_serial_device rt_serial_t;
+3
View File
@@ -152,6 +152,9 @@ extern "C" {
#include "drivers/dev_serial_v2.h"
#else
#include "drivers/dev_serial.h"
#ifdef RT_USING_SERIAL_BYPASS
#include "drivers/bypass.h"
#endif /* RT_USING_SERIAL_BYPASS */
#endif
#endif /* RT_USING_SERIAL */
+4 -1
View File
@@ -21,4 +21,7 @@ menuconfig RT_USING_SERIAL
int "Set RX buffer size"
depends on !RT_USING_SERIAL_V2
default 64
endif
config RT_USING_SERIAL_BYPASS
bool "Using serial bypass"
default y
endif
+3
View File
@@ -16,6 +16,9 @@ if GetDepend(['RT_USING_SERIAL_V2']):
else:
src += ['dev_serial.c']
if GetDepend(['RT_USING_SERIAL_BYPASS']):
src += ['bypass.c']
if GetDepend(['RT_USING_DM']):
src += ['serial_dm.c']
File diff suppressed because it is too large. Load diff
+45 -2
View File
@@ -27,6 +27,7 @@
* 2020-12-14 Meco Man implement function of setting window's size(TIOCSWINSZ)
* 2021-08-22 Meco Man implement function of getting window's size(TIOCGWINSZ)
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
* 2024-11-25 zhujiale add bypass mode
*/
#include <rthw.h>
@@ -303,6 +304,23 @@ rt_inline int _serial_int_rx(struct rt_serial_device *serial, rt_uint8_t *data,
rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx;
RT_ASSERT(rx_fifo != RT_NULL);
#ifdef RT_USING_SERIAL_BYPASS
if (serial->bypass)
{
rt_bypass_work_straight(serial);
while (length)
{
rt_uint8_t ch;
if (!rt_bypass_getchar(serial, &ch))
break;
*data = ch & 0xff;
data++; length--;
}
return size - length;
}
#endif
/* read from software FIFO */
while (length)
{
@@ -1410,13 +1428,33 @@ 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 */
level = rt_spin_lock_irqsave(&(serial->spinlock));
#ifdef RT_USING_SERIAL_BYPASS
if (serial->bypass && serial->bypass->upper_h && (serial->bypass->upper_h->head.next != &serial->bypass->upper_h->head))
{
char buf = (char)ch;
int ret;
rt_list_t* node = serial->bypass->upper_h->head.next;
do {
struct rt_serial_bypass_func* bypass_run = rt_container_of(node, struct rt_serial_bypass_func, node);
ret = bypass_run->bypass(serial, buf, bypass_run->data);
if (!ret)
{
skip = RT_TRUE;
break;
}
node = node->next;
} while (node != &serial->bypass->upper_h->head);
}
if (skip)
continue;
#endif
level = rt_spin_lock_irqsave(&(serial->spinlock));
rx_fifo->buffer[rx_fifo->put_index] = ch;
rx_fifo->put_index += 1;
if (rx_fifo->put_index >= serial->config.bufsz) rx_fifo->put_index = 0;
@@ -1435,6 +1473,11 @@ void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
rt_spin_unlock_irqrestore(&(serial->spinlock), level);
}
#ifdef RT_USING_SERIAL_BYPASS
if (serial->bypass && serial->bypass->lower_h)
rt_workqueue_dowork(serial->bypass->lower_workq, &serial->bypass->work);
#endif
/**
* Invoke callback.
* First try notify if any, and if notify is existed, rx_indicate()
+19 -1
View File
@@ -154,10 +154,25 @@ 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;
rt_inline void _setup_serial(struct rt_serial_device *serial, lwp_tty_t tp,
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)
{
#ifndef RT_USING_SERIAL_BYPASS
struct rt_device_notify notify;
softc->backup_notify = serial->rx_notify;
@@ -167,6 +182,9 @@ rt_inline void _setup_serial(struct rt_serial_device *serial, lwp_tty_t tp,
rt_device_init(&serial->parent);
rt_device_control(&serial->parent, RT_DEVICE_CTRL_NOTIFY_SET, &notify);
#else
rt_bypass_lower_register(serial, "tty",RT_BYPASS_PROTECT_LEVEL_1, _serial_ty_bypass,(void *)tp);
#endif
}
rt_inline void _restore_serial(struct rt_serial_device *serial, lwp_tty_t tp,