summaryrefslogtreecommitdiff
path: root/bsp/apm32/libraries/APM32F10x_Library/APM32F10x_StdPeriphDriver/src/apm32f10x_usart.c
blob: 4dcd768dbfdee12ecefb92efd3d122794a4f5428 (plain)
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/*!
 * @file        apm32f10x_usart.c
 *
 * @brief       This file provides all the USART firmware functions
 *
 * @version     V1.0.4
 *
 * @date        2022-12-01
 *
 * @attention
 *
 *  Copyright (C) 2020-2022 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

#include "apm32f10x_usart.h"
#include "apm32f10x_rcm.h"

/** @addtogroup APM32F10x_StdPeriphDriver
  @{
*/

/** @addtogroup USART_Driver USART Driver
  * @brief USART driver modules
  @{
*/

/** @defgroup USART_Functions Functions
  @{
*/

/*!
 * @brief     Reset usart peripheral registers to their default reset values
 *
 * @param     usart:   Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_Reset(USART_T* usart)
{
    if (USART1 == usart)
    {
        RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_USART1);
        RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_USART1);
    }
    else if (USART2 == usart)
    {
        RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_USART2);
        RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_USART2);
    }
    else if (USART3 == usart)
    {
        RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_USART3);
        RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_USART3);
    }
    else if (UART4 == usart)
    {
        RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_UART4);
        RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_UART4);
    }
    else if (UART5 == usart)
    {
        RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_UART5);
        RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_UART5);
    }
}

/*!
 * @brief     Configures the USART peripheral according to the specified parameters in the usartConfig
 *
 * @param     uart:         Select the USART or the UART peripheral
 *
 * @param     usartConfig:  pointer to a USART_Config_T structure
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_Config(USART_T* uart, USART_Config_T* usartConfig)
{
    uint32_t temp, fCLK, intDiv, fractionalDiv;

    temp = uart->CTRL1;
    temp &= 0xE9F3;
    temp |= (uint32_t)usartConfig->mode | \
            (uint32_t)usartConfig->parity | \
            (uint32_t)usartConfig->wordLength;
    uart->CTRL1 = temp;

    temp = uart->CTRL2;
    temp &= 0xCFFF;
    temp |= usartConfig->stopBits;
    uart->CTRL2 = temp;

    temp = uart->CTRL3;
    temp &= 0xFCFF;
    temp |= (uint32_t)usartConfig->hardwareFlow;
    uart->CTRL3 = temp;

    if (uart == USART1)
    {
        RCM_ReadPCLKFreq(NULL, &fCLK);
    }
    else
    {
        RCM_ReadPCLKFreq(&fCLK, NULL);
    }

    intDiv = ((25 * fCLK) / (4 * (usartConfig->baudRate)));
    temp = (intDiv / 100) << 4;
    fractionalDiv = intDiv - (100 * (temp >> 4));
    temp |= ((((fractionalDiv * 16) + 50) / 100)) & ((uint8_t)0x0F);

    uart->BR = temp;
}

/*!
 * @brief     Fills each USART_InitStruct member with its default value
 *
 * @param     usartConfig:   pointer to a USART_Config_T structure which will be initialized
 *
 * @retval    None
 */
void USART_ConfigStructInit(USART_Config_T* usartConfig)
{
    usartConfig->baudRate = 9600;
    usartConfig->wordLength = USART_WORD_LEN_8B;
    usartConfig->stopBits = USART_STOP_BIT_1;
    usartConfig->parity = USART_PARITY_NONE ;
    usartConfig->mode = USART_MODE_TX_RX;
    usartConfig->hardwareFlow = USART_HARDWARE_FLOW_NONE;
}

/*!
 * @brief     Configures communication clock
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     clockConfig: Pointer to a USART_clockConfig_T structure
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3
 */
void USART_ConfigClock(USART_T* usart, USART_ClockConfig_T* clockConfig)
{
    usart->CTRL2_B.CLKEN   = clockConfig->clock;
    usart->CTRL2_B.CPHA    = clockConfig->phase;
    usart->CTRL2_B.CPOL    = clockConfig->polarity;
    usart->CTRL2_B.LBCPOEN = clockConfig->lastBit;
}

/*!
 * @brief     Fills each clockConfig member with its default value
 *
 * @param     clockConfig: Pointer to a USART_clockConfig_T structure
 *
 * @retval    None
 *
 */
void USART_ConfigClockStructInit(USART_ClockConfig_T* clockConfig)
{
    clockConfig->clock     = USART_CLKEN_DISABLE;
    clockConfig->phase     = USART_CLKPHA_1EDGE;
    clockConfig->polarity  = USART_CLKPOL_LOW;
    clockConfig->lastBit   = USART_LBCP_DISABLE;
}

