Skip to content

Commit db8e2e4

Browse files
committed
[serial] add bypass hook to direct processing char when uart irq coming
1 parent 07c8b57 commit db8e2e4

File tree

8 files changed

+503
-7
lines changed

8 files changed

+503
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2024-11-20 zhujiale the first version
9+
*/
10+
11+
#ifndef __RTT_BYPASS_H__
12+
#define __RTT_BYPASS_H__
13+
#include <rtthread.h>
14+
#include <rttypes.h>
15+
#include <rtdevice.h>
16+
typedef rt_err_t(*bypass_function_t)(struct rt_serial_device* serial, char buf, void* data);
17+
18+
#define RT_BYPASS_LEVEL_MAX 4
19+
#define RT_BYPASS_LEVEL_1 0
20+
#define RT_BYPASS_LEVEL_2 1
21+
#define RT_BYPASS_LEVEL_3 2
22+
#define RT_BYPASS_LEVEL_4 3
23+
#define RT_BYPASS_MAX_LEVEL 4
24+
25+
/*The protect level can be register but can not be unregister we should use it carefully*/
26+
#define RT_BYPASS_PROTECT_LEVEL_1 10
27+
#define RT_BYPASS_PROTECT_LEVEL_2 11
28+
#define RT_BYPASS_PROTECT_LEVEL_3 12
29+
#define RT_BYPASS_PROTECT_LEVEL_4 13
30+
31+
struct rt_serial_bypass_func {
32+
/*The function pointer of the bypassed data processing*/
33+
bypass_function_t bypass;
34+
/*The smaller the array of levels, the higher the priority of execution*/
35+
rt_uint8_t level;
36+
rt_list_t node;
37+
char name[RT_NAME_MAX];
38+
void* data;
39+
};
40+
41+
struct rt_serial_bypass_head
42+
{
43+
rt_list_t head;
44+
struct rt_spinlock spinlock;
45+
};
46+
47+
struct rt_serial_bypass {
48+
struct rt_work work;
49+
50+
struct rt_spinlock spinlock;
51+
struct rt_workqueue* lower_workq;
52+
struct rt_serial_bypass_head* upper_h;
53+
struct rt_serial_bypass_head* lower_h;
54+
rt_mutex_t mutex;
55+
struct rt_ringbuffer* pipe;
56+
};
57+
58+
int serial_bypass_list(int argc, char** argv);
59+
60+
void rt_bypass_work_straight(struct rt_serial_device* serial);
61+
void rt_bypass_putchar(struct rt_serial_device* serial, rt_uint8_t ch);
62+
rt_size_t rt_bypass_getchar(struct rt_serial_device* serial, rt_uint8_t* ch);
63+
64+
rt_err_t rt_bypass_upper_unregister(struct rt_serial_device* serial, rt_uint8_t level);
65+
rt_err_t rt_bypass_lower_unregister(struct rt_serial_device* serial, rt_uint8_t level);
66+
67+
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);
68+
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);
69+
#endif

components/drivers/include/drivers/dev_serial.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
* if (!serial)
7777
* {
7878
* rt_kprintf("find %s failed!\n", uart_name);
79-
* return RT_ERROR;
79+
* return -RT_ERROR;
8080
* }
8181
*
8282
*
@@ -97,7 +97,7 @@
9797
* }
9898
* else
9999
* {
100-
* ret = RT_ERROR;
100+
* ret = -RT_ERROR;
101101
* }
102102
*
103103
* return ret;
@@ -264,7 +264,9 @@ struct rt_serial_device
264264
void *serial_tx;
265265

266266
struct rt_spinlock spinlock;
267-
267+
#ifdef RT_USING_SERIAL_BYPASS
268+
struct rt_serial_bypass* bypass;
269+
#endif
268270
struct rt_device_notify rx_notify;
269271
};
270272
typedef struct rt_serial_device rt_serial_t;

components/drivers/include/rtdevice.h

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ extern "C" {
9090
#include "drivers/dev_serial_v2.h"
9191
#else
9292
#include "drivers/dev_serial.h"
93+
#ifdef RT_USING_SERIAL_BYPASS
94+
#include "drivers/bypass.h"
95+
#endif /* RT_USING_SERIAL_BYPASS */
9396
#endif
9497
#endif /* RT_USING_SERIAL */
9598

components/drivers/serial/Kconfig

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ menuconfig RT_USING_SERIAL
2121
int "Set RX buffer size"
2222
depends on !RT_USING_SERIAL_V2
2323
default 64
24-
endif
24+
config RT_USING_SERIAL_BYPASS
25+
bool "Using serial bypass"
26+
default y
27+
endif

components/drivers/serial/SConscript

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ if GetDepend(['RT_USING_SERIAL_V2']):
1616
else:
1717
src += ['dev_serial.c']
1818

19+
if GetDepend(['RT_USING_SERIAL_BYPASS']):
20+
src += ['bypass.c']
21+
1922
if GetDepend(['RT_USING_DM']):
2023
src += ['serial_dm.c']
2124

0 commit comments

Comments
 (0)