1 /**
2 ******************************************************************************
3 * @file stm32f4xx_hal_gpio.c
4 * @author MCD Application Team
5 * @brief GPIO HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the General Purpose Input/Output (GPIO) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 *
11 @verbatim
12 ==============================================================================
13 ##### GPIO Peripheral features #####
14 ==============================================================================
15 [..]
16 Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each
17 port bit of the General Purpose IO (GPIO) Ports, can be individually configured by software
18 in several modes:
19 (+) Input mode
20 (+) Analog mode
21 (+) Output mode
22 (+) Alternate function mode
23 (+) External interrupt/event lines
24
25 [..]
26 During and just after reset, the alternate functions and external interrupt
27 lines are not active and the I/O ports are configured in input floating mode.
28
29 [..]
30 All GPIO pins have weak internal pull-up and pull-down resistors, which can be
31 activated or not.
32
33 [..]
34 In Output or Alternate mode, each IO can be configured on open-drain or push-pull
35 type and the IO speed can be selected depending on the VDD value.
36
37 [..]
38 All ports have external interrupt/event capability. To use external interrupt
39 lines, the port must be configured in input mode. All available GPIO pins are
40 connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
41
42 [..]
43 The external interrupt/event controller consists of up to 23 edge detectors
44 (16 lines are connected to GPIO) for generating event/interrupt requests (each
45 input line can be independently configured to select the type (interrupt or event)
46 and the corresponding trigger event (rising or falling or both). Each line can
47 also be masked independently.
48
49 ##### How to use this driver #####
50 ==============================================================================
51 [..]
52 (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
53
54 (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
55 (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
56 (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
57 structure.
58 (++) In case of Output or alternate function mode selection: the speed is
59 configured through "Speed" member from GPIO_InitTypeDef structure.
60 (++) In alternate mode is selection, the alternate function connected to the IO
61 is configured through "Alternate" member from GPIO_InitTypeDef structure.
62 (++) Analog mode is required when a pin is to be used as ADC channel
63 or DAC output.
64 (++) In case of external interrupt/event selection the "Mode" member from
65 GPIO_InitTypeDef structure select the type (interrupt or event) and
66 the corresponding trigger event (rising or falling or both).
67
68 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
69 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
70 HAL_NVIC_EnableIRQ().
71
72 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
73
74 (#) To set/reset the level of a pin configured in output mode use
75 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
76
77 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
78
79
80 (#) During and just after reset, the alternate functions are not
81 active and the GPIO pins are configured in input floating mode (except JTAG
82 pins).
83
84 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
85 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
86 priority over the GPIO function.
87
88 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
89 general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
90 The HSE has priority over the GPIO function.
91
92 @endverbatim
93 ******************************************************************************
94 * @attention
95 *
96 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
97 * All rights reserved.</center></h2>
98 *
99 * This software component is licensed by ST under BSD 3-Clause license,
100 * the "License"; You may not use this file except in compliance with the
101 * License. You may obtain a copy of the License at:
102 * opensource.org/licenses/BSD-3-Clause
103 *
104 ******************************************************************************
105 */
106
107 /* Includes ------------------------------------------------------------------*/
108 #include "stm32f4xx_hal.h"
109
110 /** @addtogroup STM32F4xx_HAL_Driver
111 * @{
112 */
113
114 /** @defgroup GPIO GPIO
115 * @brief GPIO HAL module driver
116 * @{
117 */
118
119 #ifdef HAL_GPIO_MODULE_ENABLED
120
121 /* Private typedef -----------------------------------------------------------*/
122 /* Private define ------------------------------------------------------------*/
123 /** @addtogroup GPIO_Private_Constants GPIO Private Constants
124 * @{
125 */
126 #define GPIO_MODE 0x00000003U
127 #define EXTI_MODE 0x10000000U
128 #define GPIO_MODE_IT 0x00010000U
129 #define GPIO_MODE_EVT 0x00020000U
130 #define RISING_EDGE 0x00100000U
131 #define FALLING_EDGE 0x00200000U
132 #define GPIO_OUTPUT_TYPE 0x00000010U
133
134 #define GPIO_NUMBER 16U
135 /**
136 * @}
137 */
138 /* Private macro -------------------------------------------------------------*/
139 /* Private variables ---------------------------------------------------------*/
140 /* Private function prototypes -----------------------------------------------*/
141 /* Private functions ---------------------------------------------------------*/
142 /* Exported functions --------------------------------------------------------*/
143 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
144 * @{
145 */
146
147 /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
148 * @brief Initialization and Configuration functions
149 *
150 @verbatim
151 ===============================================================================
152 ##### Initialization and de-initialization functions #####
153 ===============================================================================
154 [..]
155 This section provides functions allowing to initialize and de-initialize the GPIOs
156 to be ready for use.
157
158 @endverbatim
159 * @{
160 */
161
162
163 /**
164 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
165 * @param GPIOx where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
166 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
167 * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
168 * the configuration information for the specified GPIO peripheral.
169 * @retval None
170 */
HAL_GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_Init)171 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
172 {
173 uint32_t position;
174 uint32_t ioposition = 0x00U;
175 uint32_t iocurrent = 0x00U;
176 uint32_t temp = 0x00U;
177
178 /* Check the parameters */
179 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
180 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
181 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
182 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
183
184 /* Configure the port pins */
185 for(position = 0U; position < GPIO_NUMBER; position++)
186 {
187 /* Get the IO position */
188 ioposition = 0x01U << position;
189 /* Get the current IO position */
190 iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
191
192 if(iocurrent == ioposition)
193 {
194 /*--------------------- GPIO Mode Configuration ------------------------*/
195 /* In case of Alternate function mode selection */
196 if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
197 {
198 /* Check the Alternate function parameter */
199 assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
200 /* Configure Alternate function mapped with the current IO */
201 temp = GPIOx->AFR[position >> 3U];
202 temp &= ~(0xFU << ((uint32_t)(position & 0x07U) * 4U)) ;
203 temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & 0x07U) * 4U));
204 GPIOx->AFR[position >> 3U] = temp;
205 }
206
207 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
208 temp = GPIOx->MODER;
209 temp &= ~(GPIO_MODER_MODER0 << (position * 2U));
210 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
211 GPIOx->MODER = temp;
212
213 /* In case of Output or Alternate function mode selection */
214 if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
215 (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
216 {
217 /* Check the Speed parameter */
218 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
219 /* Configure the IO Speed */
220 temp = GPIOx->OSPEEDR;
221 temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
222 temp |= (GPIO_Init->Speed << (position * 2U));
223 GPIOx->OSPEEDR = temp;
224
225 /* Configure the IO Output Type */
226 temp = GPIOx->OTYPER;
227 temp &= ~(GPIO_OTYPER_OT_0 << position) ;
228 temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
229 GPIOx->OTYPER = temp;
230 }
231
232 /* Activate the Pull-up or Pull down resistor for the current IO */
233 temp = GPIOx->PUPDR;
234 temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
235 temp |= ((GPIO_Init->Pull) << (position * 2U));
236 GPIOx->PUPDR = temp;
237
238 /*--------------------- EXTI Mode Configuration ------------------------*/
239 /* Configure the External Interrupt or event for the current IO */
240 if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
241 {
242 /* Enable SYSCFG Clock */
243 __HAL_RCC_SYSCFG_CLK_ENABLE();
244
245 temp = SYSCFG->EXTICR[position >> 2U];
246 temp &= ~(0x0FU << (4U * (position & 0x03U)));
247 temp |= ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4U * (position & 0x03U)));
248 SYSCFG->EXTICR[position >> 2U] = temp;
249
250 /* Clear EXTI line configuration */
251 temp = EXTI->IMR;
252 temp &= ~((uint32_t)iocurrent);
253 if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
254 {
255 temp |= iocurrent;
256 }
257 EXTI->IMR = temp;
258
259 temp = EXTI->EMR;
260 temp &= ~((uint32_t)iocurrent);
261 if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
262 {
263 temp |= iocurrent;
264 }
265 EXTI->EMR = temp;
266
267 /* Clear Rising Falling edge configuration */
268 temp = EXTI->RTSR;
269 temp &= ~((uint32_t)iocurrent);
270 if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
271 {
272 temp |= iocurrent;
273 }
274 EXTI->RTSR = temp;
275
276 temp = EXTI->FTSR;
277 temp &= ~((uint32_t)iocurrent);
278 if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
279 {
280 temp |= iocurrent;
281 }
282 EXTI->FTSR = temp;
283 }
284 }
285 }
286 }
287
288 /**
289 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
290 * @param GPIOx where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
291 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
292 * @param GPIO_Pin specifies the port bit to be written.
293 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
294 * @retval None
295 */
HAL_GPIO_DeInit(GPIO_TypeDef * GPIOx,uint32_t GPIO_Pin)296 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
297 {
298 uint32_t position;
299 uint32_t ioposition = 0x00U;
300 uint32_t iocurrent = 0x00U;
301 uint32_t tmp = 0x00U;
302
303 /* Check the parameters */
304 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
305
306 /* Configure the port pins */
307 for(position = 0U; position < GPIO_NUMBER; position++)
308 {
309 /* Get the IO position */
310 ioposition = 0x01U << position;
311 /* Get the current IO position */
312 iocurrent = (GPIO_Pin) & ioposition;
313
314 if(iocurrent == ioposition)
315 {
316 /*------------------------- EXTI Mode Configuration --------------------*/
317 tmp = SYSCFG->EXTICR[position >> 2U];
318 tmp &= (0x0FU << (4U * (position & 0x03U)));
319 if(tmp == ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4U * (position & 0x03U))))
320 {
321 /* Configure the External Interrupt or event for the current IO */
322 tmp = 0x0FU << (4U * (position & 0x03U));
323 SYSCFG->EXTICR[position >> 2U] &= ~tmp;
324
325 /* Clear EXTI line configuration */
326 EXTI->IMR &= ~((uint32_t)iocurrent);
327 EXTI->EMR &= ~((uint32_t)iocurrent);
328
329 /* Clear Rising Falling edge configuration */
330 EXTI->RTSR &= ~((uint32_t)iocurrent);
331 EXTI->FTSR &= ~((uint32_t)iocurrent);
332 }
333
334 /*------------------------- GPIO Mode Configuration --------------------*/
335 /* Configure IO Direction in Input Floating Mode */
336 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2U));
337
338 /* Configure the default Alternate Function in current IO */
339 GPIOx->AFR[position >> 3U] &= ~(0xFU << ((uint32_t)(position & 0x07U) * 4U)) ;
340
341 /* Configure the default value for IO Speed */
342 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
343
344 /* Configure the default value IO Output Type */
345 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ;
346
347 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
348 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
349 }
350 }
351 }
352
353 /**
354 * @}
355 */
356
357 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
358 * @brief GPIO Read and Write
359 *
360 @verbatim
361 ===============================================================================
362 ##### IO operation functions #####
363 ===============================================================================
364
365 @endverbatim
366 * @{
367 */
368
369 /**
370 * @brief Reads the specified input port pin.
371 * @param GPIOx where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
372 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
373 * @param GPIO_Pin specifies the port bit to read.
374 * This parameter can be GPIO_PIN_x where x can be (0..15).
375 * @retval The input port pin value.
376 */
HAL_GPIO_ReadPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)377 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
378 {
379 GPIO_PinState bitstatus;
380
381 /* Check the parameters */
382 assert_param(IS_GPIO_PIN(GPIO_Pin));
383
384 if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
385 {
386 bitstatus = GPIO_PIN_SET;
387 }
388 else
389 {
390 bitstatus = GPIO_PIN_RESET;
391 }
392 return bitstatus;
393 }
394
395 /**
396 * @brief Sets or clears the selected data port bit.
397 *
398 * @note This function uses GPIOx_BSRR register to allow atomic read/modify
399 * accesses. In this way, there is no risk of an IRQ occurring between
400 * the read and the modify access.
401 *
402 * @param GPIOx where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
403 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
404 * @param GPIO_Pin specifies the port bit to be written.
405 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
406 * @param PinState specifies the value to be written to the selected bit.
407 * This parameter can be one of the GPIO_PinState enum values:
408 * @arg GPIO_PIN_RESET: to clear the port pin
409 * @arg GPIO_PIN_SET: to set the port pin
410 * @retval None
411 */
HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,GPIO_PinState PinState)412 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
413 {
414 /* Check the parameters */
415 assert_param(IS_GPIO_PIN(GPIO_Pin));
416 assert_param(IS_GPIO_PIN_ACTION(PinState));
417
418 if(PinState != GPIO_PIN_RESET)
419 {
420 GPIOx->BSRR = GPIO_Pin;
421 }
422 else
423 {
424 GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
425 }
426 }
427
428 /**
429 * @brief Toggles the specified GPIO pins.
430 * @param GPIOx Where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
431 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
432 * @param GPIO_Pin Specifies the pins to be toggled.
433 * @retval None
434 */
HAL_GPIO_TogglePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)435 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
436 {
437 /* Check the parameters */
438 assert_param(IS_GPIO_PIN(GPIO_Pin));
439
440 if ((GPIOx->ODR & GPIO_Pin) == GPIO_Pin)
441 {
442 GPIOx->BSRR = (uint32_t)GPIO_Pin << GPIO_NUMBER;
443 }
444 else
445 {
446 GPIOx->BSRR = GPIO_Pin;
447 }
448 }
449
450 /**
451 * @brief Locks GPIO Pins configuration registers.
452 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
453 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
454 * @note The configuration of the locked GPIO pins can no longer be modified
455 * until the next reset.
456 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F4 family
457 * @param GPIO_Pin specifies the port bit to be locked.
458 * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
459 * @retval None
460 */
HAL_GPIO_LockPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)461 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
462 {
463 __IO uint32_t tmp = GPIO_LCKR_LCKK;
464
465 /* Check the parameters */
466 assert_param(IS_GPIO_PIN(GPIO_Pin));
467
468 /* Apply lock key write sequence */
469 tmp |= GPIO_Pin;
470 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
471 GPIOx->LCKR = tmp;
472 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
473 GPIOx->LCKR = GPIO_Pin;
474 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
475 GPIOx->LCKR = tmp;
476 /* Read LCKK bit*/
477 tmp = GPIOx->LCKR;
478
479 if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
480 {
481 return HAL_OK;
482 }
483 else
484 {
485 return HAL_ERROR;
486 }
487 }
488
489 /**
490 * @brief This function handles EXTI interrupt request.
491 * @param GPIO_Pin Specifies the pins connected EXTI line
492 * @retval None
493 */
HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)494 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
495 {
496 /* EXTI line interrupt detected */
497 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
498 {
499 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
500 HAL_GPIO_EXTI_Callback(GPIO_Pin);
501 }
502 }
503
504 /**
505 * @brief EXTI line detection callbacks.
506 * @param GPIO_Pin Specifies the pins connected EXTI line
507 * @retval None
508 */
HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)509 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
510 {
511 /* Prevent unused argument(s) compilation warning */
512 UNUSED(GPIO_Pin);
513 /* NOTE: This function Should not be modified, when the callback is needed,
514 the HAL_GPIO_EXTI_Callback could be implemented in the user file
515 */
516 }
517
518 /**
519 * @}
520 */
521
522
523 /**
524 * @}
525 */
526
527 #endif /* HAL_GPIO_MODULE_ENABLED */
528 /**
529 * @}
530 */
531
532 /**
533 * @}
534 */
535
536 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
537