Set An ISR to execute when a GPIO is set high STM32

in this example we will be setting an ISR using the NVIC for when the PC1 is set high (on a rising edge)

image 5.png

image.png

The GPIO pull-up mode will set the pin to default to 1 so it wast be set to pull-down to default to 0

image.png

image.png

After saving the ioc file, CubeIde will generate the necessary configuration for us.

static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

 .
 .
 .

  /*Configure GPIO pin : SPI2_HS_Pin */
  GPIO_InitStruct.Pin = SPI2_HS_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(SPI2_HS_GPIO_Port, &GPIO_InitStruct);

 .
 .
 .

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI1_IRQn);

  /* USER CODE BEGIN MX_GPIO_Init_2 */

  /* USER CODE END MX_GPIO_Init_2 */
}

So you will notice that a HAL_N`VIC functions are being called these are just enabling the Interrupt Number which is

EXTI0_IRQn                  = 6,      /*!< EXTI Line0 Interrupt    

after enabling the interrupt number 6, a function inside stm32***_it.c file will be called

image.png

which calls an other function that handles the actual callBack function

image.png

image.png

__weak directive indicates that if an other function definition for **void** **HAL_GPIO_EXTI_Callback**(**uint16_t** GPIO_Pin) exist it should be using instand of the one that has the __weak

so we Redefine it in the main.c for example

image.png