stm32f7xx_hal_gpio.c
Go to the documentation of this file.
1 
107 /* Includes ------------------------------------------------------------------*/
108 #include "stm32f7xx_hal.h"
109 
119 #ifdef HAL_GPIO_MODULE_ENABLED
120 
121 /* Private typedef -----------------------------------------------------------*/
122 /* Private define ------------------------------------------------------------*/
126 #define GPIO_MODE ((uint32_t)0x00000003U)
127 #define EXTI_MODE ((uint32_t)0x10000000U)
128 #define GPIO_MODE_IT ((uint32_t)0x00010000U)
129 #define GPIO_MODE_EVT ((uint32_t)0x00020000U)
130 #define RISING_EDGE ((uint32_t)0x00100000U)
131 #define FALLING_EDGE ((uint32_t)0x00200000U)
132 #define GPIO_OUTPUT_TYPE ((uint32_t)0x00000010U)
133 
134 #define GPIO_NUMBER ((uint32_t)16U)
135 
138 /* Private macro -------------------------------------------------------------*/
139 /* Private variables ---------------------------------------------------------*/
140 /* Private function prototypes -----------------------------------------------*/
141 /* Private functions ---------------------------------------------------------*/
142 /* Exported functions --------------------------------------------------------*/
169 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
170 {
171  uint32_t position = 0x00;
172  uint32_t ioposition = 0x00;
173  uint32_t iocurrent = 0x00;
174  uint32_t temp = 0x00;
175 
176  /* Check the parameters */
178  assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
179  assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
180  assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
181 
182  /* Configure the port pins */
183  for(position = 0; position < GPIO_NUMBER; position++)
184  {
185  /* Get the IO position */
186  ioposition = ((uint32_t)0x01) << position;
187  /* Get the current IO position */
188  iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
189 
190  if(iocurrent == ioposition)
191  {
192  /*--------------------- GPIO Mode Configuration ------------------------*/
193  /* In case of Output or Alternate function mode selection */
194  if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
195  (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
196  {
197  /* Check the Speed parameter */
198  assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
199  /* Configure the IO Speed */
200  temp = GPIOx->OSPEEDR;
201  temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
202  temp |= (GPIO_Init->Speed << (position * 2));
203  GPIOx->OSPEEDR = temp;
204 
205  /* Configure the IO Output Type */
206  temp = GPIOx->OTYPER;
207  temp &= ~(GPIO_OTYPER_OT_0 << position) ;
208  temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4) << position);
209  GPIOx->OTYPER = temp;
210  }
211 
212  /* Activate the Pull-up or Pull down resistor for the current IO */
213  temp = GPIOx->PUPDR;
214  temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
215  temp |= ((GPIO_Init->Pull) << (position * 2));
216  GPIOx->PUPDR = temp;
217 
218  /* In case of Alternate function mode selection */
219  if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
220  {
221  /* Check the Alternate function parameter */
222  assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
223 
224  /* Configure Alternate function mapped with the current IO */
225  temp = GPIOx->AFR[position >> 3];
226  temp &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
227  temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07) * 4));
228  GPIOx->AFR[position >> 3] = temp;
229  }
230 
231  /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
232  temp = GPIOx->MODER;
233  temp &= ~(GPIO_MODER_MODER0 << (position * 2));
234  temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2));
235  GPIOx->MODER = temp;
236 
237  /*--------------------- EXTI Mode Configuration ------------------------*/
238  /* Configure the External Interrupt or event for the current IO */
239  if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
240  {
241  /* Enable SYSCFG Clock */
243 
244  temp = SYSCFG->EXTICR[position >> 2];
245  temp &= ~(((uint32_t)0x0F) << (4 * (position & 0x03)));
246  temp |= ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03)));
247  SYSCFG->EXTICR[position >> 2] = temp;
248 
249  /* Clear EXTI line configuration */
250  temp = EXTI->IMR;
251  temp &= ~((uint32_t)iocurrent);
252  if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
253  {
254  temp |= iocurrent;
255  }
256  EXTI->IMR = temp;
257 
258  temp = EXTI->EMR;
259  temp &= ~((uint32_t)iocurrent);
260  if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
261  {
262  temp |= iocurrent;
263  }
264  EXTI->EMR = temp;
265 
266  /* Clear Rising Falling edge configuration */
267  temp = EXTI->RTSR;
268  temp &= ~((uint32_t)iocurrent);
269  if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
270  {
271  temp |= iocurrent;
272  }
273  EXTI->RTSR = temp;
274 
275  temp = EXTI->FTSR;
276  temp &= ~((uint32_t)iocurrent);
277  if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
278  {
279  temp |= iocurrent;
280  }
281  EXTI->FTSR = temp;
282  }
283  }
284  }
285 }
286 
294 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
295 {
296  uint32_t position;
297  uint32_t ioposition = 0x00;
298  uint32_t iocurrent = 0x00;
299  uint32_t tmp = 0x00;
300 
301  /* Check the parameters */
303 
304  /* Configure the port pins */
305  for(position = 0; position < GPIO_NUMBER; position++)
306  {
307  /* Get the IO position */
308  ioposition = ((uint32_t)0x01) << position;
309  /* Get the current IO position */
310  iocurrent = (GPIO_Pin) & ioposition;
311 
312  if(iocurrent == ioposition)
313  {
314  /*------------------------- EXTI Mode Configuration --------------------*/
315  tmp = SYSCFG->EXTICR[position >> 2];
316  tmp &= (((uint32_t)0x0F) << (4 * (position & 0x03)));
317  if(tmp == ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03))))
318  {
319  /* Clear EXTI line configuration */
320  EXTI->IMR &= ~((uint32_t)iocurrent);
321  EXTI->EMR &= ~((uint32_t)iocurrent);
322 
323  /* Clear Rising Falling edge configuration */
324  EXTI->RTSR &= ~((uint32_t)iocurrent);
325  EXTI->FTSR &= ~((uint32_t)iocurrent);
326 
327  /* Configure the External Interrupt or event for the current IO */
328  tmp = ((uint32_t)0x0F) << (4 * (position & 0x03));
329  SYSCFG->EXTICR[position >> 2] &= ~tmp;
330  }
331  /*------------------------- GPIO Mode Configuration --------------------*/
332  /* Configure IO Direction in Input Floating Mode */
333  GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2));
334 
335  /* Configure the default Alternate Function in current IO */
336  GPIOx->AFR[position >> 3] &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
337 
338  /* Deactivate the Pull-up and Pull-down resistor for the current IO */
339  GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
340 
341  /* Configure the default value IO Output Type */
342  GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ;
343 
344  /* Configure the default value for IO Speed */
345  GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
346  }
347  }
348 }
349 
373 GPIO_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 
407 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
408 {
409  /* Check the parameters */
410  assert_param(IS_GPIO_PIN(GPIO_Pin));
411  assert_param(IS_GPIO_PIN_ACTION(PinState));
412 
413  if(PinState != GPIO_PIN_RESET)
414  {
415  GPIOx->BSRR = GPIO_Pin;
416  }
417  else
418  {
419  GPIOx->BSRR = (uint32_t)GPIO_Pin << 16;
420  }
421 }
422 
429 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
430 {
431  /* Check the parameters */
432  assert_param(IS_GPIO_PIN(GPIO_Pin));
433 
434  if ((GPIOx->ODR & GPIO_Pin) != 0X00u)
435  {
436  GPIOx->BSRR = (uint32_t)GPIO_Pin << GPIO_NUMBER;
437  }
438  else
439  {
440  GPIOx->BSRR = (uint32_t)GPIO_Pin;
441  }
442 }
443 
455 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
456 {
457  __IO uint32_t tmp = GPIO_LCKR_LCKK;
458 
459  /* Check the parameters */
460  assert_param(IS_GPIO_PIN(GPIO_Pin));
461 
462  /* Apply lock key write sequence */
463  tmp |= GPIO_Pin;
464  /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
465  GPIOx->LCKR = tmp;
466  /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
467  GPIOx->LCKR = GPIO_Pin;
468  /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
469  GPIOx->LCKR = tmp;
470  /* Read LCKR register. This read is mandatory to complete key lock sequence */
471  tmp = GPIOx->LCKR;
472 
473  /* Read again in order to confirm lock is active */
474  if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
475  {
476  return HAL_OK;
477  }
478  else
479  {
480  return HAL_ERROR;
481  }
482 }
483 
489 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
490 {
491  /* EXTI line interrupt detected */
492  if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
493  {
494  __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
495  HAL_GPIO_EXTI_Callback(GPIO_Pin);
496  }
497 }
498 
504 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
505 {
506  /* Prevent unused argument(s) compilation warning */
507  UNUSED(GPIO_Pin);
508 
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 
523 #endif /* HAL_GPIO_MODULE_ENABLED */
524 
532 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
GPIO_TypeDef::LCKR
__IO uint32_t LCKR
Definition: stm32f407xx.h:536
assert_param
#define assert_param(expr)
Include module's header file.
Definition: stm32f407/stm32f407g-disc1/Inc/stm32f4xx_hal_conf.h:353
HAL_GPIO_EXTI_Callback
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
GPIO_OSPEEDER_OSPEEDR0
#define GPIO_OSPEEDER_OSPEEDR0
Definition: stm32f407xx.h:8347
__IO
#define __IO
Definition: imxrt1050/imxrt1050-evkb/CMSIS/core_cm7.h:237
GPIO_MODE_AF_PP
#define GPIO_MODE_AF_PP
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:122
HAL_StatusTypeDef
HAL_StatusTypeDef
HAL Status structures definition
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:40
GPIO_MODE_OUTPUT_OD
#define GPIO_MODE_OUTPUT_OD
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:121
GPIO_TypeDef::MODER
__IO uint32_t MODER
Definition: stm32f407xx.h:529
GPIO_InitTypeDef
GPIO Init structure definition
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:47
IS_GPIO_PULL
#define IS_GPIO_PULL(PULL)
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:280
GPIO_MODER_MODER0
#define GPIO_MODER_MODER0
Definition: stm32f407xx.h:8035
IS_GPIO_AF
#define IS_GPIO_AF(AF)
Definition: stm32h735/stm32h735g-dk/Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_gpio_ex.h:356
GPIO_InitTypeDef::Alternate
uint32_t Alternate
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:61
GPIO_PIN_SET
@ GPIO_PIN_SET
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:71
HAL_ERROR
@ HAL_ERROR
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:43
IS_GPIO_MODE
#define IS_GPIO_MODE(MODE)
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:266
GPIO_TypeDef::BSRR
__IO uint32_t BSRR
Definition: stm32f407xx.h:535
HAL_GPIO_ReadPin
GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
GPIO_TypeDef::IDR
__IO uint32_t IDR
Definition: stm32f407xx.h:533
HAL_OK
@ HAL_OK
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h:42
HAL_GPIO_Init
void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
UNUSED
#define UNUSED(x)
Definition: porcupine/demo/c/dr_libs/old/dr.h:92
GPIO_InitTypeDef::Mode
uint32_t Mode
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:52
GPIO_InitTypeDef::Pull
uint32_t Pull
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:55
IS_GPIO_ALL_INSTANCE
#define IS_GPIO_ALL_INSTANCE(INSTANCE)
Definition: stm32f407xx.h:15118
__HAL_RCC_SYSCFG_CLK_ENABLE
#define __HAL_RCC_SYSCFG_CLK_ENABLE()
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc.h:599
HAL_GPIO_DeInit
void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
RESET
@ RESET
Definition: stm32f407/stm32f407g-disc1/Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h:187
GPIO_InitTypeDef::Speed
uint32_t Speed
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:58
SYSCFG
#define SYSCFG
Definition: stm32f407xx.h:1098
__HAL_GPIO_EXTI_GET_IT
#define __HAL_GPIO_EXTI_GET_IT(__EXTI_LINE__)
Checks whether the specified EXTI line is asserted or not.
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:192
GPIO_PIN_RESET
@ GPIO_PIN_RESET
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:70
__HAL_GPIO_EXTI_CLEAR_IT
#define __HAL_GPIO_EXTI_CLEAR_IT(__EXTI_LINE__)
Clears the EXTI's line pending bits.
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:200
GPIO_TypeDef
General Purpose I/O.
Definition: stm32f407xx.h:527
IS_GPIO_SPEED
#define IS_GPIO_SPEED(SPEED)
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:278
GPIO_TypeDef::ODR
__IO uint32_t ODR
Definition: stm32f407xx.h:534
GPIO_MODE_AF_OD
#define GPIO_MODE_AF_OD
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:123
GPIO_OTYPER_OT_0
#define GPIO_OTYPER_OT_0
Definition: stm32f407xx.h:8247
HAL_GPIO_EXTI_IRQHandler
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
GPIO_LCKR_LCKK
#define GPIO_LCKR_LCKK
Definition: stm32f407xx.h:8893
GPIO_GET_INDEX
#define GPIO_GET_INDEX(__GPIOx__)
Definition: stm32h735/stm32h735g-dk/Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_gpio_ex.h:440
GPIO_PinState
GPIO_PinState
GPIO Bit SET and Bit RESET enumeration.
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:68
GPIO_TypeDef::OSPEEDR
__IO uint32_t OSPEEDR
Definition: stm32f407xx.h:531
IS_GPIO_PIN
#define IS_GPIO_PIN(PIN)
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:265
IS_GPIO_PIN_ACTION
#define IS_GPIO_PIN_ACTION(ACTION)
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:264
GPIO_MODE_OUTPUT_PP
#define GPIO_MODE_OUTPUT_PP
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:120
GPIO_TypeDef::AFR
__IO uint32_t AFR[2]
Definition: stm32f407xx.h:537
EXTI
#define EXTI
Definition: stm32f407xx.h:1099
GPIO_PUPDR_PUPDR0
#define GPIO_PUPDR_PUPDR0
Definition: stm32f407xx.h:8479
HAL_GPIO_TogglePin
void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
GPIO_TypeDef::PUPDR
__IO uint32_t PUPDR
Definition: stm32f407xx.h:532
GPIO_TypeDef::OTYPER
__IO uint32_t OTYPER
Definition: stm32f407xx.h:530
HAL_GPIO_LockPin
HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
stm32f7xx_hal.h
This file contains all the functions prototypes for the HAL module driver.
HAL_GPIO_WritePin
void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
GPIO_InitTypeDef::Pin
uint32_t Pin
Definition: stm32f407/stm32f407g-disc1/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h:49


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:53