STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
stm32f7xx_hal_gpio.c
Go to the documentation of this file.
1 
125 /* Includes ------------------------------------------------------------------*/
126 #include "stm32f7xx_hal.h"
127 
137 #ifdef HAL_GPIO_MODULE_ENABLED
138 
139 /* Private typedef -----------------------------------------------------------*/
140 /* Private define ------------------------------------------------------------*/
144 #define GPIO_MODE ((uint32_t)0x00000003U)
145 #define EXTI_MODE ((uint32_t)0x10000000U)
146 #define GPIO_MODE_IT ((uint32_t)0x00010000U)
147 #define GPIO_MODE_EVT ((uint32_t)0x00020000U)
148 #define RISING_EDGE ((uint32_t)0x00100000U)
149 #define FALLING_EDGE ((uint32_t)0x00200000U)
150 #define GPIO_OUTPUT_TYPE ((uint32_t)0x00000010U)
151 
152 #define GPIO_NUMBER ((uint32_t)16U)
153 
156 /* Private macro -------------------------------------------------------------*/
157 /* Private variables ---------------------------------------------------------*/
158 /* Private function prototypes -----------------------------------------------*/
159 /* Private functions ---------------------------------------------------------*/
160 /* Exported functions --------------------------------------------------------*/
187 void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
188 {
189  uint32_t position = 0x00;
190  uint32_t ioposition = 0x00;
191  uint32_t iocurrent = 0x00;
192  uint32_t temp = 0x00;
193 
194  /* Check the parameters */
196  assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
197  assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
198  assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
199 
200  /* Configure the port pins */
201  for(position = 0; position < GPIO_NUMBER; position++)
202  {
203  /* Get the IO position */
204  ioposition = ((uint32_t)0x01) << position;
205  /* Get the current IO position */
206  iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition;
207 
208  if(iocurrent == ioposition)
209  {
210  /*--------------------- GPIO Mode Configuration ------------------------*/
211  /* In case of Alternate function mode selection */
212  if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
213  {
214  /* Check the Alternate function parameter */
215  assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
216 
217  /* Configure Alternate function mapped with the current IO */
218  temp = GPIOx->AFR[position >> 3];
219  temp &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
220  temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & (uint32_t)0x07) * 4));
221  GPIOx->AFR[position >> 3] = temp;
222  }
223 
224  /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
225  temp = GPIOx->MODER;
226  temp &= ~(GPIO_MODER_MODER0 << (position * 2));
227  temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2));
228  GPIOx->MODER = temp;
229 
230  /* In case of Output or Alternate function mode selection */
231  if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
232  (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD))
233  {
234  /* Check the Speed parameter */
235  assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
236  /* Configure the IO Speed */
237  temp = GPIOx->OSPEEDR;
238  temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
239  temp |= (GPIO_Init->Speed << (position * 2));
240  GPIOx->OSPEEDR = temp;
241 
242  /* Configure the IO Output Type */
243  temp = GPIOx->OTYPER;
244  temp &= ~(GPIO_OTYPER_OT_0 << position) ;
245  temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4) << position);
246  GPIOx->OTYPER = temp;
247  }
248 
249  /* Activate the Pull-up or Pull down resistor for the current IO */
250  temp = GPIOx->PUPDR;
251  temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
252  temp |= ((GPIO_Init->Pull) << (position * 2));
253  GPIOx->PUPDR = temp;
254 
255  /*--------------------- EXTI Mode Configuration ------------------------*/
256  /* Configure the External Interrupt or event for the current IO */
257  if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
258  {
259  /* Enable SYSCFG Clock */
261 
262  temp = SYSCFG->EXTICR[position >> 2];
263  temp &= ~(((uint32_t)0x0F) << (4 * (position & 0x03)));
264  temp |= ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03)));
265  SYSCFG->EXTICR[position >> 2] = temp;
266 
267  /* Clear EXTI line configuration */
268  temp = EXTI->IMR;
269  temp &= ~((uint32_t)iocurrent);
270  if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
271  {
272  temp |= iocurrent;
273  }
274  EXTI->IMR = temp;
275 
276  temp = EXTI->EMR;
277  temp &= ~((uint32_t)iocurrent);
278  if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
279  {
280  temp |= iocurrent;
281  }
282  EXTI->EMR = temp;
283 
284  /* Clear Rising Falling edge configuration */
285  temp = EXTI->RTSR;
286  temp &= ~((uint32_t)iocurrent);
287  if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
288  {
289  temp |= iocurrent;
290  }
291  EXTI->RTSR = temp;
292 
293  temp = EXTI->FTSR;
294  temp &= ~((uint32_t)iocurrent);
295  if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
296  {
297  temp |= iocurrent;
298  }
299  EXTI->FTSR = temp;
300  }
301  }
302  }
303 }
304 
312 void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
313 {
314  uint32_t position;
315  uint32_t ioposition = 0x00;
316  uint32_t iocurrent = 0x00;
317  uint32_t tmp = 0x00;
318 
319  /* Check the parameters */
321 
322  /* Configure the port pins */
323  for(position = 0; position < GPIO_NUMBER; position++)
324  {
325  /* Get the IO position */
326  ioposition = ((uint32_t)0x01) << position;
327  /* Get the current IO position */
328  iocurrent = (GPIO_Pin) & ioposition;
329 
330  if(iocurrent == ioposition)
331  {
332  /*------------------------- GPIO Mode Configuration --------------------*/
333  /* Configure IO Direction in Input Floating Mode */
334  GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (position * 2));
335 
336  /* Configure the default Alternate Function in current IO */
337  GPIOx->AFR[position >> 3] &= ~((uint32_t)0xF << ((uint32_t)(position & (uint32_t)0x07) * 4)) ;
338 
339  /* Configure the default value for IO Speed */
340  GPIOx->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2));
341 
342  /* Configure the default value IO Output Type */
343  GPIOx->OTYPER &= ~(GPIO_OTYPER_OT_0 << position) ;
344 
345  /* Deactivate the Pull-up and Pull-down resistor for the current IO */
346  GPIOx->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << (position * 2));
347 
348  /*------------------------- EXTI Mode Configuration --------------------*/
349  tmp = SYSCFG->EXTICR[position >> 2];
350  tmp &= (((uint32_t)0x0F) << (4 * (position & 0x03)));
351  if(tmp == ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4 * (position & 0x03))))
352  {
353  /* Configure the External Interrupt or event for the current IO */
354  tmp = ((uint32_t)0x0F) << (4 * (position & 0x03));
355  SYSCFG->EXTICR[position >> 2] &= ~tmp;
356 
357  /* Clear EXTI line configuration */
358  EXTI->IMR &= ~((uint32_t)iocurrent);
359  EXTI->EMR &= ~((uint32_t)iocurrent);
360 
361  /* Clear Rising Falling edge configuration */
362  EXTI->RTSR &= ~((uint32_t)iocurrent);
363  EXTI->FTSR &= ~((uint32_t)iocurrent);
364  }
365  }
366  }
367 }
368 
392 GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
393 {
394  GPIO_PinState bitstatus;
395 
396  /* Check the parameters */
397  assert_param(IS_GPIO_PIN(GPIO_Pin));
398 
399  if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
400  {
401  bitstatus = GPIO_PIN_SET;
402  }
403  else
404  {
405  bitstatus = GPIO_PIN_RESET;
406  }
407  return bitstatus;
408 }
409 
426 void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
427 {
428  /* Check the parameters */
429  assert_param(IS_GPIO_PIN(GPIO_Pin));
430  assert_param(IS_GPIO_PIN_ACTION(PinState));
431 
432  if(PinState != GPIO_PIN_RESET)
433  {
434  GPIOx->BSRR = GPIO_Pin;
435  }
436  else
437  {
438  GPIOx->BSRR = (uint32_t)GPIO_Pin << 16;
439  }
440 }
441 
448 void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
449 {
450  /* Check the parameters */
451  assert_param(IS_GPIO_PIN(GPIO_Pin));
452 
453  GPIOx->ODR ^= GPIO_Pin;
454 }
455 
467 HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
468 {
469  __IO uint32_t tmp = GPIO_LCKR_LCKK;
470 
471  /* Check the parameters */
472  assert_param(IS_GPIO_PIN(GPIO_Pin));
473 
474  /* Apply lock key write sequence */
475  tmp |= GPIO_Pin;
476  /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
477  GPIOx->LCKR = tmp;
478  /* Reset LCKx bit(s): LCKK='0' + LCK[15-0] */
479  GPIOx->LCKR = GPIO_Pin;
480  /* Set LCKx bit(s): LCKK='1' + LCK[15-0] */
481  GPIOx->LCKR = tmp;
482  /* Read LCKK bit*/
483  tmp = GPIOx->LCKR;
484 
485  if((GPIOx->LCKR & GPIO_LCKR_LCKK) != RESET)
486  {
487  return HAL_OK;
488  }
489  else
490  {
491  return HAL_ERROR;
492  }
493 }
494 
500 void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
501 {
502  /* EXTI line interrupt detected */
503  if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
504  {
505  __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
506  HAL_GPIO_EXTI_Callback(GPIO_Pin);
507  }
508 }
509 
515 __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
516 {
517  /* Prevent unused argument(s) compilation warning */
518  UNUSED(GPIO_Pin);
519 
520  /* NOTE: This function Should not be modified, when the callback is needed,
521  the HAL_GPIO_EXTI_Callback could be implemented in the user file
522  */
523 }
524 
534 #endif /* HAL_GPIO_MODULE_ENABLED */
535 
543 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
__IO uint32_t IDR
Definition: stm32f745xx.h:602
#define GPIO_MODE_AF_OD
__IO uint32_t PUPDR
Definition: stm32f745xx.h:601
GPIO_PinState
GPIO Bit SET and Bit RESET enumeration.
#define GPIO_OTYPER_OT_0
Definition: stm32f745xx.h:4608
#define __HAL_GPIO_EXTI_GET_IT(__EXTI_LINE__)
Checks whether the specified EXTI line is asserted or not.
#define assert_param(expr)
Include module&#39;s header file.
__IO uint32_t OTYPER
Definition: stm32f745xx.h:599
void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)
#define GPIO_OSPEEDER_OSPEEDR0
Definition: stm32f745xx.h:4626
#define IS_GPIO_PULL(PULL)
#define __HAL_GPIO_EXTI_CLEAR_IT(__EXTI_LINE__)
Clears the EXTI&#39;s line pending bits.
#define IS_GPIO_PIN(PIN)
HAL_StatusTypeDef HAL_GPIO_LockPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
__IO uint32_t AFR[2]
Definition: stm32f745xx.h:606
__IO uint32_t LCKR
Definition: stm32f745xx.h:605
#define IS_GPIO_PIN_ACTION(ACTION)
General Purpose I/O.
Definition: stm32f745xx.h:596
#define GPIO_LCKR_LCKK
Definition: stm32f745xx.h:4812
#define __IO
Definition: core_cm0.h:213
#define GPIO_GET_INDEX(__GPIOx__)
__IO uint32_t MODER
Definition: stm32f745xx.h:598
This file contains all the functions prototypes for the HAL module driver.
void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
#define GPIO_PUPDR_PUPDR0
Definition: stm32f745xx.h:4676
__IO uint32_t BSRR
Definition: stm32f745xx.h:604
#define __HAL_RCC_SYSCFG_CLK_ENABLE()
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
GPIO Init structure definition.
#define IS_GPIO_SPEED(SPEED)
#define UNUSED(x)
void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
#define EXTI
Definition: stm32f745xx.h:1301
__IO uint32_t OSPEEDR
Definition: stm32f745xx.h:600
#define IS_GPIO_ALL_INSTANCE(__INSTANCE__)
Definition: stm32f745xx.h:8821
#define IS_GPIO_MODE(MODE)
__IO uint32_t ODR
Definition: stm32f745xx.h:603
#define GPIO_MODE_AF_PP
HAL_StatusTypeDef
HAL Status structures definition.
#define GPIO_MODER_MODER0
Definition: stm32f745xx.h:4558
#define GPIO_MODE_OUTPUT_PP
GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
#define GPIO_MODE_OUTPUT_OD
#define SYSCFG
Definition: stm32f745xx.h:1300