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