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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-9-27 Wayne First version
*
******************************************************************************/
#include <rtconfig.h>
#if defined(BSP_USING_SCUART)
#include "NuMicro.h"
#include <rtdevice.h>
#include <rthw.h>
#include <drv_sys.h>
/* Private definition
* ---------------------------------------------------------------*/
#define LOG_TAG "drv.scuart"
#define DBG_ENABLE
#define DBG_SECTION_NAME "drv.scuart"
#define DBG_LEVEL DBG_ERROR
#define DBG_COLOR
#include <rtdbg.h>
enum
{
SCUART_START = -1,
#if defined(BSP_USING_SCUART0)
SCUART0_IDX,
#endif
#if defined(BSP_USING_SCUART1)
SCUART1_IDX,
#endif
SCUART_CNT
};
/* Private typedef
* --------------------------------------------------------------*/
struct nu_scuart
{
rt_serial_t dev;
char *name;
SC_T *base;
IRQn_Type irqn;
uint32_t rstidx;
uint32_t modid;
};
typedef struct nu_scuart *nu_scuart_t;
/* Private functions
* ------------------------------------------------------------*/
static rt_err_t nu_scuart_configure(struct rt_serial_device *serial,
struct serial_configure *cfg);
static rt_err_t nu_scuart_control(struct rt_serial_device *serial, int cmd,
void *arg);
static int nu_scuart_send(struct rt_serial_device *serial, char c);
static int nu_scuart_receive(struct rt_serial_device *serial);
static void nu_scuart_isr(int vector, void *param);
static const struct rt_uart_ops nu_scuart_ops =
{
.configure = nu_scuart_configure,
.control = nu_scuart_control,
.putc = nu_scuart_send,
.getc = nu_scuart_receive,
.dma_transmit = RT_NULL /* not support DMA mode */
};
static const struct serial_configure nu_scuart_default_config =
RT_SERIAL_CONFIG_DEFAULT;
static struct nu_scuart nu_scuart_arr[] =
{
#if defined(BSP_USING_SCUART0)
{
.name = "scuart0", .base = SC0, .irqn = SC0_IRQn, .rstidx = SC0_RST, .modid = SC0_MODULE,
},
#endif
#if defined(BSP_USING_SCUART1)
{
.name = "scuart1", .base = SC1, .irqn = SC1_IRQn, .rstidx = SC1_RST, .modid = SC1_MODULE,
},
#endif
}; /* scuart nu_scuart */
/**
* All SCUART interrupt service routine
*/
static void nu_scuart_isr(int vector, void *param)
{
nu_scuart_t psNuSCUart = (nu_scuart_t)param;
/* Handle RX event */
if (SCUART_GET_INT_FLAG(psNuSCUart->base, SC_INTSTS_RXTOIF_Msk) ||
SCUART_GET_INT_FLAG(psNuSCUart->base, SC_INTSTS_RDAIF_Msk))
{
rt_hw_serial_isr(&psNuSCUart->dev, RT_SERIAL_EVENT_RX_IND);
// RDA is the only interrupt enabled in this driver, this status bit
// automatically cleared after Rx FIFO empty. So no need to clear interrupt
// status here.
SCUART_CLR_INT_FLAG(psNuSCUart->base, SC_INTSTS_RXTOIF_Msk);
}
}
/**
* Configure scuart port
*/
static rt_err_t nu_scuart_configure(struct rt_serial_device *serial,
struct serial_configure *cfg)
{
rt_err_t ret = RT_EOK;
uint32_t scuart_word_len = 0;
uint32_t scuart_stop_bit = 0;
uint32_t scuart_parity = 0;
nu_scuart_t psNuSCUart = (nu_scuart_t)serial;
RT_ASSERT(psNuSCUart);
/* Check baud rate */
RT_ASSERT(cfg->baud_rate);
/* Check word len */
switch (cfg->data_bits)
{
case DATA_BITS_5:
scuart_word_len = SCUART_CHAR_LEN_5;
break;
case DATA_BITS_6:
scuart_word_len = SCUART_CHAR_LEN_6;
break;
case DATA_BITS_7:
scuart_word_len = SCUART_CHAR_LEN_7;
break;
case DATA_BITS_8:
scuart_word_len = SCUART_CHAR_LEN_8;
break;
default:
LOG_E("Unsupported data length");
ret = -RT_EINVAL;
goto exit_nu_scuart_configure;
}
/* Check stop bit */
switch (cfg->stop_bits)
{
case STOP_BITS_1:
scuart_stop_bit = SCUART_STOP_BIT_1;
break;
case STOP_BITS_2:
scuart_stop_bit = SCUART_STOP_BIT_2;
break;
default:
LOG_E("Unsupported stop bit");
ret = -RT_EINVAL;
goto exit_nu_scuart_configure;
}
/* Check parity */
switch (cfg->parity)
{
case PARITY_NONE:
scuart_parity = SCUART_PARITY_NONE;
break;
case PARITY_ODD:
scuart_parity = SCUART_PARITY_ODD;
break;
case PARITY_EVEN:
scuart_parity = SCUART_PARITY_EVEN;
break;
default:
LOG_E("Unsupported parity");
ret = -RT_EINVAL;
goto exit_nu_scuart_configure;
}
nu_sys_ip_reset(psNuSCUart->rstidx);
/* Open SCUART and set SCUART baud rate */
SCUART_Open(psNuSCUart->base, cfg->baud_rate);
/* Set line configuration. */
SCUART_SetLineConfig(psNuSCUart->base, 0, scuart_word_len, scuart_parity,
scuart_stop_bit);
/* Enable interrupt. */
rt_hw_interrupt_umask(psNuSCUart->irqn);
exit_nu_scuart_configure:
if (ret != RT_EOK)
SCUART_Close(psNuSCUart->base);
return -(ret);
}
/**
* SCUART interrupt control
*/
static rt_err_t nu_scuart_control(struct rt_serial_device *serial, int cmd,
void *arg)
{
rt_err_t result = RT_EOK;
rt_uint32_t flag;
rt_ubase_t ctrl_arg = (rt_ubase_t)arg;
nu_scuart_t psNuSCUart = (nu_scuart_t)serial;
RT_ASSERT(psNuSCUart);
switch (cmd)
{
case RT_DEVICE_CTRL_CLR_INT:
if (ctrl_arg == RT_DEVICE_FLAG_INT_RX) /* Disable INT-RX */
{
flag = SC_INTEN_RDAIEN_Msk | SC_INTEN_RXTOIEN_Msk;
SCUART_DISABLE_INT(psNuSCUart->base, flag);
}
break;
case RT_DEVICE_CTRL_SET_INT:
if (ctrl_arg == RT_DEVICE_FLAG_INT_RX) /* Enable INT-RX */
{
flag = SC_INTEN_RDAIEN_Msk | SC_INTEN_RXTOIEN_Msk;
SCUART_ENABLE_INT(psNuSCUart->base, flag);
}
break;
case RT_DEVICE_CTRL_CLOSE:
/* Disable interrupt. */
rt_hw_interrupt_mask(psNuSCUart->irqn);
/* Close SCUART port */
SCUART_Close(psNuSCUart->base);
break;
default:
result = -RT_EINVAL;
break;
}
return result;
}
/**
* SCUART put char
*/
static int nu_scuart_send(struct rt_serial_device *serial, char c)
{
nu_scuart_t psNuSCUart = (nu_scuart_t)serial;
RT_ASSERT(psNuSCUart);
/* Waiting if TX-FIFO is full. */
while (SCUART_IS_TX_FULL(psNuSCUart->base)) ;
/* Put char into TX-FIFO */
SCUART_WRITE(psNuSCUart->base, c);
return 1;
}
/**
* SCUART get char
*/
static int nu_scuart_receive(struct rt_serial_device *serial)
{
nu_scuart_t psNuSCUart = (nu_scuart_t)serial;
RT_ASSERT(psNuSCUart);
/* Return failure if RX-FIFO is empty. */
if (SCUART_GET_RX_EMPTY(psNuSCUart->base))
{
return -1;
}
/* Get char from RX-FIFO */
return SCUART_READ(psNuSCUart->base);
}
/**
* Hardware SCUART Initialization
*/
static int rt_hw_scuart_init(void)
{
int i;
rt_uint32_t flag;
rt_err_t ret = RT_EOK;
for (i = (SCUART_START + 1); i < SCUART_CNT; i++)
{
flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
nu_scuart_arr[i].dev.ops = &nu_scuart_ops;
nu_scuart_arr[i].dev.config = nu_scuart_default_config;
SYS_ResetModule(nu_scuart_arr[i].rstidx);
rt_hw_interrupt_install(nu_scuart_arr[i].irqn, nu_scuart_isr, &nu_scuart_arr[i], nu_scuart_arr[i].name);
ret = rt_hw_serial_register(&nu_scuart_arr[i].dev, nu_scuart_arr[i].name, flag, NULL);
RT_ASSERT(ret == RT_EOK);
}
return (int)ret;
}
INIT_DEVICE_EXPORT(rt_hw_scuart_init);
#endif //#if defined(BSP_USING_SCUART)
|