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