Electronic – hal_delay in external interrupt problem

cembeddedinterruptsmicrocontrollerstm32

I wrote a simple C program for STM32:

main.c

  while (1)
  {
      HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
      HAL_Delay(400);
      HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
      HAL_Delay(400);
  }

it.c

void EXTI0_IRQHandler(void)
{
      HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
      HAL_Delay(1000);
      HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
      HAL_Delay(1000);

  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
}

There is no problem when there is no delay interrupt; it enters ISR and exits.

But when delay interrupts,
the program gets stuck at delay ().

My RCC settings are correct.

Best Answer

It sounds like EXTI0_IRQHandler() is preventing the systick timer from running. I believe HAL_Delay() uses the systick timer. So the timer stops incrementing and the HAL_Delay() function waits forever.

It may be possible to fix this by adjusting the priority of EXTI0_IRQHandler() lower than the systick mechanism so that systick continues to run even while EXTI0_IRQHandler() is running. Or you could re-write it so that EXTI0_IRQHandler() sets a global flag, and then read the flag in the main loop. If the flag is set, you toggle D14 in the main loop then clear the flag and call HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0) from the main loop.