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
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-03-02 FMD-AE first version
*/
#include "board.h"
#include "drv_usart.h"
#include "drv_config.h"
#ifdef RT_USING_SERIAL
//#define DRV_DEBUG
#define LOG_TAG "drv.usart"
#include <drv_log.h>
#if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2)
#error "Please define at least one BSP_USING_UARTx"
/* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
#endif
enum
{
#ifdef BSP_USING_UART1
UART1_INDEX,
#endif
#ifdef BSP_USING_UART2
UART2_INDEX,
#endif
};
static struct ft32_uart_config uart_config[] =
{
#ifdef BSP_USING_UART1
UART1_CONFIG,
#endif
#ifdef BSP_USING_UART2
UART2_CONFIG,
#endif
};
static struct ft32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
void UART_MspInit(USART_TypeDef *USARTx)
{
GPIO_InitTypeDef GPIO_InitStruct;
if (USARTx == USART1)
{
#if defined(SOC_SERIES_FT32F0)
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
#endif
#if defined(SOC_SERIES_FT32F4)
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
#endif
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/*GPIO INIT*/
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);
#if defined(SOC_SERIES_FT32F0)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
#endif
#if defined(SOC_SERIES_FT32F4)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
#endif
/* USART1 interrupt Init */
NVIC_SetPriority(USART1_IRQn, 5);
NVIC_EnableIRQ(USART1_IRQn);
}
else if (USARTx == USART2)
{
#if defined(SOC_SERIES_FT32F0)
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
#endif
#if defined(SOC_SERIES_FT32F4)
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART2, ENABLE);
#endif
/*GPIO INIT*/
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);
#if defined(SOC_SERIES_FT32F0)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
#endif
#if defined(SOC_SERIES_FT32F4)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);
#endif
/* USART2 interrupt Init */
NVIC_SetPriority(USART2_IRQn, 5);
NVIC_EnableIRQ(USART2_IRQn);
}
}
static rt_err_t ft32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
struct ft32_uart *uart;
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL);
uart = rt_container_of(serial, struct ft32_uart, serial);
uart->Init.USART_BaudRate = cfg->baud_rate;
#if defined(SOC_SERIES_FT32F0)
uart->Init.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
#endif
#if defined(SOC_SERIES_FT32F4)
uart->Init.USART_Mode = USART_MODE_TX_RX;
#endif
switch (cfg->flowcontrol)
{
case RT_SERIAL_FLOWCONTROL_NONE:
uart->Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
break;
case RT_SERIAL_FLOWCONTROL_CTSRTS:
#if defined(SOC_SERIES_FT32F0)
uart->Init.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
#endif
#if defined(SOC_SERIES_FT32F4)
uart->Init.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_DTR;
#endif
break;
default:
uart->Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
break;
}
switch (cfg->data_bits)
{
#if defined(SOC_SERIES_FT32F0)
case DATA_BITS_8:
if (cfg->parity == PARITY_ODD || cfg->parity == PARITY_EVEN)
uart->Init.USART_WordLength = USART_WordLength_9b;
else
uart->Init.USART_WordLength = USART_WordLength_8b;
break;
case DATA_BITS_9:
uart->Init.USART_WordLength = USART_WordLength_9b;
break;
default:
uart->Init.USART_WordLength = USART_WordLength_8b;
break;
#endif
#if defined(SOC_SERIES_FT32F4)
case DATA_BITS_9:
uart->Init.USART_WordLength = USART_CHAR_LENGTH9_ENABLE;
break;
case DATA_BITS_8:
uart->Init.USART_WordLength = USART_CHAR_LENGTH_8BIT;
break;
case DATA_BITS_7:
uart->Init.USART_WordLength = USART_CHAR_LENGTH_7BIT;
break;
case DATA_BITS_6:
uart->Init.USART_WordLength = USART_CHAR_LENGTH_6BIT;
break;
case DATA_BITS_5:
uart->Init.USART_WordLength = USART_CHAR_LENGTH_5BIT;
break;
default:
uart->Init.USART_WordLength = USART_CHAR_LENGTH_8BIT;
break;
#endif
}
switch (cfg->stop_bits)
{
#if defined(SOC_SERIES_FT32F0)
case STOP_BITS_1:
uart->Init.USART_StopBits = USART_StopBits_1;
break;
case STOP_BITS_2:
uart->Init.USART_StopBits = USART_StopBits_2;
break;
default:
uart->Init.USART_StopBits = USART_StopBits_1;
break;
#endif
#if defined(SOC_SERIES_FT32F4)
case STOP_BITS_1:
uart->Init.USART_StopBits = USART_STOPBITS_1;
break;
case STOP_BITS_2:
uart->Init.USART_StopBits = USART_STOPBITS_2;
break;
default:
uart->Init.USART_StopBits = USART_STOPBITS_1;
break;
#endif
}
switch (cfg->parity)
{
#if defined(SOC_SERIES_FT32F0)
case PARITY_NONE:
uart->Init.USART_Parity = USART_Parity_No;
break;
case PARITY_ODD:
uart->Init.USART_Parity = USART_Parity_Odd;
break;
case PARITY_EVEN:
uart->Init.USART_Parity = USART_Parity_Even;
break;
default:
uart->Init.USART_Parity = USART_Parity_No;
break;
#endif
#if defined(SOC_SERIES_FT32F4)
case PARITY_NONE:
uart->Init.USART_Parity = USART_PARITY_NONE;
break;
case PARITY_ODD:
uart->Init.USART_Parity = USART_PARITY_ODD;
break;
case PARITY_EVEN:
uart->Init.USART_Parity = USART_PARITY_EVEN;
break;
default:
uart->Init.USART_Parity = USART_PARITY_NONE;
break;
#endif
}
UART_MspInit(uart->config->Instance);
USART_Init(uart->config->Instance, &(uart->Init));
USART_Cmd(uart->config->Instance, ENABLE);
return RT_EOK;
}
static rt_err_t ft32_control(struct rt_serial_device *serial, int cmd, void *arg)
{
struct ft32_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct ft32_uart, serial);
switch (cmd)
{
/* disable interrupt */
case RT_DEVICE_CTRL_CLR_INT:
/* disable rx irq */
NVIC_DisableIRQ(uart->config->irq_type);
/* disable interrupt */
#if defined(SOC_SERIES_FT32F0)
/* enable interrupt */
USART_ITConfig(uart->config->Instance, USART_IT_RXNE, DISABLE);
break;
#endif
#if defined(SOC_SERIES_FT32F4)
/* enable interrupt */
USART_ITConfig(uart->config->Instance, USART_IT_RXRDY, DISABLE);
break;
#endif
break;
/* enable interrupt */
case RT_DEVICE_CTRL_SET_INT:
/* enable rx irq */
NVIC_SetPriority(uart->config->irq_type, 1);
NVIC_EnableIRQ(uart->config->irq_type);
#if defined(SOC_SERIES_FT32F0)
/* enable interrupt */
USART_ITConfig(uart->config->Instance, USART_IT_RXNE, ENABLE);
break;
#endif
#if defined(SOC_SERIES_FT32F4)
/* enable interrupt */
USART_ITConfig(uart->config->Instance, USART_IT_RXRDY, ENABLE);
break;
#endif
break;
case RT_DEVICE_CTRL_CLOSE:
USART_DeInit(uart->config->Instance);
break;
}
return RT_EOK;
}
#if defined(SOC_SERIES_FT32F0)
rt_uint32_t ft32_uart_get_mask(rt_uint32_t word_length, rt_uint32_t parity)
{
rt_uint32_t mask;
if (word_length == USART_WordLength_8b)
{
if (parity == USART_Parity_No)
{
mask = 0x00FFU ;
}
else
{
mask = 0x007FU ;
}
}
else if (word_length == USART_WordLength_9b)
{
if (parity == USART_Parity_No)
{
mask = 0x01FFU ;
}
else
{
mask = 0x00FFU ;
}
}
else if (word_length == USART_WordLength_7b)
{
if (parity == USART_Parity_No)
{
mask = 0x007FU ;
}
else
{
mask = 0x003FU ;
}
}
else
{
mask = 0x0000U;
}
return mask;
}
#endif
static int ft32_putc(struct rt_serial_device *serial, char c)
{
struct ft32_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct ft32_uart, serial);
#if defined(SOC_SERIES_FT32F0)
UART_INSTANCE_CLEAR_FUNCTION(uart->config->Instance, USART_FLAG_TC);
uart->config->Instance->TDR = c;
#elif defined(SOC_SERIES_FT32F4)
USART_Transmit(uart->config->Instance, c);
#else
uart->config->Instance->DR = c;
#endif
#if defined(SOC_SERIES_FT32F0)
while (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_TC) == RESET);
#endif
#if defined(SOC_SERIES_FT32F4)
while (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_TXRDY) == RESET);
#endif
return 1;
}
static int ft32_getc(struct rt_serial_device *serial)
{
int ch;
struct ft32_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct ft32_uart, serial);
ch = -1;
#if defined(SOC_SERIES_FT32F0)
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_RXNE) != RESET)
#endif
#if defined(SOC_SERIES_FT32F4)
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_RXRDY) != RESET)
#endif
{
#if defined(SOC_SERIES_FT32F0)
ch = uart->config->Instance->RDR & ft32_uart_get_mask(uart->Init.USART_WordLength, uart->Init.USART_Parity);
#elif defined(SOC_SERIES_FT32F4)
ch = USART_Receive(uart->config->Instance);
#else
ch = uart->config->Instance->DR & ft32_uart_get_mask(uart->Init.USART_WordLength, uart->Init.USART_Parity);
#endif
}
return ch;
}
static rt_ssize_t ft32_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
{
RT_ASSERT(serial != RT_NULL);
RT_ASSERT(buf != RT_NULL);
if (size == 0)
{
return 0;
}
if (RT_SERIAL_DMA_TX == direction)
{
return size;
}
return 0;
}
/**
* Uart common interrupt process. This need add to uart ISR.
*
* @param serial serial device
*/
static void uart_isr(struct rt_serial_device *serial)
{
struct ft32_uart *uart;
RT_ASSERT(serial != RT_NULL);
uart = rt_container_of(serial, struct ft32_uart, serial);
#if defined(SOC_SERIES_FT32F0)
/* UART in mode Receiver -------------------------------------------------*/
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_RXNE) != RESET)
{
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
}
#endif
#if defined(SOC_SERIES_FT32F4)
/* UART in mode Receiver -------------------------------------------------*/
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_RXRDY) != RESET)
{
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
}
#endif
#if defined (SOC_SERIES_FT32F0)
else
{
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_ORE) != RESET)
{
USART_ClearFlag(uart->config->Instance, USART_FLAG_ORE);
}
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_NE) != RESET)
{
USART_ClearFlag(uart->config->Instance, USART_FLAG_NE);
}
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_FE) != RESET)
{
USART_ClearFlag(uart->config->Instance, USART_FLAG_FE);
}
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_PE) != RESET)
{
USART_ClearFlag(uart->config->Instance, USART_FLAG_PE);
}
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_CTS) != RESET)
{
UART_INSTANCE_CLEAR_FUNCTION(uart->config->Instance, USART_FLAG_CTS);
}
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_TXE) != RESET)
{
UART_INSTANCE_CLEAR_FUNCTION(uart->config->Instance, USART_FLAG_TXE);
}
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_TC) != RESET)
{
UART_INSTANCE_CLEAR_FUNCTION(uart->config->Instance, USART_FLAG_TC);
}
if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_RXNE) != RESET)
{
UART_INSTANCE_CLEAR_FUNCTION(uart->config->Instance, USART_FLAG_RXNE);
}
}
#endif
#if defined (SOC_SERIES_FT32F4)
else
{
}
#endif
}
#if defined(SOC_SERIES_FT32F0)
#if defined(BSP_USING_UART1)
void USART1_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
uart_isr(&(uart_obj[UART1_INDEX].serial));
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_UART1 */
#if defined(BSP_USING_UART2)
void USART2_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
uart_isr(&(uart_obj[UART2_INDEX].serial));
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_UART2 */
#endif
#if defined(SOC_SERIES_FT32F4)
#if defined(BSP_USING_UART1)
void USART1_Handler(void)
{
/* enter interrupt */
rt_interrupt_enter();
uart_isr(&(uart_obj[UART1_INDEX].serial));
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_UART1 */
#if defined(BSP_USING_UART2)
void USART2_Handler(void)
{
/* enter interrupt */
rt_interrupt_enter();
uart_isr(&(uart_obj[UART2_INDEX].serial));
/* leave interrupt */
rt_interrupt_leave();
}
#endif /* BSP_USING_UART2 */
#endif
static void ft32_uart_get_dma_config(void)
{
#ifdef BSP_USING_UART1
uart_obj[UART1_INDEX].uart_dma_flag = 0;
#endif
#ifdef BSP_USING_UART2
uart_obj[UART2_INDEX].uart_dma_flag = 0;
#endif
}
static const struct rt_uart_ops ft32_uart_ops =
{
.configure = ft32_configure,
.control = ft32_control,
.putc = ft32_putc,
.getc = ft32_getc,
.dma_transmit = ft32_dma_transmit
};
int rt_hw_usart_init(void)
{
rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ft32_uart);
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
rt_err_t result = 0;
ft32_uart_get_dma_config();
for (int i = 0; i < obj_num; i++)
{
/* init UART object */
uart_obj[i].config = &uart_config[i];
uart_obj[i].serial.ops = &ft32_uart_ops;
uart_obj[i].serial.config = config;
/* register UART device */
result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
RT_DEVICE_FLAG_RDWR
| RT_DEVICE_FLAG_INT_RX
| RT_DEVICE_FLAG_INT_TX
| uart_obj[i].uart_dma_flag
, NULL);
RT_ASSERT(result == RT_EOK);
}
return result;
}
#endif /* RT_USING_SERIAL */
|