1 /*********************************************************************************************************//**
2  * @file    ht32f5xxxx_usart.c
3  * @version $Rev:: 7698         $
4  * @date    $Date:: 2024-04-15 #$
5  * @brief   This file provides all the USART firmware functions.
6  *************************************************************************************************************
7  * @attention
8  *
9  * Firmware Disclaimer Information
10  *
11  * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
12  *    code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
13  *    proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
14  *    other intellectual property laws.
15  *
16  * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
17  *    code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
18  *    other than HOLTEK and the customer.
19  *
20  * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
21  *    only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
22  *    the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
23  *    the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
24  *
25  * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
26  ************************************************************************************************************/
27 
28 /* Includes ------------------------------------------------------------------------------------------------*/
29 #include "ht32f5xxxx_usart.h"
30 
31 /** @addtogroup HT32F5xxxx_Peripheral_Driver HT32F5xxxx Peripheral Driver
32   * @{
33   */
34 
35 /** @defgroup USART USART
36   * @brief USART driver modules
37   * @{
38   */
39 
40 
41 /* Private constants ---------------------------------------------------------------------------------------*/
42 /** @defgroup USART_Private_Define USART private definitions
43   * @{
44   */
45 #define CR_CLEAR_Mask             ((u32)0xFFFFE0FC)
46 
47 #define USART_BREAK_ON            ((u32)0x00004000)
48 #define USART_BREAK_OFF           ((u32)0xFFFFBFFF)
49 
50 #define USART_PBE_ON              ((u32)0x00000800)
51 #define USART_SPE_ON              ((u32)0x00002000)
52 #define USART_SPE_OFF             ((u32)0xFFFFDFFF)
53 
54 #define USART_EN_ON               ((u32)0x00000010)
55 
56 #define USART_HFCEN_ON            ((u32)0x00000008)
57 #define USART_HFCEN_OFF           ((u32)0xFFFFFFF7)
58 
59 #define USART_RXTOEN_ON           ((u32)0x00000080)
60 
61 #define FCR_TL_Mask               ((u32)0x00000030)
62 
63 #define TRSM_CLEAR_Mask           ((u32)0xFFFFFFFB)
64 #define TPR_TG_Mask               ((u32)0xFFFF00FF)
65 #define ICR_IRDAPSC_Mask          ((u32)0xFFFF00FF)
66 #define TPR_RXTOIC_Mask           ((u32)0xFFFFFF80)
67 #define RS485CR_ADDM_Mask         ((u32)0xFFFF00FF)
68 
69 #define USART_IRDA_ON             ((u32)0x00000001)
70 #define USART_IRDA_OFF            ((u32)0xFFFFFFFE)
71 
72 #define USART_INV_ON              ((u32)0x00000010)
73 
74 #define USART_RS485NMM_ON         ((u32)0x00000002)
75 #define USART_RS485NMM_OFF        ((u32)0xFFFFFFFD)
76 
77 #define USART_RS485AAD_ON         ((u32)0x00000004)
78 #define USART_RS485AAD_OFF        ((u32)0xFFFFFFFB)
79 /**
80   * @}
81   */
82 
83 /* Global functions ----------------------------------------------------------------------------------------*/
84 /** @defgroup USART_Exported_Functions USART exported functions
85   * @{
86   */
87 /*********************************************************************************************************//**
88  * @brief Deinitialize the USART/UART peripheral registers to their default reset values.
89  * @param USARTx: Parameter to select the UxART peripheral.
90  * @retval None
91  ************************************************************************************************************/
USART_DeInit(HT_USART_TypeDef * USARTx)92 void USART_DeInit(HT_USART_TypeDef* USARTx)
93 {
94   RSTCU_PeripReset_TypeDef RSTCUReset = {{0}};
95   u32 uIPAddr = (u32)USARTx;
96 
97   /* Check the parameters                                                                                   */
98   Assert_Param(IS_USART(USARTx));
99 
100   switch (uIPAddr)
101   {
102     #if (!LIBCFG_NO_USART0)
103     case HT_USART0_BASE:
104     {
105       RSTCUReset.Bit.USART0 = 1;
106       break;
107     }
108     #endif
109     #if (LIBCFG_USART1)
110     case HT_USART1_BASE:
111     {
112       RSTCUReset.Bit.USART1 = 1;
113       break;
114     }
115     #endif
116     case HT_UART0_BASE:
117     {
118       RSTCUReset.Bit.UART0 = 1;
119       break;
120     }
121     #if (LIBCFG_UART1)
122     case HT_UART1_BASE:
123     {
124       RSTCUReset.Bit.UART1 = 1;
125       break;
126     }
127     #endif
128     #if (LIBCFG_UART2)
129     case HT_UART2_BASE:
130     {
131       RSTCUReset.Bit.UART2 = 1;
132       break;
133     }
134     #endif
135     #if (LIBCFG_UART3)
136     case HT_UART3_BASE:
137     {
138       RSTCUReset.Bit.UART3 = 1;
139       break;
140     }
141     #endif
142   }
143 
144   RSTCU_PeripReset(RSTCUReset, ENABLE);
145 }
146 
147 /*********************************************************************************************************//**
148  * @brief Initialize the USART/UART peripheral according to the specified parameters in the USART_InitStruct.
149  * @param USARTx: Parameter to select the UxART peripheral.
150  * @param USART_InitStruct: pointer to a USART_InitTypeDef structure.
151  * @retval None
152  ************************************************************************************************************/
USART_Init(HT_USART_TypeDef * USARTx,USART_InitTypeDef * USART_InitStruct)153 void USART_Init(HT_USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
154 {
155   u32 uIPClock = 0;
156   u32 uIPAddr = (u32)USARTx;
157 
158   /* Check the parameters                                                                                   */
159   Assert_Param(IS_USART(USARTx));
160   Assert_Param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
161   Assert_Param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
162   Assert_Param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
163   Assert_Param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
164   Assert_Param(IS_USART_MODE(USART_InitStruct->USART_Mode));
165 
166   USARTx->CR = (USARTx->CR & CR_CLEAR_Mask) | USART_InitStruct->USART_StopBits |
167                 USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
168                 USART_InitStruct->USART_Mode;
169 
170   switch (uIPAddr)
171   {
172     #if (!LIBCFG_NO_USART0)
173     case HT_USART0_BASE:
174     {
175       uIPClock = CKCU_GetPeripFrequency(CKCU_PCLK_USART0);
176       break;
177     }
178     #endif
179     #if (LIBCFG_USART1)
180     case HT_USART1_BASE:
181     {
182       uIPClock = CKCU_GetPeripFrequency(CKCU_PCLK_USART1);
183       break;
184     }
185     #endif
186     case HT_UART0_BASE:
187     {
188       uIPClock = CKCU_GetPeripFrequency(CKCU_PCLK_UART0);
189       break;
190     }
191     #if (LIBCFG_UART1)
192     case HT_UART1_BASE:
193     {
194       uIPClock = CKCU_GetPeripFrequency(CKCU_PCLK_UART1);
195       break;
196     }
197     #endif
198     #if (LIBCFG_UART2)
199     case HT_UART2_BASE:
200     {
201       uIPClock = CKCU_GetPeripFrequency(CKCU_PCLK_UART2);
202       break;
203     }
204     #endif
205     #if (LIBCFG_UART3)
206     case HT_UART3_BASE:
207     {
208       uIPClock = CKCU_GetPeripFrequency(CKCU_PCLK_UART3);
209       break;
210     }
211     #endif
212   }
213 
214   USARTx->DLR = uIPClock / (u32)USART_InitStruct->USART_BaudRate;
215 }
216 
217 /*********************************************************************************************************//**
218  * @brief Fill each USART_InitStruct member with its default value.
219  * @param USART_InitStruct: pointer to a USART_InitTypeDef structure.
220  * @retval None
221  ************************************************************************************************************/
USART_StructInit(USART_InitTypeDef * USART_InitStruct)222 void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
223 {
224   /* USART_InitStruct members default value                                                                 */
225   USART_InitStruct->USART_BaudRate = 9600;
226   USART_InitStruct->USART_WordLength = USART_WORDLENGTH_8B;
227   USART_InitStruct->USART_StopBits = USART_STOPBITS_1;
228   USART_InitStruct->USART_Parity = USART_PARITY_NO;
229   USART_InitStruct->USART_Mode = USART_MODE_NORMAL;
230 }
231 
232 /*********************************************************************************************************//**
233  * @brief USART/UART send data to Tx.
234  * @param USARTx: Parameter to select the UxART peripheral.
235  * @param Data: the data to be transmitted.
236  * @retval None
237  ************************************************************************************************************/
USART_SendData(HT_USART_TypeDef * USARTx,u16 Data)238 void USART_SendData(HT_USART_TypeDef* USARTx, u16 Data)
239 {
240   /* Check the parameters                                                                                   */
241   Assert_Param(IS_USART(USARTx));
242   Assert_Param(IS_USART_DATA(Data));
243 
244   USARTx->DR = Data;
245 }
246 
247 /*********************************************************************************************************//**
248  * @brief USART/UART receive data from Rx.
249  * @param USARTx: Parameter to select the UxART peripheral.
250  * @retval The received data.
251  ************************************************************************************************************/
USART_ReceiveData(HT_USART_TypeDef * USARTx)252 u16 USART_ReceiveData(HT_USART_TypeDef* USARTx)
253 {
254   /* Check the parameters                                                                                   */
255   Assert_Param(IS_USART(USARTx));
256 
257   return (u16)(USARTx->DR);
258 }
259 
260 /*********************************************************************************************************//**
261  * @brief Get the specified USART/UART status flags.
262  * @param USARTx: Parameter to select the UxART peripheral.
263  * @param USART_FLAG_x: Specify the flag to be check.
264  *   This parameter can be one of the following values:
265  *     @arg USART_FLAG_RXDNE :
266  *     @arg USART_FLAG_OE    :
267  *     @arg USART_FLAG_PE    :
268  *     @arg USART_FLAG_FE    :
269  *     @arg USART_FLAG_BI    :
270  *     @arg USART_FLAG_RXDR  :
271  *     @arg USART_FLAG_TOUT  :
272  *     @arg USART_FLAG_TXDE  :
273  *     @arg USART_FLAG_TXC   :
274  *     @arg USART_FLAG_RSADD :
275  *     @arg USART_FLAG_CTSC  :
276  *     @arg USART_FLAG_CTSS  :
277  *     @arg USART_FLAG_LBD   :
278  * @retval SET or RESET
279  ************************************************************************************************************/
USART_GetFlagStatus(HT_USART_TypeDef * USARTx,u32 USART_FLAG_x)280 FlagStatus USART_GetFlagStatus(HT_USART_TypeDef* USARTx, u32 USART_FLAG_x)
281 {
282   /* Check the parameters                                                                                   */
283   Assert_Param(IS_USART(USARTx));
284   Assert_Param(IS_USART_FLAG(USART_FLAG_x));
285 
286   if ((USARTx->SR & USART_FLAG_x) != (u32)RESET)
287   {
288     return (SET);
289   }
290   else
291   {
292     return (RESET);
293   }
294 }
295 
296 /*********************************************************************************************************//**
297  * @brief Get the specified USART/UART INT status.
298  * @param USARTx: Parameter to select the UxART peripheral.
299  * @param USART_INT_x: Specify if the USART/UART interrupt source.
300  *   This parameter can be one of the following values:
301  *     @arg USART_INT_RXDR  :
302  *     @arg USART_INT_TXDE  :
303  *     @arg USART_INT_TXC   :
304  *     @arg USART_INT_OE    :
305  *     @arg USART_INT_PE    :
306  *     @arg USART_INT_FE    :
307  *     @arg USART_INT_BI    :
308  *     @arg USART_INT_RSADD :
309  *     @arg USART_INT_TOUT  :
310  *     @arg USART_INT_CTS   :
311  *     @arg USART_INT_LBD   :
312  * @retval SET or RESET
313  ************************************************************************************************************/
USART_GetIntStatus(HT_USART_TypeDef * USARTx,u32 USART_INT_x)314 FlagStatus USART_GetIntStatus(HT_USART_TypeDef* USARTx, u32 USART_INT_x)
315 {
316   /* Check the parameters                                                                                   */
317   Assert_Param(IS_USART(USARTx));
318   Assert_Param(IS_USART_INT(USART_INT_x));
319 
320   if ((USARTx->IER & USART_INT_x) != (u32)RESET)
321   {
322     return (SET);
323   }
324   else
325   {
326     return (RESET);
327   }
328 }
329 
330 /*********************************************************************************************************//**
331  * @brief Clear the specified USART/UART flags.
332  * @param USARTx: where USARTx is the selected USART/UART from the USART/UART peripherals.
333  * @param USART_Flag: Specify the flag to check.
334  *   This parameter can be any combination of the following values:
335  *     @arg USART_FLAG_OE    :
336  *     @arg USART_FLAG_PE    :
337  *     @arg USART_FLAG_FE    :
338  *     @arg USART_FLAG_BI    :
339  *     @arg USART_FLAG_TOUT  :
340  *     @arg USART_FLAG_RSADD :
341  *     @arg USART_FLAG_CTSC  :
342  *     @arg USART_FLAG_LBD   :
343  * @retval SET or RESET
344  ************************************************************************************************************/
USART_ClearFlag(HT_USART_TypeDef * USARTx,u32 USART_Flag)345 void USART_ClearFlag(HT_USART_TypeDef* USARTx, u32 USART_Flag)
346 {
347   /* Check the parameters                                                                                   */
348   Assert_Param(IS_USART(USARTx));
349   Assert_Param(IS_USART_CLEAR_FLAG(USART_Flag));
350 
351   USARTx->SR &= USART_Flag;
352 }
353 
354 /*********************************************************************************************************//**
355  * @brief Enable or Disable the USART/UART interrupts.
356  * @param USARTx: Parameter to select the UxART peripheral.
357  * @param USART_INT_x: Specify if the USART/UART interrupt source to be enabled or disabled.
358  *   This parameter can be one of the following values:
359  *     @arg USART_INT_RXDR  :
360  *     @arg USART_INT_TXDE  :
361  *     @arg USART_INT_TXC   :
362  *     @arg USART_INT_OE    :
363  *     @arg USART_INT_PE    :
364  *     @arg USART_INT_FE    :
365  *     @arg USART_INT_BI    :
366  *     @arg USART_INT_RSADD :
367  *     @arg USART_INT_TOUT  :
368  *     @arg USART_INT_CTS   :
369  *     @arg USART_INT_LBD   :
370  * @param NewState: This parameter can be ENABLE or DISABLE.
371  * @retval None
372  ************************************************************************************************************/
USART_IntConfig(HT_USART_TypeDef * USARTx,u32 USART_INT_x,ControlStatus NewState)373 void USART_IntConfig(HT_USART_TypeDef* USARTx, u32 USART_INT_x, ControlStatus NewState)
374 {
375   /* Check the parameters                                                                                   */
376   Assert_Param(IS_USART(USARTx));
377   Assert_Param(IS_USART_INT(USART_INT_x));
378   Assert_Param(IS_CONTROL_STATUS(NewState));
379 
380   if (NewState != DISABLE)
381   {
382     USARTx->IER |= USART_INT_x;
383   }
384   else
385   {
386     USARTx->IER &= ~USART_INT_x;
387   }
388 }
389 
390 /*********************************************************************************************************//**
391  * @brief Enable or Disable the USART Tx/Rx.
392  * @param USARTx: Parameter to select the USART peripheral.
393  * @param TxRx: This parameter can be USART_CMD_TX or USART_CMD_RX.
394  * @param NewState: This parameter can be ENABLE or DISABLE.
395  * @retval None
396  ************************************************************************************************************/
USART_TxRxCmd(HT_USART_TypeDef * USARTx,u32 TxRx,ControlStatus NewState)397 void USART_TxRxCmd(HT_USART_TypeDef* USARTx, u32 TxRx, ControlStatus NewState)
398 {
399   /* Check the parameters                                                                                   */
400   Assert_Param(IS_USART(USARTx));
401   Assert_Param(IS_CONTROL_STATUS(NewState));
402   if (NewState != DISABLE)
403   {
404     USARTx->CR |= (USART_EN_ON << TxRx);
405   }
406   else
407   {
408     USARTx->CR &= ~(USART_EN_ON << TxRx);
409   }
410 }
411 
412 #if (LIBCFG_PDMA)
413 /*********************************************************************************************************//**
414  * @brief Enable or Disable the USART/UART PDMA interface.
415  * @param USARTx: Parameter to select the UxART peripheral.
416  * @param USART_PDMAREQ: specify the USART/UART PDMA transfer request to be enabled or disabled.
417  *   This parameter can be any combination of the following values:
418  *     @arg USART_PDMAREQ_TX
419  *     @arg USART_PDMAREQ_RX
420  * @param NewState: This parameter can be ENABLE or DISABLE.
421  * @retval None
422  ************************************************************************************************************/
USART_PDMACmd(HT_USART_TypeDef * USARTx,u32 USART_PDMAREQ,ControlStatus NewState)423 void USART_PDMACmd(HT_USART_TypeDef* USARTx, u32 USART_PDMAREQ, ControlStatus NewState)
424 {
425   /* Check the parameters                                                                                   */
426   Assert_Param(IS_USART(USARTx));
427   Assert_Param(IS_USART_PDMA_REQ(USART_PDMAREQ));
428   Assert_Param(IS_CONTROL_STATUS(NewState));
429 
430   if (NewState != DISABLE)
431   {
432     USARTx->CR |= USART_PDMAREQ;
433   }
434   else
435   {
436     USARTx->CR &= ~USART_PDMAREQ;
437   }
438 }
439 #endif
440 
441 /*********************************************************************************************************//**
442  * @brief Enable or Disable the USART/UART break control function.
443  * @param USARTx: Parameter to select the USART peripheral.
444  * @param NewState: This parameter can be ENABLE or DISABLE.
445  * @retval None
446  ************************************************************************************************************/
USART_ForceBreakCmd(HT_USART_TypeDef * USARTx,ControlStatus NewState)447 void USART_ForceBreakCmd(HT_USART_TypeDef* USARTx, ControlStatus NewState)
448 {
449   /* Check the parameters                                                                                   */
450   Assert_Param(IS_USART(USARTx));
451   Assert_Param(IS_CONTROL_STATUS(NewState));
452 
453   if (NewState != DISABLE)
454   {
455     USARTx->CR |= USART_BREAK_ON;
456   }
457   else
458   {
459     USARTx->CR &= USART_BREAK_OFF;
460   }
461 }
462 
463 /*********************************************************************************************************//**
464  * @brief Enable or Disable the USART/UART stick parity function.
465  * @param USARTx: Parameter to select the UxART peripheral.
466  * @param NewState: This parameter can be ENABLE or DISABLE.
467  * @retval None
468  ************************************************************************************************************/
USART_StickParityCmd(HT_USART_TypeDef * USARTx,ControlStatus NewState)469 void USART_StickParityCmd(HT_USART_TypeDef* USARTx, ControlStatus NewState)
470 {
471   /* Check the parameters                                                                                   */
472   Assert_Param(IS_USART(USARTx));
473   Assert_Param(IS_CONTROL_STATUS(NewState));
474 
475   if (NewState != DISABLE)
476   {
477     USARTx->CR |= USART_SPE_ON | USART_PBE_ON;
478   }
479   else
480   {
481     USARTx->CR &= USART_SPE_OFF;
482   }
483 }
484 
485 /*********************************************************************************************************//**
486  * @brief Configure the stick parity value of the USART/UART.
487  * @param USARTx: Parameter to select the UxART peripheral.
488  * @param USART_StickParity: Specify the stick parity of the USART/UART.
489  *   This parameter can be one of the following values:
490  *     @arg USART_STICK_LOW
491  *     @arg USART_STICK_HIGH
492  * @retval None
493  ************************************************************************************************************/
USART_StickParityConfig(HT_USART_TypeDef * USARTx,u32 USART_StickParity)494 void USART_StickParityConfig(HT_USART_TypeDef * USARTx, u32 USART_StickParity)
495 {
496   /* Check the parameters                                                                                   */
497   Assert_Param(IS_USART(USARTx));
498   Assert_Param(IS_USART_STICK_PARITY(USART_StickParity));
499 
500   if (USART_StickParity != USART_STICK_HIGH)
501   {
502     USARTx->CR |= USART_STICK_LOW;
503   }
504   else
505   {
506     USARTx->CR &= USART_STICK_HIGH;
507   }
508 }
509 
510 /*********************************************************************************************************//**
511  * @brief Set the specified USART guard time.
512  * @param USARTx: Parameter to select the USART peripheral.
513  * @param USART_GuardTime: Specify the guard time.
514  * @retval None
515  ************************************************************************************************************/
USART_SetGuardTime(HT_USART_TypeDef * USARTx,u32 USART_GuardTime)516 void USART_SetGuardTime(HT_USART_TypeDef* USARTx, u32 USART_GuardTime)
517 {
518   /* Check the parameters                                                                                   */
519   Assert_Param(IS_USART(USARTx));
520   Assert_Param(IS_USART_GUARD_TIME(USART_GuardTime));
521 
522   USARTx->TPR = (USARTx->TPR & TPR_TG_Mask) | (USART_GuardTime << 0x08);
523 }
524 
525 /*********************************************************************************************************//**
526  * @brief Configure the Tx/Rx FIFO Interrupt Trigger Level.
527  * @param USARTx: Parameter to select the USART peripheral.
528  * @param TxRx: This parameter can be USART_CMD_TX or USART_CMD_RX.
529  * @param USART_tl: Specify the USART Tx/Rx FIFO interrupt trigger level.
530  *   This parameter can be one of the following values:
531  *     @arg USART_RXTL_01
532  *     @arg USART_RXTL_02
533  *     @arg USART_RXTL_04
534  *     @arg USART_RXTL_06
535  *     @arg USART_TXTL_00
536  *     @arg USART_TXTL_02
537  *     @arg USART_TXTL_04
538  *     @arg USART_TXTL_06
539  * @retval None
540  ************************************************************************************************************/
USART_TXRXTLConfig(HT_USART_TypeDef * USARTx,u32 TxRx,u32 USART_tl)541 void USART_TXRXTLConfig(HT_USART_TypeDef* USARTx, u32 TxRx, u32 USART_tl)
542 {
543   /* Check the parameters                                                                                   */
544   Assert_Param(IS_USART(USARTx));
545   Assert_Param(IS_USART_TL(USART_tl));
546 
547   USARTx->FCR = (USARTx->FCR & ~(FCR_TL_Mask << (TxRx * 2))) | (USART_tl << (TxRx * 2));
548 }
549 
550 /*********************************************************************************************************//**
551  * @brief Set the USART FIFO time-out value.
552  * @param USARTx: Parameter to select the USART peripheral.
553  * @param USART_TimeOut: Specify the time-out value.
554  * @retval None
555  ************************************************************************************************************/
USART_SetTimeOutValue(HT_USART_TypeDef * USARTx,u32 USART_TimeOut)556 void USART_SetTimeOutValue(HT_USART_TypeDef* USARTx, u32 USART_TimeOut)
557 {
558   /* Check the parameters                                                                                   */
559   Assert_Param(IS_USART(USARTx));
560   Assert_Param(IS_USART_TIMEOUT(USART_TimeOut));
561 
562   USARTx->TPR = (USARTx->TPR & TPR_RXTOIC_Mask) | USART_TimeOut | USART_RXTOEN_ON;
563 }
564 
565 /*********************************************************************************************************//**
566  * @brief Clear both the write and read point in USART Tx FIFO or Rx FIFO.
567  * @param USARTx: Parameter to select the USART peripheral.
568  * @param USART_FIFODirection: Determine TX FIFO or Rx FIFO that is to be reset.
569  *   This parameter can be any combination of the following values:
570  *     @arg USART_FIFO_TX
571  *     @arg USART_FIFO_RX
572  * @retval None
573  ************************************************************************************************************/
USART_FIFOReset(HT_USART_TypeDef * USARTx,u32 USART_FIFODirection)574 void USART_FIFOReset(HT_USART_TypeDef* USARTx, u32 USART_FIFODirection)
575 {
576   /* Check the parameters                                                                                   */
577   Assert_Param(IS_USART(USARTx));
578   Assert_Param(IS_USART_FIFO_DIRECTION(USART_FIFODirection));
579 
580   USARTx->FCR |= USART_FIFODirection;
581 }
582 
583 /*********************************************************************************************************//**
584  * @brief Return the status of specified USART FIFO.
585  * @param USARTx: Parameter to select the USART peripheral.
586  * @param USART_FIFODirection: specify the FIFO that is to be check.
587  *   This parameter can be one of the following values:
588  *     @arg USART_FIFO_TX
589  *     @arg USART_FIFO_RX
590  * @retval The number of data in Tx FIFO or Rx FIFO.
591  ************************************************************************************************************/
USART_GetFIFOStatus(HT_USART_TypeDef * USARTx,u32 USART_FIFODirection)592 u8 USART_GetFIFOStatus(HT_USART_TypeDef* USARTx, u32 USART_FIFODirection)
593 {
594   /* Check the parameters                                                                                   */
595   Assert_Param(IS_USART(USARTx));
596   Assert_Param(IS_USART_FIFO_DIRECTION(USART_FIFODirection));
597 
598   if (USART_FIFODirection == USART_FIFO_TX)
599   {
600     return (u8)((USARTx->FCR & 0xF0000) >> 16);
601   }
602   else
603   {
604     return (u8)((USARTx->FCR & 0xF000000) >> 24);
605   }
606 }
607 
608 /*********************************************************************************************************//**
609  * @brief Enable or Disable the USART hardware flow control.
610  * @param USARTx: Parameter to select the USART peripheral.
611  * @param NewState: This parameter can be ENABLE or DISABLE.
612  * @retval None
613  ************************************************************************************************************/
USART_HardwareFlowControlCmd(HT_USART_TypeDef * USARTx,ControlStatus NewState)614 void USART_HardwareFlowControlCmd(HT_USART_TypeDef* USARTx, ControlStatus NewState)
615 {
616   /* Check the parameters                                                                                   */
617   Assert_Param(IS_USART(USARTx));
618   Assert_Param(IS_CONTROL_STATUS(NewState));
619 
620   if (NewState != DISABLE)
621   {
622     USARTx->CR |= USART_HFCEN_ON;
623   }
624   else
625   {
626     USARTx->CR &= USART_HFCEN_OFF;
627   }
628 }
629 
630 /*********************************************************************************************************//**
631  * @brief Enable or Disable the USART IrDA interface.
632  * @param USARTx: Parameter to select the USART peripheral.
633  * @param NewState: This parameter can be ENABLE or DISABLE.
634  * @retval None
635  ************************************************************************************************************/
USART_IrDACmd(HT_USART_TypeDef * USARTx,ControlStatus NewState)636 void USART_IrDACmd(HT_USART_TypeDef* USARTx, ControlStatus NewState)
637 {
638   /* Check the parameters                                                                                   */
639   Assert_Param(IS_USART(USARTx));
640   Assert_Param(IS_CONTROL_STATUS(NewState));
641 
642   if (NewState != DISABLE)
643   {
644     USARTx->ICR |= USART_IRDA_ON;
645   }
646   else
647   {
648     USARTx->ICR &= USART_IRDA_OFF;
649   }
650 }
651 
652 /*********************************************************************************************************//**
653  * @brief Configure the USART IrDA interface.
654  * @param USARTx: Parameter to select the USART peripheral.
655  * @param USART_IrDAMode: Specify the USART IrDA mode.
656  *   This parameter can be one of the following values:
657  *     @arg USART_IRDA_LOWPOWER
658  *     @arg USART_IRDA_NORMAL
659  * @retval None
660  ************************************************************************************************************/
USART_IrDAConfig(HT_USART_TypeDef * USARTx,u32 USART_IrDAMode)661 void USART_IrDAConfig(HT_USART_TypeDef* USARTx, u32 USART_IrDAMode)
662 {
663   /* Check the parameters                                                                                   */
664   Assert_Param(IS_USART(USARTx));
665   Assert_Param(IS_USART_IRDA_MODE(USART_IrDAMode));
666 
667   if (USART_IrDAMode != USART_IRDA_NORMAL)
668   {
669     USARTx->ICR |= USART_IRDA_LOWPOWER;
670   }
671   else
672   {
673     USARTx->ICR &= USART_IRDA_NORMAL;
674   }
675 }
676 
677 /*********************************************************************************************************//**
678  * @brief Set the specified USART IrDA prescaler.
679  * @param USARTx: Parameter to select the USART peripheral.
680  * @param USART_IrDAPrescaler: Specify the USART IrDA prescaler.
681  * @retval None
682  ************************************************************************************************************/
USART_SetIrDAPrescaler(HT_USART_TypeDef * USARTx,u32 USART_IrDAPrescaler)683 void USART_SetIrDAPrescaler(HT_USART_TypeDef* USARTx, u32 USART_IrDAPrescaler)
684 {
685   /* Check the parameters                                                                                   */
686   Assert_Param(IS_USART(USARTx));
687   Assert_Param(IS_USART_IRDA_PRESCALER(USART_IrDAPrescaler));
688 
689   USARTx->ICR = (USARTx->ICR & ICR_IRDAPSC_Mask) | (USART_IrDAPrescaler << 0x08);
690 }
691 
692 /*********************************************************************************************************//**
693  * @brief Enable the IrDA transmitter or receiver.
694  * @param USARTx: Parameter to select the USART peripheral, x can be 0 or 1.
695  * @param USART_IrDADirection: Specify the USART IrDA direction select.
696  *   This parameter can be one of the following values:
697  *     @arg USART_IRDA_TX
698  *     @arg USART_IRDA_RX
699  * @retval None
700  ************************************************************************************************************/
USART_IrDADirectionConfig(HT_USART_TypeDef * USARTx,u32 USART_IrDADirection)701 void USART_IrDADirectionConfig(HT_USART_TypeDef* USARTx, u32 USART_IrDADirection)
702 {
703   /* Check the parameters                                                                                   */
704   Assert_Param(IS_USART(USARTx));
705   Assert_Param(IS_USART_IRDA_DIRECTION(USART_IrDADirection));
706 
707   if (USART_IrDADirection != USART_IRDA_RX)
708   {
709     USARTx->ICR |= USART_IRDA_TX;
710   }
711   else
712   {
713     USARTx->ICR &= USART_IRDA_RX;
714   }
715 }
716 
717 /*********************************************************************************************************//**
718  * @brief Enable or Disable inverting serial output/input function of IrDA on the specified USART.
719  * @param USARTx: Parameter to select the USART peripheral.
720  * @param inout: This parameter can be USART_CMD_OUT or USART_CMD_IN.
721  * @param NewState: This parameter can be ENABLE or DISABLE.
722  * @retval None
723  ************************************************************************************************************/
USART_IrDAInvtCmd(HT_USART_TypeDef * USARTx,u32 inout,ControlStatus NewState)724 void USART_IrDAInvtCmd(HT_USART_TypeDef* USARTx, u32 inout, ControlStatus NewState)
725 {
726   /* Check the parameters                                                                                   */
727   Assert_Param(IS_USART(USARTx));
728   Assert_Param(IS_CONTROL_STATUS(NewState));
729 
730   if (NewState != DISABLE)
731   {
732     USARTx->ICR |= (USART_INV_ON << inout);
733   }
734   else
735   {
736     USARTx->ICR &= ~(USART_INV_ON << inout);
737   }
738 }
739 
740 /*********************************************************************************************************//**
741  * @brief Configure the polarity of USART RS485 transmitter enable signal.
742  * @param USARTx: Parameter to select the USART peripheral.
743  * @param USART_RS485Polarity: Specify the polarity of USART RS485 Tx enable signal.
744  *   This parameter can be one of the following values:
745  *     @arg USART_RS485POL_LOW
746  *     @arg USART_RS485POL_HIGH
747  * @retval None
748  ************************************************************************************************************/
USART_RS485TxEnablePolarityConfig(HT_USART_TypeDef * USARTx,u32 USART_RS485Polarity)749 void USART_RS485TxEnablePolarityConfig(HT_USART_TypeDef* USARTx, u32 USART_RS485Polarity)
750 {
751   /* Check the parameters                                                                                   */
752   Assert_Param(IS_USART(USARTx));
753   Assert_Param(IS_USART_RS485_POLARITY(USART_RS485Polarity));
754 
755   if (USART_RS485Polarity != USART_RS485POLARITY_HIGH)
756   {
757     USARTx->RCR |= USART_RS485POLARITY_LOW;
758   }
759   else
760   {
761     USARTx->RCR &= USART_RS485POLARITY_HIGH;
762   }
763 }
764 
765 /*********************************************************************************************************//**
766  * @brief Enable or Disable the USART RS485 normal multi-drop operation mode.
767  * @param USARTx: Parameter to select the USART peripheral.
768  * @param NewState: This parameter can be ENABLE or DISABLE.
769  * @retval None
770  ************************************************************************************************************/
USART_RS485NMMCmd(HT_USART_TypeDef * USARTx,ControlStatus NewState)771 void USART_RS485NMMCmd(HT_USART_TypeDef* USARTx, ControlStatus NewState)
772 {
773   /* Check the parameters                                                                                   */
774   Assert_Param(IS_USART(USARTx));
775   Assert_Param(IS_CONTROL_STATUS(NewState));
776 
777   if (NewState != DISABLE)
778   {
779     USARTx->RCR |= USART_RS485NMM_ON;
780   }
781   else
782   {
783     USARTx->RCR &= USART_RS485NMM_OFF;
784   }
785 }
786 
787 /*********************************************************************************************************//**
788  * @brief Enable or Disable the USART RS485 normal multi-drop operation mode.
789  * @param USARTx: Parameter to select the USART peripheral.
790  * @param NewState: This parameter can be ENABLE or DISABLE.
791  * @retval None
792  ************************************************************************************************************/
USART_RS485AADCmd(HT_USART_TypeDef * USARTx,ControlStatus NewState)793 void USART_RS485AADCmd(HT_USART_TypeDef* USARTx, ControlStatus NewState)
794 {
795   /* Check the parameters                                                                                   */
796   Assert_Param(IS_USART(USARTx));
797   Assert_Param(IS_CONTROL_STATUS(NewState));
798 
799   if (NewState != DISABLE)
800   {
801     USARTx->RCR |= USART_RS485AAD_ON;
802   }
803   else
804   {
805     USARTx->RCR &= USART_RS485AAD_OFF;
806   }
807 }
808 
809 /*********************************************************************************************************//**
810  * @brief Set the specified USART RS485 address match value.
811  * @param USARTx: Parameter to select the USART peripheral.
812  * @param USART_AddressMatchValue: specify the USART RS485 address match value.
813  * @retval None
814  ************************************************************************************************************/
USART_SetAddressMatchValue(HT_USART_TypeDef * USARTx,u32 USART_AddressMatchValue)815 void USART_SetAddressMatchValue(HT_USART_TypeDef* USARTx, u32 USART_AddressMatchValue)
816 {
817   /* Check the parameters                                                                                   */
818   Assert_Param(IS_USART(USARTx));
819   Assert_Param(IS_USART_ADDRESS_MATCH_VALUE(USART_AddressMatchValue));
820 
821   USARTx->RCR = (USARTx->RCR & RS485CR_ADDM_Mask) | (u32)(USART_AddressMatchValue << 0x08);
822 }
823 
824 /*********************************************************************************************************//**
825  * @brief Initialize the clock of the USART peripheral according to the specified parameters
826  *        in the USART_ClockInitStruct.
827  * @param USARTx: Parameter to select the USART peripheral.
828  * @param USART_SynClock_InitStruct: pointer to a USART_SynClock_InitTypeDef structure.
829  * @retval None
830  ************************************************************************************************************/
USART_SynClockInit(HT_USART_TypeDef * USARTx,USART_SynClock_InitTypeDef * USART_SynClock_InitStruct)831 void USART_SynClockInit(HT_USART_TypeDef* USARTx, USART_SynClock_InitTypeDef* USART_SynClock_InitStruct)
832 {
833   /* Check the parameters                                                                                   */
834   Assert_Param(IS_USART(USARTx));
835   Assert_Param(IS_USART_SYNCHRONOUS_CLOCK(USART_SynClock_InitStruct->USART_ClockEnable));
836   Assert_Param(IS_USART_SYNCHRONOUS_PHASE(USART_SynClock_InitStruct->USART_ClockPhase));
837   Assert_Param(IS_USART_SYNCHRONOUS_POLARITY(USART_SynClock_InitStruct->USART_ClockPolarity));
838   Assert_Param(IS_USART_TRANSFER_MODE(USART_SynClock_InitStruct->USART_TransferSelectMode));
839 
840   USARTx->SCR = USART_SynClock_InitStruct->USART_ClockEnable | USART_SynClock_InitStruct->USART_ClockPhase |
841                 USART_SynClock_InitStruct->USART_ClockPolarity;
842 
843   USARTx->CR = (USARTx->CR & TRSM_CLEAR_Mask) | USART_SynClock_InitStruct->USART_TransferSelectMode;
844 }
845 
846 /*********************************************************************************************************//**
847  * @brief Fill each USART_SynClockInitStruct member with its default value.
848  * @param USART_SynClock_InitStruct: pointer to a USART_SynClock_InitTypeDef structure.
849  * @retval None
850  ************************************************************************************************************/
USART_SynClockStructInit(USART_SynClock_InitTypeDef * USART_SynClock_InitStruct)851 void USART_SynClockStructInit(USART_SynClock_InitTypeDef* USART_SynClock_InitStruct)
852 {
853   /* USART_ClockInitStruct members default value                                                            */
854   USART_SynClock_InitStruct->USART_ClockEnable =  USART_SYN_CLOCK_DISABLE;
855   USART_SynClock_InitStruct->USART_ClockPhase = USART_SYN_CLOCK_PHASE_FIRST;
856   USART_SynClock_InitStruct->USART_ClockPolarity = USART_SYN_CLOCK_POLARITY_LOW;
857   USART_SynClock_InitStruct->USART_TransferSelectMode = USART_LSB_FIRST;
858 }
859 
860 #if (LIBCFG_USART_LIN)
861 /*********************************************************************************************************//**
862  * @brief USART/UART LIN Mode send break to Tx.
863  * @param USARTx: where USARTx is the selected USART/UART from the USART/UART peripherals.
864  * @retval None
865  ************************************************************************************************************/
USART_LIN_SendBreak(HT_USART_TypeDef * USARTx)866 void USART_LIN_SendBreak(HT_USART_TypeDef* USARTx)
867 {
868   /* Check the parameters                                                                                   */
869   Assert_Param(IS_USART(USARTx));
870 
871   USARTx->CR |= USART_LINSENDBREAK;
872 }
873 
874 /*********************************************************************************************************//**
875  * @brief Configure the break detection length in LIN mode.
876  * @param USARTx: where USARTx is the selected USART/UART from the USART/UART peripherals.
877  * @param USART_LIN_Length: data length in byte.
878  *   This parameter can be one of the following values:
879  *     @arg USART_LINLENGTH_11BIT
880  *     @arg USART_LINLENGTH_10BIT
881  * @retval None
882  ************************************************************************************************************/
USART_LIN_LengthSelect(HT_USART_TypeDef * USARTx,u32 USART_LIN_Length)883 void USART_LIN_LengthSelect(HT_USART_TypeDef* USARTx, u32 USART_LIN_Length)
884 {
885   /* Check the parameters                                                                                   */
886   Assert_Param(IS_USART(USARTx));
887   Assert_Param(IS_USART_LINLENGTH(USART_LIN_Length));
888 
889   if (USART_LIN_Length != USART_LINLENGTH_10BIT)
890   {
891     USARTx->CR |= USART_LINLENGTH_11BIT;
892   }
893   else
894   {
895     USARTx->CR &= USART_LINLENGTH_10BIT;
896   }
897 }
898 #endif
899 /**
900   * @}
901   */
902 
903 
904 /**
905   * @}
906   */
907 
908 /**
909   * @}
910   */
911