/*!
 * @brief     Enable the specified USART peripheral
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_Enable(USART_T* usart)
{
    usart->CTRL1_B.UEN = BIT_SET;
}

/*!
 * @brief     Disable the specified USART peripheral
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_Disable(USART_T* usart)
{
    usart->CTRL1_B.UEN = BIT_RESET;
}

/*!
 * @brief     Enable the USART DMA interface
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     dmaReq: Specifies the DMA request
 *                     This parameter can be one of the following values:
 *                     @arg USART_DMA_TX:     USART DMA receive request
 *                     @arg USART_DMA_RX:     USART DMA transmit request
 *                     @arg USART_DMA_TX_RX:  USART DMA transmit/receive request
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_EnableDMA(USART_T* usart, USART_DMA_T dmaReq)
{
    usart->CTRL3 |= dmaReq;
}

/*!
 * @brief     Disable the USART DMA interface
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     dmaReq: Specifies the DMA request
 *                     This parameter can be one of the following values:
 *                     @arg USART_DMA_TX:     USART DMA receive request
 *                     @arg USART_DMA_RX:     USART DMA transmit request
 *                     @arg USART_DMA_TX_RX:  USART DMA transmit/receive request
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_DisableDMA(USART_T* usart, USART_DMA_T dmaReq)
{
    usart->CTRL3 &= (uint32_t)~dmaReq;
}

/*!
 * @brief     Configures the address of the USART node
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     address: Indicates the address of the USART node
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_Address(USART_T* usart, uint8_t address)
{
    usart->CTRL2_B.ADDR = address;
}

/*!
 * @brief     Selects the USART WakeUp method.
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     wakeup: Specifies the selected USART auto baud rate method
 *                   This parameter can be one of the following values:
 *                   @arg USART_WAKEUP_IDLE_LINE:    WakeUp by an idle line detection
 *                   @arg USART_WAKEUP_ADDRESS_MARK: WakeUp by an address mark
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_ConfigWakeUp(USART_T* usart, USART_WAKEUP_T wakeup)
{
    usart->CTRL1_B.WUPMCFG = wakeup;
}

/*!
 * @brief     Enable USART Receiver in mute mode
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_EnableMuteMode(USART_T* usart)
{
    usart->CTRL1_B.RXMUTEEN = BIT_SET;
}

/*!
 * @brief     Disable USART Receiver in active mode
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_DisableMuteMode(USART_T* usart)
{
    usart->CTRL1_B.RXMUTEEN = BIT_RESET;
}

/*!
 * @brief     Set the USART LIN Break detection length
 *
 * @param     usart:   Select the USART or the UART peripheral
 *
 * @param     length:  Specifies the LIN break detection length
 *                     This parameter can be one of the following values:
 *                     @arg USART_LBDL_10B: 10-bit break detection
 *                     @arg USART_LBDL_10B: 11-bit break detection
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_ConfigLINBreakDetectLength(USART_T* usart, USART_LBDL_T length)
{
    usart->CTRL2_B.LBDLCFG = length;
}

/*!
 * @brief     Enable the USART LIN MODE
 *
 * @param     usart:   Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_EnableLIN(USART_T* usart)
{
    usart->CTRL2_B.LINMEN = BIT_SET;
}

/*!
 * @brief     Disable the USART LIN MODE
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_DisableLIN(USART_T* usart)
{
    usart->CTRL2_B.LINMEN = BIT_RESET;
}

/*!
 * @brief     Transmitter enable
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_EnableTx(USART_T* usart)
{
    usart->CTRL1_B.TXEN = BIT_SET;
}

/*!
 * @brief     Transmitter disable
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_DisableTx(USART_T* usart)
{
    usart->CTRL1_B.TXEN = BIT_RESET;
}

/*!
 * @brief     Receiver enable
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_EnableRx(USART_T* usart)
{
    usart->CTRL1_B.RXEN = BIT_SET;
}

/*!
 * @brief     Receiver disable
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_DisableRx(USART_T* usart)
{
    usart->CTRL1_B.RXEN = BIT_RESET;
}

/*!
 * @brief     Transmits single data
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     data: the data to transmit
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_TxData(USART_T* usart, uint16_t data)
{
    usart->DATA_B.DATA = data;
}

/*!
 * @brief     Return the most recent received data
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
uint16_t USART_RxData(USART_T* usart)
{
    return (uint16_t)(usart->DATA_B.DATA);
}

/*!
 * @brief     Transmits break characters
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_TxBreak(USART_T* usart)
{
    usart->CTRL1_B.TXBF = BIT_SET;
}

/*!
 * @brief     Set the specified USART guard time
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     guardTime: Specifies the guard time
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3
 */
