1 /**
2 ******************************************************************************
3 * @file stm32l0xx_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 "stm32l0xx_ll_gpio.h"
22 #include "stm32l0xx_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 STM32L0xx_LL_Driver
30 * @{
31 */
32
33 #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOH)
34
35 /** @addtogroup GPIO_LL
36 * @{
37 */
38
39 /* Private types -------------------------------------------------------------*/
40 /* Private variables ---------------------------------------------------------*/
41 /* Private constants ---------------------------------------------------------*/
42 /* Private macros ------------------------------------------------------------*/
43 /** @addtogroup GPIO_LL_Private_Macros
44 * @{
45 */
46 #define IS_LL_GPIO_PIN(__VALUE__) (((0x00000000UL) < (__VALUE__)) && ((__VALUE__) <= (LL_GPIO_PIN_ALL)))
47
48 #define IS_LL_GPIO_MODE(__VALUE__) (((__VALUE__) == LL_GPIO_MODE_INPUT) ||\
49 ((__VALUE__) == LL_GPIO_MODE_OUTPUT) ||\
50 ((__VALUE__) == LL_GPIO_MODE_ALTERNATE) ||\
51 ((__VALUE__) == LL_GPIO_MODE_ANALOG))
52
53 #define IS_LL_GPIO_OUTPUT_TYPE(__VALUE__) (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL) ||\
54 ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
55
56 #define IS_LL_GPIO_SPEED(__VALUE__) (((__VALUE__) == LL_GPIO_SPEED_FREQ_LOW) ||\
57 ((__VALUE__) == LL_GPIO_SPEED_FREQ_MEDIUM) ||\
58 ((__VALUE__) == LL_GPIO_SPEED_FREQ_HIGH) ||\
59 ((__VALUE__) == LL_GPIO_SPEED_FREQ_VERY_HIGH))
60
61 #define IS_LL_GPIO_PULL(__VALUE__) (((__VALUE__) == LL_GPIO_PULL_NO) ||\
62 ((__VALUE__) == LL_GPIO_PULL_UP) ||\
63 ((__VALUE__) == LL_GPIO_PULL_DOWN))
64
65 #define IS_LL_GPIO_ALTERNATE(__VALUE__) (((__VALUE__) == LL_GPIO_AF_0 ) ||\
66 ((__VALUE__) == LL_GPIO_AF_1 ) ||\
67 ((__VALUE__) == LL_GPIO_AF_2 ) ||\
68 ((__VALUE__) == LL_GPIO_AF_3 ) ||\
69 ((__VALUE__) == LL_GPIO_AF_4 ) ||\
70 ((__VALUE__) == LL_GPIO_AF_5 ) ||\
71 ((__VALUE__) == LL_GPIO_AF_6 ) ||\
72 ((__VALUE__) == LL_GPIO_AF_7 ))
73 /**
74 * @}
75 */
76
77 /* Private function prototypes -----------------------------------------------*/
78
79 /* Exported functions --------------------------------------------------------*/
80 /** @addtogroup GPIO_LL_Exported_Functions
81 * @{
82 */
83
84 /** @addtogroup GPIO_LL_EF_Init
85 * @{
86 */
87
88 /**
89 * @brief De-initialize GPIO registers (Registers restored to their default values).
90 * @param GPIOx GPIO Port
91 * @retval An ErrorStatus enumeration value:
92 * - SUCCESS: GPIO registers are de-initialized
93 * - ERROR: Wrong GPIO Port
94 */
LL_GPIO_DeInit(GPIO_TypeDef * GPIOx)95 ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
96 {
97 ErrorStatus status = SUCCESS;
98
99 /* Check the parameters */
100 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
101
102 /* Force and Release reset on clock of GPIOx Port */
103 if (GPIOx == GPIOA)
104 {
105 LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOA);
106 LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOA);
107 }
108 else if (GPIOx == GPIOB)
109 {
110 LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOB);
111 LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOB);
112 }
113 else if (GPIOx == GPIOC)
114 {
115 LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOC);
116 LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOC);
117 }
118 #if defined(GPIOD)
119 else if (GPIOx == GPIOD)
120 {
121 LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOD);
122 LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOD);
123 }
124 #endif /* GPIOD */
125 #if defined(GPIOE)
126 else if (GPIOx == GPIOE)
127 {
128 LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOE);
129 LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOE);
130 }
131 #endif /* GPIOE */
132 #if defined(GPIOH)
133 else if (GPIOx == GPIOH)
134 {
135 LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOH);
136 LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOH);
137 }
138 #endif /* GPIOH */
139 else
140 {
141 status = ERROR;
142 }
143
144 return (status);
145 }
146
147 /**
148 * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
149 * @param GPIOx GPIO Port
150 * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
151 * that contains the configuration information for the specified GPIO peripheral.
152 * @retval An ErrorStatus enumeration value:
153 * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
154 * - ERROR: Not applicable
155 */
LL_GPIO_Init(GPIO_TypeDef * GPIOx,LL_GPIO_InitTypeDef * GPIO_InitStruct)156 ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
157 {
158 uint32_t pinpos = 0x00000000U;
159 uint32_t currentpin = 0x00000000U;
160
161 /* Check the parameters */
162 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
163 assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
164 assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
165 assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
166
167 /* ------------------------- Configure the port pins ---------------- */
168 /* Initialize pinpos on first pin set */
169 /* pinpos = 0; useless as already done in default initialization */
170
171 /* Configure the port pins */
172 while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00000000U)
173 {
174 /* Get current io position */
175 currentpin = (GPIO_InitStruct->Pin) & (0x00000001U << pinpos);
176
177 if (currentpin)
178 {
179 if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
180 {
181 /* Check Speed mode parameters */
182 assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
183
184 /* Speed mode configuration */
185 LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
186
187 /* Check Output mode parameters */
188 assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
189
190 /* Output mode configuration*/
191 LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
192 }
193
194 /* Pull-up Pull down resistor configuration*/
195 LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
196
197 if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
198 {
199 /* Check Alternate parameter */
200 assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
201
202 /* Speed mode configuration */
203 if (currentpin < LL_GPIO_PIN_8)
204 {
205 LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
206 }
207 else
208 {
209 LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
210 }
211 }
212
213 /* Pin Mode configuration */
214 LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
215 }
216 pinpos++;
217 }
218
219
220 return (SUCCESS);
221 }
222
223 /**
224 * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
225 * @param GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
226 * whose fields will be set to default values.
227 * @retval None
228 */
229
LL_GPIO_StructInit(LL_GPIO_InitTypeDef * GPIO_InitStruct)230 void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
231 {
232 /* Reset GPIO init structure parameters values */
233 GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
234 GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
235 GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
236 GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
237 GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
238 GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
239 }
240
241 /**
242 * @}
243 */
244
245 /**
246 * @}
247 */
248
249 /**
250 * @}
251 */
252
253 #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOH) */
254
255 /**
256 * @}
257 */
258
259 #endif /* USE_FULL_LL_DRIVER */
260
261
262