1 /**
2 ******************************************************************************
3 * @file stm32f2xx_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 ******************************************************************************
12 * @attention
13 *
14 * Copyright (c) 2017 STMicroelectronics.
15 * All rights reserved.
16 *
17 * This software is licensed under terms that can be found in the LICENSE file
18 * in the root directory of this software component.
19 * If no LICENSE file comes with this software, it is provided AS-IS.
20 *
21 ******************************************************************************
22 @verbatim
23 ==============================================================================
24 ##### GPIO Peripheral features #####
25 ==============================================================================
26 [..]
27 Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each
28 port bit of the General Purpose IO (GPIO) Ports, can be individually configured by software
29 in several modes:
30 (+) Input mode
31 (+) Analog mode
32 (+) Output mode
33 (+) Alternate function mode
34 (+) External interrupt/event lines
35
36 [..]
37 During and just after reset, the alternate functions and external interrupt
38 lines are not active and the I/O ports are configured in input floating mode.
39
40 [..]
41 All GPIO pins have weak internal pull-up and pull-down resistors, which can be
42 activated or not.
43
44 [..]
45 In Output or Alternate mode, each IO can be configured on open-drain or push-pull
46 type and the IO speed can be selected depending on the VDD value.
47
48 [..]
49 All ports have external interrupt/event capability. To use external interrupt
50 lines, the port must be configured in input mode. All available GPIO pins are
51 connected to the 16 external interrupt/event lines from EXTI0 to EXTI15.
52
53 [..]
54 The external interrupt/event controller consists of up to 23 edge detectors
55 (16 lines are connected to GPIO) for generating event/interrupt requests (each
56 input line can be independently configured to select the type (interrupt or event)
57 and the corresponding trigger event (rising or falling or both). Each line can
58 also be masked independently.
59
60 ##### How to use this driver #####
61 ==============================================================================
62 [..]
63 (#) Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
64
65 (#) Configure the GPIO pin(s) using HAL_GPIO_Init().
66 (++) Configure the IO mode using "Mode" member from GPIO_InitTypeDef structure
67 (++) Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef
68 structure.
69 (++) In case of Output or alternate function mode selection: the speed is
70 configured through "Speed" member from GPIO_InitTypeDef structure.
71 (++) In alternate mode is selection, the alternate function connected to the IO
72 is configured through "Alternate" member from GPIO_InitTypeDef structure.
73 (++) Analog mode is required when a pin is to be used as ADC channel
74 or DAC output.
75 (++) In case of external interrupt/event selection the "Mode" member from
76 GPIO_InitTypeDef structure select the type (interrupt or event) and
77 the corresponding trigger event (rising or falling or both).
78
79 (#) In case of external interrupt/event mode selection, configure NVIC IRQ priority
80 mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using
81 HAL_NVIC_EnableIRQ().
82
83 (#) To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
84
85 (#) To set/reset the level of a pin configured in output mode use
86 HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
87
88 (#) To lock pin configuration until next reset use HAL_GPIO_LockPin().
89
90
91 (#) During and just after reset, the alternate functions are not
92 active and the GPIO pins are configured in input floating mode (except JTAG
93 pins).
94
95 (#) The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose
96 (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has
97 priority over the GPIO function.
98
99 (#) The HSE oscillator pins OSC_IN/OSC_OUT can be used as
100 general purpose PH0 and PH1, respectively, when the HSE oscillator is off.
101 The HSE has priority over the GPIO function.
102
103 @endverbatim
104 ******************************************************************************
105 */
106
107 /* Includes ------------------------------------------------------------------*/
108 #include "stm32f2xx_hal.h"
109
110 /** @addtogroup STM32F2xx_HAL_Driver
111 * @{
112 */
113
114 /** @defgroup GPIO GPIO
115 * @brief GPIO HAL module driver
116 * @{
117 */
118
119 /** MISRA C:2012 deviation rule has been granted for following rules:
120 * Rule-18.1_d - Medium: Array pointer `GPIOx' is accessed with index [..,..]
121 * which may be out of array bounds [..,UNKNOWN] in following APIs:
122 * HAL_GPIO_Init
123 * HAL_GPIO_DeInit
124 */
125
126 #ifdef HAL_GPIO_MODULE_ENABLED
127
128 /* Private typedef -----------------------------------------------------------*/
129 /* Private define ------------------------------------------------------------*/
130 /** @addtogroup GPIO_Private_Constants GPIO Private Constants
131 * @{
132 */
133 #define GPIO_NUMBER 16U
134 /**
135 * @}
136 */
137 /* Private macro -------------------------------------------------------------*/
138 /* Private variables ---------------------------------------------------------*/
139 /* Private function prototypes -----------------------------------------------*/
140 /* Private functions ---------------------------------------------------------*/
141 /* Exported functions --------------------------------------------------------*/
142 /** @defgroup GPIO_Exported_Functions GPIO Exported Functions
143 * @{
144 */
145
146 /** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
147 * @brief Initialization and Configuration functions
148 *
149 @verbatim
150 ===============================================================================
151 ##### Initialization and de-initialization functions #####
152 ===============================================================================
153 [..]
154 This section provides functions allowing to initialize and de-initialize the GPIOs
155 to be ready for use.
156
157 @endverbatim
158 * @{
159 */
160
161
162 /**
163 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
164 * @param GPIOx where x can be (A..I) to select the GPIO peripheral.
165 * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
166 * the configuration information for the specified GPIO peripheral.
167 * @retval None
168 */
HAL_GPIO_Init(GPIO_TypeDef * GPIOx,GPIO_InitTypeDef * GPIO_Init)169 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
170 {
171 uint32_t position = 0x00u;
172 uint32_t iocurrent;
173 uint32_t temp;
174
175 /* Check the parameters */
176 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
177 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
178 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
179
180 /* Configure the port pins */
181 while (((GPIO_Init->Pin) >> position) != 0x00u)
182 {
183 /* Get current io position */
184 iocurrent = (GPIO_Init->Pin) & (1uL << position);
185
186 if (iocurrent != 0x00u)
187 {
188 /* In case of Output or Alternate function mode selection */
189 if(((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) || ((GPIO_Init->Mode & GPIO_MODE) == MODE_AF))
190 {
191 /* Check the Speed parameter */
192 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
193 /* Configure the IO Speed */
194 temp = GPIOx->OSPEEDR;
195 temp &= ~(GPIO_OSPEEDR_OSPEED0 << (position * 2u));
196 temp |= (GPIO_Init->Speed << (position * 2u));
197 GPIOx->OSPEEDR = temp;
198
199 /* Configure the IO Output Type */
200 temp = GPIOx->OTYPER;
201 temp &= ~(GPIO_OTYPER_OT0 << position) ;
202 temp |= (((GPIO_Init->Mode & OUTPUT_TYPE) >> OUTPUT_TYPE_Pos) << position);
203 GPIOx->OTYPER = temp;
204 }
205 if((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG)
206 {
207 /* Check the Pull parameter */
208 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
209
210 /* Activate the Pull-up or Pull down resistor for the current IO */
211 temp = GPIOx->PUPDR;
212 temp &= ~(GPIO_PUPDR_PUPD0 << (position * 2u));
213 temp |= ((GPIO_Init->Pull) << (position * 2u));
214 GPIOx->PUPDR = temp;
215 }
216
217 /*--------------------- GPIO Mode Configuration ------------------------*/
218 /* In case of Alternate function mode selection */
219 if((GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
220 {
221 /* Check the Alternate function parameters */
222 assert_param(IS_GPIO_AF_INSTANCE(GPIOx));
223 assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
224
225 /* Configure Alternate function mapped with the current IO */
226 temp = GPIOx->AFR[position >> 3u];
227 temp &= ~(0xFu << ((position & 0x07u) * 4u));
228 temp |= ((GPIO_Init->Alternate) << ((position & 0x07u) * 4u));
229 GPIOx->AFR[position >> 3u] = temp;
230 }
231
232 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
233 temp = GPIOx->MODER;
234 temp &= ~(GPIO_MODER_MODE0 << (position * 2u));
235 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2u));
236 GPIOx->MODER = temp;
237
238 /*--------------------- EXTI Mode Configuration ------------------------*/
239 /* Configure the External Interrupt or event for the current IO */
240 if((GPIO_Init->Mode & EXTI_MODE) != 0x00u)
241 {
242 /* Enable SYSCFG Clock */
243 __HAL_RCC_SYSCFG_CLK_ENABLE();
244
245 temp = SYSCFG->EXTICR[position >> 2u];
246 temp &= ~(0x0FuL << (4u * (position & 0x03u)));
247 temp |= (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u)));
248 SYSCFG->EXTICR[position >> 2u] = temp;
249
250 /* Clear Rising Falling edge configuration */
251 temp = EXTI->RTSR;
252 temp &= ~(iocurrent);
253 if((GPIO_Init->Mode & TRIGGER_RISING) != 0x00u)
254 {
255 temp |= iocurrent;
256 }
257 EXTI->RTSR = temp;
258
259 temp = EXTI->FTSR;
260 temp &= ~(iocurrent);
261 if((GPIO_Init->Mode & TRIGGER_FALLING) != 0x00u)
262 {
263 temp |= iocurrent;
264 }
265 EXTI->FTSR = temp;
266
267 temp = EXTI->EMR;
268 temp &= ~(iocurrent);
269 if((GPIO_Init->Mode & EXTI_EVT) != 0x00u)
270 {
271 temp |= iocurrent;
272 }
273 EXTI->EMR = temp;
274
275 /* Clear EXTI line configuration */
276 temp = EXTI->IMR;
277 temp &= ~(iocurrent);
278 if((GPIO_Init->Mode & EXTI_IT) != 0x00u)
279 {
280 temp |= iocurrent;
281 }
282 EXTI->IMR = temp;
283 }
284 }
285
286 position++;
287 }
288 }
289
290 /**
291 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
292 * @param GPIOx where x can be (A..I) to select the GPIO peripheral.
293 * @param GPIO_Pin specifies the port bit to be written.
294 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
295 * @retval None
296 */
HAL_GPIO_DeInit(GPIO_TypeDef * GPIOx,uint32_t GPIO_Pin)297 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
298 {
299 uint32_t position = 0x00u;
300 uint32_t iocurrent;
301 uint32_t tmp;
302
303 /* Check the parameters */
304 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
305 assert_param(IS_GPIO_PIN(GPIO_Pin));
306
307 /* Configure the port pins */
308 while ((GPIO_Pin >> position) != 0x00u)
309 {
310 /* Get current io position */
311 iocurrent = (GPIO_Pin) & (1uL << position);
312
313 if (iocurrent != 0x00u)
314 {
315 /*------------------------- EXTI Mode Configuration --------------------*/
316 /* Clear the External Interrupt or Event for the current IO */
317
318 tmp = SYSCFG->EXTICR[position >> 2u];
319 tmp &= (0x0FuL << (4u * (position & 0x03u)));
320 if (tmp == (GPIO_GET_INDEX(GPIOx) << (4u * (position & 0x03u))))
321 {
322 /* Clear EXTI line configuration */
323 EXTI->IMR &= ~((uint32_t)iocurrent);
324 EXTI->EMR &= ~((uint32_t)iocurrent);
325
326 /* Clear Rising Falling edge configuration */
327 EXTI->FTSR &= ~((uint32_t)iocurrent);
328 EXTI->RTSR &= ~((uint32_t)iocurrent);
329
330 /* Configure the External Interrupt or event for the current IO */
331 tmp = 0x0FuL << (4u * (position & 0x03u));
332 SYSCFG->EXTICR[position >> 2u] &= ~tmp;
333 }
334
335 /*------------------------- GPIO Mode Configuration --------------------*/
336 /* Configure IO Direction in Input Floating Mode */
337 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2u));
338
339 /* Configure the default Alternate Function in current IO */
340 GPIOx->AFR[position >> 3u] &= ~(0xFu << ((uint32_t)(position & 0x07u) * 4u));
341
342 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
343 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
344
345 /* Configure the default value IO Output Type */
346 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position);
347
348 /* Configure the default value for IO Speed */
349 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2u));
350 }
351
352 position++;
353 }
354 }
355
356 /**
357 * @}
358 */
359
360 /** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
361 * @brief GPIO Read and Write
362 *
363 @verbatim
364 ===============================================================================
365 ##### IO operation functions #####
366 ===============================================================================
367
368 @endverbatim
369 * @{
370 */
371
372 /**
373 * @brief Reads the specified input port pin.
374 * @param GPIOx where x can be (A..I) to select the GPIO peripheral.
375 * @param GPIO_Pin specifies the port bit to read.
376 * This parameter can be GPIO_PIN_x where x can be (0..15).
377 * @retval The input port pin value.
378 */
HAL_GPIO_ReadPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)379 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
380 {
381 GPIO_PinState bitstatus;
382
383 /* Check the parameters */
384 assert_param(IS_GPIO_PIN(GPIO_Pin));
385
386 if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
387 {
388 bitstatus = GPIO_PIN_SET;
389 }
390 else
391 {
392 bitstatus = GPIO_PIN_RESET;
393 }
394 return bitstatus;
395 }
396
397 /**
398 * @brief Sets or clears the selected data port bit.
399 *
400 * @note This function uses GPIOx_BSRR register to allow atomic read/modify
401 * accesses. In this way, there is no risk of an IRQ occurring between
402 * the read and the modify access.
403 *
404 * @param GPIOx where x can be (A..I) to select the GPIO peripheral for all STM32F2XX devices
405 * @param GPIO_Pin specifies the port bit to be written.
406 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
407 * @param PinState specifies the value to be written to the selected bit.
408 * This parameter can be one of the GPIO_PinState enum values:
409 * @arg GPIO_PIN_RESET: to clear the port pin
410 * @arg GPIO_PIN_SET: to set the port pin
411 * @retval None
412 */
HAL_GPIO_WritePin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin,GPIO_PinState PinState)413 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
414 {
415 /* Check the parameters */
416 assert_param(IS_GPIO_PIN(GPIO_Pin));
417 assert_param(IS_GPIO_PIN_ACTION(PinState));
418
419 if(PinState != GPIO_PIN_RESET)
420 {
421 GPIOx->BSRR = GPIO_Pin;
422 }
423 else
424 {
425 GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
426 }
427 }
428
429 /**
430 * @brief Toggles the specified GPIO pins.
431 * @param GPIOx where x can be (A..I) to select the GPIO peripheral.
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 uint32_t odr;
438
439 /* Check the parameters */
440 assert_param(IS_GPIO_PIN(GPIO_Pin));
441
442 /* get current Output Data Register value */
443 odr = GPIOx->ODR;
444
445 /* Set selected pins that were at low level, and reset ones that were high */
446 GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
447 }
448
449 /**
450 * @brief Locks GPIO Pins configuration registers.
451 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
452 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
453 * @note The configuration of the locked GPIO pins can no longer be modified
454 * until the next reset.
455 * @param GPIOx where x can be (A..I) to select the GPIO peripheral for STM32F2XX family
456 * @param GPIO_Pin specifies the port bit to be locked.
457 * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
458 * @retval None
459 */
HAL_GPIO_LockPin(GPIO_TypeDef * GPIOx,uint16_t GPIO_Pin)460 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
461 {
462 __IO uint32_t tmp = GPIO_LCKR_LCKK;
463
464 /* Check the parameters */
465 assert_param(IS_GPIO_PIN(GPIO_Pin));
466
467 /* Apply lock key write sequence */
468 tmp |= GPIO_Pin;
469 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
470 GPIOx->LCKR = tmp;
471 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
472 GPIOx->LCKR = GPIO_Pin;
473 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
474 GPIOx->LCKR = tmp;
475 /* Read LCKK register. This read is mandatory to complete key lock sequence */
476 tmp = GPIOx->LCKR;
477
478 /* read again in order to confirm lock is active */
479 if((GPIOx->LCKR & GPIO_LCKR_LCKK) != 0x00u)
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) != 0x00u)
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 #endif /* HAL_GPIO_MODULE_ENABLED */
527 /**
528 * @}
529 */
530
531 /**
532 * @}
533 */
534
535