void USART_ConfigGuardTime(USART_T* usart, uint8_t guardTime)
{
    usart->GTPSC_B.GRDT = guardTime;
}

/*!
 * @brief     Set the system clock divider number
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     div: specifies the divider number
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3
 */
void USART_ConfigPrescaler(USART_T* usart, uint8_t div)
{
    usart->GTPSC_B.PSC = div;
}

/*!
 * @brief     Enable the USART Smart Card mode
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The Smart Card mode is not available for UART4 and UART5
 */
void USART_EnableSmartCard(USART_T* usart)
{
    usart->CTRL3_B.SCEN = BIT_SET;
}

/*!
 * @brief     Disable the USART Smart Card mode
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The Smart Card mode is not available for UART4 and UART5
 */
void USART_DisableSmartCard(USART_T* usart)
{
    usart->CTRL3_B.SCEN = BIT_RESET;
}

/*!
 * @brief     Enable NACK transmission
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The Smart Card mode is not available for UART4 and UART5
 */
void USART_EnableSmartCardNACK(USART_T* usart)
{
    usart->CTRL3_B.SCNACKEN = BIT_SET;
}

/*!
 * @brief     Disable NACK transmission
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The Smart Card mode is not available for UART4 and UART5
 */
void USART_DisableSmartCardNACK(USART_T* usart)
{
    usart->CTRL3_B.SCNACKEN = BIT_RESET;
}

