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