1 /*!
2  * @file       apm32e10x_usart.c
3  *
4  * @brief      This file provides all the USART firmware functions
5  *
6  * @version     V1.0.2
7  *
8  * @date        2022-12-31
9  *
10  * @attention
11  *
12  *  Copyright (C) 2021-2023 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be useful and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 #include "apm32e10x_usart.h"
27 #include "apm32e10x_rcm.h"
28 
29 /** @addtogroup APM32E10x_StdPeriphDriver
30   @{
31 */
32 
33 /** @addtogroup USART_Driver
34   * @brief USART driver modules
35   @{
36 */
37 
38 /** @defgroup USART_Functions Functions
39   @{
40 */
41 
42 /*!
43  * @brief     Reset usart peripheral registers to their default reset values
44  *
45  * @param     usart:   Select the USART or the UART peripheral
46  *
47  * @retval    None
48  *
49  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
50  */
USART_Reset(USART_T * usart)51 void USART_Reset(USART_T* usart)
52 {
53     if (USART1 == usart)
54     {
55         RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_USART1);
56         RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_USART1);
57     }
58     else if (USART2 == usart)
59     {
60         RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_USART2);
61         RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_USART2);
62     }
63     else if (USART3 == usart)
64     {
65         RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_USART3);
66         RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_USART3);
67     }
68     else if (UART4 == usart)
69     {
70         RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_UART4);
71         RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_UART4);
72     }
73     else if (UART5 == usart)
74     {
75         RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_UART5);
76         RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_UART5);
77     }
78 }
79 
80 /*!
81  * @brief     Config the USART peripheral according to the specified parameters in the usartConfig
82  *
83  * @param     uart:         Select the USART or the UART peripheral
84  *
85  * @param     usartConfig:  pointer to a USART_Config_T structure
86  *
87  * @retval    None
88  *
89  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
90  */
USART_Config(USART_T * uart,USART_Config_T * usartConfig)91 void USART_Config(USART_T* uart, USART_Config_T* usartConfig)
92 {
93     uint32_t temp, fCLK, intDiv, fractionalDiv;
94 
95     temp = uart->CTRL1;
96     temp &= 0xE9F3;
97     temp |= (uint32_t)usartConfig->mode | \
98             (uint32_t)usartConfig->parity | \
99             (uint32_t)usartConfig->wordLength;
100     uart->CTRL1 = temp;
101 
102     temp = uart->CTRL2;
103     temp &= 0xCFFF;
104     temp |= usartConfig->stopBits;
105     uart->CTRL2 = temp;
106 
107     temp = uart->CTRL3;
108     temp &= 0xFCFF;
109     temp |= (uint32_t)usartConfig->hardwareFlow;
110     uart->CTRL3 = temp;
111 
112     if (uart == USART1)
113     {
114         RCM_ReadPCLKFreq(NULL, &fCLK);
115     }
116     else
117     {
118         RCM_ReadPCLKFreq(&fCLK, NULL);
119     }
120 
121     intDiv = ((25 * fCLK) / (4 * (usartConfig->baudRate)));
122     temp = (intDiv / 100) << 4;
123     fractionalDiv = intDiv - (100 * (temp >> 4));
124     temp |= ((((fractionalDiv * 16) + 50) / 100)) & ((uint8_t)0x0F);
125 
126     uart->BR = temp;
127 }
128 
129 /*!
130  * @brief     Fills each USART_InitStruct member with its default value
131  *
132  * @param     usartConfig:   pointer to a USART_Config_T structure which will be initialized
133  *
134  * @retval    None
135  */
USART_ConfigStructInit(USART_Config_T * usartConfig)136 void USART_ConfigStructInit(USART_Config_T* usartConfig)
137 {
138     usartConfig->baudRate = 9600;
139     usartConfig->wordLength = USART_WORD_LEN_8B;
140     usartConfig->stopBits = USART_STOP_BIT_1;
141     usartConfig->parity = USART_PARITY_NONE ;
142     usartConfig->mode = USART_MODE_TX_RX;
143     usartConfig->hardwareFlow = USART_HARDWARE_FLOW_NONE;
144 }
145 
146 /*!
147  * @brief     Configuration communication clock
148  *
149  * @param     usart:  Select the USART or the UART peripheral
150  *
151  * @param     clockConfig:   Pointer to a USART_clockConfig_T structure
152  *
153  * @retval    None
154  *
155  * @note      The usart can be USART1, USART2, USART3
156  */
USART_ConfigClock(USART_T * usart,USART_ClockConfig_T * clockConfig)157 void USART_ConfigClock(USART_T* usart, USART_ClockConfig_T* clockConfig)
158 {
159     usart->CTRL2_B.CLKEN   = clockConfig->clock;
160     usart->CTRL2_B.CPHA    = clockConfig->phase;
161     usart->CTRL2_B.CPOL    = clockConfig->polarity;
162     usart->CTRL2_B.LBCPOEN = clockConfig->lastBit;
163 }
164 
165 /*!
166  * @brief     Fills each clockConfig member with its default value
167  *
168  * @param     clockConfig:   Pointer to a USART_clockConfig_T structure
169  *
170  * @retval    None
171  *
172  * @note
173  */
USART_ConfigClockStructInit(USART_ClockConfig_T * clockConfig)174 void USART_ConfigClockStructInit(USART_ClockConfig_T* clockConfig)
175 {
176     clockConfig->clock     = USART_CLKEN_DISABLE;
177     clockConfig->phase     = USART_CLKPHA_1EDGE;
178     clockConfig->polarity  = USART_CLKPOL_LOW;
179     clockConfig->lastBit   = USART_LBCP_DISABLE;
180 }
181 
182 /*!
183  * @brief     Enables the specified USART peripheral
184  *
185  * @param     usart:   Select the USART or the UART peripheral
186  *
187  * @retval    None
188  *
189  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
190  */
USART_Enable(USART_T * usart)191 void USART_Enable(USART_T* usart)
192 {
193     usart->CTRL1_B.UEN = BIT_SET;
194 }
195 
196 /*!
197  * @brief     Disable the specified USART peripheral
198  *
199  * @param     usart:   Select the USART or the UART peripheral
200  *
201  * @retval    None
202  *
203  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
204  */
USART_Disable(USART_T * usart)205 void USART_Disable(USART_T* usart)
206 {
207     usart->CTRL1_B.UEN = BIT_RESET;
208 }
209 
210 /*!
211  * @brief     Enables the USART DMA interface
212  *
213  * @param     usart:   Select the USART or the UART peripheral
214  *
215  * @param     dmaReq:  Specifies the DMA request
216  *                     This parameter can be one of the following values:
217  *                     @arg USART_DMA_TX:     USART DMA receive request
218  *                     @arg USART_DMA_RX:     USART DMA transmit request
219  *                     @arg USART_DMA_TX_RX:  USART DMA transmit/receive request
220  *
221  * @retval    None
222  *
223  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
224  */
USART_EnableDMA(USART_T * usart,USART_DMA_T dmaReq)225 void USART_EnableDMA(USART_T* usart, USART_DMA_T dmaReq)
226 {
227     usart->CTRL3 |= dmaReq;
228 }
229 
230 /*!
231  * @brief     Disable the USART DMA interface
232  *
233  * @param     usart:   Select the USART or the UART peripheral
234  *
235  * @param     dmaReq:  Specifies the DMA request
236  *                     This parameter can be one of the following values:
237  *                     @arg USART_DMA_TX:     USART DMA receive request
238  *                     @arg USART_DMA_RX:     USART DMA transmit request
239  *                     @arg USART_DMA_TX_RX:  USART DMA transmit/receive request
240  *
241  * @retval    None
242  *
243  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
244  */
USART_DisableDMA(USART_T * usart,USART_DMA_T dmaReq)245 void USART_DisableDMA(USART_T* usart, USART_DMA_T dmaReq)
246 {
247     usart->CTRL3 &= (uint32_t)~dmaReq;
248 }
249 
250 /*!
251  * @brief     Configures the address of the USART node
252  *
253  * @param     usart:   Select the USART or the UART peripheral
254  *
255  * @param     address: Indicates the address of the USART node
256  *
257  * @retval    None
258  *
259  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
260  */
USART_Address(USART_T * usart,uint8_t address)261 void USART_Address(USART_T* usart, uint8_t address)
262 {
263     usart->CTRL2_B.ADDR = address;
264 }
265 
266 /*!
267  * @brief     Selects the USART WakeUp method.
268  *
269  * @param     usart:  Select the USART or the UART peripheral
270  *
271  * @param     wakeup: Specifies the selected USART auto baud rate method
272  *                    This parameter can be one of the following values:
273  *                    @arg USART_WAKEUP_IDLE_LINE:    WakeUp by an idle line detection
274  *                    @arg USART_WAKEUP_ADDRESS_MARK: WakeUp by an address mark
275  *
276  * @retval    None
277  *
278  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
279  */
USART_ConfigWakeUp(USART_T * usart,USART_WAKEUP_T wakeup)280 void USART_ConfigWakeUp(USART_T* usart, USART_WAKEUP_T wakeup)
281 {
282     usart->CTRL1_B.WUPMCFG = wakeup;
283 }
284 
285 /*!
286  * @brief     Enable USART Receiver in mute mode
287  *
288  * @param     usart: Select the USART or the UART peripheral
289  *
290  * @retval    None
291  *
292  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
293  */
USART_EnableMuteMode(USART_T * usart)294 void USART_EnableMuteMode(USART_T* usart)
295 {
296     usart->CTRL1_B.RXMUTEEN = BIT_SET;
297 }
298 
299 /*!
300  * @brief     Disable USART Receiver in active mode
301  *
302  * @param     usart: Select the USART or the UART peripheral
303  *
304  * @retval    None
305  *
306  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
307  */
USART_DisableMuteMode(USART_T * usart)308 void USART_DisableMuteMode(USART_T* usart)
309 {
310     usart->CTRL1_B.RXMUTEEN = BIT_RESET;
311 }
312 
313 /*!
314  * @brief     Sets the USART LIN Break detection length
315  *
316  * @param     usart:   Select the USART or the UART peripheral
317  *
318  * @param     length:  Specifies the LIN break detection length
319  *                     This parameter can be one of the following values:
320  *                     @arg USART_LBDL_10B: 10-bit break detection
321  *                     @arg USART_LBDL_10B: 11-bit break detection
322  *
323  * @retval    None
324  *
325  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
326  */
USART_ConfigLINBreakDetectLength(USART_T * usart,USART_LBDL_T length)327 void USART_ConfigLINBreakDetectLength(USART_T* usart, USART_LBDL_T length)
328 {
329     usart->CTRL2_B.LBDLCFG = length;
330 }
331 
332 /*!
333  * @brief     Enables the USART LIN MODE
334  *
335  * @param     usart:   Select the USART or the UART peripheral
336  *
337  * @retval    None
338  *
339  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
340  */
USART_EnableLIN(USART_T * usart)341 void USART_EnableLIN(USART_T* usart)
342 {
343     usart->CTRL2_B.LINMEN = BIT_SET;
344 }
345 
346 /*!
347  * @brief     Disable the USART LIN MODE
348  *
349  * @param     usart: Select the USART or the UART peripheral
350  *
351  * @retval    None
352  *
353  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
354  */
USART_DisableLIN(USART_T * usart)355 void USART_DisableLIN(USART_T* usart)
356 {
357     usart->CTRL2_B.LINMEN = BIT_RESET;
358 }
359 
360 /*!
361  * @brief     Transmitter Enable
362  *
363  * @param     usart: Select the USART or the UART peripheral
364  *
365  * @retval    None
366  *
367  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
368  */
USART_EnableTx(USART_T * usart)369 void USART_EnableTx(USART_T* usart)
370 {
371     usart->CTRL1_B.TXEN = BIT_SET;
372 }
373 
374 /*!
375  * @brief     Transmitter Disable
376  *
377  * @param     usart: Select the USART or the UART peripheral
378  *
379  * @retval    None
380  *
381  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
382  */
USART_DisableTx(USART_T * usart)383 void USART_DisableTx(USART_T* usart)
384 {
385     usart->CTRL1_B.TXEN = BIT_RESET;
386 }
387 
388 /*!
389  * @brief     Receiver enable
390  *
391  * @param     usart: Select the USART or the UART peripheral
392  *
393  * @retval    None
394  *
395  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
396  */
USART_EnableRx(USART_T * usart)397 void USART_EnableRx(USART_T* usart)
398 {
399     usart->CTRL1_B.RXEN = BIT_SET;
400 }
401 
402 /*!
403  * @brief     Receiver disable
404  *
405  * @param     usart: Select the USART or the UART peripheral
406  *
407  * @retval    None
408  *
409  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
410  */
USART_DisableRx(USART_T * usart)411 void USART_DisableRx(USART_T* usart)
412 {
413     usart->CTRL1_B.RXEN = BIT_RESET;
414 }
415 
416 /*!
417  * @brief     Transmits single data
418  *
419  * @param     usart: Select the USART or the UART peripheral
420  *
421  * @param     data:  the data to transmit
422  *
423  * @retval    None
424  *
425  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
426  */
USART_TxData(USART_T * usart,uint16_t data)427 void USART_TxData(USART_T* usart, uint16_t data)
428 {
429     usart->DATA_B.DATA = data;
430 }
431 
432 /*!
433  * @brief     Returns the most recent received data
434  *
435  * @param     usart: Select the USART or the UART peripheral
436  *
437  * @retval    None
438  *
439  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
440  */
USART_RxData(USART_T * usart)441 uint16_t USART_RxData(USART_T* usart)
442 {
443     return (uint16_t)(usart->DATA_B.DATA);
444 }
445 
446 /*!
447  * @brief     Transmits break characters
448  *
449  * @param     usart: Select the USART or the UART peripheral
450  *
451  * @retval    None
452  *
453  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
454  */
USART_TxBreak(USART_T * usart)455 void USART_TxBreak(USART_T* usart)
456 {
457     usart->CTRL1_B.TXBF = BIT_SET;
458 }
459 
460 /*!
461  * @brief     Sets the specified USART guard time
462  *
463  * @param     usart:     Select the USART or the UART peripheral
464  *
465  * @param     guardTime: Specifies the guard time
466  *
467  * @retval    None
468  *
469  * @note      The usart can be USART1, USART2, USART3
470  */
USART_ConfigGuardTime(USART_T * usart,uint8_t guardTime)471 void USART_ConfigGuardTime(USART_T* usart, uint8_t guardTime)
472 {
473     usart->GTPSC_B.GRDT = guardTime;
474 }
475 
476 /*!
477  * @brief     Sets the system clock divider number
478  *
479  * @param     usart: Select the USART or the UART peripheral
480  *
481  * @param     div:   specifies the divider number
482  *
483  * @retval    None
484  *
485  * @note      The usart can be USART1, USART2, USART3
486  */
USART_ConfigPrescaler(USART_T * usart,uint8_t div)487 void USART_ConfigPrescaler(USART_T* usart, uint8_t div)
488 {
489     usart->GTPSC_B.PSC = div;
490 }
491 
492 /*!
493  * @brief     Enables the USART Smart Card mode
494  *
495  * @param     usart: Select the USART or the UART peripheral
496  *
497  * @retval    None
498  *
499  * @note      The Smart Card mode is not available for UART4 and UART5
500  */
USART_EnableSmartCard(USART_T * usart)501 void USART_EnableSmartCard(USART_T* usart)
502 {
503     usart->CTRL3_B.SCEN = BIT_SET;
504 }
505 
506 /*!
507  * @brief     Disable the USART Smart Card mode
508  *
509  * @param     usart: Select the USART or the UART peripheral
510  *
511  * @retval    None
512  *
513  * @note      The Smart Card mode is not available for UART4 and UART5
514  */
USART_DisableSmartCard(USART_T * usart)515 void USART_DisableSmartCard(USART_T* usart)
516 {
517     usart->CTRL3_B.SCEN = BIT_RESET;
518 }
519 
520 /*!
521  * @brief     Enables NACK transmission
522  *
523  * @param     usart: Select the USART or the UART peripheral
524  *
525  * @retval    None
526  *
527  * @note      The Smart Card mode is not available for UART4 and UART5
528  */
USART_EnableSmartCardNACK(USART_T * usart)529 void USART_EnableSmartCardNACK(USART_T* usart)
530 {
531     usart->CTRL3_B.SCNACKEN = BIT_SET;
532 }
533 
534 /*!
535  * @brief     Disable NACK transmission
536  *
537  * @param     usart: Select the USART or the UART peripheral
538  *
539  * @retval    None
540  *
541  * @note      The Smart Card mode is not available for UART4 and UART5
542  */
USART_DisableSmartCardNACK(USART_T * usart)543 void USART_DisableSmartCardNACK(USART_T* usart)
544 {
545     usart->CTRL3_B.SCNACKEN = BIT_RESET;
546 }
547 
548 /*!
549  * @brief     Enables USART Half Duplex communication
550  *
551  * @param     usart: Select the USART or the UART peripheral
552  *
553  * @retval    None
554  *
555  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
556  */
USART_EnableHalfDuplex(USART_T * usart)557 void USART_EnableHalfDuplex(USART_T* usart)
558 {
559     usart->CTRL3_B.HDEN = BIT_SET;
560 }
561 
562 /*!
563  * @brief     Disable USART Half Duplex communication
564  *
565  * @param     usart: Select the USART or the UART peripheral
566  *
567  * @retval    None
568  *
569  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
570  */
USART_DisableHalfDuplex(USART_T * usart)571 void USART_DisableHalfDuplex(USART_T* usart)
572 {
573     usart->CTRL3_B.HDEN = BIT_RESET;
574 }
575 
576 /*!
577  * @brief     Configures the USART's IrDA interface
578  *
579  * @param     usart:    Select the USART or the UART peripheral
580  *
581  * @param     IrDAMode: Specifies the IrDA mode
582  *                      This parameter can be one of the following values:
583  *                      @arg USART_IRDALP_NORMAL:   Normal
584  *                      @arg USART_IRDALP_LOWPOWER: Low-Power
585  * @retval    None
586  *
587  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
588  */
USART_ConfigIrDA(USART_T * usart,USART_IRDALP_T IrDAMode)589 void USART_ConfigIrDA(USART_T* usart, USART_IRDALP_T IrDAMode)
590 {
591     usart->CTRL3_B.IRLPEN = IrDAMode;
592 }
593 
594 /*!
595  * @brief     Enables the USART's IrDA interface
596  *
597  * @param     usart: Select the USART or the UART peripheral
598  *
599  * @retval    None
600  *
601  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
602  */
USART_EnableIrDA(USART_T * usart)603 void USART_EnableIrDA(USART_T* usart)
604 {
605     usart->CTRL3_B.IREN = BIT_SET;
606 }
607 
608 /*!
609  * @brief     Disable the USART's IrDA interface
610  *
611  * @param     usart: Select the USART or the UART peripheral
612  *
613  * @retval    None
614  *
615  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
616  */
USART_DisableIrDA(USART_T * usart)617 void USART_DisableIrDA(USART_T* usart)
618 {
619     usart->CTRL3_B.IREN = BIT_RESET;
620 }
621 
622 /*!
623  * @brief     Enable the specified USART interrupts
624  *
625  * @param     usart:     Select the USART or the UART peripheral
626  *
627  * @param     interrupt:  Specifies the USART interrupts sources
628  *                        The parameter can be one of following values:
629  *                        @arg USART_INT_PE:    Parity error interrupt
630  *                        @arg USART_INT_TXBE:  Tansmit data buffer empty interrupt
631  *                        @arg USART_INT_TXC:   Transmission complete interrupt
632  *                        @arg USART_INT_RXBNE: Receive data buffer not empty interrupt
633  *                        @arg USART_INT_IDLE:  Idle line detection interrupt
634  *                        @arg USART_INT_LBD:   LIN break detection interrupt
635  *                        @arg USART_INT_CTS:   CTS change interrupt
636  *                        @arg USART_INT_ERR:   Error interrupt(Frame error, noise error, overrun error)
637  *
638  * @retval    None
639  *
640  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
641  */
USART_EnableInterrupt(USART_T * usart,USART_INT_T interrupt)642 void USART_EnableInterrupt(USART_T* usart, USART_INT_T interrupt)
643 {
644     uint32_t temp;
645 
646     temp = (uint32_t)(interrupt & 0xffff);
647 
648     if (interrupt & 0X10000)
649     {
650         usart->CTRL1 |= temp;
651     }
652 
653     if (interrupt & 0X20000)
654     {
655         usart->CTRL2 |= temp;
656     }
657 
658     if (interrupt & 0X40000)
659     {
660         usart->CTRL3 |= temp;
661     }
662 }
663 
664 /*!
665  * @brief     Disables the specified USART interrupts
666  *
667  * @param     usart:      Select the USART or the UART peripheral
668  *
669  * @param     interrupt:  Specifies the USART interrupts sources
670  *                        The parameter can be one of following values:
671  *                        @arg USART_INT_PE:    Parity error interrupt
672  *                        @arg USART_INT_TXBE:  Tansmit data buffer empty interrupt
673  *                        @arg USART_INT_TXC:   Transmission complete interrupt
674  *                        @arg USART_INT_RXBNE: Receive data buffer not empty interrupt
675  *                        @arg USART_INT_IDLE:  Idle line detection interrupt
676  *                        @arg USART_INT_LBD:   LIN break detection interrupt
677  *                        @arg USART_INT_CTS:   CTS change interrupt
678  *                        @arg USART_INT_ERR:   Error interrupt(Frame error, noise error, overrun error)
679  *
680  * @retval    None
681  *
682  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
683  */
USART_DisableInterrupt(USART_T * usart,USART_INT_T interrupt)684 void USART_DisableInterrupt(USART_T* usart, USART_INT_T interrupt)
685 {
686     uint32_t temp;
687 
688     temp = (uint32_t)~(interrupt & 0xffff);
689 
690     if (interrupt & 0X10000)
691     {
692         usart->CTRL1 &= temp;
693     }
694 
695     if (interrupt & 0X20000)
696     {
697         usart->CTRL2 &= temp;
698     }
699 
700     if (interrupt & 0X40000)
701     {
702         usart->CTRL3 &= temp;
703     }
704 }
705 
706 /*!
707  * @brief     Read the specified USART flag
708  *
709  * @param     usart: Select the USART or the UART peripheral
710  *
711  * @param     flag:  Specifies the flag to check
712  *                   The parameter can be one of following values:
713  *                   @arg USART_FLAG_CTS:    CTS Change flag (not available for UART4 and UART5)
714  *                   @arg USART_FLAG_LBD:    LIN Break detection flag
715  *                   @arg USART_FLAG_TXBE:   Transmit data buffer empty flag
716  *                   @arg USART_FLAG_TXC:    Transmission Complete flag
717  *                   @arg USART_FLAG_RXBNE:  Receive data buffer not empty flag
718  *                   @arg USART_FLAG_IDLE:   Idle Line detection flag
719  *                   @arg USART_FLAG_OVRE:   OverRun Error flag
720  *                   @arg USART_FLAG_NE:     Noise Error flag
721  *                   @arg USART_FLAG_FE:     Framing Error flag
722  *                   @arg USART_FLAG_PE:     Parity Error flag
723  *
724  * @retval    The new state of flag (SET or RESET)
725  *
726  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
727  */
USART_ReadStatusFlag(USART_T * usart,USART_FLAG_T flag)728 uint8_t USART_ReadStatusFlag(USART_T* usart, USART_FLAG_T flag)
729 {
730     return (usart->STS & flag) ? SET : RESET;
731 }
732 
733 /*!
734  * @brief     Clears the USARTx's pending flags
735  *
736  * @param     usart:   Select the USART or the UART peripheral
737  *
738  * @param     flag:  Specifies the flag to clear
739  *                   The parameter can be one of following values:
740  *                   @arg USART_FLAG_CTS:    CTS Change flag (not available for UART4 and UART5)
741  *                   @arg USART_FLAG_LBD:    LIN Break detection flag
742  *                   @arg USART_FLAG_TXC:    Transmission Complete flag
743  *                   @arg USART_FLAG_RXBNE:  Receive data buffer not empty flag
744  *
745  * @retval    None
746  *
747  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
748  */
USART_ClearStatusFlag(USART_T * usart,USART_FLAG_T flag)749 void USART_ClearStatusFlag(USART_T* usart, USART_FLAG_T flag)
750 {
751     usart->STS &= (uint32_t)~flag;
752 }
753 
754 /*!
755  * @brief     Read the specified USART interrupt flag
756  *
757  * @param     usart: Select the USART or the UART peripheral
758  *
759  * @param     flag:  Specifies the USART interrupt source to check
760  *                        The parameter can be one of following values:
761  *                        @arg USART_INT_TXBE:  Tansmit data buffer empty interrupt
762  *                        @arg USART_INT_TXC:   Transmission complete interrupt
763  *                        @arg USART_INT_RXBNE: Receive data buffer not empty interrupt
764  *                        @arg USART_INT_IDLE:  Idle line detection interrupt
765  *                        @arg USART_INT_LBD:   LIN break detection interrupt
766  *                        @arg USART_INT_CTS:   CTS change interrupt
767  *                        @arg USART_INT_OVRE:  OverRun Error interruptpt
768  *                        @arg USART_INT_NE:    Noise Error interrupt
769  *                        @arg USART_INT_FE:    Framing Error interrupt
770  *                        @arg USART_INT_PE:    Parity error interrupt
771  *
772  * @retval    The new state of flag (SET or RESET)
773  *
774  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
775  */
USART_ReadIntFlag(USART_T * usart,USART_INT_T flag)776 uint8_t USART_ReadIntFlag(USART_T* usart, USART_INT_T flag)
777 {
778     uint32_t itFlag, srFlag;
779 
780     if (flag & 0x10000)
781     {
782         itFlag = usart->CTRL1 & flag & 0xffff;
783     }
784     else if (flag & 0x20000)
785     {
786         itFlag = usart->CTRL2 & flag & 0xffff;
787     }
788     else
789     {
790         itFlag = usart->CTRL3 & flag & 0xffff;
791     }
792 
793     srFlag = flag >> 24;
794     srFlag = (uint32_t)(1 << srFlag);
795     srFlag = usart->STS & srFlag;
796 
797     if (srFlag && itFlag)
798     {
799         return SET;
800     }
801 
802     return RESET;
803 }
804 
805 /*!
806  * @brief     Clears the USART interrupt pending bits
807  *
808  * @param     usart: Select the USART or the UART peripheral
809  *
810  * @param     flag:  Specifies the interrupt pending bit to clear
811  *                   The parameter can be one of following values:
812  *                   @arg USART_INT_RXBNE: Receive data buffer not empty interrupt
813  *                   @arg USART_INT_TXC:   Transmission complete interrupt
814  *                   @arg USART_INT_LBD:   LIN break detection interrupt
815  *                   @arg USART_INT_CTS:   CTS change interrupt
816  *
817  * @retval    None
818  *
819  * @note      The usart can be USART1, USART2, USART3, UART4 and UART5
820  */
USART_ClearIntFlag(USART_T * usart,USART_INT_T flag)821 void USART_ClearIntFlag(USART_T* usart, USART_INT_T flag)
822 {
823     uint32_t srFlag;
824 
825     srFlag = flag >> 24;
826     srFlag = (uint32_t)(1 << srFlag);
827 
828     usart->STS &= (uint32_t)~srFlag;
829 }
830 
831 /**@} end of group USART_Functions */
832 /**@} end of group USART_Driver */
833 /**@} end of group APM32E10x_StdPeriphDriver */
834