1 /*!
2 * @file apm32f0xx_usart.c
3 *
4 * @brief This file provides all the USART firmware functions
5 *
6 * @version V1.0.3
7 *
8 * @date 2022-09-20
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 "apm32f0xx_usart.h"
27 #include "apm32f0xx_rcm.h"
28
29 /** @addtogroup APM32F0xx_StdPeriphDriver
30 @{
31 */
32
33 /** @addtogroup USART_Driver USART Driver
34 @{
35 */
36
37 /** @defgroup USART_Macros Macros
38 @{
39 */
40
41 /**@} end of group USART_Macros */
42
43 /** @defgroup USART_Enumerations Enumerations
44 @{
45 */
46
47 /**@} end of group USART_Enumerations */
48
49 /** @defgroup USART_Structures Structures
50 @{
51 */
52
53 /**@} end of group USART_Structures */
54
55 /** @defgroup USART_Variables Variables
56 @{
57 */
58
59 /**@} end of group USART_Variables */
60
61 /** @defgroup USART_Functions Functions
62 @{
63 */
64
65 /*!
66 * @brief Reset usart peripheral registers to their default reset values
67 *
68 * @param usart: Select the the USART peripheral.
69 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
70 *
71 * @retval None
72 *
73 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
74 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
75 */
76
USART_Reset(USART_T * usart)77 void USART_Reset(USART_T* usart)
78 {
79 if (USART1 == usart)
80 {
81 RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_USART1);
82 RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_USART1);
83 }
84 else if (USART2 == usart)
85 {
86 RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_USART2);
87 RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_USART2);
88 }
89 else if (USART3 == usart)
90 {
91 RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_USART3);
92 RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_USART3);
93 }
94 else if (USART4 == usart)
95 {
96 RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_USART4);
97 RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_USART4);
98 }
99 else if (USART5 == usart)
100 {
101 RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_USART5);
102 RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_USART5);
103 }
104 else if (USART6 == usart)
105 {
106 RCM_EnableAPB1PeriphReset(RCM_APB2_PERIPH_USART6);
107 RCM_DisableAPB1PeriphReset(RCM_APB2_PERIPH_USART6);
108 }
109 else if (USART7 == usart)
110 {
111 RCM_EnableAPB1PeriphReset(RCM_APB2_PERIPH_USART7);
112 RCM_DisableAPB1PeriphReset(RCM_APB2_PERIPH_USART7);
113 }
114 else if (USART8 == usart)
115 {
116 RCM_EnableAPB1PeriphReset(RCM_APB2_PERIPH_USART8);
117 RCM_DisableAPB1PeriphReset(RCM_APB2_PERIPH_USART8);
118 }
119 }
120
121 /*!
122 * @brief Config the USART peripheral according to the specified parameters in the USART_InitStruct
123 *
124 * @param usart: Select the the USART peripheral.
125 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
126 *
127 * @param configStruct pointer to a USART_Config_T structure
128 *
129 * @retval None
130 *
131 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
132 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
133 */
USART_Config(USART_T * uart,USART_Config_T * configStruct)134 void USART_Config(USART_T* uart, USART_Config_T* configStruct)
135 {
136 uint32_t temp, fCLK, intDiv, fractionalDiv;
137
138 /** Disable USART */
139 uart->CTRL1_B.UEN = 0x00;
140
141 /** WLS, PCEN, TXEN, RXEN */
142 temp = uart->CTRL1;
143 temp &= 0xE9F3;
144 temp |= (uint32_t)configStruct->mode | \
145 (uint32_t)configStruct->parity | \
146 (uint32_t)configStruct->wordLength;
147 uart->CTRL1 = temp;
148
149 /** STOP bits */
150 temp = uart->CTRL2;
151 temp &= 0xCFFF;
152 temp |= configStruct->stopBits;
153 uart->CTRL2 = temp;
154
155 /** Hardware Flow Control */
156 temp = uart->CTRL3;
157 temp &= 0xFCFF;
158 temp |= (uint32_t)configStruct->hardwareFlowCtrl;
159 uart->CTRL3 = temp;
160
161 if (uart == USART1)
162 {
163 fCLK = RCM_ReadUSART1CLKFreq();
164 }
165 else if (uart == USART2)
166 {
167 fCLK = RCM_ReadUSART2CLKFreq();
168 }
169 else
170 {
171 fCLK = RCM_ReadPCLKFreq();
172 }
173
174 intDiv = ((25 * fCLK) / (4 * (configStruct->baudRate)));
175 temp = (intDiv / 100) << 4;
176 fractionalDiv = intDiv - (100 * (temp >> 4));
177 temp |= ((((fractionalDiv * 16) + 50) / 100)) & ((uint8_t)0x0F);
178
179 uart->BR = temp;
180 }
181
182 /*!
183 * @brief Fills each USART_InitStruct member with its default value
184 *
185 * @param configStruct�� pointer to a USART_Config_T structure which will be initialized
186 *
187 * @retval None
188 */
USART_ConfigStructInit(USART_Config_T * configStruct)189 void USART_ConfigStructInit(USART_Config_T* configStruct)
190 {
191 configStruct->baudRate = 9600;
192 configStruct->wordLength = USART_WORD_LEN_8B;
193 configStruct->stopBits = USART_STOP_BIT_1;
194 configStruct->parity = USART_PARITY_NONE ;
195 configStruct->mode = USART_MODE_TX_RX;
196 configStruct->hardwareFlowCtrl = USART_FLOW_CTRL_NONE;
197 }
198
199 /*!
200 * @brief Synchronous communication clock configuration
201 *
202 * @param usart: Select the the USART peripheral.
203 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
204 *
205 * @param SyncClockConfig: Pointer to a USART_SyncClockConfig_T structure that
206 * contains the configuration information for the clock
207 *
208 * @retval None
209 *
210 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
211 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
212 */
USART_ConfigSyncClock(USART_T * usart,USART_SyncClockConfig_T * SyncClockConfig)213 void USART_ConfigSyncClock(USART_T* usart, USART_SyncClockConfig_T* SyncClockConfig)
214 {
215 usart->CTRL2_B.CLKEN = SyncClockConfig->enable;
216 usart->CTRL2_B.CPHA = SyncClockConfig->phase;
217 usart->CTRL2_B.CPOL = SyncClockConfig->polarity;
218 usart->CTRL2_B.LBCPOEN = SyncClockConfig->lastBitClock;
219 }
220
221 /*!
222 * @brief Fills each SyncClockConfig member with its default value
223 *
224 * @param SyncClockConfig: Pointer to a USART_SyncClockConfig_T structure
225 *
226 * @retval None
227 */
USART_ConfigSyncClockStructInit(USART_SyncClockConfig_T * SyncClockConfig)228 void USART_ConfigSyncClockStructInit(USART_SyncClockConfig_T* SyncClockConfig)
229 {
230 SyncClockConfig->enable = USART_CLKEN_DISABLE;
231 SyncClockConfig->phase = USART_CLKPHA_1EDGE;
232 SyncClockConfig->polarity = USART_CLKPOL_LOW;
233 SyncClockConfig->lastBitClock = USART_LBCP_DISABLE;
234 }
235
236 /*!
237 * @brief Enables the specified USART peripheral
238 *
239 * @param usart: Select the the USART peripheral.
240 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
241 *
242 * @retval None
243 *
244 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
245 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
246 */
USART_Enable(USART_T * usart)247 void USART_Enable(USART_T* usart)
248 {
249 usart->CTRL1_B.UEN = BIT_SET;
250 }
251
252 /*!
253 * @brief Disables the specified USART peripheral
254 *
255 * @param usart: Select the the USART peripheral.
256 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
257 *
258 * @retval None
259 *
260 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
261 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
262 */
USART_Disable(USART_T * usart)263 void USART_Disable(USART_T* usart)
264 {
265 usart->CTRL1_B.UEN = BIT_RESET;
266 }
267
268 /*!
269 * @brief Enables the USART direction mode
270 *
271 * @param usart: Select the the USART peripheral.
272 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
273 *
274 * @param mode: Specifies the USART direction
275 * The parameter can be one of following values:
276 * @arg USART_MODE_RX: USART Transmitter
277 * @arg USART_MODE_TX: USART Receiver
278 * @arg USART_MODE_TX_RX: USART Transmitter and Receiver
279 *
280 * @retval None
281 *
282 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
283 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
284 */
USART_EnableDirectionMode(USART_T * usart,USART_MODE_T mode)285 void USART_EnableDirectionMode(USART_T* usart, USART_MODE_T mode)
286 {
287 if (mode == USART_MODE_RX)
288 {
289 usart->CTRL1_B.RXEN = BIT_SET;
290 }
291
292 if (mode == USART_MODE_TX)
293 {
294 usart->CTRL1_B.TXEN = BIT_SET;
295 }
296
297 if (mode == USART_MODE_TX_RX)
298 {
299 usart->CTRL1_B.TXEN = BIT_SET;
300 usart->CTRL1_B.RXEN = BIT_SET;
301 }
302 }
303
304 /*!
305 * @brief Disables the USART direction mode
306 *
307 * @param usart: Select the the USART peripheral.
308 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
309 *
310 * @param mode: Specifies the USART direction
311 * The parameter can be one of following values:
312 * @arg USART_MODE_RX: USART Transmitter
313 * @arg USART_MODE_TX: USART Receiver
314 * @arg USART_MODE_TX_RX: USART Transmitter and Receiver
315 *
316 * @retval None
317 *
318 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
319 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
320 */
USART_DisableDirectionMode(USART_T * usart,USART_MODE_T mode)321 void USART_DisableDirectionMode(USART_T* usart, USART_MODE_T mode)
322 {
323 if (mode == USART_MODE_RX)
324 {
325 usart->CTRL1_B.RXEN = BIT_RESET;
326 }
327
328 if (mode == USART_MODE_TX)
329 {
330 usart->CTRL1_B.TXEN = BIT_RESET;
331 }
332
333 if (mode == USART_MODE_TX_RX)
334 {
335 usart->CTRL1_B.TXEN = BIT_RESET;
336 usart->CTRL1_B.RXEN = BIT_RESET;
337 }
338 }
339
340 /*!
341 * @brief Enables the Over Sampling 8/16 mode
342 *
343 * @param usart: Select the the USART peripheral.
344 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
345 *
346 * @retval None
347 *
348 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
349 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
350 *
351 * @note 0: Oversampling by 16 1: Oversampling by 8
352 */
USART_EnableOverSampling8(USART_T * usart)353 void USART_EnableOverSampling8(USART_T* usart)
354 {
355 usart->CTRL1_B.OSMCFG = BIT_SET;
356 }
357
358 /*!
359 * @brief Disables the the Over Sampling 8/16 mode
360 *
361 * @param usart: Select the the USART peripheral.
362 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
363 *
364 * @retval None
365 *
366 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
367 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
368 *
369 * @note 0: Oversampling by 16 1: Oversampling by 8
370 */
USART_DisableOverSampling8(USART_T * usart)371 void USART_DisableOverSampling8(USART_T* usart)
372 {
373 usart->CTRL1_B.OSMCFG = BIT_RESET;
374 }
375
376 /*!
377 * @brief Enables the USART's one bit sampling method.
378 *
379 * @param usart: Select the the USART peripheral.
380 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
381 *
382 * @retval None
383 *
384 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
385 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
386 */
USART_EnableOneBitMethod(USART_T * usart)387 void USART_EnableOneBitMethod(USART_T* usart)
388 {
389 usart->CTRL3_B.SAMCFG = BIT_SET;
390 }
391
392 /*!
393 * @brief Disables the USART's one bit sampling method.
394 *
395 * @param usart: Select the the USART peripheral.
396 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
397 *
398 * @retval None
399 *
400 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
401 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
402 */
USART_DisableOneBitMethod(USART_T * usart)403 void USART_DisableOneBitMethod(USART_T* usart)
404 {
405 usart->CTRL3_B.SAMCFG = BIT_RESET;
406 }
407
408 /*!
409 * @brief Enables the most significant bit first
410 *
411 * @param usart: Select the the USART peripheral.
412 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
413 *
414 * @retval None
415 *
416 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
417 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
418 */
USART_EnableMSBFirst(USART_T * usart)419 void USART_EnableMSBFirst(USART_T* usart)
420 {
421 usart->CTRL2_B.MSBFEN = BIT_SET;
422 }
423
424 /*!
425 * @brief Disables the most significant bit first
426 *
427 * @param usart: Select the the USART peripheral.
428 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
429 *
430 * @retval None
431 *
432 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
433 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
434 */
USART_DisableMSBFirst(USART_T * usart)435 void USART_DisableMSBFirst(USART_T* usart)
436 {
437 usart->CTRL2_B.MSBFEN = BIT_RESET;
438 }
439
440 /*!
441 * @brief Enables the the binary data inversion
442 *
443 * @param usart: Select the the USART peripheral.
444 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
445 *
446 * @retval None
447 *
448 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
449 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
450 */
USART_EnableDataInv(USART_T * usart)451 void USART_EnableDataInv(USART_T* usart)
452 {
453 usart->CTRL2_B.BINVEN = BIT_SET;
454 }
455
456 /*!
457 * @brief Disables the the binary data inversion
458 *
459 * @param usart: Select the the USART peripheral.
460 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
461 *
462 * @retval None
463 *
464 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
465 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
466 */
USART_DisableDataInv(USART_T * usart)467 void USART_DisableDataInv(USART_T* usart)
468 {
469 usart->CTRL2_B.BINVEN = BIT_RESET;
470 }
471
472 /*!
473 * @brief Enables the specified USART peripheral
474 *
475 * @param usart: Select the the USART peripheral.
476 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
477 *
478 * @param invPin: specifies the USART pin(s) to invert
479 * This parameter can be one of the following values:
480 * @arg USART_INVERSION_RX: USART Tx pin active level inversion
481 * @arg USART_INVERSION_TX: USART Rx pin active level inversion
482 * @arg USART_INVERSION_TX_RX: USART TX Rx pin active level inversion
483 * @retval None
484 *
485 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
486 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
487 */
USART_EnableInvPin(USART_T * usart,USART_INVERSION_T invPin)488 void USART_EnableInvPin(USART_T* usart, USART_INVERSION_T invPin)
489 {
490 if (invPin == USART_INVERSION_RX)
491 {
492 usart->CTRL2_B.RXINVEN = BIT_SET;
493 }
494
495 if (invPin == USART_INVERSION_TX)
496 {
497 usart->CTRL2_B.TXINVEN = BIT_SET;
498 }
499
500 if (invPin == (USART_INVERSION_TX_RX))
501 {
502 usart->CTRL2_B.TXINVEN = BIT_SET;
503 usart->CTRL2_B.RXINVEN = BIT_SET;
504 }
505 }
506
507 /*!
508 * @brief Disables the specified USART peripheral
509 *
510 * @param usart: Select the the USART peripheral.
511 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
512 *
513 * @param invPin: specifies the USART pin(s) to invert
514 * This parameter can be one of the following values:
515 * @arg USART_INVERSION_RX: USART Tx pin active level inversion
516 * @arg USART_INVERSION_TX: USART Rx pin active level inversion
517 * @arg USART_INVERSION_TX_RX: USART TX Rx pin active level inversion
518 *
519 * @retval None
520 *
521 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
522 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
523 */
USART_DisableInvPin(USART_T * usart,USART_INVERSION_T invPin)524 void USART_DisableInvPin(USART_T* usart, USART_INVERSION_T invPin)
525 {
526 if (invPin == USART_INVERSION_RX)
527 {
528 usart->CTRL2_B.RXINVEN = BIT_RESET;
529 }
530
531 if (invPin == USART_INVERSION_TX)
532 {
533 usart->CTRL2_B.TXINVEN = BIT_RESET;
534 }
535
536 if (invPin == USART_INVERSION_TX_RX)
537 {
538 usart->CTRL2_B.TXINVEN = BIT_RESET;
539 usart->CTRL2_B.RXINVEN = BIT_RESET;
540 }
541 }
542
543 /*!
544 * @brief Enables the swap Tx/Rx pins
545 *
546 * @param usart: Select the the USART peripheral.
547 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
548 *
549 * @retval None
550 *
551 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
552 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
553 */
USART_EnableSWAPPin(USART_T * usart)554 void USART_EnableSWAPPin(USART_T* usart)
555 {
556 usart->CTRL2_B.SWAPEN = BIT_SET;
557 }
558
559 /*!
560 * @brief Disables the swap Tx/Rx pins
561 *
562 * @param usart: Select the the USART peripheral.
563 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
564 *
565 * @retval None
566 *
567 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
568 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
569 */
USART_DisableSWAPPin(USART_T * usart)570 void USART_DisableSWAPPin(USART_T* usart)
571 {
572 usart->CTRL2_B.SWAPEN = BIT_RESET;
573 }
574
575 /*!
576 * @brief Enables the receiver Time Out feature
577 *
578 * @param usart: Select the the USART peripheral.
579 * It can be USART1/USART2/USART3.
580 *
581 * @retval None
582 *
583 * @note The USART2 only for APM32F072 and APM32F091 devices.
584 * The USART3 only for APM32F091 devices.
585 */
USART_EnableReceiverTimeOut(USART_T * usart)586 void USART_EnableReceiverTimeOut(USART_T* usart)
587 {
588 usart->CTRL2_B.RXTODEN = BIT_SET;
589 }
590
591 /*!
592 * @brief Disables the receiver Time Out feature
593 *
594 * @param usart: Select the the USART peripheral.
595 * It can be USART1/USART2/USART3.
596 *
597 * @retval None
598 *
599 * @note The USART2 only for APM32F072 and APM32F091 devices.
600 * The USART3 only for APM32F091 devices.
601 */
USART_DisableReceiverTimeOut(USART_T * usart)602 void USART_DisableReceiverTimeOut(USART_T* usart)
603 {
604 usart->CTRL2_B.RXTODEN = BIT_RESET;
605 }
606
607 /*!
608 * @brief Sets the receiver Time Out value
609 *
610 * @param usart: Select the the USART peripheral.
611 * It can be USART1/USART2/USART3.
612 *
613 * @param timeOut: Specifies the Receiver Time Out value
614 *
615 * @retval None
616 *
617 * @note The USART2 only for APM32F072 and APM32F091 devices.
618 * The USART3 only for APM32F091 devices.
619 *
620 * @note The value must less than 0x00FFFFFF
621 */
USART_ReceiverTimeOutValue(USART_T * usart,uint32_t timeOut)622 void USART_ReceiverTimeOutValue(USART_T* usart, uint32_t timeOut)
623 {
624 usart->RXTO = (uint32_t)0x00;
625
626 if (timeOut <= (uint32_t)0x00FFFFFF)
627 {
628 usart->RXTO = ((uint32_t)timeOut & 0x00FFFFFF);
629 }
630 }
631
632 /*!
633 * @brief Sets the system clock divider.
634 *
635 * @param usart: Select the the USART peripheral.
636 * It can be USART1/USART2/USART3.
637 *
638 * @param divider: Specifies the prescaler clock value
639 *
640 * @retval None
641 *
642 * @note It's not for APM32F030 devices.
643 * The USART2 only for APM32F072 and APM32F091 devices.
644 * The USART3 only for APM32F091 devices.
645 */
USART_ConfigDivider(USART_T * usart,uint8_t divider)646 void USART_ConfigDivider(USART_T* usart, uint8_t divider)
647 {
648 usart->GTPSC &= (uint16_t)0xFF00;
649 usart->GTPSC_B.PSC = (uint8_t)divider;
650 }
651
652 /*!
653 * @brief Enables the stop mode
654 *
655 * @param usart: Select the the USART peripheral.
656 * It can be USART1/USART2/USART3.
657 *
658 * @retval None
659 *
660 * @note The USART2 only for APM32F072 and APM32F091 devices.
661 * The USART3 only for APM32F091 devices.
662 */
USART_EnableStopMode(USART_T * usart)663 void USART_EnableStopMode(USART_T* usart)
664 {
665 usart->CTRL1_B.USWMEN = BIT_SET;
666 }
667
668 /*!
669 * @brief Disables the stop mode
670 *
671 * @param usart: Select the the USART peripheral.
672 * It can be USART1/USART2/USART3.
673 *
674 * @retval None
675 *
676 * @note The USART2 only for APM32F072 and APM32F091 devices.
677 * The USART3 only for APM32F091 devices.
678 */
USART_DisableStopMode(USART_T * usart)679 void USART_DisableStopMode(USART_T* usart)
680 {
681 usart->CTRL1_B.USWMEN = BIT_RESET;
682 }
683
684 /*!
685 * @brief Selects the USART WakeUp method form stop mode.
686 *
687 * @param usart: Select the the USART peripheral.
688 * It can be USART1/USART2/USART3.
689 *
690 * @param source: Specifies the selected USART wakeup method
691 * This parameter can be one of the following values:
692 * @arg USART_WAKEUP_SOURCE_ADDRESS: WUP active on address match.
693 * @arg USART_WAKEUP_SOURCE_START: WUP active on Start bit detection.
694 * @arg USART_WAKEUP_SOURCE_RXNE: WUP active on RXNE.
695 *
696 * @note It's not for APM32F030 devices.
697 * The USART2 only for APM32F072 and APM32F091 devices.
698 * The USART3 only for APM32F091 devices.
699 */
USART_ConfigStopModeWakeUpSource(USART_T * usart,USART_WAKEUP_SOURCE_T source)700 void USART_ConfigStopModeWakeUpSource(USART_T* usart, USART_WAKEUP_SOURCE_T source)
701 {
702 usart->CTRL3_B.WSIFLGSEL = source;
703 }
704
705 /*!
706 * @brief Enables the auto baud rate
707 *
708 * @param usart: Select the the USART peripheral.
709 * It can be USART1/USART2/USART3.
710 *
711 * @retval None
712 *
713 * @note The USART2 only for APM32F072 and APM32F091 devices.
714 * The USART3 only for APM32F091 devices.
715 */
USART_EnableAutoBaudRate(USART_T * usart)716 void USART_EnableAutoBaudRate(USART_T* usart)
717 {
718 usart->CTRL2_B.ABRDEN = BIT_SET;
719 }
720
721 /*!
722 * @brief Disables the auto baud rate
723 *
724 * @param usart: Select the the USART peripheral.
725 * It can be USART1/USART2/USART3.
726 *
727 * @retval None
728 *
729 * @note The USART2 only for APM32F072 and APM32F091 devices.
730 * The USART3 only for APM32F091 devices.
731 */
USART_DisableAutoBaudRate(USART_T * usart)732 void USART_DisableAutoBaudRate(USART_T* usart)
733 {
734 usart->CTRL2_B.ABRDEN = BIT_RESET;
735 }
736
737 /*!
738 * @brief Enables the auto baud rate
739 *
740 * @param usart: Select the the USART peripheral.
741 * It can be USART1/USART2/USART3.
742 *
743 * @param mode: specifies the selected USART auto baud rate method
744 * This parameter can be one of the following values:
745 * @arg USART_AUTO_BAUD_RATE_STARTBIT: Start Bit duration measurement
746 * @arg USART_AUTO_BAUD_RATE_FALLINGEDGE: Falling edge to falling edge measurement
747 *
748 * @retval None
749 *
750 * @note The USART2 only for APM32F072 and APM32F091 devices.
751 * The USART3 only for APM32F091 devices.
752 */
USART_ConfigAutoBaudRate(USART_T * usart,USART_AUTO_BAUD_RATE_T mode)753 void USART_ConfigAutoBaudRate(USART_T* usart, USART_AUTO_BAUD_RATE_T mode)
754 {
755 usart->CTRL2_B.ABRDCFG = mode;
756 }
757
758 /*!
759 * @brief Transmit Data
760 *
761 * @param usart: Select the the USART peripheral.
762 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
763 *
764 * @param data: Specifies the Transmits data value
765 *
766 * @retval None
767 *
768 * @note It's not for APM32F030 devices.
769 * The USART2 only for APM32F072 and APM32F091 devices.
770 * The USART3 only for APM32F091 devices.
771 *
772 * @note The value must less than 0x01FF
773 */
USART_TxData(USART_T * usart,uint16_t data)774 void USART_TxData(USART_T* usart, uint16_t data)
775 {
776 usart->TXDATA = (data & (uint16_t)0x01FF);
777 }
778
779 /*!
780 * @brief Received Data
781 *
782 * @param usart: Select the the USART peripheral.
783 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
784 *
785 * @retval Returns the received data value
786 *
787 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
788 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
789 */
USART_RxData(USART_T * usart)790 uint16_t USART_RxData(USART_T* usart)
791 {
792 return (uint16_t)(usart->RXDATA & (uint16_t)0x01FF);
793 }
794
795 /*!
796 * @brief Sets USART the address
797 *
798 * @param usart: Select the the USART peripheral.
799 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
800 *
801 * @retval None
802 *
803 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
804 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
805 */
USART_Address(USART_T * usart,uint8_t address)806 void USART_Address(USART_T* usart, uint8_t address)
807 {
808 usart->CTRL2_B.ADDRL = ((uint8_t)address & 0x0F);
809 usart->CTRL2_B.ADDRH = ((uint8_t)address >> 4 & 0x0F);
810
811 }
812
813 /*!
814 * @brief Enables the USART's mute mode
815 *
816 * @param usart: Select the the USART peripheral.
817 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
818 *
819 * @retval None
820 *
821 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
822 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
823 */
USART_EnableMuteMode(USART_T * usart)824 void USART_EnableMuteMode(USART_T* usart)
825 {
826 usart->CTRL1_B.RXMUTEEN = BIT_SET;
827 }
828
829 /*!
830 * @brief Disables the USART's mute mode
831 *
832 * @param usart: Select the the USART peripheral.
833 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
834 *
835 * @retval None
836 *
837 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
838 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
839 */
USART_DisableMuteMode(USART_T * usart)840 void USART_DisableMuteMode(USART_T* usart)
841 {
842 usart->CTRL1_B.RXMUTEEN = BIT_RESET;
843 }
844
845 /*!
846 * @brief Selects the USART WakeUp method from mute mode
847 *
848 * @param usart: Select the the USART peripheral.
849 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
850 *
851 * @param wakeup: Specifies the selected USART auto baud rate method
852 * This parameter can be one of the following values:
853 * @arg USART_WAKEUP_IDLE_LINE: WakeUp by an idle line detection
854 * @arg USART_WAKEUP_ADDRESS_MARK: WakeUp by an address mark
855 *
856 * @retval None
857 *
858 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
859 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
860 */
USART_ConfigMuteModeWakeUp(USART_T * usart,USART_WAKEUP_T wakeup)861 void USART_ConfigMuteModeWakeUp(USART_T* usart, USART_WAKEUP_T wakeup)
862 {
863 usart->CTRL1_B.WUPMCFG = wakeup;
864 }
865
866 /*!
867 * @brief Enables the USART's LIN mode
868 *
869 * @param usart: Select the the USART peripheral.
870 * It can be USART1/USART2/USART3.
871 *
872 * @retval None
873 *
874 * @note It's not for APM32F030 devices.
875 * The USART2 only for APM32F072 and APM32F091 devices.
876 * The USART3 only for APM32F091 devices.
877 */
USART_EnableLINmode(USART_T * usart)878 void USART_EnableLINmode(USART_T* usart)
879 {
880 usart->CTRL2_B.LINMEN = BIT_SET;
881 }
882
883 /*!
884 * @brief Disables the USART's LIN mode
885 *
886 * @param usart: Select the the USART peripheral.
887 * It can be USART1/USART2/USART3.
888 *
889 * @retval None
890 *
891 * @note It's not for APM32F030 devices.
892 * The USART2 only for APM32F072 and APM32F091 devices.
893 * The USART3 only for APM32F091 devices.
894 */
USART_DisableLINmode(USART_T * usart)895 void USART_DisableLINmode(USART_T* usart)
896 {
897 usart->CTRL2_B.LINMEN = BIT_RESET;
898 }
899
900 /*!
901 * @brief Sets the USART LIN Break detection length.
902 *
903 * @param usart: Select the the USART peripheral.
904 * It can be USART1/USART2/USART3.
905 *
906 * @param length: Specifies the selected USART auto baud rate method
907 * This parameter can be one of the following values:
908 * @arg USART_BREAK_LENGTH_10B: 10-bit break detection
909 * @arg USART_BREAK_LENGTH_11B: 11-bit break detection
910 *
911 * @retval None
912 *
913 * @note It's not for APM32F030 devices.
914 * The USART2 only for APM32F072 and APM32F091 devices.
915 * The USART3 only for APM32F091 devices.
916 */
USART_ConfigLINbreakDetectLength(USART_T * usart,USART_BREAK_LENGTH_T length)917 void USART_ConfigLINbreakDetectLength(USART_T* usart, USART_BREAK_LENGTH_T length)
918 {
919 usart->CTRL2_B.LBDLCFG = length;
920 }
921
922 /*!
923 * @brief Enables the USART's Half-duplex mode
924 *
925 * @param usart: Select the the USART peripheral.
926 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
927 * @retval None
928 *
929 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
930 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
931 */
USART_EnableHalfDuplex(USART_T * usart)932 void USART_EnableHalfDuplex(USART_T* usart)
933 {
934 usart->CTRL3_B.HDEN = BIT_SET;
935 }
936
937 /*!
938 * @brief Disables the USART's Half-duplex mode
939 *
940 * @param usart: Select the the USART peripheral.
941 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
942 * @retval None
943 *
944 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
945 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
946 */
USART_DisableHalfDuplex(USART_T * usart)947 void USART_DisableHalfDuplex(USART_T* usart)
948 {
949 usart->CTRL3_B.HDEN = BIT_RESET;
950 }
951
952 /*!
953 * @brief Enables the USART's Smart Card mode
954 *
955 * @param usart: Select the the USART peripheral.
956 * It can be USART1/USART2/USART3.
957 *
958 * @retval None
959 *
960 * @note It's not for APM32F030 devices.
961 * The USART2 only for APM32F072 and APM32F091 devices.
962 * The USART3 only for APM32F091 devices.
963 */
USART_EnableSmartCard(USART_T * usart)964 void USART_EnableSmartCard(USART_T* usart)
965 {
966 usart->CTRL3_B.SCEN = BIT_SET;
967 }
968
969 /*!
970 * @brief Disables the USART's Smart Card mode
971 *
972 * @param usart: Select the the USART peripheral.
973 * It can be USART1/USART2/USART3.
974 *
975 * @retval None
976 *
977 * @note It's not for APM32F030 devices.
978 * The USART2 only for APM32F072 and APM32F091 devices.
979 * The USART3 only for APM32F091 devices.
980 */
USART_DisableSmartCard(USART_T * usart)981 void USART_DisableSmartCard(USART_T* usart)
982 {
983 usart->CTRL3_B.SCEN = BIT_RESET;
984 }
985
986 /*!
987 * @brief Enables the USART's NACK transmission
988 *
989 * @param usart: Select the the USART peripheral.
990 * It can be USART1/USART2/USART3.
991 *
992 * @retval None
993 *
994 * @note It's not for APM32F030 devices.
995 * The USART2 only for APM32F072 and APM32F091 devices.
996 * The USART3 only for APM32F091 devices.
997 */
USART_EnableSmartCardNACK(USART_T * usart)998 void USART_EnableSmartCardNACK(USART_T* usart)
999 {
1000 usart->CTRL3_B.SCNACKEN = BIT_SET;
1001 }
1002
1003 /*!
1004 * @brief Disables the USART's NACK transmission
1005 *
1006 * @param usart: Select the the USART peripheral.
1007 * It can be USART1/USART2/USART3.
1008 *
1009 * @retval None
1010 *
1011 * @note It's not for APM32F030 devices.
1012 * The USART2 only for APM32F072 and APM32F091 devices.
1013 * The USART3 only for APM32F091 devices.
1014 */
USART_DisableSmartCardNACK(USART_T * usart)1015 void USART_DisableSmartCardNACK(USART_T* usart)
1016 {
1017 usart->CTRL3_B.SCNACKEN = BIT_RESET;
1018 }
1019
1020 /*!
1021 * @brief Config the specified USART guard time.
1022 *
1023 * @param usart: Select the the USART peripheral.
1024 * It can be USART1/USART2/USART3.
1025 *
1026 * @param guardTime: specifies the guard time value
1027 *
1028 * @retval None
1029 *
1030 * @note It's not for APM32F030 devices.
1031 * The USART2 only for APM32F072 and APM32F091 devices.
1032 * The USART3 only for APM32F091 devices.
1033 */
USART_ConfigGuardTime(USART_T * usart,uint8_t guardTime)1034 void USART_ConfigGuardTime(USART_T* usart, uint8_t guardTime)
1035 {
1036 usart->GTPSC &= (uint16_t)0x00FF;
1037 usart->GTPSC_B.GRDT = (uint8_t)guardTime;
1038 }
1039
1040 /*!
1041 * @brief Config the specified USART Smart Card number.
1042 *
1043 * @param usart: Select the the USART peripheral.
1044 * It can be USART1/USART2/USART3.
1045 *
1046 * @param autoCount: specifies the Smart Card auto retry count.
1047 * It's <= 0x07.
1048 *
1049 * @retval None
1050 *
1051 * @note It's not for APM32F030 devices.
1052 * The USART2 only for APM32F072 and APM32F091 devices.
1053 * The USART3 only for APM32F091 devices.
1054 */
USART_ConfigAutoCount(USART_T * usart,uint8_t autoCount)1055 void USART_ConfigAutoCount(USART_T* usart, uint8_t autoCount)
1056 {
1057 usart->CTRL3_B.SCARCCFG = autoCount;
1058 }
1059
1060 /*!
1061 * @brief Config the Smart Card Block length.
1062 *
1063 * @param usart: Select the the USART peripheral.
1064 * It can be USART1/USART2/USART3.
1065 *
1066 * @param blockSize: specifies the Smart Card block size.
1067 *
1068 * @retval None
1069 *
1070 * @note It's not for APM32F030 devices.
1071 * The USART2 only for APM32F072 and APM32F091 devices.
1072 * The USART3 only for APM32F091 devices.
1073 */
USART_ConfigBlockSize(USART_T * usart,uint8_t blockSize)1074 void USART_ConfigBlockSize(USART_T* usart, uint8_t blockSize)
1075 {
1076 usart->RXTO_B.BLEN = (uint8_t)blockSize;
1077 }
1078
1079 /*!
1080 * @brief Enables the USART's IrDA mode.
1081 *
1082 * @param usart: Select the the USART peripheral.
1083 * It can be USART1/USART2/USART3.
1084 *
1085 * @retval None
1086 *
1087 * @note It's not for APM32F030 devices.
1088 * The USART2 only for APM32F072 and APM32F091 devices.
1089 * The USART3 only for APM32F091 devices.
1090 */
USART_EnableIrDA(USART_T * usart)1091 void USART_EnableIrDA(USART_T* usart)
1092 {
1093 usart->CTRL3_B.IREN = BIT_SET;
1094 }
1095
1096 /*!
1097 * @brief Disables the USART's IrDA mode.
1098 *
1099 * @param usart: Select the the USART peripheral.
1100 * It can be USART1/USART2/USART3.
1101 *
1102 * @retval None
1103 *
1104 * @note It's not for APM32F030 devices.
1105 * The USART2 only for APM32F072 and APM32F091 devices.
1106 * The USART3 only for APM32F091 devices.
1107 */
USART_DisableIrDA(USART_T * usart)1108 void USART_DisableIrDA(USART_T* usart)
1109 {
1110 usart->CTRL3_B.IREN = BIT_RESET;
1111 }
1112
1113 /*!
1114 * @brief Configures the USART's IrDA interface.
1115 *
1116 * @param usart: Select the the USART peripheral.
1117 * It can be USART1/USART2/USART3.
1118 *
1119 * @param IrDAMode:Specifies the selected USART auto baud rate method
1120 * This parameter can be one of the following values:
1121 * @arg USART_IRDA_MODE_NORMAL: Normal
1122 * @arg USART_IRDA_MODE_LOWPOWER: Low-Power
1123 *
1124 * @retval None
1125 *
1126 * @note It's not for APM32F030 devices.
1127 * The USART2 only for APM32F072 and APM32F091 devices.
1128 * The USART3 only for APM32F091 devices.
1129 */
USART_ConfigIrDAMode(USART_T * usart,USART_IRDA_MODE_T IrDAMode)1130 void USART_ConfigIrDAMode(USART_T* usart, USART_IRDA_MODE_T IrDAMode)
1131 {
1132 usart->CTRL3_B.IRLPEN = IrDAMode;
1133 }
1134
1135 /*!
1136 * @brief Configure the the USART Address detection length.
1137 *
1138 * @param usart: Select the the USART peripheral.
1139 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1140 *
1141 * @param address: Specifies the selected USART auto baud rate method
1142 * This parameter can be one of the following values:
1143 * @arg USART_ADDRESS_MODE_4B: 4-bit address length detection
1144 * @arg USART_ADDRESS_MODE_7B: 7-bit address length detection
1145 *
1146 * @retval None
1147 *
1148 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1149 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1150 */
USART_ConfigAddressDetection(USART_T * usart,USART_ADDRESS_MODE_T address)1151 void USART_ConfigAddressDetection(USART_T* usart, USART_ADDRESS_MODE_T address)
1152 {
1153 usart->CTRL2_B.ADDRLEN = address;
1154 }
1155
1156 /*!
1157 * @brief Enables the DE functionality
1158 *
1159 * @param usart: Select the the USART peripheral.
1160 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1161 *
1162 * @retval None
1163 *
1164 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1165 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1166 */
USART_EnableDE(USART_T * usart)1167 void USART_EnableDE(USART_T* usart)
1168 {
1169 usart->CTRL3_B.DEN = BIT_SET;
1170 }
1171
1172 /*!
1173 * @brief Disables the DE functionality
1174 *
1175 * @param usart: Select the the USART peripheral.
1176 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1177 *
1178 * @retval None
1179 *
1180 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1181 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1182 */
USART_DisableDE(USART_T * usart)1183 void USART_DisableDE(USART_T* usart)
1184 {
1185 usart->CTRL3_B.DEN = BIT_RESET;
1186 }
1187
1188 /*!
1189 * @brief Selects the USART WakeUp method from mute mode
1190 *
1191 * @param usart: Select the the USART peripheral.
1192 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1193 *
1194 * @param polarity: Specifies the selected USART auto baud rate method
1195 * This parameter can be one of the following values:
1196 * @arg USART_DE_POL_HIGH: DE signal is active high
1197 * @arg USART_DE_POL_LOW: DE signal is active low
1198 *
1199 * @retval None
1200 *
1201 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1202 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1203 */
USART_ConfigDEPolarity(USART_T * usart,USART_DE_POL_T polarity)1204 void USART_ConfigDEPolarity(USART_T* usart, USART_DE_POL_T polarity)
1205 {
1206 usart->CTRL3_B.DPCFG = polarity;
1207 }
1208
1209 /*!
1210 * @brief Sets the driver enable assertion time value
1211 *
1212 * @param usart: Select the the USART peripheral.
1213 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1214 *
1215 * @param value: Specifies the DE assertion time value
1216 *
1217 * @retval None
1218 *
1219 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1220 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1221 */
USART_DEAssertionTimeValue(USART_T * usart,uint8_t value)1222 void USART_DEAssertionTimeValue(USART_T* usart, uint8_t value)
1223 {
1224 usart->CTRL1_B.DLTEN = (uint8_t)0x00;
1225
1226 if (value <= (uint8_t)0x1F)
1227 {
1228 usart->CTRL1_B.DLTEN = ((uint8_t)value & 0x1F);
1229 }
1230 }
1231
1232 /*!
1233 * @brief Sets the driver enable deassertion time value
1234 *
1235 * @param usart: Select the the USART peripheral.
1236 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1237 *
1238 * @param value: Specifies the DE deassertion time value
1239 *
1240 * @retval None
1241 *
1242 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1243 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1244 */
USART_DEDeassertionTimeValue(USART_T * usart,uint8_t value)1245 void USART_DEDeassertionTimeValue(USART_T* usart, uint8_t value)
1246 {
1247 usart->CTRL1_B.DDLTEN = (uint8_t)0x00;
1248
1249 if (value <= (uint8_t)0x1F)
1250 {
1251 usart->CTRL1_B.DDLTEN = ((uint8_t)value & 0x1F);
1252 }
1253 }
1254
1255 /*!
1256 * @brief Enables the USART DMA interface
1257 *
1258 * @param usart: Select the the USART peripheral.
1259 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1260 *
1261 * @param dmaReq: Specifies the DMA request
1262 * This parameter can be any combination of the following values:
1263 * @arg USART_DMA_REQUEST_RX: USART DMA receive request
1264 * @arg USART_DMA_REQUEST_TX: USART DMA transmit request
1265 *
1266 * @retval None
1267 *
1268 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1269 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1270 */
USART_EnableDMA(USART_T * usart,uint32_t dmaReq)1271 void USART_EnableDMA(USART_T* usart, uint32_t dmaReq)
1272 {
1273 if (dmaReq == USART_DMA_REQUEST_RX)
1274 {
1275 usart->CTRL3_B.DMARXEN = BIT_SET;
1276 }
1277 else if (dmaReq == USART_DMA_REQUEST_TX)
1278 {
1279 usart->CTRL3_B.DMATXEN = BIT_SET;
1280 }
1281 else if (dmaReq == (BIT6 | BIT7))
1282 {
1283 usart->CTRL3_B.DMATXEN = BIT_SET;
1284 usart->CTRL3_B.DMARXEN = BIT_SET;
1285 }
1286 }
1287
1288 /*!
1289 * @brief Disables the USART DMA interface
1290 *
1291 * @param usart: Select the the USART peripheral.
1292 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1293 *
1294 * @param dmaReq: Specifies the DMA request
1295 * This parameter can be any combination of the following values:
1296 * @arg USART_DMA_REQUEST_RX: USART DMA receive request
1297 * @arg USART_DMA_REQUEST_TX: USART DMA transmit request
1298 *
1299 * @retval None
1300 *
1301 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1302 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1303 */
USART_DisableDMA(USART_T * usart,uint32_t dmaReq)1304 void USART_DisableDMA(USART_T* usart, uint32_t dmaReq)
1305 {
1306 if (dmaReq == USART_DMA_REQUEST_RX)
1307 {
1308 usart->CTRL3_B.DMARXEN = BIT_RESET;
1309 }
1310 else if (dmaReq == USART_DMA_REQUEST_TX)
1311 {
1312 usart->CTRL3_B.DMATXEN = BIT_RESET;
1313 }
1314 else if (dmaReq == (BIT6 | BIT7))
1315 {
1316 usart->CTRL3_B.DMATXEN = BIT_RESET;
1317 usart->CTRL3_B.DMARXEN = BIT_RESET;
1318 }
1319 }
1320
1321 /*!
1322 * @brief Enables or disables the USART DMA interface when reception error occurs
1323 *
1324 * @param usart: Select the the USART peripheral.
1325 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1326 *
1327 * @param dmaReq: Specifies the DMA request
1328 * This parameter can be one of the following values:
1329 * @arg USART_DMA_RXERR_ENABLE: DMA receive request enabled
1330 * @arg USART_DMA_RXERR_DISABLE: DMA receive request disabled
1331 *
1332 * @retval None
1333 *
1334 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1335 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1336 */
USART_ConfigDMAReceptionError(USART_T * usart,USART_DMA_RXERR_T error)1337 void USART_ConfigDMAReceptionError(USART_T* usart, USART_DMA_RXERR_T error)
1338 {
1339 usart->CTRL3_B.DDISRXEEN = error;
1340 }
1341
1342 /*!
1343 * @brief Enables the specified interrupts
1344 *
1345 * @param usart: Select the the USART peripheral.
1346 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1347 *
1348 * @param interrupt: Specifies the USART interrupts sources
1349 * The parameter can be one of following values:
1350 * @arg USART_INT_WAKEUP: Wake up interrupt (Not for APM32F030 devices)
1351 * @arg USART_INT_CMIE: Character match interrupt
1352 * @arg USART_INT_EOBIE: End of Block interrupt
1353 * @arg USART_INT_RXTOIE: Receive time out interrupt
1354 * @arg USART_INT_CTSIE: CTS change interrupt
1355 * @arg USART_INT_LBDIE: LIN Break detection interrupt (Not for APM32F030 devices)
1356 * @arg USART_INT_TXBEIE: Tansmit Data Register empty interrupt
1357 * @arg USART_INT_TXCIE: Transmission complete interrupt
1358 * @arg USART_INT_RXBNEIE: Receive Data register not empty interrupt
1359 * @arg USART_INT_IDLEIE: Idle line detection interrupt
1360 * @arg USART_INT_PEIE: Parity Error interrupt
1361 * @arg USART_INT_ERRIE: Error interrupt
1362 *
1363 * @retval None
1364 *
1365 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1366 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1367 */
USART_EnableInterrupt(USART_T * usart,USART_INT_T interrupt)1368 void USART_EnableInterrupt(USART_T* usart, USART_INT_T interrupt)
1369 {
1370 if ((interrupt == USART_INT_ERRIE) | (interrupt == USART_INT_CTSIE) | (interrupt == USART_INT_WAKEUP))
1371 {
1372 usart->CTRL3 |= (uint32_t)interrupt;
1373 }
1374 else if (interrupt == USART_INT_LBDIE)
1375 {
1376 usart->CTRL2 |= (uint32_t)interrupt;
1377 }
1378 else
1379 {
1380 usart->CTRL1 |= (uint32_t)interrupt;
1381 }
1382 }
1383
1384 /*!
1385 * @brief Disables the specified interrupts
1386 *
1387 * @param usart: Select the the USART peripheral.
1388 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1389 *
1390 * @param interrupt: Specifies the USART interrupts sources
1391 * The parameter can be one of following values:
1392 * @arg USART_INT_WAKEUP: Wake up interrupt (Not for APM32F030 devices)
1393 * @arg USART_INT_CMIE: Character match interrupt
1394 * @arg USART_INT_EOBIE: End of Block interrupt
1395 * @arg USART_INT_RXTOIE: Receive time out interrupt
1396 * @arg USART_INT_CTSIE: CTS change interrupt
1397 * @arg USART_INT_LBDIE: LIN Break detection interrupt (Not for APM32F030 devices)
1398 * @arg USART_INT_TXBEIE: Tansmit Data Register empty interrupt
1399 * @arg USART_INT_TXCIE: Transmission complete interrupt
1400 * @arg USART_INT_RXBNEIE: Receive Data register not empty interrupt
1401 * @arg USART_INT_IDLEIE: Idle line detection interrupt
1402 * @arg USART_INT_PEIE: Parity Error interrupt
1403 * @arg USART_INT_ERRIE: Error interrupt
1404 *
1405 * @retval None
1406 *
1407 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1408 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1409 */
USART_DisableInterrupt(USART_T * usart,USART_INT_T interrupt)1410 void USART_DisableInterrupt(USART_T* usart, USART_INT_T interrupt)
1411 {
1412 if ((interrupt == USART_INT_ERRIE) | (interrupt == USART_INT_CTSIE) | (interrupt == USART_INT_WAKEUP))
1413 {
1414 usart->CTRL3 &= (uint32_t)~interrupt;
1415 }
1416 else if (interrupt == USART_INT_LBDIE)
1417 {
1418 usart->CTRL2 &= (uint32_t)~interrupt;
1419 }
1420 else
1421 {
1422 usart->CTRL1 &= (uint32_t)~interrupt;
1423 }
1424 }
1425
1426 /*!
1427 * @brief Enables the specified USART's Request.
1428 *
1429 * @param usart: Select the the USART peripheral.
1430 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1431 *
1432 * @param request: specifies the USART request
1433 * This parameter can be one of the following values:
1434 * @arg USART_REQUEST_ABRQ: Auto Baud Rate Request
1435 * @arg USART_REQUEST_SBQ: Send Break Request
1436 * @arg USART_REQUEST_MMQ: Mute Mode Request
1437 * @arg USART_REQUEST_RDFQ: Receive data flush Request
1438 * @arg USART_REQUEST_TDFQ: Transmit data flush Request
1439 *
1440 * @retval None
1441 *
1442 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1443 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1444 */
USART_EnableRequest(USART_T * usart,USART_REQUEST_T request)1445 void USART_EnableRequest(USART_T* usart, USART_REQUEST_T request)
1446 {
1447 if (request == USART_REQUEST_ABRQ)
1448 {
1449 usart->REQUEST_B.ABRDQ = BIT_SET;
1450 }
1451
1452 if (request == USART_REQUEST_SBQ)
1453 {
1454 usart->REQUEST_B.TXBFQ = BIT_SET;
1455 }
1456
1457 if (request == USART_REQUEST_MMQ)
1458 {
1459 usart->REQUEST_B.MUTEQ = BIT_SET;
1460 }
1461
1462 if (request == USART_REQUEST_RDFQ)
1463 {
1464 usart->REQUEST_B.RXDFQ = BIT_SET;
1465 }
1466
1467 if (request == USART_REQUEST_TDFQ)
1468 {
1469 usart->REQUEST_B.TXDFQ = BIT_SET;
1470 }
1471 }
1472
1473 /*!
1474 * @brief Disables the specified USART's Request.
1475 *
1476 * @param usart: Select the the USART peripheral.
1477 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1478 *
1479 * @param request: specifies the USART request
1480 * This parameter can be one of the following values:
1481 * @arg USART_REQUEST_ABRQ: Auto Baud Rate Request
1482 * @arg USART_REQUEST_SBQ: Send Break Request
1483 * @arg USART_REQUEST_MMQ: Mute Mode Request
1484 * @arg USART_REQUEST_RDFQ: Receive data flush Request
1485 * @arg USART_REQUEST_TDFQ: Transmit data flush Request
1486 *
1487 * @retval None
1488 *
1489 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1490 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1491 */
USART_DisableRequest(USART_T * usart,USART_REQUEST_T request)1492 void USART_DisableRequest(USART_T* usart, USART_REQUEST_T request)
1493 {
1494 if (request == USART_REQUEST_ABRQ)
1495 {
1496 usart->REQUEST_B.ABRDQ = BIT_RESET;
1497 }
1498
1499 if (request == USART_REQUEST_SBQ)
1500 {
1501 usart->REQUEST_B.TXBFQ = BIT_RESET;
1502 }
1503
1504 if (request == USART_REQUEST_MMQ)
1505 {
1506 usart->REQUEST_B.MUTEQ = BIT_RESET;
1507 }
1508
1509 if (request == USART_REQUEST_RDFQ)
1510 {
1511 usart->REQUEST_B.RXDFQ = BIT_RESET;
1512 }
1513
1514 if (request == USART_REQUEST_TDFQ)
1515 {
1516 usart->REQUEST_B.TXDFQ = BIT_RESET;
1517 }
1518 }
1519
1520 /*!
1521 * @brief Enables or disables the USART DMA interface when reception error occurs
1522 *
1523 * @param usart: Select the the USART peripheral.
1524 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1525 *
1526 * @param overDetection: specifies the OVR detection status in case of OVR error
1527 * This parameter can be one of the following values:
1528 * @arg USART_OVER_DETECTION_ENABLE: OVR error detection enabled
1529 * @arg USART_OVER_DETECTION_DISABLE: OVR error detection disabled
1530 *
1531 * @retval None
1532 *
1533 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1534 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1535 */
USART_ConfigOverrunDetection(USART_T * usart,USART_OVER_DETECTION_T overDetection)1536 void USART_ConfigOverrunDetection(USART_T* usart, USART_OVER_DETECTION_T overDetection)
1537 {
1538 usart->CTRL3_B.OVRDEDIS = overDetection;
1539 }
1540
1541 /*!
1542 * @brief Read the specified USART flag
1543 *
1544 * @param usart: Select the the USART peripheral.
1545 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1546 *
1547 * @param flag: Specifies the flag to check
1548 * The parameter can be one of following values:
1549 * @arg USART_FLAG_RXENACKF: Receive Enable Acknowledge Flag
1550 * @arg USART_FLAG_TXENACKF: Transmit Enable Acknowledge Flag
1551 * @arg USART_FLAG_WAKEUP: Wake Up from stop mode Flag (Not for APM32F030 devices)
1552 * @arg USART_FLAG_RWF: Send Break flag (Not for APM32F030 devices)
1553 * @arg USART_FLAG_SBF: Send Break flag
1554 * @arg USART_FLAG_CMF: Character match flag
1555 * @arg USART_FLAG_BUSY: Busy flag
1556 * @arg USART_FLAG_ABRTF: Auto baud rate flag
1557 * @arg USART_FLAG_ABRTE: Auto baud rate error flag
1558 * @arg USART_FLAG_EOBF: End of block flag (Not for APM32F030 devices)
1559 * @arg USART_FLAG_RXTOF: Receive time out flag
1560 * @arg USART_FLAG_CTSF: CTS Change flag
1561 * @arg USART_FLAG_CTSIF: CTS interrupt flag
1562 * @arg USART_FLAG_LBDF�� LIN Break Detection Flag (Not for APM32F030 devices)
1563 * @arg USART_FLAG_TXBE: Transmit data register empty flag
1564 * @arg USART_FLAG_TXC: Transmission Complete flag
1565 * @arg USART_FLAG_RXBNE: Receive data buffer not empty flag
1566 * @arg USART_FLAG_IDLEF: Idle Line detection flag
1567 * @arg USART_FLAG_OVRE: OverRun Error flag
1568 * @arg USART_FLAG_NEF: Noise Error flag
1569 * @arg USART_FLAG_FEF: Framing Error flag
1570 * @arg USART_FLAG_PEF: Parity Error flag
1571 *
1572 * @retval The new state of flag (SET or RESET)
1573 *
1574 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1575 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1576 */
1577
USART_ReadStatusFlag(USART_T * usart,USART_FLAG_T flag)1578 uint8_t USART_ReadStatusFlag(USART_T* usart, USART_FLAG_T flag)
1579 {
1580 if ((usart->STS & (uint32_t)flag) != RESET)
1581 {
1582 return SET;
1583 }
1584
1585 return RESET;
1586 }
1587
1588 /*!
1589 * @brief Clear the specified USART flag
1590 *
1591 * @param usart: Select the the USART peripheral.
1592 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1593 *
1594 * @param flag: Specifies the flag to clear
1595 * The parameter can be any combination of following values:
1596 * @arg USART_FLAG_WAKEUP: Wake Up from stop mode Flag (Not for APM32F030 devices)
1597 * @arg USART_FLAG_CMF: Character match flag
1598 * @arg USART_FLAG_EOBF: End of block flag (Not for APM32F030 devices)
1599 * @arg USART_FLAG_RXTOF: Receive time out flag
1600 * @arg USART_FLAG_CTSIF: CTS interrupt flag
1601 * @arg USART_FLAG_LBDF�� LIN Break Detection Flag (Not for APM32F030 devices)
1602 * @arg USART_FLAG_TXC: Transmission Complete flag
1603 * @arg USART_FLAG_IDLEF: Idle Line detection flag
1604 * @arg USART_FLAG_OVRE: OverRun Error flag
1605 * @arg USART_FLAG_NEF: Noise Error flag
1606 * @arg USART_FLAG_FEF: Framing Error flag
1607 * @arg USART_FLAG_PEF: Parity Error flag
1608 *
1609 * @retval Note
1610 *
1611 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1612 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1613 */
1614
USART_ClearStatusFlag(USART_T * usart,uint32_t flag)1615 void USART_ClearStatusFlag(USART_T* usart, uint32_t flag)
1616 {
1617 usart->INTFCLR = (uint32_t)flag;
1618 }
1619
1620 /*!
1621 * @brief Read the specified USART interrupt flag
1622 *
1623 * @param usart: Select the the USART peripheral.
1624 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1625 *
1626 * @param flag: Specifies the USART interrupt flag to check
1627 * The parameter can be one of following values:
1628 * @arg USART_INT_FLAG_WAKEUP: Wake up interrupt flag (Not for APM32F030 devices)
1629 * @arg USART_INT_FLAG_CMF: Character match interrupt flag
1630 * @arg USART_INT_FLAG_EOBF: End of block interrupt flag Not for APM32F030 devices)
1631 * @arg USART_INT_FLAG_RXTOF: Receive time out interrupt flag
1632 * @arg USART_INT_FLAG_CTSIF: CTS interrupt flag
1633 * @arg USART_INT_FLAG_LBDF: LIN Break detection interrupt flag (Not for APM32F030 devices)
1634 * @arg USART_INT_FLAG_TXBE: Transmit data register empty interrupt flag
1635 * @arg USART_INT_FLAG_TXC: Transmission Complete interrupt flag
1636 * @arg USART_INT_FLAG_RXBNE: Receive data buffer not empty interrupt flag
1637 * @arg USART_INT_FLAG_IDLE: Idle Line detection interrupt flag
1638 * @arg USART_INT_FLAG_OVRE: OverRun Error interrupt flag
1639 * @arg USART_INT_FLAG_NE: Noise Error interrupt flag
1640 * @arg USART_INT_FLAG_FE: Framing Error interrupt flag
1641 * @arg USART_INT_FLAG_PE: Parity Error interrupt flag
1642 *
1643 * @retval The new state of flag (SET or RESET)
1644 *
1645 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1646 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1647 */
1648
USART_ReadIntFlag(USART_T * usart,USART_INT_FLAG_T flag)1649 uint8_t USART_ReadIntFlag(USART_T* usart, USART_INT_FLAG_T flag)
1650 {
1651 uint32_t intEnable = 0;
1652 uint32_t intFlag = 0;
1653
1654 if (flag & 0x0E)
1655 {
1656 intEnable = usart->CTRL3_B.ERRIEN;
1657 intFlag = (usart->STS) & flag;
1658 }
1659 else if (flag & 0xF0)
1660 {
1661 intEnable = (usart->CTRL1)& flag;
1662 intFlag = (usart->STS) & flag;
1663 }
1664 else if (flag & 0x01)
1665 {
1666 intEnable = usart->CTRL1_B.PEIEN;
1667 intFlag = usart->STS_B.PEFLG;
1668 }
1669 else if (flag & 0x200)
1670 {
1671 intEnable = usart->CTRL3_B.CTSIEN;
1672 intFlag = usart->STS_B.CTSFLG;
1673 }
1674 else if (flag & 0x100)
1675 {
1676 intEnable = usart->CTRL2_B.LBDIEN;
1677 intFlag = usart->STS_B.LBDFLG;
1678 }
1679 else if (flag & 0x800)
1680 {
1681 intEnable = usart->CTRL1_B.RXTOIEN;
1682 intFlag = usart->STS_B.RXTOFLG;
1683 }
1684 else if (flag & 0x1000)
1685 {
1686 intEnable = usart->CTRL1_B.EOBIEN;
1687 intFlag = usart->STS_B.EOBFLG;
1688 }
1689 else if (flag & 0x20000)
1690 {
1691 intEnable = usart->CTRL1_B.CMIEN;
1692 intFlag = usart->STS_B.CMFLG;
1693 }
1694 else if (flag & 0x100000)
1695 {
1696 intEnable = usart->CTRL3_B.WSMIEN;
1697 intFlag = usart->STS_B.WSMFLG;
1698 }
1699
1700 if (intFlag && intEnable)
1701 {
1702 return SET;
1703 }
1704
1705 return RESET;
1706 }
1707
1708 /*!
1709 * @brief Clears the USART interrupt pending bits
1710 *
1711 * @param usart: Select the the USART peripheral.
1712 * It can be USART1/USART2/USART3/USART4/USART5/USART6/USART7/USART8.
1713 *
1714 * @param flag: Specifies the USART interrupt flag to clear
1715 * The parameter can be any combination following values:
1716 * @arg USART_INT_FLAG_WAKEUP: Wake up interrupt flag (Not for APM32F030 devices)
1717 * @arg USART_INT_FLAG_CMF: Character match interrupt flag
1718 * @arg USART_INT_FLAG_EOBF: End of block interrupt flag (Not for APM32F030 devices)
1719 * @arg USART_INT_FLAG_RXTOF: Receive time out interrupt flag
1720 * @arg USART_INT_FLAG_CTSIF: CTS interrupt flag
1721 * @arg USART_INT_FLAG_LBDF: LIN Break detection interrupt flag (Not for APM32F030 devices)
1722 * @arg USART_INT_FLAG_TXC: Transmission Complete interrupt flag
1723 * @arg USART_INT_FLAG_IDLE: Idle Line detection interrupt flag
1724 * @arg USART_INT_FLAG_OVRE: OverRun Error interrupt flag
1725 * @arg USART_INT_FLAG_NE: Noise Error interrupt flag
1726 * @arg USART_INT_FLAG_FE: Framing Error interrupt flag
1727 * @arg USART_INT_FLAG_PE: Parity Error interrupt flag
1728 *
1729 * @retval None
1730 *
1731 * @note The USART3/USART4 only for APM32F072 and APM32F091 devices.
1732 * USART5, USART6, USART7 and USART8 are available only for APM32F091 devices.
1733 */
USART_ClearIntFlag(USART_T * usart,uint32_t flag)1734 void USART_ClearIntFlag(USART_T* usart, uint32_t flag)
1735 {
1736 usart->INTFCLR |= (uint32_t)flag;
1737 }
1738
1739 /**@} end of group USART_Functions*/
1740 /**@} end of group USART_Driver*/
1741 /**@} end of group APM32F0xx_StdPeriphDriver*/
1742