Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2 | Rev 6 | ||
---|---|---|---|
Line 128... | Line 128... | ||
128 | 128 | ||
129 | #ifdef HAL_GPIO_MODULE_ENABLED |
129 | #ifdef HAL_GPIO_MODULE_ENABLED |
130 | 130 | ||
131 | /* Private typedef -----------------------------------------------------------*/ |
131 | /* Private typedef -----------------------------------------------------------*/ |
132 | /* Private defines -----------------------------------------------------------*/ |
132 | /* Private defines -----------------------------------------------------------*/ |
133 | /** @defgroup GPIO_Private_Defines GPIO Private Defines |
133 | /** @addtogroup GPIO_Private_Constants GPIO Private Constants |
134 | * @{ |
134 | * @{ |
135 | */ |
135 | */ |
136 | #define GPIO_MODE (0x00000003U) |
- | |
137 | #define EXTI_MODE (0x10000000U) |
- | |
138 | #define GPIO_MODE_IT (0x00010000U) |
- | |
139 | #define GPIO_MODE_EVT (0x00020000U) |
- | |
140 | #define RISING_EDGE (0x00100000U) |
- | |
141 | #define FALLING_EDGE (0x00200000U) |
- | |
142 | #define GPIO_OUTPUT_TYPE (0x00000010U) |
- | |
143 | - | ||
144 | #define GPIO_NUMBER (16U) |
136 | #define GPIO_NUMBER 16U |
145 | /** |
137 | /** |
146 | * @} |
138 | * @} |
147 | */ |
139 | */ |
148 | 140 | ||
149 | /* Private macros ------------------------------------------------------------*/ |
141 | /* Private macros ------------------------------------------------------------*/ |
Line 173... | Line 165... | ||
173 | * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains |
165 | * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains |
174 | * the configuration information for the specified GPIO peripheral. |
166 | * the configuration information for the specified GPIO peripheral. |
175 | * @retval None |
167 | * @retval None |
176 | */ |
168 | */ |
177 | void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init) |
169 | void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init) |
178 | { |
170 | { |
179 | uint32_t position = 0x00u; |
171 | uint32_t position = 0x00u; |
180 | uint32_t iocurrent; |
172 | uint32_t iocurrent; |
181 | uint32_t temp; |
173 | uint32_t temp; |
182 | 174 | ||
183 | /* Check the parameters */ |
175 | /* Check the parameters */ |
184 | assert_param(IS_GPIO_ALL_INSTANCE(GPIOx)); |
176 | assert_param(IS_GPIO_ALL_INSTANCE(GPIOx)); |
185 | assert_param(IS_GPIO_PIN(GPIO_Init->Pin)); |
177 | assert_param(IS_GPIO_PIN(GPIO_Init->Pin)); |
186 | assert_param(IS_GPIO_MODE(GPIO_Init->Mode)); |
178 | assert_param(IS_GPIO_MODE(GPIO_Init->Mode)); |
187 | assert_param(IS_GPIO_PULL(GPIO_Init->Pull)); |
- | |
188 | 179 | ||
189 | /* Configure the port pins */ |
180 | /* Configure the port pins */ |
190 | while (((GPIO_Init->Pin) >> position) != 0x00u) |
181 | while (((GPIO_Init->Pin) >> position) != 0x00u) |
191 | { |
182 | { |
192 | /* Get current io position */ |
183 | /* Get current io position */ |
193 | iocurrent = (GPIO_Init->Pin) & (1uL << position); |
184 | iocurrent = (GPIO_Init->Pin) & (1uL << position); |
194 | 185 | ||
195 | if (iocurrent != 0x00u) |
186 | if (iocurrent != 0x00u) |
196 | { |
187 | { |
197 | /*--------------------- GPIO Mode Configuration ------------------------*/ |
188 | /*--------------------- GPIO Mode Configuration ------------------------*/ |
- | 189 | /* In case of Output or Alternate function mode selection */ |
|
- | 190 | if(((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) || |
|
- | 191 | ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF)) |
|
- | 192 | { |
|
- | 193 | /* Check the Speed parameter */ |
|
- | 194 | assert_param(IS_GPIO_SPEED(GPIO_Init->Speed)); |
|
- | 195 | /* Configure the IO Speed */ |
|
- | 196 | temp = GPIOx->OSPEEDR; |
|
- | 197 | temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2u)); |
|
- | 198 | temp |= (GPIO_Init->Speed << (position * 2u)); |
|
- | 199 | GPIOx->OSPEEDR = temp; |
|
- | 200 | ||
- | 201 | /* Configure the IO Output Type */ |
|
- | 202 | temp = GPIOx->OTYPER; |
|
- | 203 | temp &= ~(GPIO_OTYPER_OT_0 << position) ; |
|
- | 204 | temp |= (((GPIO_Init->Mode & OUTPUT_TYPE) >> OUTPUT_TYPE_Pos) << position); |
|
- | 205 | GPIOx->OTYPER = temp; |
|
- | 206 | } |
|
- | 207 | ||
- | 208 | if((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG) |
|
- | 209 | { |
|
- | 210 | /* Check the Pull parameter */ |
|
- | 211 | assert_param(IS_GPIO_PULL(GPIO_Init->Pull)); |
|
- | 212 | ||
- | 213 | /* Activate the Pull-up or Pull down resistor for the current IO */ |
|
- | 214 | temp = GPIOx->PUPDR; |
|
- | 215 | temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2u)); |
|
- | 216 | temp |= ((GPIO_Init->Pull) << (position * 2u)); |
|
- | 217 | GPIOx->PUPDR = temp; |
|
- | 218 | } |
|
- | 219 | ||
198 | /* In case of Alternate function mode selection */ |
220 | /* In case of Alternate function mode selection */ |
199 | if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD)) |
221 | if((GPIO_Init->Mode & GPIO_MODE) == MODE_AF) |
200 | { |
222 | { |
201 | /* Check the Alternate function parameters */ |
223 | /* Check the Alternate function parameters */ |
202 | assert_param(IS_GPIO_AF_INSTANCE(GPIOx)); |
224 | assert_param(IS_GPIO_AF_INSTANCE(GPIOx)); |
203 | assert_param(IS_GPIO_AF(GPIO_Init->Alternate)); |
225 | assert_param(IS_GPIO_AF(GPIO_Init->Alternate)); |
204 | 226 | ||
Line 213... | Line 235... | ||
213 | temp = GPIOx->MODER; |
235 | temp = GPIOx->MODER; |
214 | temp &= ~(GPIO_MODER_MODER0 << (position * 2u)); |
236 | temp &= ~(GPIO_MODER_MODER0 << (position * 2u)); |
215 | temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2u)); |
237 | temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2u)); |
216 | GPIOx->MODER = temp; |
238 | GPIOx->MODER = temp; |
217 | 239 | ||
218 | /* In case of Output or Alternate function mode selection */ |
- | |
219 | if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) || |
- | |
220 | (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD)) |
- | |
221 | { |
- | |
222 | /* Check the Speed parameter */ |
- | |
223 | assert_param(IS_GPIO_SPEED(GPIO_Init->Speed)); |
- | |
224 | /* Configure the IO Speed */ |
- | |
225 | temp = GPIOx->OSPEEDR; |
- | |
226 | temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2u)); |
- | |
227 | temp |= (GPIO_Init->Speed << (position * 2u)); |
- | |
228 | GPIOx->OSPEEDR = temp; |
- | |
229 | - | ||
230 | /* Configure the IO Output Type */ |
- | |
231 | temp = GPIOx->OTYPER; |
- | |
232 | temp &= ~(GPIO_OTYPER_OT_0 << position) ; |
- | |
233 | temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4u) << position); |
- | |
234 | GPIOx->OTYPER = temp; |
- | |
235 | } |
- | |
236 | - | ||
237 | /* Activate the Pull-up or Pull down resistor for the current IO */ |
- | |
238 | temp = GPIOx->PUPDR; |
- | |
239 | temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2u)); |
- | |
240 | temp |= ((GPIO_Init->Pull) << (position * 2u)); |
- | |
241 | GPIOx->PUPDR = temp; |
- | |
242 | - | ||
243 | /*--------------------- EXTI Mode Configuration ------------------------*/ |
240 | /*--------------------- EXTI Mode Configuration ------------------------*/ |
244 | /* Configure the External Interrupt or event for the current IO */ |
241 | /* Configure the External Interrupt or event for the current IO */ |
245 | if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE) |
242 | if((GPIO_Init->Mode & EXTI_MODE) != 0x00u) |
246 | { |
243 | { |
247 | /* Enable SYSCFG Clock */ |
244 | /* Enable SYSCFG Clock */ |
248 | __HAL_RCC_SYSCFG_CLK_ENABLE(); |
245 | __HAL_RCC_SYSCFG_CLK_ENABLE(); |
249 | 246 | ||
250 | temp = SYSCFG->EXTICR[position >> 2u]; |
247 | temp = SYSCFG->EXTICR[position >> 2u]; |
Line 253... | Line 250... | ||
253 | SYSCFG->EXTICR[position >> 2u] = temp; |
250 | SYSCFG->EXTICR[position >> 2u] = temp; |
254 | 251 | ||
255 | /* Clear EXTI line configuration */ |
252 | /* Clear EXTI line configuration */ |
256 | temp = EXTI->IMR; |
253 | temp = EXTI->IMR; |
257 | temp &= ~(iocurrent); |
254 | temp &= ~(iocurrent); |
258 | if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT) |
255 | if((GPIO_Init->Mode & EXTI_IT) != 0x00u) |
259 | { |
256 | { |
260 | temp |= iocurrent; |
257 | temp |= iocurrent; |
261 | } |
258 | } |
262 | EXTI->IMR = temp; |
259 | EXTI->IMR = temp; |
263 | 260 | ||
264 | temp = EXTI->EMR; |
261 | temp = EXTI->EMR; |
265 | temp &= ~(iocurrent); |
262 | temp &= ~(iocurrent); |
266 | if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT) |
263 | if((GPIO_Init->Mode & EXTI_EVT) != 0x00u) |
267 | { |
264 | { |
268 | temp |= iocurrent; |
265 | temp |= iocurrent; |
269 | } |
266 | } |
270 | EXTI->EMR = temp; |
267 | EXTI->EMR = temp; |
271 | 268 | ||
272 | /* Clear Rising Falling edge configuration */ |
269 | /* Clear Rising Falling edge configuration */ |
273 | temp = EXTI->RTSR; |
270 | temp = EXTI->RTSR; |
274 | temp &= ~(iocurrent); |
271 | temp &= ~(iocurrent); |
275 | if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE) |
272 | if((GPIO_Init->Mode & TRIGGER_RISING) != 0x00u) |
276 | { |
273 | { |
277 | temp |= iocurrent; |
274 | temp |= iocurrent; |
278 | } |
275 | } |
279 | EXTI->RTSR = temp; |
276 | EXTI->RTSR = temp; |
280 | 277 | ||
281 | temp = EXTI->FTSR; |
278 | temp = EXTI->FTSR; |
282 | temp &= ~(iocurrent); |
279 | temp &= ~(iocurrent); |
283 | if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE) |
280 | if((GPIO_Init->Mode & TRIGGER_FALLING) != 0x00u) |
284 | { |
281 | { |
285 | temp |= iocurrent; |
282 | temp |= iocurrent; |
286 | } |
283 | } |
287 | EXTI->FTSR = temp; |
284 | EXTI->FTSR = temp; |
288 | } |
285 | } |
Line 342... | Line 339... | ||
342 | GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2u)); |
339 | GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2u)); |
343 | 340 | ||
344 | /* Configure the default Alternate Function in current IO */ |
341 | /* Configure the default Alternate Function in current IO */ |
345 | GPIOx->AFR[position >> 3u] &= ~(0xFu << ((uint32_t)(position & 0x07u) * 4u)) ; |
342 | GPIOx->AFR[position >> 3u] &= ~(0xFu << ((uint32_t)(position & 0x07u) * 4u)) ; |
346 | 343 | ||
347 | /* Configure the default value for IO Speed */ |
344 | /* Deactivate the Pull-up and Pull-down resistor for the current IO */ |
348 | GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2u)); |
345 | GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2u)); |
349 | 346 | ||
350 | /* Configure the default value IO Output Type */ |
347 | /* Configure the default value IO Output Type */ |
351 | GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ; |
348 | GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ; |
352 | 349 | ||
353 | /* Deactivate the Pull-up and Pull-down resistor for the current IO */ |
350 | /* Configure the default value for IO Speed */ |
354 | GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U)); |
351 | GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2u)); |
- | 352 | ||
355 | } |
353 | } |
356 | 354 | ||
357 | position++; |
355 | position++; |
358 | } |
356 | } |
359 | } |
357 | } |
Line 436... | Line 434... | ||
436 | * @param GPIO_Pin specifies the pin to be toggled. |
434 | * @param GPIO_Pin specifies the pin to be toggled. |
437 | * @retval None |
435 | * @retval None |
438 | */ |
436 | */ |
439 | void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) |
437 | void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) |
440 | { |
438 | { |
- | 439 | uint32_t odr; |
|
- | 440 | ||
441 | /* Check the parameters */ |
441 | /* Check the parameters */ |
442 | assert_param(IS_GPIO_PIN(GPIO_Pin)); |
442 | assert_param(IS_GPIO_PIN(GPIO_Pin)); |
443 | 443 | ||
- | 444 | /* get current Ouput Data Register value */ |
|
444 | if ((GPIOx->ODR & GPIO_Pin) != 0X00u) |
445 | odr = GPIOx->ODR; |
445 | { |
446 | |
446 | GPIOx->BSRR = (uint32_t)GPIO_Pin << GPIO_NUMBER; |
447 | /* Set selected pins that were at low level, and reset ones that were high */ |
447 | } |
- | |
448 | else |
- | |
449 | { |
- | |
450 | GPIOx->BSRR = (uint32_t)GPIO_Pin; |
448 | GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin); |
451 | } |
- | |
452 | } |
449 | } |
453 | 450 | ||
454 | /** |
451 | /** |
455 | * @brief Locks GPIO Pins configuration registers. |
452 | * @brief Locks GPIO Pins configuration registers. |
456 | * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR, |
453 | * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR, |