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