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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-7-21 Egbert First version
*
******************************************************************************/
#include <rtconfig.h>
#if defined(BSP_USING_SCUART)
#include "NuMicro.h"
#include <rtdevice.h>
#include <rthw.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
#if defined(BSP_USING_SCUART2)
SCUART2_IDX,
#endif
SCUART_CNT
};
/* Private typedef
* --------------------------------------------------------------*/
struct nu_scuart
{
rt_serial_t dev;
char *name;
SC_T *scuart_base;
uint32_t scuart_rst;
IRQn_Type scuart_irq_n;
};
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(nu_scuart_t serial);
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",
.scuart_base = SC0,
.scuart_rst = SC0_RST,
.scuart_irq_n = SC0_IRQn,
},
#endif
#if defined(BSP_USING_SCUART1)
{
.name = "scuart1",
.scuart_base = SC1,
.scuart_rst = SC1_RST,
.scuart_irq_n = SC1_IRQn,
},
#endif
#if defined(BSP_USING_SCUART2)
{
.name = "scuart2",
.scuart_base = SC2,
.scuart_rst = SC2_RST,
.scuart_irq_n = SC2_IRQn,
},
#endif
{0}
}; /* scuart nu_scuart */
/* Interrupt Handle Function
* ----------------------------------------------------*/
#if defined(BSP_USING_SCUART0)
/* SCUART0 interrupt entry */
void SC0_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
nu_scuart_isr(&nu_scuart_arr[SCUART0_IDX]);
/* leave interrupt */
rt_interrupt_leave();
}
#endif
#if defined(BSP_USING_SCUART1)
/* SCUART1 interrupt entry */
void SC1_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
nu_scuart_isr(&nu_scuart_arr[SCUART1_IDX]);
/* leave interrupt */
rt_interrupt_leave();
}
#endif
#if defined(BSP_USING_SCUART2)
/* SCUART2 interrupt entry */
void SC2_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
nu_scuart_isr(&nu_scuart_arr[SCUART2_IDX]);
/* leave interrupt */
rt_interrupt_leave();
}
#endif
/**
* All SCUART interrupt service routine
*/
static void nu_scuart_isr(nu_scuart_t serial)
{
/* Get base address of scuart register */
SC_T *scuart_base = ((nu_scuart_t)serial)->scuart_base;
/* Get interrupt event */
uint32_t u32IntSts = scuart_base->INTSTS;
/* Handle RX event */
if (u32IntSts & (SC_INTSTS_RDAIF_Msk | SC_INTSTS_RXTOIF_Msk))
{
rt_hw_serial_isr(&serial->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_base->INTSTS = 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;
/* Get base address of scuart register */
SC_T *scuart_base = ((nu_scuart_t)serial)->scuart_base;
/* Check baud rate */
RT_ASSERT(cfg->baud_rate != 0);
/* 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;
}
/* Reset this module */
SYS_ResetModule(((nu_scuart_t)serial)->scuart_rst);
/* Open SCUART and set SCUART baud rate */
SCUART_Open(scuart_base, cfg->baud_rate);
/* Set line configuration. */
SCUART_SetLineConfig(scuart_base, 0, scuart_word_len, scuart_parity,
scuart_stop_bit);
/* Enable NVIC interrupt. */
NVIC_EnableIRQ(((nu_scuart_t)serial)->scuart_irq_n);
exit_nu_scuart_configure:
if (ret != RT_EOK)
SCUART_Close(scuart_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;
RT_ASSERT(serial != RT_NULL);
/* Get base address of scuart register */
SC_T *scuart_base = ((nu_scuart_t)serial)->scuart_base;
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(scuart_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(scuart_base, flag);
}
break;
case RT_DEVICE_CTRL_CLOSE:
/* Disable NVIC interrupt. */
NVIC_DisableIRQ(((nu_scuart_t)serial)->scuart_irq_n);
/* Reset this module */
SYS_ResetModule(((nu_scuart_t)serial)->scuart_rst);
/* Close SCUART port */
SCUART_Close(scuart_base);
break;
default:
result = -RT_EINVAL;
break;
}
return result;
}
/**
* SCUART put char
*/
static int nu_scuart_send(struct rt_serial_device *serial, char c)
{
RT_ASSERT(serial != RT_NULL);
/* Get base address of scuart register */
SC_T *scuart_base = ((nu_scuart_t)serial)->scuart_base;
/* Waiting if TX-FIFO is full. */
while (SCUART_IS_TX_FULL(scuart_base))
;
/* Put char into TX-FIFO */
SCUART_WRITE(scuart_base, c);
return 1;
}
/**
* SCUART get char
*/
static int nu_scuart_receive(struct rt_serial_device *serial)
{
RT_ASSERT(serial != RT_NULL);
/* Get base address of scuart register */
SC_T *scuart_base = ((nu_scuart_t)serial)->scuart_base;
/* Return failure if RX-FIFO is empty. */
if (SCUART_GET_RX_EMPTY(scuart_base))
{
return -1;
}
/* Get char from RX-FIFO */
return SCUART_READ(scuart_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;
ret = rt_hw_serial_register(&nu_scuart_arr[i].dev, nu_scuart_arr[i].name,
flag, NULL);
RT_ASSERT(ret == RT_EOK);
}
return ret;
}
INIT_DEVICE_EXPORT(rt_hw_scuart_init);
#endif //#if defined(BSP_USING_SCUART)
|