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