1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-11-16 GuEe-GUI first version
*/
#include <rtatomic.h>
#include <drivers/serial_dm.h>
static int uid_min = -1;
static volatile rt_atomic_t uid = 0;
int serial_dev_set_name(struct rt_serial_device *sdev)
{
int id = -1;
RT_ASSERT(sdev != RT_NULL);
#ifdef RT_USING_OFW
if (sdev->parent.ofw_node)
{
id = rt_ofw_get_alias_id(sdev->parent.ofw_node, "serial");
if (id < 0)
{
id = rt_ofw_get_alias_id(sdev->parent.ofw_node, "uart");
}
}
#endif /* RT_USING_OFW */
if (id < 0)
{
id = (int)rt_atomic_add(&uid, 1);
}
return rt_dm_dev_set_name(&sdev->parent, "uart%u", id);
}
static int serial_dm_naming_framework_init(void)
{
#ifdef RT_USING_OFW
uid_min = rt_ofw_get_alias_last_id("serial");
if (uid_min < 0)
{
uid_min = rt_ofw_get_alias_last_id("uart");
}
uid_min = uid_min < 0 ? 0 : (uid_min + 1);
rt_hw_atomic_store(&uid, uid_min);
#endif /* RT_USING_OFW */
return 0;
}
INIT_PLATFORM_EXPORT(serial_dm_naming_framework_init);
void *serial_base_from_args(char *str)
{
rt_ubase_t base = 0;
while (*str && !(*str == 'x' || *str == 'X'))
{
++str;
}
++str;
/* The str may get from bootargs that we need check it */
while (*str)
{
if ((*str >= 'a' && *str <= 'f') || (*str >= 'A' && *str <= 'F'))
{
base = (base << 4) | (((*str | ' ') - 'a') + 10);
}
else if (*str >= '0' && *str <= '9')
{
base = (base << 4) | (*str - '0');
}
else break;
++str;
}
return (void *)base;
}
struct serial_configure serial_cfg_from_args(char *_str)
{
char *str = _str;
struct serial_configure cfg = RT_SERIAL_CONFIG_DEFAULT;
/* Format baudrate/parity/bits/flow (BBBBPNF), Default is 115200n8 */
if (str && *str)
{
rt_uint32_t baudrate = 0;
/* BBBB is the speed */
while (*str && (*str >= '0' && *str <= '9'))
{
baudrate *= 10;
baudrate += *str - '0';
++str;
}
if (baudrate)
{
cfg.baud_rate = baudrate;
}
/* P is parity (n/o/e) */
switch (*str)
{
case 'n':
cfg.parity = PARITY_NONE;
break;
case 'o':
cfg.parity = PARITY_ODD;
break;
case 'e':
cfg.parity = PARITY_EVEN;
break;
default:
--str;
break;
}
++str;
/* N is number of bits */
if (*str && (*str >= '0' && *str <= '9'))
{
cfg.data_bits = *str - '0';
++str;
}
/* F is flow ontrol ('r' for RTS) */
if (*str)
{
cfg.flowcontrol = (*str == 'r' ? RT_SERIAL_FLOWCONTROL_CTSRTS : RT_SERIAL_FLOWCONTROL_NONE);
++str;
}
#ifdef RT_USING_OFW
if (*str == '\0')
{
const char earlycon_magic[] = { 'O', 'F', 'W', '\0' };
if (!rt_strcmp(++str, earlycon_magic))
{
/* Is OFW earlycon, we should ACK it */
rt_memset(str, 0, RT_ARRAY_SIZE(earlycon_magic));
}
}
#endif /* RT_USING_OFW */
}
return cfg;
}
|