/*!
 * @brief     Enable USART Half Duplex communication
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_EnableHalfDuplex(USART_T* usart)
{
    usart->CTRL3_B.HDEN = BIT_SET;
}

/*!
 * @brief     Disable USART Half Duplex communication
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_DisableHalfDuplex(USART_T* usart)
{
    usart->CTRL3_B.HDEN = BIT_RESET;
}

/*!
 * @brief     Configures the USART's IrDA interface
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     IrDAMode: Specifies the IrDA mode
 *                     This parameter can be one of the following values:
 *                     @arg USART_IRDALP_NORMAL:   Normal
 *                     @arg USART_IRDALP_LOWPOWER: Low-Power
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_ConfigIrDA(USART_T* usart, USART_IRDALP_T IrDAMode)
{
    usart->CTRL3_B.IRLPEN = IrDAMode;
}

/*!
 * @brief     Enable the USART's IrDA interface
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_EnableIrDA(USART_T* usart)
{
    usart->CTRL3_B.IREN = BIT_SET;
}

/*!
 * @brief     Disable the USART's IrDA interface
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_DisableIrDA(USART_T* usart)
{
    usart->CTRL3_B.IREN = BIT_RESET;
}

/*!
 * @brief     Enable the specified USART interrupts
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     interrupt: Specifies the USART interrupts sources
 *                     The parameter can be one of following values:
 *                     @arg USART_INT_PE:    Parity error interrupt
 *                     @arg USART_INT_TXBE:  Tansmit data buffer empty interrupt
 *                     @arg USART_INT_TXC:   Transmission complete interrupt
 *                     @arg USART_INT_RXBNE: Receive data buffer not empty interrupt
 *                     @arg USART_INT_IDLE:  Idle line detection interrupt
 *                     @arg USART_INT_LBD:   LIN break detection interrupt
 *                     @arg USART_INT_CTS:   CTS change interrupt
 *                     @arg USART_INT_ERR:   Error interrupt(Frame error, noise error, overrun error)
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_EnableInterrupt(USART_T* usart, USART_INT_T interrupt)
{
    uint32_t temp;

    temp = (uint32_t)(interrupt & 0xffff);

    if (interrupt & 0x10000)
    {
        usart->CTRL1 |= temp;
    }

    if (interrupt & 0x20000)
    {
        usart->CTRL2 |= temp;
    }

    if (interrupt & 0x40000)
    {
        usart->CTRL3 |= temp;
    }
}

/*!
 * @brief     Disable the specified USART interrupts
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     interrupt: Specifies the USART interrupts sources
 *                     The parameter can be one of following values:
 *                     @arg USART_INT_PE:    Parity error interrupt
 *                     @arg USART_INT_TXBE:  Tansmit data buffer empty interrupt
 *                     @arg USART_INT_TXC:   Transmission complete interrupt
 *                     @arg USART_INT_RXBNE: Receive data buffer not empty interrupt
 *                     @arg USART_INT_IDLE:  Idle line detection interrupt
 *                     @arg USART_INT_LBD:   LIN break detection interrupt
 *                     @arg USART_INT_CTS:   CTS change interrupt
 *                     @arg USART_INT_ERR:   Error interrupt(Frame error, noise error, overrun error)
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_DisableInterrupt(USART_T* usart, USART_INT_T interrupt)
{
    uint32_t temp;

    temp = (uint32_t)~(interrupt & 0xffff);

    if (interrupt & 0x10000)
    {
        usart->CTRL1 &= temp;
    }

    if (interrupt & 0x20000)
    {
        usart->CTRL2 &= temp;
    }

    if (interrupt & 0x40000)
    {
        usart->CTRL3 &= temp;
    }
}

/*!
 * @brief     Read the specified USART flag
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     flag: Specifies the flag to check
 *                     The parameter can be one of following values:
 *                     @arg USART_FLAG_CTS:    CTS Change flag (not available for UART4 and UART5)
 *                     @arg USART_FLAG_LBD:    LIN Break detection flag
 *                     @arg USART_FLAG_TXBE:   Transmit data buffer empty flag
 *                     @arg USART_FLAG_TXC:    Transmission Complete flag
 *                     @arg USART_FLAG_RXBNE:  Receive data buffer not empty flag
 *                     @arg USART_FLAG_IDLE:   Idle Line detection flag
 *                     @arg USART_FLAG_OVRE:   OverRun Error flag
 *                     @arg USART_FLAG_NE:     Noise Error flag
 *                     @arg USART_FLAG_FE:     Framing Error flag
 *                     @arg USART_FLAG_PE:     Parity Error flag
 *
 * @retval    The new state of flag (SET or RESET)
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
uint8_t USART_ReadStatusFlag(USART_T* usart, USART_FLAG_T flag)
{
    return (usart->STS & flag) ? SET : RESET;
}

/*!
 * @brief     Clear the USARTx's pending flags
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     flag: Specifies the flag to clear
 *                     The parameter can be one of following values:
 *                     @arg USART_FLAG_CTS:    CTS Change flag (not available for UART4 and UART5)
 *                     @arg USART_FLAG_LBD:    LIN Break detection flag
 *                     @arg USART_FLAG_TXC:    Transmission Complete flag
 *                     @arg USART_FLAG_RXBNE:  Receive data buffer not empty flag
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_ClearStatusFlag(USART_T* usart, USART_FLAG_T flag)
{
    usart->STS &= (uint32_t)~flag;
}

/*!
 * @brief     Read the specified USART interrupt flag
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     flag:  Specifies the USART interrupt source to check
 *                     The parameter can be one of following values:
 *                     @arg USART_INT_TXBE:  Tansmit data buffer empty interrupt
 *                     @arg USART_INT_TXC:   Transmission complete interrupt
 *                     @arg USART_INT_RXBNE: Receive data buffer not empty interrupt
 *                     @arg USART_INT_IDLE:  Idle line detection interrupt
 *                     @arg USART_INT_LBD:   LIN break detection interrupt
 *                     @arg USART_INT_CTS:   CTS change interrupt
 *                     @arg USART_INT_OVRE:  OverRun Error interruptpt
 *                     @arg USART_INT_NE:    Noise Error interrupt
 *                     @arg USART_INT_FE:    Framing Error interrupt
 *                     @arg USART_INT_PE:    Parity error interrupt
 *
 * @retval    The new state of flag (SET or RESET)
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
uint8_t USART_ReadIntFlag(USART_T* usart, USART_INT_T flag)
{
    uint32_t itFlag, srFlag;

    if (flag & 0x10000)
    {
        itFlag = usart->CTRL1 & flag & 0xffff;
    }
    else if (flag & 0x20000)
    {
        itFlag = usart->CTRL2 & flag & 0xffff;
    }
    else
    {
        itFlag = usart->CTRL3 & flag & 0xffff;
    }

    srFlag = flag >> 24;
    srFlag = (uint32_t)(1 << srFlag);
    srFlag = usart->STS & srFlag;

    if (srFlag && itFlag)
    {
        return SET;
    }

    return RESET;
}

/*!
 * @brief     Clear the USART interrupt pending bits
 *
 * @param     usart: Select the USART or the UART peripheral
 *
 * @param     flag: Specifies the interrupt pending bit to clear
 *                     The parameter can be one of following values:
 *                     @arg USART_INT_RXBNE: Receive data buffer not empty interrupt
 *                     @arg USART_INT_TXC:   Transmission complete interrupt
 *                     @arg USART_INT_LBD:   LIN break detection interrupt
 *                     @arg USART_INT_CTS:   CTS change interrupt
 *
 * @retval    None
 *
 * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
 */
void USART_ClearIntFlag(USART_T* usart, USART_INT_T flag)
{
    uint32_t srFlag;

    srFlag = flag >> 24;
    srFlag = (uint32_t)(1 << srFlag);

    usart->STS &= (uint32_t)~srFlag;
}

/**@} end of group USART_Functions */
/**@} end of group USART_Driver */
/**@} end of group APM32F10x_StdPeriphDriver */