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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
/*
* Copyright (c) 2021 HPMicro
*
* SPDX-License-Identifier: BSD-3-Clause
*
* Change Logs:
* Date Author Notes
* 2021-09-19 HPMICRO First version
*
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <rtdbg.h>
#include "board.h"
#include "drv_uart.h"
#include "hpm_uart_drv.h"
#include "hpm_sysctl_drv.h"
#include "hpm_clock_drv.h"
#ifdef RT_USING_SERIAL
#define UART_ROOT_CLK_FREQ BOARD_APP_UART_SRC_FREQ
struct hpm_uart {
UART_Type *uart_base;
rt_uint32_t irq_num;
rt_uint8_t irq_priority;
clock_name_t clk_name;
struct rt_serial_device *serial;
char *device_name;
};
extern void init_uart_pins(UART_Type *ptr);
static void hpm_uart_isr(struct rt_serial_device *serial);
static rt_err_t hpm_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
static rt_err_t hpm_uart_control(struct rt_serial_device *serial, int cmd, void *arg);
static int hpm_uart_putc(struct rt_serial_device *serial, char ch);
static int hpm_uart_getc(struct rt_serial_device *serial);
#ifndef HPM_USING_RTTHREAD_INTERRUPT_FRAMEWORK
#if defined(BSP_USING_UART0)
struct rt_serial_device serial0;
void uart0_isr(void)
{
hpm_uart_isr(&serial0);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART0,uart0_isr)
#endif
#if defined(BSP_USING_UART1)
struct rt_serial_device serial1;
void uart1_isr(void)
{
hpm_uart_isr(&serial1);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART1,uart1_isr)
#endif
#if defined(BSP_USING_UART2)
struct rt_serial_device serial2;
void uart2_isr(void)
{
hpm_uart_isr(&serial2);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART2,uart2_isr)
#endif
#if defined(BSP_USING_UART3)
struct rt_serial_device serial3;
void uart3_isr(void)
{
hpm_uart_isr(&serial3);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART3,uart3_isr)
#endif
#if defined(BSP_USING_UART4)
struct rt_serial_device serial4;
void uart4_isr(void)
{
hpm_uart_isr(&serial4);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART4,uart4_isr)
#endif
#if defined(BSP_USING_UART5)
struct rt_serial_device serial5;
void uart5_isr(void)
{
hpm_uart_isr(&serial5);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART5,uart5_isr)
#endif
#if defined(BSP_USING_UART6)
struct rt_serial_device serial6;
void uart6_isr(void)
{
hpm_uart_isr(&serial6);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART6,uart6_isr)
#endif
#if defined(BSP_USING_UART7)
struct rt_serial_device serial7;
void uart7_isr(void)
{
hpm_uart_isr(&serial7);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART7,uart7_isr)
#endif
#if defined(BSP_USING_UART8)
struct rt_serial_device serial8;
void uart8_isr(void)
{
hpm_uart_isr(&serial8);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART8,uart8_isr)
#endif
#if defined(BSP_USING_UART9)
struct rt_serial_device serial9;
void uart9_isr(void)
{
hpm_uart_isr(&serial9);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART9,uart9_isr)
#endif
#if defined(BSP_USING_UART10)
struct rt_serial_device serial10;
void uart10_isr(void)
{
hpm_uart_isr(&serial10);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART10,uart10_isr)
#endif
#if defined(BSP_USING_UART11)
struct rt_serial_device serial11;
void uart11_isr(void)
{
hpm_uart_isr(&serial11);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART11,uart11_isr)
#endif
#if defined(BSP_USING_UART12)
struct rt_serial_device serial12;
void uart12_isr(void)
{
hpm_uart_isr(&serial12);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART12,uart12_isr)
#endif
#if defined(BSP_USING_UART13)
struct rt_serial_device serial13;
void uart13_isr(void)
{
hpm_uart_isr(&serial13);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART13,uart13_isr)
#endif
#if defined(BSP_USING_UART14)
struct rt_serial_device serial14;
void uart14_isr(void)
{
hpm_uart_isr(&serial14);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART14,uart14_isr)
#endif
#if defined(BSP_USING_UART15)
struct rt_serial_device serial15;
void uart15_isr(void)
{
hpm_uart_isr(&serial15);
}
SDK_DECLARE_EXT_ISR_M(IRQn_UART15,uart15_isr)
#endif
#endif /* HPM_USING_RTTHREAD_INTERRUPT_FRAMEWORK */
static const struct hpm_uart uarts[] = {
#if defined(BSP_USING_UART0)
{
HPM_UART0,
IRQn_UART0,
#if defined(BSP_UART0_IRQ_PRIORITY)
.irq_priority = BSP_UART0_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart0,
&serial0,
"uart0",
},
#endif
#if defined(BSP_USING_UART1)
{
HPM_UART1,
IRQn_UART1,
#if defined(BSP_UART1_IRQ_PRIORITY)
.irq_priority = BSP_UART1_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart1,
&serial1,
"uart1",
},
#endif
#if defined(BSP_USING_UART2)
{
HPM_UART2,
IRQn_UART2,
#if defined(BSP_UART2_IRQ_PRIORITY)
.irq_priority = BSP_UART2_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart2,
&serial2,
"uart2",
},
#endif
#if defined(BSP_USING_UART3)
{
HPM_UART3,
IRQn_UART3,
#if defined(BSP_UART3_IRQ_PRIORITY)
.irq_priority = BSP_UART3_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart3,
&serial3,
"uart3",
},
#endif
#if defined(BSP_USING_UART4)
{
HPM_UART4,
IRQn_UART4,
#if defined(BSP_UART4_IRQ_PRIORITY)
.irq_priority = BSP_UART4_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart4,
&serial4,
"uart4",
},
#endif
#if defined(BSP_USING_UART5)
{
HPM_UART5,
IRQn_UART5,
#if defined(BSP_UART5_IRQ_PRIORITY)
.irq_priority = BSP_UART5_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart5,
&serial5,
"uart5",
},
#endif
#if defined(BSP_USING_UART6)
{
HPM_UART6,
IRQn_UART6,
#if defined(BSP_UART6_IRQ_PRIORITY)
.irq_priority = BSP_UART6_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart6,
&serial6,
"uart6",
},
#endif
#if defined(BSP_USING_UART7)
{
HPM_UART7,
IRQn_UART7,
#if defined(BSP_UART7_IRQ_PRIORITY)
.irq_priority = BSP_UART7_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart7,
&serial7,
"uart7",
},
#endif
#if defined(BSP_USING_UART8)
{
HPM_UART8,
IRQn_UART8,
#if defined(BSP_UART8_IRQ_PRIORITY)
.irq_priority = BSP_UART8_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart8,
&serial8,
"uart8",
},
#endif
#if defined(BSP_USING_UART9)
{
HPM_UART9,
IRQn_UART9,
#if defined(BSP_UART9_IRQ_PRIORITY)
.irq_priority = BSP_UART9_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart9,
&serial9,
"uart9",
},
#endif
#if defined(BSP_USING_UART10)
{
HPM_UART10,
IRQn_UART10,
#if defined(BSP_UART10_IRQ_PRIORITY)
.irq_priority = BSP_UART10_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart10,
&serial10,
"uart10",
},
#endif
#if defined(BSP_USING_UART11)
{
HPM_UART11,
IRQn_UART11,
#if defined(BSP_UART11_IRQ_PRIORITY)
.irq_priority = BSP_UART11_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart11,
&serial11,
"uart11",
},
#endif
#if defined(BSP_USING_UART12)
{
HPM_UART12,
IRQn_UART12,
#if defined(BSP_UART12_IRQ_PRIORITY)
.irq_priority = BSP_UART12_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart12,
&serial12,
"uart12",
},
#endif
#if defined(BSP_USING_UART13)
{
HPM_UART13,
IRQn_UART13,
#if defined(BSP_UART13_IRQ_PRIORITY)
.irq_priority = BSP_UART13_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart13,
&serial13,
"uart13",
},
#endif
#if defined(BSP_USING_UART14)
{
HPM_UART14,
IRQn_UART14,
#if defined(BSP_UART14_IRQ_PRIORITY)
.irq_priority = BSP_UART14_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart14,
&serial14,
"uart14",
},
#endif
#if defined(BSP_USING_UART15)
{
HPM_UART15,
IRQn_UART15,
#if defined(BSP_UART15_IRQ_PRIORITY)
.irq_priority = BSP_UART15_IRQ_PRIORITY,
#else
.irq_priority = 1,
#endif
clock_uart15,
&serial15,
"uart15",
},
#endif
};
/**
* @brief UART common interrupt process. This
*
* @param serial Serial device
*/
#ifndef HPM_USING_RTTHREAD_INTERRUPT_FRAMEWORK
static void hpm_uart_isr(struct rt_serial_device *serial)
#else
static void hpm_uart_isr(int vector, struct rt_serial_device *serial)
#endif /* HPM_USING_RTTHREAD_INTERRUPT_FRAMEWORK */
{
struct hpm_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = (struct hpm_uart *)serial->parent.user_data;
RT_ASSERT(uart != RT_NULL);
/* UART in mode Receiver */
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
}
static rt_err_t hpm_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL);
uart_config_t uart_config;
hpm_stat_t stat;
struct hpm_uart *uart = (struct hpm_uart *)serial->parent.user_data;
init_uart_pins(uart->uart_base);
uart_default_config(uart->uart_base, &uart_config);
clock_add_to_group(uart->clk_name, BOARD_RUNNING_CORE & 0x1);
uart_config.src_freq_in_hz = clock_get_frequency(uart->clk_name);
uart_config.baudrate = cfg->baud_rate;
uart_config.num_of_stop_bits = cfg->stop_bits;
uart_config.parity = cfg->parity;
uart_config.word_length = cfg->data_bits - DATA_BITS_5;
stat = uart_init(uart->uart_base, &uart_config);
if (stat != status_success)
{
return -RT_ERROR;
}
return hpm_uart_control(serial, RT_DEVICE_CTRL_SET_INT, NULL);
}
static rt_err_t hpm_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
{
RT_ASSERT(serial != RT_NULL);
struct hpm_uart *uart = (struct hpm_uart *)serial->parent.user_data;
switch (cmd) {
case RT_DEVICE_CTRL_CLR_INT:
/* disable rx irq */
uart_disable_irq(uart->uart_base, uart_intr_rx_data_avail_or_timeout);
intc_m_disable_irq(uart->irq_num);
break;
case RT_DEVICE_CTRL_SET_INT:
/* enable rx irq */
uart_enable_irq(uart->uart_base, uart_intr_rx_data_avail_or_timeout);
intc_m_enable_irq_with_priority(uart->irq_num, uart->irq_priority);
break;
}
return RT_EOK;
}
static int hpm_uart_putc(struct rt_serial_device *serial, char ch)
{
struct hpm_uart *uart = (struct hpm_uart *)serial->parent.user_data;
uart_write_byte(uart->uart_base, ch);
while(!uart_check_status(uart->uart_base, uart_stat_transmitter_empty)) {
}
}
static int hpm_uart_getc(struct rt_serial_device *serial)
{
int result = -1;
struct hpm_uart *uart = (struct hpm_uart *)serial->parent.user_data;
if (uart_check_status(uart->uart_base, uart_stat_data_ready)) {
uart_receive_byte(uart->uart_base, (uint8_t*)&result);
}
return result;
}
static const struct rt_uart_ops hpm_uart_ops = {
hpm_uart_configure,
hpm_uart_control,
hpm_uart_putc,
hpm_uart_getc,
};
int rt_hw_uart_init(void)
{
/* Added bypass logic here since the rt_hw_uart_init function will be initialized twice, the 2nd initialization should be bypassed */
static bool initialized;
rt_err_t err = RT_EOK;
if (initialized)
{
return err;
}
else
{
initialized = true;
}
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
for (uint32_t i = 0; i < sizeof(uarts) / sizeof(uarts[0]); i++) {
uarts[i].serial->ops = &hpm_uart_ops;
uarts[i].serial->config = config;
/* register UART deivce */
err = rt_hw_serial_register(uarts[i].serial,
uarts[i].device_name,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
(void*)&uarts[i]);
#ifdef HPM_USING_RTTHREAD_INTERRUPT_FRAMEWORK
/* Register UART device to irq table */
rt_hw_interrupt_install(uarts[i].irq_num, (rt_isr_handler_t)hpm_uart_isr, uarts[i].serial, uarts[i].device_name);
#endif /* HPM_USING_RTTHREAD_INTERRUPT_FRAMEWORK */
}
return err;
}
INIT_BOARD_EXPORT(rt_hw_uart_init);
#endif /* RT_USING_SERIAL */
|