summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorzhujiale <[email protected]>2024-11-20 10:04:53 +0800
committerRbb666 <[email protected]>2024-12-17 11:08:14 +0800
commit27cf024585bee854eb032b4240f1a2390a3c29d7 (patch)
tree14632d9251b8fc7c52e4845fb09ef90a8ad42bf1 /components
parentc1166e0bf16aeda85e5facd01506973afcf6e8c6 (diff)
[serial] add bypass hook to direct processing char when uart irq coming
Diffstat (limited to 'components')
-rw-r--r--components/drivers/include/drivers/bypass.h69
-rw-r--r--components/drivers/include/drivers/dev_serial.h8
-rw-r--r--components/drivers/include/rtdevice.h3
-rw-r--r--components/drivers/serial/Kconfig5
-rw-r--r--components/drivers/serial/SConscript3
-rw-r--r--components/drivers/serial/bypass.c355
-rw-r--r--components/drivers/serial/dev_serial.c47
-rw-r--r--components/drivers/serial/serial_tty.c20
8 files changed, 503 insertions, 7 deletions
diff --git a/components/drivers/include/drivers/bypass.h b/components/drivers/include/drivers/bypass.h
new file mode 100644
index 0000000000..890c8947d1
--- /dev/null
+++ b/components/drivers/include/drivers/bypass.h
@@ -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
diff --git a/components/drivers/include/drivers/dev_serial.h b/components/drivers/include/drivers/dev_serial.h
index df2a62030a..b7f627e8cd 100644
--- a/components/drivers/include/drivers/dev_serial.h
+++ b/components/drivers/include/drivers/dev_serial.h
@@ -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;
diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h
index 09f5e455f7..31ac538945 100644
--- a/components/drivers/include/rtdevice.h
+++ b/components/drivers/include/rtdevice.h
@@ -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 */
diff --git a/components/drivers/serial/Kconfig b/components/drivers/serial/Kconfig
index 00fcfb6461..59eda6232d 100644
--- a/components/drivers/serial/Kconfig
+++ b/components/drivers/serial/Kconfig
@@ -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
diff --git a/components/drivers/serial/SConscript b/components/drivers/serial/SConscript
index ce9d221643..04fa37ed44 100644
--- a/components/drivers/serial/SConscript
+++ b/components/drivers/serial/SConscript
@@ -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']
diff --git a/components/drivers/serial/bypass.c b/components/drivers/serial/bypass.c
new file mode 100644
index 0000000000..04ed71a724
--- /dev/null
+++ b/components/drivers/serial/bypass.c
@@ -0,0 +1,355 @@
+/*
+ * 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<rtdevice.h>
+#define DBG_TAG "UART"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
+static struct rt_serial_bypass_func* rt_bypass_alloc_func(const char* name, rt_uint8_t level, bypass_function_t func, void* data)
+{
+ struct rt_serial_bypass_func* bypass;
+
+ if (!func)
+ return RT_NULL;
+
+ bypass = rt_malloc(sizeof(struct rt_serial_bypass_func));
+ rt_memset(bypass, 0, sizeof(struct rt_serial_bypass_func));
+
+ if (rt_strlen(name) > RT_NAME_MAX - 1)
+ rt_memcpy(bypass->name, name, RT_NAME_MAX);
+ else
+ rt_memcpy(bypass->name, name, rt_strlen(name) + 1);
+
+ bypass->level = level;
+
+ rt_list_init(&bypass->node);
+ bypass->bypass = func;
+ bypass->data = data;
+
+ return bypass;
+}
+
+rt_err_t rt_serial_bypass_init(struct rt_serial_device* serial)
+{
+ serial->bypass = rt_malloc(sizeof(struct rt_serial_bypass));
+ rt_memset(serial->bypass, 0, sizeof(struct rt_serial_bypass));
+ serial->bypass->pipe = rt_ringbuffer_create(serial->config.bufsz);
+ serial->bypass->mutex = rt_mutex_create("serial_bypass", RT_IPC_FLAG_FIFO);
+
+ return RT_EOK;
+}
+
+static rt_err_t rt_bypass_register(struct rt_serial_bypass_head* bypass, const char* name, rt_uint8_t level, bypass_function_t func, void* data)
+{
+ struct rt_serial_bypass_func* pass = RT_NULL;
+ struct rt_list_node* node;
+ int flags;
+
+ RT_DEBUG_NOT_IN_INTERRUPT;
+
+ pass = rt_bypass_alloc_func(name, level, func, data);
+
+ RT_ASSERT(bypass != RT_NULL);
+
+ node = bypass->head.next;
+
+ if (node == &bypass->head)
+ {
+ rt_list_insert_before(&pass->node, node);
+ return RT_EOK;
+ }
+ flags = rt_spin_lock_irqsave(&(bypass->spinlock));
+ do {
+ struct rt_serial_bypass_func* temp_curr;
+ temp_curr = rt_container_of(node, struct rt_serial_bypass_func, node);
+
+ if (level < temp_curr->level)
+ {
+ rt_list_insert_before(node, &pass->node);
+
+ rt_spin_unlock_irqrestore(&(bypass->spinlock), flags);
+ return RT_EOK;
+ }
+ else if (level == temp_curr->level)
+ {
+ rt_spin_unlock_irqrestore(&(bypass->spinlock), flags);
+ LOG_E("Conflict: bypass [%s] level conflicts with [%s] at level [%d]\n", name, temp_curr->name, level);
+ rt_free(pass);
+ return -RT_ERROR;
+ }
+
+ node = node->next;
+
+ } while (node != &bypass->head);
+
+ rt_list_insert_before(&bypass->head, &pass->node);
+
+ rt_spin_unlock_irqrestore(&(bypass->spinlock), flags);
+ return RT_EOK;
+
+}
+
+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)
+{
+ if (!serial->bypass)
+ rt_serial_bypass_init(serial);
+
+ if (!serial->bypass->upper_h)
+ {
+ serial->bypass->upper_h = rt_malloc(sizeof(struct rt_serial_bypass_head));
+ rt_spin_lock_init(&serial->bypass->upper_h->spinlock);
+ rt_list_init(&serial->bypass->upper_h->head);
+
+ }
+
+ return rt_bypass_register(serial->bypass->upper_h, name, level, func, data);
+}
+
+
+
+void rt_bypass_putchar(struct rt_serial_device* serial, rt_uint8_t ch)
+{
+ rt_mutex_take(serial->bypass->mutex, RT_WAITING_FOREVER);
+ rt_ringbuffer_putchar(serial->bypass->pipe, ch);
+ rt_mutex_release(serial->bypass->mutex);
+}
+
+rt_size_t rt_bypass_getchar(struct rt_serial_device* serial, rt_uint8_t* ch)
+{
+ int flags;
+ rt_mutex_take(serial->bypass->mutex, RT_WAITING_FOREVER);
+ flags = rt_ringbuffer_getchar(serial->bypass->pipe, ch);
+ rt_mutex_release(serial->bypass->mutex);
+ return flags;
+}
+
+static inline rt_err_t _bypass_getchar_form_serial_fifo(struct rt_serial_device* serial, char* ch)
+{
+ rt_base_t level;
+ struct rt_serial_rx_fifo* rx_fifo;
+ rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
+ /* disable interrupt */
+ level = rt_spin_lock_irqsave(&(serial->spinlock));
+
+ /* there's no data: */
+ if ((rx_fifo->get_index == rx_fifo->put_index) && (rx_fifo->is_full == RT_FALSE))
+ {
+ /* no data, enable interrupt and break out */
+ rt_spin_unlock_irqrestore(&(serial->spinlock), level);
+ return -RT_EEMPTY;
+ }
+
+ /* otherwise there's the data: */
+ *ch = rx_fifo->buffer[rx_fifo->get_index];
+ rx_fifo->get_index += 1;
+ if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
+
+ if (rx_fifo->is_full == RT_TRUE)
+ {
+ rx_fifo->is_full = RT_FALSE;
+ }
+
+ /* enable interrupt */
+ rt_spin_unlock_irqrestore(&(serial->spinlock), level);
+
+ return RT_EOK;
+}
+
+static void _lower_work(struct rt_serial_device* serial)
+{
+ struct rt_list_node* node;
+ struct rt_serial_bypass_func* temp_curr = RT_NULL;
+ if (serial->bypass && serial->bypass->lower_h)
+ {
+ while (1)
+ {
+ char ch;
+ if (_bypass_getchar_form_serial_fifo(serial, &ch))
+ return;
+
+ node = serial->bypass->lower_h->head.next;
+
+ while (node != &serial->bypass->lower_h->head)
+ {
+ temp_curr = rt_container_of(node, struct rt_serial_bypass_func, node);
+
+ if (!temp_curr->bypass(serial, ch, temp_curr->data))
+ {
+ break;
+ }
+
+ node = node->next;
+ }
+ if (node == &serial->bypass->lower_h->head)
+ {
+ rt_bypass_putchar(serial, ch);
+ }
+ }
+ }
+}
+
+
+static void rt_lower_work(struct rt_work* work, void* work_data)
+{
+ struct rt_serial_device* serial = (struct rt_serial_device*)work_data;
+
+ RT_ASSERT(serial != RT_NULL);
+
+ _lower_work(serial);
+}
+
+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)
+{
+ if (!serial->bypass)
+ rt_serial_bypass_init(serial);
+
+ if (!serial->bypass->lower_h)
+ {
+ serial->bypass->lower_workq = rt_workqueue_create("serial bypass", RT_SYSTEM_WORKQUEUE_STACKSIZE,
+ RT_SYSTEM_WORKQUEUE_PRIORITY);
+ rt_work_init(&serial->bypass->work, rt_lower_work, (void*)serial);
+
+ serial->bypass->lower_h = rt_malloc(sizeof(struct rt_serial_bypass_head));
+ rt_spin_lock_init(&serial->bypass->lower_h->spinlock);
+ rt_list_init(&serial->bypass->lower_h->head);
+
+ }
+
+ return rt_bypass_register(serial->bypass->lower_h, name, level, func, data);
+}
+
+void rt_bypass_work_straight(struct rt_serial_device* serial)
+{
+ if (serial->bypass && serial->bypass->lower_h)
+ {
+ _lower_work(serial);
+ return;
+ }
+
+ while (1)
+ {
+ char ch;
+ if (_bypass_getchar_form_serial_fifo(serial, &ch))
+ return;
+
+ rt_bypass_putchar(serial, ch);
+ }
+
+}
+
+rt_err_t rt_bypass_unregister(struct rt_serial_bypass_head* bypass, rt_uint8_t level)
+{
+ struct rt_list_node* node;
+ struct rt_serial_bypass_func* temp_curr = RT_NULL;
+ int flags;
+
+ /*Can not unregister protect level in bypass it general be msh or tty*/
+ if (level > RT_BYPASS_PROTECT_LEVEL_1)
+ return -RT_ERROR;
+
+ if (!bypass)
+ return -RT_ERROR;
+
+ node = bypass->head.next;
+ flags = rt_spin_lock_irqsave(&(bypass->spinlock));
+
+ do {
+ temp_curr = rt_container_of(node, struct rt_serial_bypass_func, node);
+
+ if (level == temp_curr->level)
+ {
+ rt_list_remove(node);
+ rt_spin_unlock_irqrestore(&(bypass->spinlock), flags);
+ rt_free(temp_curr);
+ return RT_EOK;
+ }
+
+ node = node->next;
+ } while (node != &bypass->head);
+
+ LOG_E("Can't find bypass with level [%d]", level);
+ rt_spin_unlock_irqrestore(&(bypass->spinlock), flags);
+ return -RT_ERROR;
+}
+
+rt_err_t rt_bypass_upper_unregister(struct rt_serial_device* serial, rt_uint8_t level)
+{
+ if (!serial->bypass || !serial->bypass->upper_h)
+ return -RT_ERROR;
+ return rt_bypass_unregister(serial->bypass->upper_h, level);
+}
+
+rt_err_t rt_bypass_lower_unregister(struct rt_serial_device* serial, rt_uint8_t level)
+{
+ if (!serial->bypass || !serial->bypass->lower_h)
+ return -RT_ERROR;
+ return rt_bypass_unregister(serial->bypass->lower_h, level);
+}
+
+int serial_bypass_list(int argc, char** argv)
+{
+ struct rt_serial_device* serial = RT_NULL;
+ struct rt_serial_bypass_func* current;
+ struct rt_list_node* node;
+ int flags;
+
+ serial = (struct rt_serial_device*)rt_console_get_device();
+ if (!serial || !serial->bypass)
+ {
+ rt_kprintf("Serial bypass not initialized.\n");
+ return -1;
+ }
+
+ /* 遍历 Upper Bypass 链表 */
+ if (serial->bypass->upper_h)
+ {
+ rt_kprintf("Upper bypass chain:\n");
+ node = serial->bypass->upper_h->head.next;
+
+ flags = rt_spin_lock_irqsave(&(serial->bypass->upper_h->spinlock)); /* 加锁*/
+ while (node != &serial->bypass->upper_h->head)
+ {
+ current = rt_container_of(node, struct rt_serial_bypass_func, node);
+ rt_kprintf(" - Name: [%s], Level: [%d]\n", current->name, current->level);
+ node = node->next;
+ }
+ rt_spin_unlock_irqrestore(&(serial->bypass->upper_h->spinlock), flags); /* 解锁*/
+ }
+ else
+ {
+ rt_kprintf("Upper bypass chain is empty.\n");
+ }
+
+ /* 遍历 Lower Bypass 链表 */
+ if (serial->bypass->lower_h)
+ {
+ rt_kprintf("Lower bypass chain:\n");
+ node = serial->bypass->lower_h->head.next;
+
+ flags = rt_spin_lock_irqsave(&(serial->bypass->lower_h->spinlock)); /* 加锁*/
+ while (node != &serial->bypass->lower_h->head)
+ {
+ current = rt_container_of(node, struct rt_serial_bypass_func, node);
+ rt_kprintf(" - Name: [%s], Level: [%d]\n", current->name, current->level);
+ node = node->next;
+ }
+ rt_spin_unlock_irqrestore(&(serial->bypass->lower_h->spinlock), flags); /* 解锁*/
+ }
+ else
+ {
+ rt_kprintf("Lower bypass chain is empty.\n");
+ }
+
+ return 0;
+}
+
+
+MSH_CMD_EXPORT(serial_bypass_list, serial bypass list)
diff --git a/components/drivers/serial/dev_serial.c b/components/drivers/serial/dev_serial.c
index 19043e2ad2..3a78ca5195 100644
--- a/components/drivers/serial/dev_serial.c
+++ b/components/drivers/serial/dev_serial.c
@@ -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()
diff --git a/components/drivers/serial/serial_tty.c b/components/drivers/serial/serial_tty.c
index bbf61f8ae0..a75790d856 100644
--- a/components/drivers/serial/serial_tty.c
+++ b/components/drivers/serial/serial_tty.c
@@ -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;
+
+ tty_lock(tp);
+ ttydisc_rint(tp, ch, 0);
+ ttydisc_rint_done(tp);
+ tty_unlock(tp);
-rt_inline void _setup_serial(struct rt_serial_device *serial, lwp_tty_t 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,