1 /**
2   ******************************************************************************
3   * @file    stm32h7xx_ll_gpio.c
4   * @author  MCD Application Team
5   * @brief   GPIO LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics.
10   * All rights reserved.</center></h2>
11   *
12   * This software component is licensed by ST under BSD 3-Clause license,
13   * the "License"; You may not use this file except in compliance with the
14   * License. You may obtain a copy of the License at:
15   *                        opensource.org/licenses/BSD-3-Clause
16   *
17   ******************************************************************************
18   */
19 
20 #if defined(USE_FULL_LL_DRIVER)
21 
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32h7xx_ll_gpio.h"
24 #include "stm32h7xx_ll_bus.h"
25 #ifdef  USE_FULL_ASSERT
26 #include "stm32_assert.h"
27 #else
28 #define assert_param(expr) ((void)0U)
29 #endif
30 
31 /** @addtogroup STM32H7xx_LL_Driver
32   * @{
33   */
34 
35 #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI) || defined (GPIOJ) || defined (GPIOK)
36 
37 /** @addtogroup GPIO_LL
38   * @{
39   */
40 
41 /* Private types -------------------------------------------------------------*/
42 /* Private variables ---------------------------------------------------------*/
43 /* Private constants ---------------------------------------------------------*/
44 /* Private macros ------------------------------------------------------------*/
45 /** @addtogroup GPIO_LL_Private_Macros
46   * @{
47   */
48 #define IS_LL_GPIO_PIN(__VALUE__)          (((0x00000000U) < (__VALUE__)) && ((__VALUE__) <= (LL_GPIO_PIN_ALL)))
49 
50 #define IS_LL_GPIO_MODE(__VALUE__)         (((__VALUE__) == LL_GPIO_MODE_INPUT)     ||\
51                                             ((__VALUE__) == LL_GPIO_MODE_OUTPUT)    ||\
52                                             ((__VALUE__) == LL_GPIO_MODE_ALTERNATE) ||\
53                                             ((__VALUE__) == LL_GPIO_MODE_ANALOG))
54 
55 #define IS_LL_GPIO_OUTPUT_TYPE(__VALUE__)  (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL)  ||\
56                                             ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
57 
58 #define IS_LL_GPIO_SPEED(__VALUE__)        (((__VALUE__) == LL_GPIO_SPEED_FREQ_LOW)       ||\
59                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_MEDIUM)    ||\
60                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_HIGH)      ||\
61                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_VERY_HIGH))
62 
63 #define IS_LL_GPIO_PULL(__VALUE__)         (((__VALUE__) == LL_GPIO_PULL_NO)   ||\
64                                             ((__VALUE__) == LL_GPIO_PULL_UP)   ||\
65                                             ((__VALUE__) == LL_GPIO_PULL_DOWN))
66 
67 #define IS_LL_GPIO_ALTERNATE(__VALUE__)    (((__VALUE__) == LL_GPIO_AF_0  )   ||\
68                                             ((__VALUE__) == LL_GPIO_AF_1  )   ||\
69                                             ((__VALUE__) == LL_GPIO_AF_2  )   ||\
70                                             ((__VALUE__) == LL_GPIO_AF_3  )   ||\
71                                             ((__VALUE__) == LL_GPIO_AF_4  )   ||\
72                                             ((__VALUE__) == LL_GPIO_AF_5  )   ||\
73                                             ((__VALUE__) == LL_GPIO_AF_6  )   ||\
74                                             ((__VALUE__) == LL_GPIO_AF_7  )   ||\
75                                             ((__VALUE__) == LL_GPIO_AF_8  )   ||\
76                                             ((__VALUE__) == LL_GPIO_AF_9  )   ||\
77                                             ((__VALUE__) == LL_GPIO_AF_10 )   ||\
78                                             ((__VALUE__) == LL_GPIO_AF_11 )   ||\
79                                             ((__VALUE__) == LL_GPIO_AF_12 )   ||\
80                                             ((__VALUE__) == LL_GPIO_AF_13 )   ||\
81                                             ((__VALUE__) == LL_GPIO_AF_14 )   ||\
82                                             ((__VALUE__) == LL_GPIO_AF_15 ))
83 /**
84   * @}
85   */
86 
87 /* Private function prototypes -----------------------------------------------*/
88 
89 /* Exported functions --------------------------------------------------------*/
90 /** @addtogroup GPIO_LL_Exported_Functions
91   * @{
92   */
93 
94 /** @addtogroup GPIO_LL_EF_Init
95   * @{
96   */
97 
98 /**
99   * @brief  De-initialize GPIO registers (Registers restored to their default values).
100   * @param  GPIOx GPIO Port
101   * @retval An ErrorStatus enumeration value:
102   *          - SUCCESS: GPIO registers are de-initialized
103   *          - ERROR:   Wrong GPIO Port
104   */
LL_GPIO_DeInit(GPIO_TypeDef * GPIOx)105 ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
106 {
107   ErrorStatus status = SUCCESS;
108 
109   /* Check the parameters */
110   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
111 
112   /* Force and Release reset on clock of GPIOx Port */
113   if (GPIOx == GPIOA)
114   {
115     LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOA);
116     LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOA);
117   }
118   else if (GPIOx == GPIOB)
119   {
120     LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOB);
121     LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOB);
122   }
123   else if (GPIOx == GPIOC)
124   {
125     LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOC);
126     LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOC);
127   }
128 #if defined(GPIOD)
129   else if (GPIOx == GPIOD)
130   {
131     LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOD);
132     LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOD);
133   }
134 #endif /* GPIOD */
135 #if defined(GPIOE)
136   else if (GPIOx == GPIOE)
137   {
138     LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOE);
139     LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOE);
140   }
141 #endif /* GPIOE */
142 #if defined(GPIOF)
143   else if (GPIOx == GPIOF)
144   {
145     LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOF);
146     LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOF);
147   }
148 #endif /* GPIOF */
149 #if defined(GPIOG)
150   else if (GPIOx == GPIOG)
151   {
152     LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOG);
153     LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOG);
154   }
155 #endif /* GPIOG */
156 #if defined(GPIOH)
157   else if (GPIOx == GPIOH)
158   {
159     LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOH);
160     LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOH);
161   }
162 #endif /* GPIOH */
163 #if defined(GPIOI)
164   else if (GPIOx == GPIOI)
165   {
166     LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOI);
167     LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOI);
168   }
169 #endif /* GPIOI */
170 #if defined(GPIOJ)
171   else if (GPIOx == GPIOJ)
172   {
173     LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOJ);
174     LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOJ);
175   }
176 #endif /* GPIOJ */
177 #if defined(GPIOK)
178   else if (GPIOx == GPIOK)
179   {
180     LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOK);
181     LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOK);
182   }
183 #endif /* GPIOK */
184   else
185   {
186     status = ERROR;
187   }
188 
189   return (status);
190 }
191 
192 /**
193   * @brief  Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
194   * @param  GPIOx GPIO Port
195   * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
196   *         that contains the configuration information for the specified GPIO peripheral.
197   * @retval An ErrorStatus enumeration value:
198   *          - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
199   *          - ERROR:   Not applicable
200   */
LL_GPIO_Init(GPIO_TypeDef * GPIOx,LL_GPIO_InitTypeDef * GPIO_InitStruct)201 ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
202 {
203   uint32_t pinpos, currentpin;
204 
205   /* Check the parameters */
206   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
207   assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
208   assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
209   assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
210 
211   /* ------------------------- Configure the port pins ---------------- */
212   /* Initialize  pinpos on first pin set */
213   pinpos = POSITION_VAL(GPIO_InitStruct->Pin);
214 
215   /* Configure the port pins */
216   while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00000000U)
217   {
218     /* Get current io position */
219     currentpin = (GPIO_InitStruct->Pin) & (0x00000001UL << pinpos);
220 
221     if (currentpin != 0x00000000U)
222     {
223       /* Pin Mode configuration */
224       LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
225 
226       if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
227       {
228         /* Check Speed mode parameters */
229         assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
230 
231         /* Speed mode configuration */
232         LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
233       }
234 
235       /* Pull-up Pull down resistor configuration*/
236       LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
237 
238       if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
239       {
240         /* Check Alternate parameter */
241         assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
242 
243         /* Speed mode configuration */
244         if (currentpin < LL_GPIO_PIN_8)
245         {
246           LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
247         }
248         else
249         {
250           LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
251         }
252       }
253     }
254     pinpos++;
255   }
256 
257   if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
258   {
259     /* Check Output mode parameters */
260     assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
261 
262     /* Output mode configuration*/
263     LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
264 
265   }
266   return (SUCCESS);
267 }
268 
269 /**
270   * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
271   * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
272   *                          whose fields will be set to default values.
273   * @retval None
274   */
275 
LL_GPIO_StructInit(LL_GPIO_InitTypeDef * GPIO_InitStruct)276 void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
277 {
278   /* Reset GPIO init structure parameters values */
279   GPIO_InitStruct->Pin        = LL_GPIO_PIN_ALL;
280   GPIO_InitStruct->Mode       = LL_GPIO_MODE_ANALOG;
281   GPIO_InitStruct->Speed      = LL_GPIO_SPEED_FREQ_LOW;
282   GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
283   GPIO_InitStruct->Pull       = LL_GPIO_PULL_NO;
284   GPIO_InitStruct->Alternate  = LL_GPIO_AF_0;
285 }
286 
287 /**
288   * @}
289   */
290 
291 /**
292   * @}
293   */
294 
295 /**
296   * @}
297   */
298 
299 #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI) || defined (GPIOJ) || defined (GPIOK) */
300 
301 /**
302   * @}
303   */
304 
305 #endif /* USE_FULL_LL_DRIVER */
306 
307 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
308 
309