1 /*********************************************************************************************************//**
2  * @file    ht32f5xxxx_gpio.c
3  * @version $Rev:: 8260         $
4  * @date    $Date:: 2024-11-05 #$
5  * @brief   This file provides all the GPIO and AFIO 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_gpio.h"
30 
31 /** @addtogroup HT32F5xxxx_Peripheral_Driver HT32F5xxxx Peripheral Driver
32   * @{
33   */
34 
35 /** @defgroup GPIO GPIO
36   * @brief GPIO driver modules
37   * @{
38   */
39 
40 
41 /* Private function prototypes -----------------------------------------------------------------------------*/
42 u32 _GPIO_ClockControl(HT_GPIO_TypeDef* HT_GPIOx, ControlStatus Cmd);
43 u32 _AFIO_ClockControl(ControlStatus Cmd);
44 
45 /* Private macro -------------------------------------------------------------------------------------------*/
46 /** @defgroup GPIO_Private_Macro GPIO private macros
47   * @{
48   */
49 #if (AUTO_CK_CONTROL == 1)
50   #define GPIO_CK_ST       u32 isAlreadyOn
51   #define GPIO_CK_ON()     (isAlreadyOn = _GPIO_ClockControl(HT_GPIOx, ENABLE))
52   #define GPIO_CK_OFF()    if (isAlreadyOn == FALSE) _GPIO_ClockControl(HT_GPIOx, DISABLE)
53   #define AFIO_CK_ST       u32 isAlreadyOn
54   #define AFIO_CK_ON()     (isAlreadyOn = _AFIO_ClockControl(ENABLE))
55   #define AFIO_CK_OFF()    if (isAlreadyOn == FALSE) _AFIO_ClockControl(DISABLE)
56 #else
57   #define GPIO_CK_ST
58   #define GPIO_CK_ON(...)
59   #define GPIO_CK_OFF(...)
60   #define AFIO_CK_ST
61   #define AFIO_CK_ON(...)
62   #define AFIO_CK_OFF(...)
63 #endif
64 /**
65   * @}
66   */
67 
68 /* Global functions ----------------------------------------------------------------------------------------*/
69 /** @addtogroup GPIO_Exported_Functions GPIO exported functions
70   * @{
71   */
72 /*********************************************************************************************************//**
73  * @brief Deinitializes the GPIO peripheral registers to their default reset values.
74  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
75  * @retval None
76  ************************************************************************************************************/
GPIO_DeInit(HT_GPIO_TypeDef * HT_GPIOx)77 void GPIO_DeInit(HT_GPIO_TypeDef* HT_GPIOx)
78 {
79   RSTCU_PeripReset_TypeDef RSTCUReset = {{0}};
80 
81   /* Check the parameters                                                                                   */
82   Assert_Param(IS_GPIO(HT_GPIOx));
83 
84   if (HT_GPIOx == HT_GPIOA)
85   {
86     RSTCUReset.Bit.PA = 1;
87   }
88   else if (HT_GPIOx == HT_GPIOB)
89   {
90     RSTCUReset.Bit.PB = 1;
91   }
92   #if (LIBCFG_GPIOC)
93   else if (HT_GPIOx == HT_GPIOC)
94   {
95     RSTCUReset.Bit.PC = 1;
96   }
97   #endif
98   #if (LIBCFG_GPIOD)
99   else if (HT_GPIOx == HT_GPIOD)
100   {
101     RSTCUReset.Bit.PD = 1;
102   }
103   #endif
104   #if (LIBCFG_GPIOE)
105   else if (HT_GPIOx == HT_GPIOE)
106   {
107     RSTCUReset.Bit.PE = 1;
108   }
109   #endif
110   #if (LIBCFG_GPIOF)
111   else if (HT_GPIOx == HT_GPIOF)
112   {
113     RSTCUReset.Bit.PF = 1;
114   }
115   #endif
116 
117   RSTCU_PeripReset(RSTCUReset, ENABLE);
118 }
119 
120 /*********************************************************************************************************//**
121  * @brief Configure the direction of specified GPIO pins.
122  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
123  * @param GPIO_PIN_nBITMAP: The port pins.
124  *   This parameter can be any combination of GPIO_PIN_x.
125  * @param GPIO_DIR_INorOUT:
126  *   This parameter can be one of below:
127  *     @arg GPIO_DIR_IN  : The pins are input mode
128  *     @arg GPIO_DIR_OUT : The pins are output mode
129  * @retval None
130  ************************************************************************************************************/
GPIO_DirectionConfig(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_nBITMAP,GPIO_DIR_Enum GPIO_DIR_INorOUT)131 void GPIO_DirectionConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, GPIO_DIR_Enum GPIO_DIR_INorOUT)
132 {
133   GPIO_CK_ST;
134 
135   /* Check the parameters                                                                                   */
136   Assert_Param(IS_GPIO(HT_GPIOx));
137   Assert_Param(IS_GPIO_DIR(GPIO_DIR_INorOUT));
138 
139   GPIO_CK_ON();
140 
141   if (GPIO_DIR_INorOUT != GPIO_DIR_IN)
142     HT_GPIOx->DIRCR |= GPIO_PIN_nBITMAP;
143   else
144     HT_GPIOx->DIRCR &= ~GPIO_PIN_nBITMAP;
145 
146   GPIO_CK_OFF();
147 }
148 
149 /*********************************************************************************************************//**
150  * @brief Configure the pull resistor of specified GPIO pins.
151  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
152  * @param GPIO_PIN_nBITMAP: The port pins.
153  *   This parameter can be any combination of GPIO_PIN_x.
154  * @param GPIO_PR_x: Selection of Pull resistor.
155  *   This parameter can be one of below:
156  *     @arg GPIO_PR_UP      : The pins with internal pull-up resistor
157  *     @arg GPIO_PR_DOWN    : The pins with internal pull-down resistor
158  *     @arg GPIO_PR_DISABLE : The pins without pull resistor
159  * @retval None
160  ************************************************************************************************************/
GPIO_PullResistorConfig(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_nBITMAP,GPIO_PR_Enum GPIO_PR_x)161 void GPIO_PullResistorConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, GPIO_PR_Enum GPIO_PR_x)
162 {
163   u32 temp_up, temp_down;
164   GPIO_CK_ST;
165 
166   /* Check the parameters                                                                                   */
167   Assert_Param(IS_GPIO(HT_GPIOx));
168   Assert_Param(IS_GPIO_PR(GPIO_PR_x));
169 
170   GPIO_CK_ON();
171   temp_up = HT_GPIOx->PUR;
172   temp_down = HT_GPIOx->PDR;
173 
174   #if (LIBCFG_GPIO_PR_STRONG_UP)
175   temp_up   &= ~(GPIO_PIN_nBITMAP << 16);
176   #endif
177   temp_up   &= ~GPIO_PIN_nBITMAP;
178   temp_down &= ~GPIO_PIN_nBITMAP;
179 
180   switch (GPIO_PR_x)
181   {
182     case GPIO_PR_UP:
183       temp_up   |= GPIO_PIN_nBITMAP;
184       break;
185     case GPIO_PR_DOWN:
186       temp_down |= GPIO_PIN_nBITMAP;
187       break;
188     case GPIO_PR_DISABLE:
189       break;
190     #if (LIBCFG_GPIO_PR_STRONG_UP)
191     case GPIO_PR_STRONG_UP:
192       temp_up   |= (GPIO_PIN_nBITMAP << 16);
193       break;
194     case GPIO_PR_STRONGEST_UP:
195       temp_up   |= (GPIO_PIN_nBITMAP << 16);
196       temp_up   |= GPIO_PIN_nBITMAP;
197       break;
198     #endif
199     default:
200       break;
201   }
202 
203   HT_GPIOx->PUR = temp_up;
204   HT_GPIOx->PDR = temp_down;
205 
206   GPIO_CK_OFF();
207 }
208 
209 /*********************************************************************************************************//**
210  * @brief Enable or Disable the input control of specified GPIO pins.
211  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
212  * @param GPIO_PIN_nBITMAP: The port pins.
213  *   This parameter can be any combination of GPIO_PIN_x.
214  * @param Cmd: This parameter can be ENABLE or DISABLE.
215  * @retval None
216  ************************************************************************************************************/
GPIO_InputConfig(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_nBITMAP,ControlStatus Cmd)217 void GPIO_InputConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, ControlStatus Cmd)
218 {
219   GPIO_CK_ST;
220 
221   /* Check the parameters                                                                                   */
222   Assert_Param(IS_GPIO(HT_GPIOx));
223   Assert_Param(IS_CONTROL_STATUS(Cmd));
224 
225   GPIO_CK_ON();
226   if (Cmd != DISABLE)
227     HT_GPIOx->INER |= GPIO_PIN_nBITMAP;
228   else
229     HT_GPIOx->INER &= ~GPIO_PIN_nBITMAP;
230   GPIO_CK_OFF();
231 }
232 
233 /*********************************************************************************************************//**
234  * @brief Select the driving current of specified GPIO pins.
235  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
236  * @param GPIO_PIN_nBITMAP: The port pins.
237  *   This parameter can be any combination of GPIO_PIN_x.
238  * @param GPIO_DV_nMA:
239  *   This parameter can be one of below:
240  *     @arg GPIO_DV_4MA  : Select output driving current as  4 mA
241  *     @arg GPIO_DV_8MA  : Select output driving current as  8 mA
242  *     @arg GPIO_DV_12MA : Select output driving current as 12 mA
243  *     @arg GPIO_DV_16MA : Select output driving current as 16 mA
244  * @retval None
245  ************************************************************************************************************/
GPIO_DriveConfig(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_nBITMAP,GPIO_DV_Enum GPIO_DV_nMA)246 void GPIO_DriveConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, GPIO_DV_Enum GPIO_DV_nMA)
247 {
248   u32 index, temp, CurrentMode = 0, PinPosition = 0;
249   GPIO_CK_ST;
250 
251   /* Check the parameters                                                                                   */
252   Assert_Param(IS_GPIO(HT_GPIOx));
253   Assert_Param(IS_GPIO_DV(GPIO_DV_nMA));
254 
255   for (index = 0; index < 16; index++)
256   {
257     if ((GPIO_PIN_nBITMAP & 0x0001) == 1)
258     {
259       temp = index << 1;
260       CurrentMode |= ((u32) GPIO_DV_nMA << temp);
261       PinPosition |= ((u32) 0x03  << temp);
262     }
263     GPIO_PIN_nBITMAP >>= 1;
264   }
265 
266   GPIO_CK_ON();
267   HT_GPIOx->DRVR &= ~PinPosition;
268   HT_GPIOx->DRVR |= CurrentMode;
269   GPIO_CK_OFF();
270 }
271 
272 /*********************************************************************************************************//**
273  * @brief Enable or Disable the open drain function of specified GPIO pins.
274  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
275  * @param GPIO_PIN_nBITMAP: The port pins.
276  *   This parameter can be any combination of GPIO_PIN_x.
277  * @param Cmd: This parameter can be ENABLE or DISABLE.
278  * @retval None
279  ************************************************************************************************************/
GPIO_OpenDrainConfig(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_nBITMAP,ControlStatus Cmd)280 void GPIO_OpenDrainConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, ControlStatus Cmd)
281 {
282   GPIO_CK_ST;
283 
284   /* Check the parameters                                                                                   */
285   Assert_Param(IS_GPIO(HT_GPIOx));
286   Assert_Param(IS_CONTROL_STATUS(Cmd));
287 
288   GPIO_CK_ON();
289   if (Cmd != DISABLE)
290     HT_GPIOx->ODR |= GPIO_PIN_nBITMAP;
291   else
292     HT_GPIOx->ODR &= ~GPIO_PIN_nBITMAP;
293   GPIO_CK_OFF();
294 }
295 
296 #if (LIBCFG_GPIO_SINK_CURRENT_ENHANCED)
297 /*********************************************************************************************************//**
298  * @brief Select the sink current of specified GPIO pins.
299  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
300  * @param GPIO_PIN_n: The port pins.
301  *   This parameter can be any combination of GPIO_PIN_x.
302  * @param Cmd: This parameter can be ENABLE or DISABLE.
303  * @retval None
304  ************************************************************************************************************/
GPIO_SinkConfig(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_n,ControlStatus Cmd)305 void GPIO_SinkConfig(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_n, ControlStatus Cmd)
306 {
307   /* Check the parameters                                                                                   */
308   Assert_Param(IS_GPIO(HT_GPIOx));
309   Assert_Param(IS_CONTROL_STATUS(Cmd));
310 
311   if (Cmd != DISABLE)
312     HT_GPIOx->SCER |= GPIO_PIN_n;
313   else
314     HT_GPIOx->SCER &= ~GPIO_PIN_n;
315 }
316 #endif
317 
318 /*********************************************************************************************************//**
319  * @brief Get the input data of specified port pin.
320  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
321  * @param GPIO_PIN_n: This parameter can be GPIO_PIN_x.
322  * @retval SET or RESET
323  ************************************************************************************************************/
GPIO_ReadInBit(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_n)324 FlagStatus GPIO_ReadInBit(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_n)
325 {
326   FlagStatus result;
327   GPIO_CK_ST;
328 
329   /* Check the parameters                                                                                   */
330   Assert_Param(IS_GPIO(HT_GPIOx));
331 
332   GPIO_CK_ON();
333   if ((HT_GPIOx->DINR & GPIO_PIN_n) != RESET)
334     result = SET;
335   else
336     result = RESET;
337 
338   GPIO_CK_OFF();
339   return result;
340 }
341 
342 /*********************************************************************************************************//**
343  * @brief Get the input data of specified GPIO port.
344  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
345  * @retval The value of input data register.
346  ************************************************************************************************************/
GPIO_ReadInData(HT_GPIO_TypeDef * HT_GPIOx)347 u16 GPIO_ReadInData(HT_GPIO_TypeDef* HT_GPIOx)
348 {
349   u16 uValue;
350   GPIO_CK_ST;
351 
352   /* Check the parameters                                                                                   */
353   Assert_Param(IS_GPIO(HT_GPIOx));
354 
355   GPIO_CK_ON();
356   uValue = (u16)HT_GPIOx->DINR;
357   GPIO_CK_OFF();
358   return (uValue);
359 }
360 
361 /*********************************************************************************************************//**
362  * @brief Get the output data of specified port pin.
363  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
364  * @param GPIO_PIN_n: This parameter can be GPIO_PIN_x.
365  * @retval SET or RESET
366  ************************************************************************************************************/
GPIO_ReadOutBit(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_n)367 FlagStatus GPIO_ReadOutBit(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_n)
368 {
369   FlagStatus result;
370   GPIO_CK_ST;
371   /* Check the parameters                                                                                   */
372   Assert_Param(IS_GPIO(HT_GPIOx));
373 
374   GPIO_CK_ON();
375   if ((HT_GPIOx->DOUTR & GPIO_PIN_n) != RESET)
376     result =  SET;
377   else
378     result = RESET;
379 
380   GPIO_CK_OFF();
381   return result;
382 }
383 
384 /*********************************************************************************************************//**
385  * @brief Get the output data of specified GPIO port.
386  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
387  * @retval The value of output data register.
388  ************************************************************************************************************/
GPIO_ReadOutData(HT_GPIO_TypeDef * HT_GPIOx)389 u16 GPIO_ReadOutData(HT_GPIO_TypeDef* HT_GPIOx)
390 {
391   u32 uValue;
392   GPIO_CK_ST;
393 
394   /* Check the parameters                                                                                   */
395   Assert_Param(IS_GPIO(HT_GPIOx));
396 
397   GPIO_CK_ON();
398   uValue = (u16)HT_GPIOx->DOUTR;
399   GPIO_CK_OFF();
400   return uValue;
401 }
402 
403 /*********************************************************************************************************//**
404  * @brief Set the selected port bits of output data.
405  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
406  * @param GPIO_PIN_nBITMAP: Specify the port bit to be set.
407  *   This parameter can be any combination of GPIO_PIN_x.
408  * @retval None
409  ************************************************************************************************************/
GPIO_SetOutBits(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_nBITMAP)410 void GPIO_SetOutBits(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP)
411 {
412   GPIO_CK_ST;
413 
414   /* Check the parameters                                                                                   */
415   Assert_Param(IS_GPIO(HT_GPIOx));
416 
417   GPIO_CK_ON();
418   HT_GPIOx->SRR = GPIO_PIN_nBITMAP;
419   GPIO_CK_OFF();
420 }
421 
422 /*********************************************************************************************************//**
423  * @brief Clear the selected port bits of output data.
424  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
425  * @param GPIO_PIN_nBITMAP: Specify the port bit to be clear.
426  *   This parameter can be any combination of GPIO_PIN_x.
427  * @retval None
428  ************************************************************************************************************/
GPIO_ClearOutBits(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_nBITMAP)429 void GPIO_ClearOutBits(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP)
430 {
431   GPIO_CK_ST;
432 
433   /* Check the parameters                                                                                   */
434   Assert_Param(IS_GPIO(HT_GPIOx));
435 
436   GPIO_CK_ON();
437   HT_GPIOx->RR = GPIO_PIN_nBITMAP;
438   GPIO_CK_OFF();
439 }
440 
441 /*********************************************************************************************************//**
442  * @brief Set or Clear the selected port bits of data.
443  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
444  * @param GPIO_PIN_nBITMAP: Specify the port bits.
445  *   This parameter can be any combination of GPIO_PIN_x.
446  * @param Status: This parameter can be SET or RESET.
447  * @retval None
448  ************************************************************************************************************/
GPIO_WriteOutBits(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_nBITMAP,FlagStatus Status)449 void GPIO_WriteOutBits(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP, FlagStatus Status)
450 {
451   GPIO_CK_ST;
452 
453   /* Check the parameters                                                                                   */
454   Assert_Param(IS_GPIO(HT_GPIOx));
455 
456   GPIO_CK_ON();
457   if (Status != RESET)
458     HT_GPIOx->SRR = GPIO_PIN_nBITMAP;
459   else
460     HT_GPIOx->RR = GPIO_PIN_nBITMAP;
461   GPIO_CK_OFF();
462 }
463 
464 /*********************************************************************************************************//**
465  * @brief Put data to the specified GPIO data port.
466  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
467  * @param Data: Specify the data to be written to the port data register.
468  * @retval None
469  ************************************************************************************************************/
GPIO_WriteOutData(HT_GPIO_TypeDef * HT_GPIOx,u16 Data)470 void GPIO_WriteOutData(HT_GPIO_TypeDef* HT_GPIOx, u16 Data)
471 {
472   GPIO_CK_ST;
473 
474   /* Check the parameters                                                                                   */
475   Assert_Param(IS_GPIO(HT_GPIOx));
476 
477   GPIO_CK_ON();
478   HT_GPIOx->DOUTR = Data;
479   GPIO_CK_OFF();
480 }
481 
482 /*********************************************************************************************************//**
483  * @brief Lock configuration of GPIO pins.
484  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
485  * @param GPIO_PIN_nBITMAP: Specify the port bits.
486  *   This parameter can be any combination of GPIO_PIN_x.
487  * @retval None
488  ************************************************************************************************************/
GPIO_PinLock(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_nBITMAP)489 void GPIO_PinLock(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_nBITMAP)
490 {
491   GPIO_CK_ST;
492 
493   /* Check the parameters                                                                                   */
494   Assert_Param(IS_GPIO(HT_GPIOx));
495 
496   GPIO_CK_ON();
497   HT_GPIOx->LOCKR = (u32)0x5FA00000 | GPIO_PIN_nBITMAP;
498   GPIO_CK_OFF();
499 }
500 
501 /*********************************************************************************************************//**
502  * @brief Get the lock state of specified GPIO port.
503  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
504  * @retval TRUE or FALSE
505  ************************************************************************************************************/
GPIO_IsPortLocked(HT_GPIO_TypeDef * HT_GPIOx)506 bool GPIO_IsPortLocked(HT_GPIO_TypeDef* HT_GPIOx)
507 {
508   bool result;
509   GPIO_CK_ST;
510 
511   /* Check the parameters                                                                                   */
512   Assert_Param(IS_GPIO(HT_GPIOx));
513 
514   GPIO_CK_ON();
515   if ((HT_GPIOx->LOCKR >> 16) == 0)
516     result = FALSE;
517   else
518     result = TRUE;
519   GPIO_CK_OFF();
520   return result;
521 }
522 
523 /*********************************************************************************************************//**
524  * @brief Get the lock state of specified GPIO port pin.
525  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
526  * @param GPIO_PIN_n: This parameter can be GPIO_PIN_x.
527  * @retval TRUE or FALSE
528  ************************************************************************************************************/
GPIO_IsPinLocked(HT_GPIO_TypeDef * HT_GPIOx,u16 GPIO_PIN_n)529 bool GPIO_IsPinLocked(HT_GPIO_TypeDef* HT_GPIOx, u16 GPIO_PIN_n)
530 {
531   bool result;
532   GPIO_CK_ST;
533 
534   /* Check the parameters                                                                                   */
535   Assert_Param(IS_GPIO(HT_GPIOx));
536 
537   GPIO_CK_ON();
538   if ((HT_GPIOx->LOCKR & GPIO_PIN_n) == 0)
539     result = FALSE;
540   else
541     result = TRUE;
542   GPIO_CK_OFF();
543   return result;
544 }
545 
546 #if (LIBCFG_GPIO_DISABLE_DEBUG_PORT)
547 /*********************************************************************************************************//**
548  * @brief  Disable DEBUG port to prevent unexpected security lock.
549  * @retval None
550  ************************************************************************************************************/
GPIO_DisableDebugPort(void)551 void GPIO_DisableDebugPort(void)
552 {
553   CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
554   CKCUClock.Bit.PA         = 1;
555   CKCUClock.Bit.AFIO       = 1;
556   CKCU_PeripClockConfig(CKCUClock, ENABLE);
557 
558   AFIO_GPxConfig(GPIO_PA, GPIO_PIN_13, AFIO_FUN_GPIO);
559   GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_13, GPIO_PR_DOWN);
560 
561   #if defined(USE_HT32F52342_52)
562   GPIO_InputConfig(HT_GPIOA, GPIO_PIN_13, DISABLE);
563   #endif
564 
565   #if 0
566   GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_12, GPIO_PR_DOWN);
567   GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_12, GPIO_PR_UP);
568   GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_12, GPIO_PR_DOWN);
569   GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_12, GPIO_PR_UP);
570   GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_12, GPIO_PR_DOWN);
571   GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_12, GPIO_PR_UP);
572   GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_12, GPIO_PR_DOWN);
573   GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_12, GPIO_PR_UP);
574   AFIO_GPxConfig(GPIO_PA, GPIO_PIN_12, AFIO_FUN_GPIO);
575   #endif
576 }
577 #endif
578 
579 /*********************************************************************************************************//**
580  * @brief Convert HT_GPIOx to GPIO_Px
581  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
582  * @retval GPIO_Px: GPIO ID
583  ************************************************************************************************************/
GPIO_GetID(HT_GPIO_TypeDef * HT_GPIOx)584 u32 GPIO_GetID(HT_GPIO_TypeDef* HT_GPIOx)
585 {
586   // Convert 0x400B0000 ~ 0x400C6000 to 0 ~ 11
587   u32 GPIO_Px = (((u32)HT_GPIOx) >> (12 + 1)) & 0x7F;
588   GPIO_Px -= 0x58;  // 0xB0000 >> 13 = 0x58
589 
590   return GPIO_Px;
591 }
592 
593 /*********************************************************************************************************//**
594  * @brief Deinitialize the AFIO peripheral registers to their default reset values.
595  * @retval None
596  ************************************************************************************************************/
AFIO_DeInit(void)597 void AFIO_DeInit(void)
598 {
599   RSTCU_PeripReset_TypeDef RSTCUReset = {{0}};
600 
601   RSTCUReset.Bit.AFIO = 1;
602   RSTCU_PeripReset(RSTCUReset, ENABLE);
603 }
604 
605 /*********************************************************************************************************//**
606  * @brief Configure alternated mode of GPIO with specified pins.
607  * @param GPIO_Px: GPIO_PA ~ GPIO_PD.
608  * @param AFIO_PIN_n: This parameter can be any combination of AFIO_PIN_x.
609  * @param AFIO_MODE_n: This parameter can be one of the following values:
610  *        @arg AFIO_MODE_DEFAULT : The default I/O function
611  *        @arg AFIO_MODE_1       : Alternated mode 1
612  *        @arg AFIO_MODE_2       : Alternated mode 2
613  *        @arg AFIO_MODE_3       : Alternated mode 3
614  *        @arg AFIO_MODE_4       : Alternated mode 4
615  *        @arg AFIO_MODE_5       : Alternated mode 5
616  *        @arg AFIO_MODE_6       : Alternated mode 6
617  *        @arg AFIO_MODE_7       : Alternated mode 7
618  *        @arg AFIO_MODE_8       : Alternated mode 8
619  *        @arg AFIO_MODE_9       : Alternated mode 9
620  *        @arg AFIO_MODE_10      : Alternated mode 10
621  *        @arg AFIO_MODE_11      : Alternated mode 11
622  *        @arg AFIO_MODE_12      : Alternated mode 12
623  *        @arg AFIO_MODE_13      : Alternated mode 13
624  *        @arg AFIO_MODE_14      : Alternated mode 14
625  *        @arg AFIO_MODE_15      : Alternated mode 15
626  * @retval None
627  ************************************************************************************************************/
AFIO_GPxConfig(u32 GPIO_Px,u32 AFIO_PIN_n,AFIO_MODE_Enum AFIO_MODE_n)628 void AFIO_GPxConfig(u32 GPIO_Px, u32 AFIO_PIN_n, AFIO_MODE_Enum AFIO_MODE_n)
629 {
630   vu32* pGPxCFGR = ((vu32*)&HT_AFIO->GPACFGR[0]) + GPIO_Px * 2;
631   u32 index = 0;
632   u32 Mask = 0, PinMode = 0;
633   s32 i;
634   AFIO_CK_ST;
635 
636   Assert_Param(IS_AFIO_MODE(AFIO_MODE_n));
637   AFIO_CK_ON();
638 
639   for (i = 0; i <= 8; i += 8)
640   {
641     Mask = 0;
642     PinMode = 0;
643     if (AFIO_PIN_n & (0x00FF << i))
644     {
645       for (index = 0; index < 8; index++)
646       {
647         if ((AFIO_PIN_n >> index) & (0x0001 << i))
648         {
649           Mask |= (0xF << (index * 4));
650           PinMode |= (AFIO_MODE_n << (index * 4));
651         }
652       }
653       *pGPxCFGR = (*pGPxCFGR & (~Mask)) | PinMode;
654     }
655     pGPxCFGR++;
656   }
657 
658   AFIO_CK_OFF();
659 }
660 
661 /*********************************************************************************************************//**
662  * @brief Select the GPIO pin to be used as EXTI channel.
663  * @param GPIO_PIN_NUM_n: Specify the GPIO pin number to be configured.
664  * @param GPIO_Px: GPIO_PA ~ GPIO_PF.
665  * @retval None
666  ************************************************************************************************************/
AFIO_EXTISourceConfig(u32 GPIO_PIN_NUM_n,u32 GPIO_Px)667 void AFIO_EXTISourceConfig(u32 GPIO_PIN_NUM_n, u32 GPIO_Px)
668 {
669   u8 index = 0;
670   u32 tmp = 0;
671   AFIO_CK_ST;
672 
673   /* Check the parameters                                                                                   */
674   Assert_Param(IS_GPIO_PORT(GPIO_Px));
675   Assert_Param(IS_GPIO_PIN_NUM(GPIO_PIN_NUM_n));
676 
677   AFIO_CK_ON();
678 
679   if (GPIO_PIN_NUM_n > 7)
680   {
681     #if (LIBCFG_EXTI_8CH)
682     GPIO_Px += 8;
683     #else
684     index = 1;
685     #endif
686     GPIO_PIN_NUM_n -= 8;
687   }
688 
689   tmp = HT_AFIO->ESSR[index];
690   tmp &= ~((u32)0x0F << (GPIO_PIN_NUM_n * 4));
691   tmp |= (u32)GPIO_Px << (GPIO_PIN_NUM_n * 4);
692   HT_AFIO->ESSR[index] = tmp;
693   AFIO_CK_OFF();
694 }
695 /**
696   * @}
697   */
698 
699 /* Private functions ---------------------------------------------------------------------------------------*/
700 /** @defgroup GPIO_Private_Functions GPIO private functions
701   * @{
702   */
703 /*********************************************************************************************************//**
704  * @brief Turn on/Turn off specify GPIO clock.
705  * @param HT_GPIOx: where HT_GPIOx is the selected GPIO from the GPIO peripherals.
706  * @param Cmd: This parameter can be ENABLE or DISABLE.
707  * @retval TRUE or FALSE (TRUE: already turn on, FALSE, Turn on by this call)
708  ***********************************************************************************************************/
_GPIO_ClockControl(HT_GPIO_TypeDef * HT_GPIOx,ControlStatus Cmd)709 u32 _GPIO_ClockControl(HT_GPIO_TypeDef* HT_GPIOx, ControlStatus Cmd)
710 {
711   u32 PxENStatus;
712   /*--------------------------------------------------------------------------------------------------------*/
713   /* ((0x400Bx000 & 0x0000F000) >> 12 ) / 2 + 16 =                                                          */
714   /*  (0x0 ~ 0x4) + 16 = 16 ~ 20 for AHBCCR PAEN ~ PEEN bit offset                                          */
715   /*--------------------------------------------------------------------------------------------------------*/
716   u32 offset = ((((u32)HT_GPIOx) & 0x0000F000) >> 12) / 2 + 16;
717 
718   PxENStatus = HT_CKCU->AHBCCR & (1 << offset);
719 
720   if (PxENStatus != 0)
721   {
722     if (Cmd == DISABLE)
723     {
724       HT_CKCU->AHBCCR &= (~(1 << offset));
725     }
726     return TRUE;
727   }
728 
729   HT_CKCU->AHBCCR |= (1 << offset);
730   return FALSE;
731 }
732 
733 /*********************************************************************************************************//**
734  * @brief Turn on/Turn off AFIO clock.
735  * @param Cmd: This parameter can be ENABLE or DISABLE.
736  * @retval TRUE or FALSE (TRUE: already turn on, FALSE, Turn on by this call)
737  ***********************************************************************************************************/
_AFIO_ClockControl(ControlStatus Cmd)738 u32 _AFIO_ClockControl(ControlStatus Cmd)
739 {
740   u32 AFIOENStatus;
741 
742   AFIOENStatus = HT_CKCU->APBCCR0 & (1 << 14);
743 
744   if (AFIOENStatus != 0)
745   {
746     if (Cmd == DISABLE)
747     {
748       HT_CKCU->APBCCR0 &= (~(1 << 14));
749     }
750     return TRUE;
751   }
752 
753   HT_CKCU->APBCCR0 |= (1 << 14);
754   return FALSE;
755 }
756 /**
757   * @}
758   */
759 
760 
761 /**
762   * @}
763   */
764 
765 /**
766   * @}
767   */
768