xref: /btstack/port/stm32-l451-miromico-sx1280/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_gpio.c (revision 2fd737d36a1de5d778cacc671d4b4d8c4f3fed82)
1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_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 #if defined(USE_FULL_LL_DRIVER)
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32l4xx_ll_gpio.h"
23 #include "stm32l4xx_ll_bus.h"
24 #ifdef  USE_FULL_ASSERT
25 #include "stm32_assert.h"
26 #else
27 #define assert_param(expr) ((void)0U)
28 #endif
29 
30 /** @addtogroup STM32L4xx_LL_Driver
31   * @{
32   */
33 
34 #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI)
35 
36 /** @addtogroup GPIO_LL
37   * @{
38   */
39 /** MISRA C:2012 deviation rule has been granted for following rules:
40   * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
41   * range of the shift operator in following API :
42   * LL_GPIO_Init
43   */
44 
45 /* Private types -------------------------------------------------------------*/
46 /* Private variables ---------------------------------------------------------*/
47 /* Private constants ---------------------------------------------------------*/
48 /* Private macros ------------------------------------------------------------*/
49 /** @addtogroup GPIO_LL_Private_Macros
50   * @{
51   */
52 #define IS_LL_GPIO_PIN(__VALUE__)          (((0x00u) < (__VALUE__)) && ((__VALUE__) <= (LL_GPIO_PIN_ALL)))
53 
54 #define IS_LL_GPIO_MODE(__VALUE__)         (((__VALUE__) == LL_GPIO_MODE_INPUT)     ||\
55                                             ((__VALUE__) == LL_GPIO_MODE_OUTPUT)    ||\
56                                             ((__VALUE__) == LL_GPIO_MODE_ALTERNATE) ||\
57                                             ((__VALUE__) == LL_GPIO_MODE_ANALOG))
58 
59 #define IS_LL_GPIO_OUTPUT_TYPE(__VALUE__)  (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL)  ||\
60                                             ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
61 
62 #define IS_LL_GPIO_SPEED(__VALUE__)        (((__VALUE__) == LL_GPIO_SPEED_FREQ_LOW)       ||\
63                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_MEDIUM)    ||\
64                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_HIGH)      ||\
65                                             ((__VALUE__) == LL_GPIO_SPEED_FREQ_VERY_HIGH))
66 
67 #define IS_LL_GPIO_PULL(__VALUE__)         (((__VALUE__) == LL_GPIO_PULL_NO)   ||\
68                                             ((__VALUE__) == LL_GPIO_PULL_UP)   ||\
69                                             ((__VALUE__) == LL_GPIO_PULL_DOWN))
70 
71 #define IS_LL_GPIO_ALTERNATE(__VALUE__)    (((__VALUE__) == LL_GPIO_AF_0  )   ||\
72                                             ((__VALUE__) == LL_GPIO_AF_1  )   ||\
73                                             ((__VALUE__) == LL_GPIO_AF_2  )   ||\
74                                             ((__VALUE__) == LL_GPIO_AF_3  )   ||\
75                                             ((__VALUE__) == LL_GPIO_AF_4  )   ||\
76                                             ((__VALUE__) == LL_GPIO_AF_5  )   ||\
77                                             ((__VALUE__) == LL_GPIO_AF_6  )   ||\
78                                             ((__VALUE__) == LL_GPIO_AF_7  )   ||\
79                                             ((__VALUE__) == LL_GPIO_AF_8  )   ||\
80                                             ((__VALUE__) == LL_GPIO_AF_9  )   ||\
81                                             ((__VALUE__) == LL_GPIO_AF_10 )   ||\
82                                             ((__VALUE__) == LL_GPIO_AF_11 )   ||\
83                                             ((__VALUE__) == LL_GPIO_AF_12 )   ||\
84                                             ((__VALUE__) == LL_GPIO_AF_13 )   ||\
85                                             ((__VALUE__) == LL_GPIO_AF_14 )   ||\
86                                             ((__VALUE__) == LL_GPIO_AF_15 ))
87 /**
88   * @}
89   */
90 
91 /* Private function prototypes -----------------------------------------------*/
92 
93 /* Exported functions --------------------------------------------------------*/
94 /** @addtogroup GPIO_LL_Exported_Functions
95   * @{
96   */
97 
98 /** @addtogroup GPIO_LL_EF_Init
99   * @{
100   */
101 
102 /**
103   * @brief  De-initialize GPIO registers (Registers restored to their default values).
104   * @param  GPIOx GPIO Port
105   * @retval An ErrorStatus enumeration value:
106   *          - SUCCESS: GPIO registers are de-initialized
107   *          - ERROR:   Wrong GPIO Port
108   */
LL_GPIO_DeInit(GPIO_TypeDef * GPIOx)109 ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
110 {
111   ErrorStatus status = SUCCESS;
112 
113   /* Check the parameters */
114   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
115 
116   /* Force and Release reset on clock of GPIOx Port */
117   if (GPIOx == GPIOA)
118   {
119     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOA);
120     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOA);
121   }
122   else if (GPIOx == GPIOB)
123   {
124     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOB);
125     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOB);
126   }
127   else if (GPIOx == GPIOC)
128   {
129     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOC);
130     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOC);
131   }
132 #if defined(GPIOD)
133   else if (GPIOx == GPIOD)
134   {
135     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOD);
136     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOD);
137   }
138 #endif /* GPIOD */
139 #if defined(GPIOE)
140   else if (GPIOx == GPIOE)
141   {
142     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOE);
143     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOE);
144   }
145 #endif /* GPIOE */
146 #if defined(GPIOF)
147   else if (GPIOx == GPIOF)
148   {
149     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOF);
150     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOF);
151   }
152 #endif /* GPIOF */
153 #if defined(GPIOG)
154   else if (GPIOx == GPIOG)
155   {
156     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOG);
157     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOG);
158   }
159 #endif /* GPIOG */
160 #if defined(GPIOH)
161   else if (GPIOx == GPIOH)
162   {
163     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOH);
164     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOH);
165   }
166 #endif /* GPIOH */
167 #if defined(GPIOI)
168   else if (GPIOx == GPIOI)
169   {
170     LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOI);
171     LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOI);
172   }
173 #endif /* GPIOI */
174   else
175   {
176     status = ERROR;
177   }
178 
179   return (status);
180 }
181 
182 /**
183   * @brief  Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
184   * @param  GPIOx GPIO Port
185   * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
186   *         that contains the configuration information for the specified GPIO peripheral.
187   * @retval An ErrorStatus enumeration value:
188   *          - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
189   *          - ERROR:   Not applicable
190   */
LL_GPIO_Init(GPIO_TypeDef * GPIOx,LL_GPIO_InitTypeDef * GPIO_InitStruct)191 ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
192 {
193   uint32_t pinpos;
194   uint32_t currentpin;
195 
196   /* Check the parameters */
197   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
198   assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
199   assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
200   assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
201 
202   /* ------------------------- Configure the port pins ---------------- */
203   /* Initialize  pinpos on first pin set */
204   pinpos = POSITION_VAL(GPIO_InitStruct->Pin);
205 
206   /* Configure the port pins */
207   while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00u)
208   {
209     /* Get current io position */
210     currentpin = (GPIO_InitStruct->Pin) & (0x00000001uL << pinpos);
211 
212     if (currentpin != 0x00u)
213     {
214       /* Pin Mode configuration */
215       LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
216 
217       if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
218       {
219         /* Check Speed mode parameters */
220         assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
221 
222         /* Speed mode configuration */
223         LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
224       }
225 
226       /* Pull-up Pull down resistor configuration*/
227       LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
228 
229       if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
230       {
231         /* Check Alternate parameter */
232         assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
233 
234         /* Speed mode configuration */
235         if (currentpin < LL_GPIO_PIN_8)
236         {
237           LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
238         }
239         else
240         {
241           LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
242         }
243       }
244     }
245     pinpos++;
246   }
247 
248   if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
249   {
250     /* Check Output mode parameters */
251     assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
252 
253     /* Output mode configuration*/
254     LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
255 
256   }
257   return (SUCCESS);
258 }
259 
260 /**
261   * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
262   * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
263   *                          whose fields will be set to default values.
264   * @retval None
265   */
266 
LL_GPIO_StructInit(LL_GPIO_InitTypeDef * GPIO_InitStruct)267 void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
268 {
269   /* Reset GPIO init structure parameters values */
270   GPIO_InitStruct->Pin        = LL_GPIO_PIN_ALL;
271   GPIO_InitStruct->Mode       = LL_GPIO_MODE_ANALOG;
272   GPIO_InitStruct->Speed      = LL_GPIO_SPEED_FREQ_LOW;
273   GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
274   GPIO_InitStruct->Pull       = LL_GPIO_PULL_NO;
275   GPIO_InitStruct->Alternate  = LL_GPIO_AF_0;
276 }
277 
278 /**
279   * @}
280   */
281 
282 /**
283   * @}
284   */
285 
286 /**
287   * @}
288   */
289 
290 #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI) */
291 
292 /**
293   * @}
294   */
295 
296 #endif /* USE_FULL_LL_DRIVER */
297 
298 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
299