source: S-port/trunk/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 19.0 KB
Line 
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>&copy; 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
127#define GPIO_NUMBER 16U
128/**
129 * @}
130 */
131/* Private macro -------------------------------------------------------------*/
132/* Private variables ---------------------------------------------------------*/
133/* Private function prototypes -----------------------------------------------*/
134/* Private functions ---------------------------------------------------------*/
135/* Exported functions --------------------------------------------------------*/
136/** @defgroup GPIO_Exported_Functions GPIO Exported Functions
137 * @{
138 */
139
140/** @defgroup GPIO_Exported_Functions_Group1 Initialization and de-initialization functions
141 * @brief Initialization and Configuration functions
142 *
143@verbatim
144 ===============================================================================
145 ##### Initialization and de-initialization functions #####
146 ===============================================================================
147 [..]
148 This section provides functions allowing to initialize and de-initialize the GPIOs
149 to be ready for use.
150
151@endverbatim
152 * @{
153 */
154
155
156/**
157 * @brief Initializes the GPIOx peripheral according to the specified parameters in the GPIO_Init.
158 * @param GPIOx where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
159 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
160 * @param GPIO_Init pointer to a GPIO_InitTypeDef structure that contains
161 * the configuration information for the specified GPIO peripheral.
162 * @retval None
163 */
164void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
165{
166 uint32_t position;
167 uint32_t ioposition = 0x00U;
168 uint32_t iocurrent = 0x00U;
169 uint32_t temp = 0x00U;
170
171 /* Check the parameters */
172 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
173 assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
174 assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
175 assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
176
177 /* Configure the port pins */
178 for(position = 0U; position < GPIO_NUMBER; position++)
179 {
180 /* Get the IO position */
181 ioposition = 0x01U << position;
182 /* Get the current IO position */
183 iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
184
185 if(iocurrent == ioposition)
186 {
187 /*--------------------- GPIO Mode Configuration ------------------------*/
188 /* In case of Output or Alternate function mode selection */
189 if(((GPIO_Init->Mode & GPIO_MODE) == MODE_OUTPUT) || \
190 (GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
191 {
192 /* Check the Speed parameter */
193 assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
194 /* Configure the IO Speed */
195 temp = GPIOx->OSPEEDR;
196 temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
197 temp |= (GPIO_Init->Speed << (position * 2U));
198 GPIOx->OSPEEDR = temp;
199
200 /* Configure the IO Output Type */
201 temp = GPIOx->OTYPER;
202 temp &= ~(GPIO_OTYPER_OT_0 << position) ;
203 temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
204 GPIOx->OTYPER = temp;
205 }
206
207 if((GPIO_Init->Mode & GPIO_MODE) != MODE_ANALOG)
208 {
209 /* Activate the Pull-up or Pull down resistor for the current IO */
210 temp = GPIOx->PUPDR;
211 temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
212 temp |= ((GPIO_Init->Pull) << (position * 2U));
213 GPIOx->PUPDR = temp;
214 }
215
216 /* In case of Alternate function mode selection */
217 if((GPIO_Init->Mode & GPIO_MODE) == MODE_AF)
218 {
219 /* Check the Alternate function parameter */
220 assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
221 /* Configure Alternate function mapped with the current IO */
222 temp = GPIOx->AFR[position >> 3U];
223 temp &= ~(0xFU << ((uint32_t)(position & 0x07U) * 4U)) ;
224 temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & 0x07U) * 4U));
225 GPIOx->AFR[position >> 3U] = temp;
226 }
227
228 /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
229 temp = GPIOx->MODER;
230 temp &= ~(GPIO_MODER_MODER0 << (position * 2U));
231 temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U));
232 GPIOx->MODER = temp;
233
234 /*--------------------- EXTI Mode Configuration ------------------------*/
235 /* Configure the External Interrupt or event for the current IO */
236 if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
237 {
238 /* Enable SYSCFG Clock */
239 __HAL_RCC_SYSCFG_CLK_ENABLE();
240
241 temp = SYSCFG->EXTICR[position >> 2U];
242 temp &= ~(0x0FU << (4U * (position & 0x03U)));
243 temp |= ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4U * (position & 0x03U)));
244 SYSCFG->EXTICR[position >> 2U] = temp;
245
246 /* Clear EXTI line configuration */
247 temp = EXTI->IMR;
248 temp &= ~((uint32_t)iocurrent);
249 if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
250 {
251 temp |= iocurrent;
252 }
253 EXTI->IMR = temp;
254
255 temp = EXTI->EMR;
256 temp &= ~((uint32_t)iocurrent);
257 if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
258 {
259 temp |= iocurrent;
260 }
261 EXTI->EMR = temp;
262
263 /* Clear Rising Falling edge configuration */
264 temp = EXTI->RTSR;
265 temp &= ~((uint32_t)iocurrent);
266 if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
267 {
268 temp |= iocurrent;
269 }
270 EXTI->RTSR = temp;
271
272 temp = EXTI->FTSR;
273 temp &= ~((uint32_t)iocurrent);
274 if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
275 {
276 temp |= iocurrent;
277 }
278 EXTI->FTSR = temp;
279 }
280 }
281 }
282}
283
284/**
285 * @brief De-initializes the GPIOx peripheral registers to their default reset values.
286 * @param GPIOx where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
287 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
288 * @param GPIO_Pin specifies the port bit to be written.
289 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
290 * @retval None
291 */
292void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
293{
294 uint32_t position;
295 uint32_t ioposition = 0x00U;
296 uint32_t iocurrent = 0x00U;
297 uint32_t tmp = 0x00U;
298
299 /* Check the parameters */
300 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
301
302 /* Configure the port pins */
303 for(position = 0U; position < GPIO_NUMBER; position++)
304 {
305 /* Get the IO position */
306 ioposition = 0x01U << position;
307 /* Get the current IO position */
308 iocurrent = (GPIO_Pin) & ioposition;
309
310 if(iocurrent == ioposition)
311 {
312 /*------------------------- EXTI Mode Configuration --------------------*/
313 tmp = SYSCFG->EXTICR[position >> 2U];
314 tmp &= (0x0FU << (4U * (position & 0x03U)));
315 if(tmp == ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4U * (position & 0x03U))))
316 {
317 /* Clear EXTI line configuration */
318 EXTI->IMR &= ~((uint32_t)iocurrent);
319 EXTI->EMR &= ~((uint32_t)iocurrent);
320
321 /* Clear Rising Falling edge configuration */
322 EXTI->RTSR &= ~((uint32_t)iocurrent);
323 EXTI->FTSR &= ~((uint32_t)iocurrent);
324
325 /* Configure the External Interrupt or event for the current IO */
326 tmp = 0x0FU << (4U * (position & 0x03U));
327 SYSCFG->EXTICR[position >> 2U] &= ~tmp;
328 }
329
330 /*------------------------- GPIO Mode Configuration --------------------*/
331 /* Configure IO Direction in Input Floating Mode */
332 GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2U));
333
334 /* Configure the default Alternate Function in current IO */
335 GPIOx->AFR[position >> 3U] &= ~(0xFU << ((uint32_t)(position & 0x07U) * 4U)) ;
336
337 /* Deactivate the Pull-up and Pull-down resistor for the current IO */
338 GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
339
340 /* Configure the default value IO Output Type */
341 GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ;
342
343 /* Configure the default value for IO Speed */
344 GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
345 }
346 }
347}
348
349/**
350 * @}
351 */
352
353/** @defgroup GPIO_Exported_Functions_Group2 IO operation functions
354 * @brief GPIO Read and Write
355 *
356@verbatim
357 ===============================================================================
358 ##### IO operation functions #####
359 ===============================================================================
360
361@endverbatim
362 * @{
363 */
364
365/**
366 * @brief Reads the specified input port pin.
367 * @param GPIOx where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
368 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
369 * @param GPIO_Pin specifies the port bit to read.
370 * This parameter can be GPIO_PIN_x where x can be (0..15).
371 * @retval The input port pin value.
372 */
373GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
374{
375 GPIO_PinState bitstatus;
376
377 /* Check the parameters */
378 assert_param(IS_GPIO_PIN(GPIO_Pin));
379
380 if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
381 {
382 bitstatus = GPIO_PIN_SET;
383 }
384 else
385 {
386 bitstatus = GPIO_PIN_RESET;
387 }
388 return bitstatus;
389}
390
391/**
392 * @brief Sets or clears the selected data port bit.
393 *
394 * @note This function uses GPIOx_BSRR register to allow atomic read/modify
395 * accesses. In this way, there is no risk of an IRQ occurring between
396 * the read and the modify access.
397 *
398 * @param GPIOx where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
399 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
400 * @param GPIO_Pin specifies the port bit to be written.
401 * This parameter can be one of GPIO_PIN_x where x can be (0..15).
402 * @param PinState specifies the value to be written to the selected bit.
403 * This parameter can be one of the GPIO_PinState enum values:
404 * @arg GPIO_PIN_RESET: to clear the port pin
405 * @arg GPIO_PIN_SET: to set the port pin
406 * @retval None
407 */
408void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
409{
410 /* Check the parameters */
411 assert_param(IS_GPIO_PIN(GPIO_Pin));
412 assert_param(IS_GPIO_PIN_ACTION(PinState));
413
414 if(PinState != GPIO_PIN_RESET)
415 {
416 GPIOx->BSRR = GPIO_Pin;
417 }
418 else
419 {
420 GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
421 }
422}
423
424/**
425 * @brief Toggles the specified GPIO pins.
426 * @param GPIOx Where x can be (A..K) to select the GPIO peripheral for STM32F429X device or
427 * x can be (A..I) to select the GPIO peripheral for STM32F40XX and STM32F427X devices.
428 * @param GPIO_Pin Specifies the pins to be toggled.
429 * @retval None
430 */
431void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
432{
433 uint32_t odr;
434
435 /* Check the parameters */
436 assert_param(IS_GPIO_PIN(GPIO_Pin));
437
438 /* get current Ouput Data Register value */
439 odr = GPIOx->ODR;
440
441 /* Set selected pins that were at low level, and reset ones that were high */
442 GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
443}
444
445/**
446 * @brief Locks GPIO Pins configuration registers.
447 * @note The locked registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR,
448 * GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.
449 * @note The configuration of the locked GPIO pins can no longer be modified
450 * until the next reset.
451 * @param GPIOx where x can be (A..F) to select the GPIO peripheral for STM32F4 family
452 * @param GPIO_Pin specifies the port bit to be locked.
453 * This parameter can be any combination of GPIO_PIN_x where x can be (0..15).
454 * @retval None
455 */
456HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
457{
458 __IO uint32_t tmp = GPIO_LCKR_LCKK;
459
460 /* Check the parameters */
461 assert_param(IS_GPIO_PIN(GPIO_Pin));
462
463 /* Apply lock key write sequence */
464 tmp |= GPIO_Pin;
465 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
466 GPIOx->LCKR = tmp;
467 /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
468 GPIOx->LCKR = GPIO_Pin;
469 /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
470 GPIOx->LCKR = tmp;
471 /* Read LCKR register. This read is mandatory to complete key lock sequence */
472 tmp = GPIOx->LCKR;
473
474 /* Read again in order to confirm lock is active */
475 if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
476 {
477 return HAL_OK;
478 }
479 else
480 {
481 return HAL_ERROR;
482 }
483}
484
485/**
486 * @brief This function handles EXTI interrupt request.
487 * @param GPIO_Pin Specifies the pins connected EXTI line
488 * @retval None
489 */
490void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
491{
492 /* EXTI line interrupt detected */
493 if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
494 {
495 __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
496 HAL_GPIO_EXTI_Callback(GPIO_Pin);
497 }
498}
499
500/**
501 * @brief EXTI line detection callbacks.
502 * @param GPIO_Pin Specifies the pins connected EXTI line
503 * @retval None
504 */
505__weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
506{
507 /* Prevent unused argument(s) compilation warning */
508 UNUSED(GPIO_Pin);
509 /* NOTE: This function Should not be modified, when the callback is needed,
510 the HAL_GPIO_EXTI_Callback could be implemented in the user file
511 */
512}
513
514/**
515 * @}
516 */
517
518
519/**
520 * @}
521 */
522
523#endif /* HAL_GPIO_MODULE_ENABLED */
524/**
525 * @}
526 */
527
528/**
529 * @}
530 */
531
532/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.