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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <rtconfig.h>
#include <rthw.h>
#include "board.h"
#include "drv_uart.h"
#ifdef RT_USING_SERIAL
#define LOG_TAG "drv.uart"
#include <rtdbg.h>
#if defined(SOC_SERIES_MM32F3277)
#include "drv_config.h"
#if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) && \
!defined(BSP_USING_UART4) && !defined(BSP_USING_UART5) && !defined(BSP_USING_UART6) && \
!defined(BSP_USING_UART7) && !defined(BSP_USING_UART8)
#error "Please define at least one BSP_USING_UARTx"
#endif
static struct mm32_uart_config uart_config[] =
{
#ifdef BSP_USING_UART1
UART1_CONFIG,
#endif
#ifdef BSP_USING_UART2
UART2_CONFIG,
#endif
#ifdef BSP_USING_UART3
UART3_CONFIG,
#endif
#ifdef BSP_USING_UART4
UART4_CONFIG,
#endif
#ifdef BSP_USING_UART5
UART5_CONFIG,
#endif
#ifdef BSP_USING_UART6
UART6_CONFIG,
#endif
#ifdef BSP_USING_UART7
UART7_CONFIG,
#endif
#ifdef BSP_USING_UART8
UART8_CONFIG,
#endif
};
static struct mm32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
static struct mm32_uart *mm32_uart_get_by_instance(UART_Type *instance)
{
rt_size_t i;
for (i = 0; i < sizeof(uart_obj) / sizeof(uart_obj[0]); i++)
{
if (uart_obj[i].config->Instance == instance)
{
return &uart_obj[i];
}
}
return RT_NULL;
}
static rt_err_t mm32_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
struct mm32_uart *uart;
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL);
uart = rt_container_of(serial, struct mm32_uart, serial);
uart->handle.ClockFreqHz = HAL_Get_APB2_Clock();
uart->handle.BaudRate = cfg->baud_rate;
uart->handle.XferMode = UART_XferMode_RxTx;
switch (cfg->flowcontrol)
{
case RT_SERIAL_FLOWCONTROL_CTSRTS:
uart->handle.HwFlowControl = UART_HwFlowControl_RTS_CTS;
break;
case RT_SERIAL_FLOWCONTROL_NONE:
default:
uart->handle.HwFlowControl = UART_HwFlowControl_None;
break;
}
switch (cfg->data_bits)
{
case DATA_BITS_5:
uart->handle.WordLength = UART_WordLength_5b;
break;
case DATA_BITS_6:
uart->handle.WordLength = UART_WordLength_6b;
break;
case DATA_BITS_7:
uart->handle.WordLength = UART_WordLength_7b;
break;
case DATA_BITS_8:
default:
uart->handle.WordLength = UART_WordLength_8b;
break;
}
switch (cfg->stop_bits)
{
case STOP_BITS_2:
uart->handle.StopBits = UART_StopBits_2;
break;
case STOP_BITS_1:
default:
uart->handle.StopBits = UART_StopBits_1;
break;
}
switch (cfg->parity)
{
case PARITY_ODD:
uart->handle.Parity = UART_Parity_Odd;
break;
case PARITY_EVEN:
uart->handle.Parity = UART_Parity_Even;
break;
case PARITY_NONE:
default:
uart->handle.Parity = UART_Parity_None;
break;
}
extern void UART_MspInit(UART_Type *muart);
UART_MspInit(uart->config->Instance);
UART_Init(uart->config->Instance, &uart->handle);
UART_Enable(uart->config->Instance, true);
return RT_EOK;
}
static rt_err_t mm32_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
{
struct mm32_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct mm32_uart, serial);
switch (cmd)
{
case RT_DEVICE_CTRL_CLR_INT:
UART_EnableInterrupts(uart->config->Instance, UART_INT_RX_DONE, false);
NVIC_DisableIRQ(uart->config->irq_type);
break;
case RT_DEVICE_CTRL_SET_INT:
UART_EnableInterrupts(uart->config->Instance, UART_INT_RX_DONE, true);
NVIC_SetPriority(uart->config->irq_type, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 1, 0));
NVIC_EnableIRQ(uart->config->irq_type);
break;
default:
break;
}
return RT_EOK;
}
static int mm32_uart_putc(struct rt_serial_device *serial, char c)
{
struct mm32_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct mm32_uart, serial);
UART_PutData(uart->config->Instance, (rt_uint8_t)c);
while ((UART_GetStatus(uart->config->Instance) & UART_STATUS_TX_EMPTY) == 0)
{
}
return 1;
}
static int mm32_uart_getc(struct rt_serial_device *serial)
{
struct mm32_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct mm32_uart, serial);
if ((UART_GetStatus(uart->config->Instance) & UART_STATUS_RX_DONE) != 0)
{
return UART_GetData(uart->config->Instance);
}
return -1;
}
static void mm32_uart_isr(struct mm32_uart *uart)
{
RT_ASSERT(uart != RT_NULL);
if (((UART_GetInterruptStatus(uart->config->Instance) & UART_INT_RX_DONE) != 0) &&
((UART_GetStatus(uart->config->Instance) & UART_STATUS_RX_DONE) != 0))
{
rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_RX_IND);
UART_ClearInterruptStatus(uart->config->Instance, UART_INT_RX_DONE);
}
}
#define MM32_UART_IRQHandler(n) \
void UART##n##_IRQHandler(void) \
{ \
struct mm32_uart *uart; \
rt_interrupt_enter(); \
uart = mm32_uart_get_by_instance(UART##n); \
if (uart != RT_NULL) \
{ \
mm32_uart_isr(uart); \
} \
rt_interrupt_leave(); \
}
#ifdef BSP_USING_UART1
MM32_UART_IRQHandler(1)
#endif
#ifdef BSP_USING_UART2
MM32_UART_IRQHandler(2)
#endif
#ifdef BSP_USING_UART3
MM32_UART_IRQHandler(3)
#endif
#ifdef BSP_USING_UART4
MM32_UART_IRQHandler(4)
#endif
#ifdef BSP_USING_UART5
MM32_UART_IRQHandler(5)
#endif
#ifdef BSP_USING_UART6
MM32_UART_IRQHandler(6)
#endif
#ifdef BSP_USING_UART7
MM32_UART_IRQHandler(7)
#endif
#ifdef BSP_USING_UART8
MM32_UART_IRQHandler(8)
#endif
static const struct rt_uart_ops mm32_uart_ops =
{
.configure = mm32_uart_configure,
.control = mm32_uart_control,
.putc = mm32_uart_putc,
.getc = mm32_uart_getc,
};
int rt_hw_uart_init(void)
{
rt_size_t i;
rt_err_t result = RT_EOK;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
for (i = 0; i < sizeof(uart_obj) / sizeof(uart_obj[0]); i++)
{
uart_obj[i].config = &uart_config[i];
uart_obj[i].serial.ops = &mm32_uart_ops;
uart_obj[i].serial.config = config;
result = rt_hw_serial_register(&uart_obj[i].serial,
uart_obj[i].config->name,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
RT_NULL);
RT_ASSERT(result == RT_EOK);
}
return result;
}
#else
#if defined SOC_SERIES_MM32L0
#include <HAL_gpio.h>
#include <HAL_uart.h>
#include <HAL_rcc.h>
#include <HAL_misc.h>
#else
#include <hal_gpio.h>
#include <hal_uart.h>
#include <hal_rcc.h>
#include <hal_misc.h>
#endif
struct mm32_uart_config
{
const char *name;
UART_TypeDef *instance;
IRQn_Type irq_type;
};
struct mm32_uart
{
const struct mm32_uart_config *config;
struct rt_serial_device serial;
};
static struct mm32_uart_config uart_config[] =
{
#ifdef BSP_USING_UART1
{
.name = "uart1",
.instance = UART1,
.irq_type = UART1_IRQn,
},
#endif
#ifdef BSP_USING_UART2
{
.name = "uart2",
.instance = UART2,
.irq_type = UART2_IRQn,
},
#endif
#ifdef BSP_USING_UART3
{
.name = "uart3",
.instance = UART3,
.irq_type = UART3_IRQn,
},
#endif
};
static struct mm32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
static struct mm32_uart *mm32_uart_get_by_instance(UART_TypeDef *instance)
{
rt_size_t i;
for (i = 0; i < sizeof(uart_obj) / sizeof(uart_obj[0]); i++)
{
if (uart_obj[i].config->instance == instance)
{
return &uart_obj[i];
}
}
return RT_NULL;
}
#if defined(SOC_SERIES_MM32F526)
static void mm32_uart_msp_init(UART_TypeDef *instance)
{
mm32_msp_uart_init((void *)instance);
}
static void mm32_uart_init_struct(UART_InitTypeDef *init, const struct serial_configure *cfg)
{
init->BaudRate = cfg->baud_rate;
switch (cfg->data_bits)
{
case DATA_BITS_5:
init->WordLength = UART_WordLength_5b;
break;
case DATA_BITS_6:
init->WordLength = UART_WordLength_6b;
break;
case DATA_BITS_7:
init->WordLength = UART_WordLength_7b;
break;
case DATA_BITS_8:
default:
init->WordLength = UART_WordLength_8b;
break;
}
init->StopBits = (cfg->stop_bits == STOP_BITS_2) ? UART_StopBits_2 : UART_StopBits_1;
init->Parity = UART_Parity_No;
init->HWFlowControl = (cfg->flowcontrol == RT_SERIAL_FLOWCONTROL_CTSRTS) ?
UART_HWFlowControl_RTS_CTS : UART_HWFlowControl_None;
init->Mode = UART_Mode_Rx | UART_Mode_Tx;
}
#define MM32_UART_RX_IT UART_IT_RX
#define MM32_UART_TX_IT UART_IT_TX
#define MM32_UART_TX_DONE_FLAG UART_CSR_TXC_Msk
#else
static void mm32_uart_gpio_init(UART_TypeDef *instance)
{
GPIO_InitTypeDef gpio_init;
if (instance == UART1)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE);
#if defined(SOC_SERIES_MM32F327) || defined(SOC_SERIES_MM32L0)
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
#else
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
#endif
gpio_init.GPIO_Pin = GPIO_Pin_9;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
gpio_init.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &gpio_init);
gpio_init.GPIO_Pin = GPIO_Pin_10;
#if defined(SOC_SERIES_MM32F327)
gpio_init.GPIO_Mode = GPIO_Mode_IPU;
#else
gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
#endif
GPIO_Init(GPIOA, &gpio_init);
#if defined(SOC_SERIES_MM32F327)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
#elif defined(SOC_SERIES_MM32L0)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
#endif
}
else if (instance == UART2)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART2, ENABLE);
#if defined(SOC_SERIES_MM32F327) || defined(SOC_SERIES_MM32L0)
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
#else
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
#endif
gpio_init.GPIO_Pin = GPIO_Pin_2;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
gpio_init.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &gpio_init);
gpio_init.GPIO_Pin = GPIO_Pin_3;
gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &gpio_init);
#if defined(SOC_SERIES_MM32F327)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);
#elif defined(SOC_SERIES_MM32L0)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
#endif
}
#if defined(UART3) && (defined(SOC_SERIES_MM32F103) || defined(SOC_SERIES_MM32L3))
else if (instance == UART3)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART3, ENABLE);
#if defined(RCC_APB2Periph_GPIOB)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
#endif
gpio_init.GPIO_Pin = GPIO_Pin_10;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
gpio_init.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &gpio_init);
gpio_init.GPIO_Pin = GPIO_Pin_11;
gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &gpio_init);
}
#endif
}
static void mm32_uart_msp_init(UART_TypeDef *instance)
{
mm32_uart_gpio_init(instance);
}
static void mm32_uart_init_struct(UART_InitTypeDef *init, const struct serial_configure *cfg)
{
init->UART_BaudRate = cfg->baud_rate;
switch (cfg->data_bits)
{
case DATA_BITS_5:
init->UART_WordLength = UART_WordLength_5b;
break;
case DATA_BITS_6:
init->UART_WordLength = UART_WordLength_6b;
break;
case DATA_BITS_7:
init->UART_WordLength = UART_WordLength_7b;
break;
case DATA_BITS_8:
default:
init->UART_WordLength = UART_WordLength_8b;
break;
}
init->UART_StopBits = (cfg->stop_bits == STOP_BITS_2) ? UART_StopBits_2 : UART_StopBits_1;
init->UART_Parity = UART_Parity_No;
#ifdef UART_HardwareFlowControl_RTS_CTS
init->UART_HardwareFlowControl = (cfg->flowcontrol == RT_SERIAL_FLOWCONTROL_CTSRTS) ?
UART_HardwareFlowControl_RTS_CTS : UART_HardwareFlowControl_None;
#else
init->UART_HardwareFlowControl = UART_HardwareFlowControl_None;
#endif
init->UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
}
#define MM32_UART_RX_IT UART_IT_RXIEN
#define MM32_UART_TX_IT UART_IT_TXIEN
#define MM32_UART_TX_DONE_FLAG UART_CSR_TXC
#endif
static rt_err_t mm32_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
struct mm32_uart *uart;
UART_InitTypeDef init;
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL);
uart = rt_container_of(serial, struct mm32_uart, serial);
mm32_uart_msp_init(uart->config->instance);
mm32_uart_init_struct(&init, cfg);
UART_Init(uart->config->instance, &init);
UART_Cmd(uart->config->instance, ENABLE);
return RT_EOK;
}
static rt_err_t mm32_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
{
struct mm32_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct mm32_uart, serial);
switch (cmd)
{
case RT_DEVICE_CTRL_CLR_INT:
UART_ITConfig(uart->config->instance, MM32_UART_RX_IT, DISABLE);
NVIC_DisableIRQ(uart->config->irq_type);
break;
case RT_DEVICE_CTRL_SET_INT:
UART_ITConfig(uart->config->instance, MM32_UART_RX_IT, ENABLE);
NVIC_EnableIRQ(uart->config->irq_type);
break;
default:
break;
}
return RT_EOK;
}
static int mm32_uart_putc(struct rt_serial_device *serial, char c)
{
struct mm32_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct mm32_uart, serial);
while ((uart->config->instance->CSR & MM32_UART_TX_DONE_FLAG) == 0)
{
}
uart->config->instance->TDR = (rt_uint8_t)c;
return 1;
}
static int mm32_uart_getc(struct rt_serial_device *serial)
{
struct mm32_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct mm32_uart, serial);
if ((uart->config->instance->CSR & UART_FLAG_RXAVL) != 0)
{
return uart->config->instance->RDR & 0xff;
}
return -1;
}
static void mm32_uart_isr(struct mm32_uart *uart)
{
RT_ASSERT(uart != RT_NULL);
if (UART_GetITStatus(uart->config->instance, MM32_UART_RX_IT) != RESET)
{
UART_ClearITPendingBit(uart->config->instance, MM32_UART_RX_IT);
rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_RX_IND);
}
if (UART_GetITStatus(uart->config->instance, MM32_UART_TX_IT) != RESET)
{
UART_ClearITPendingBit(uart->config->instance, MM32_UART_TX_IT);
}
}
#define MM32_UART_IRQHandler(n) \
void UART##n##_IRQHandler(void) \
{ \
struct mm32_uart *uart; \
rt_interrupt_enter(); \
uart = mm32_uart_get_by_instance(UART##n); \
if (uart != RT_NULL) \
{ \
mm32_uart_isr(uart); \
} \
rt_interrupt_leave(); \
}
#ifdef BSP_USING_UART1
MM32_UART_IRQHandler(1)
#endif
#ifdef BSP_USING_UART2
MM32_UART_IRQHandler(2)
#endif
#ifdef BSP_USING_UART3
MM32_UART_IRQHandler(3)
#endif
static const struct rt_uart_ops mm32_uart_ops =
{
.configure = mm32_uart_configure,
.control = mm32_uart_control,
.putc = mm32_uart_putc,
.getc = mm32_uart_getc,
};
int rt_hw_uart_init(void)
{
rt_size_t i;
rt_err_t result = RT_EOK;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
for (i = 0; i < sizeof(uart_obj) / sizeof(uart_obj[0]); i++)
{
uart_obj[i].config = &uart_config[i];
uart_obj[i].serial.ops = &mm32_uart_ops;
uart_obj[i].serial.config = config;
result = rt_hw_serial_register(&uart_obj[i].serial,
uart_obj[i].config->name,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
RT_NULL);
RT_ASSERT(result == RT_EOK);
}
return result;
}
#if !defined(SOC_SERIES_MM32F103) && !defined(SOC_SERIES_MM32F526)
INIT_BOARD_EXPORT(rt_hw_uart_init);
#endif
#endif
#endif /* RT_USING_SERIAL */
|