1 /**
2 ******************************************************************************
3 * @file stm32f0xx_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
19 #if defined(USE_FULL_LL_DRIVER)
20
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32f0xx_ll_gpio.h"
23 #include "stm32f0xx_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 STM32F0xx_LL_Driver
31 * @{
32 */
33
34 #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF)
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__) (((0x00u) < (__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
77 #define IS_LL_GPIO_PULL(__VALUE__) (((__VALUE__) == LL_GPIO_PULL_NO) ||\
78 ((__VALUE__) == LL_GPIO_PULL_UP) ||\
79 ((__VALUE__) == LL_GPIO_PULL_DOWN))
80
81 #define IS_LL_GPIO_ALTERNATE(__VALUE__) (((__VALUE__) == LL_GPIO_AF_0 ) ||\
82 ((__VALUE__) == LL_GPIO_AF_1 ) ||\
83 ((__VALUE__) == LL_GPIO_AF_2 ) ||\
84 ((__VALUE__) == LL_GPIO_AF_3 ) ||\
85 ((__VALUE__) == LL_GPIO_AF_4 ) ||\
86 ((__VALUE__) == LL_GPIO_AF_5 ) ||\
87 ((__VALUE__) == LL_GPIO_AF_6 ) ||\
88 ((__VALUE__) == LL_GPIO_AF_7 ))
89 /**
90 * @}
91 */
92
93 /* Private function prototypes -----------------------------------------------*/
94
95 /* Exported functions --------------------------------------------------------*/
96 /** @addtogroup GPIO_LL_Exported_Functions
97 * @{
98 */
99
100 /** @addtogroup GPIO_LL_EF_Init
101 * @{
102 */
103
104 /**
105 * @brief De-initialize GPIO registers (Registers restored to their default values).
106 * @param GPIOx GPIO Port
107 * @retval An ErrorStatus enumeration value:
108 * - SUCCESS: GPIO registers are de-initialized
109 * - ERROR: Wrong GPIO Port
110 */
LL_GPIO_DeInit(GPIO_TypeDef * GPIOx)111 ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
112 {
113 ErrorStatus status = SUCCESS;
114
115 /* Check the parameters */
116 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
117
118 /* Force and Release reset on clock of GPIOx Port */
119 if (GPIOx == GPIOA)
120 {
121 LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOA);
122 LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOA);
123 }
124 else if (GPIOx == GPIOB)
125 {
126 LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOB);
127 LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOB);
128 }
129 else if (GPIOx == GPIOC)
130 {
131 LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOC);
132 LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOC);
133 }
134 #if defined(GPIOD)
135 else if (GPIOx == GPIOD)
136 {
137 LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOD);
138 LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOD);
139 }
140 #endif /* GPIOD */
141 #if defined(GPIOE)
142 else if (GPIOx == GPIOE)
143 {
144 LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOE);
145 LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOE);
146 }
147 #endif /* GPIOE */
148 #if defined(GPIOF)
149 else if (GPIOx == GPIOF)
150 {
151 LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOF);
152 LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOF);
153 }
154 #endif /* GPIOF */
155 else
156 {
157 status = ERROR;
158 }
159
160 return (status);
161 }
162
163 /**
164 * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
165 * @param GPIOx GPIO Port
166 * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
167 * that contains the configuration information for the specified GPIO peripheral.
168 * @retval An ErrorStatus enumeration value:
169 * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
170 * - ERROR: Not applicable
171 */
LL_GPIO_Init(GPIO_TypeDef * GPIOx,LL_GPIO_InitTypeDef * GPIO_InitStruct)172 ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
173 {
174 uint32_t pinpos;
175 uint32_t currentpin;
176
177 /* Check the parameters */
178 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
179 assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
180 assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
181 assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
182
183 /* ------------------------- Configure the port pins ---------------- */
184 /* Initialize pinpos on first pin set */
185 pinpos = 0;
186
187 /* Configure the port pins */
188 while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00u)
189 {
190 /* Get current io position */
191 currentpin = (GPIO_InitStruct->Pin) & (0x00000001uL << pinpos);
192
193 if (currentpin != 0x00u)
194 {
195 if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
196 {
197 /* Check Speed mode parameters */
198 assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
199
200 /* Speed mode configuration */
201 LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
202
203 /* Check Output mode parameters */
204 assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
205
206 /* Output mode configuration*/
207 LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
208 }
209
210 /* Pull-up Pull down resistor configuration*/
211 LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
212
213 if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
214 {
215 /* Check Alternate parameter */
216 assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
217
218 /* Speed mode configuration */
219 if (currentpin < LL_GPIO_PIN_8)
220 {
221 LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
222 }
223 else
224 {
225 LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
226 }
227 }
228
229 /* Pin Mode configuration */
230 LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
231 }
232 pinpos++;
233 }
234
235 return (SUCCESS);
236 }
237
238 /**
239 * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
240 * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
241 * whose fields will be set to default values.
242 * @retval None
243 */
244
LL_GPIO_StructInit(LL_GPIO_InitTypeDef * GPIO_InitStruct)245 void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
246 {
247 /* Reset GPIO init structure parameters values */
248 GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
249 GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
250 GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
251 GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
252 GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
253 GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
254 }
255
256 /**
257 * @}
258 */
259
260 /**
261 * @}
262 */
263
264 /**
265 * @}
266 */
267
268 #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) */
269
270 /**
271 * @}
272 */
273
274 #endif /* USE_FULL_LL_DRIVER */
275
276