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