cancel
Showing results for 
Search instead for 
Did you mean: 

EXTI PI2 not working on STM327508-DK

DRobe.4
Senior

I have viewed every example on the internet, I think and seen many different commands and approaches. As long as the interrupt is disabled, the program works, but it crashes when enabled. Here are snippets related to EXTI. Maybe I have mixed code from too many sources and something conflicts.

   RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;

  __HAL_RCC_GPIOI_CLK_ENABLE();

      GPIO_InitStruct.Mode       = GPIO_MODE_INPUT;

//   GPIO_InitStruct.Mode       = GPIO_MODE_IT_RISING;

      GPIO_InitStruct.Pull         = GPIO_PULLUP;                        

      GPIO_InitStruct.Pin        = GPIO_PIN_2;

      GPIO_InitStruct.Speed      = GPIO_SPEED_LOW;

      HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);

      HAL_NVIC_SetPriority(EXTI2_IRQn, 2, 3);                           

      NVIC_EnableIRQ(EXTI2_IRQn);

   EXTI->IMR |= 1<<2;                                                         

   EXTI->RTSR |= 1<<2;                                                         // Enable the 'rising edge' trigger (button release)

   SYSCFG->EXTICR[0] = 0x800;                                             // EXTICR[0] is EXTICR1. Line2 is 3rd set of 4 bits, PortI is 8th port starting with A

void EXTI2_IRQHandler(void)

{

   if (EXTI->PR & (1<<2))

    {

     EXTI->PR |= (1<<2);                                                         // Clear the EXTI status flag

//     NVIC_ClearPendingIRQ(EXTI2_IRQn);                               // Clear previous interrupt in NVIC

    HAL_GPIO_TogglePin(GPIOI, GPIO_PIN_1);                        // Toggle PI1

      }

}

.

1 ACCEPTED SOLUTION

Accepted Solutions

Define "crash", when you stop in the debugger WHERE is it?

If in the Default Handler that would suggest the linker isn't binding the one you have supplied.

Is this using a .CPP or compiled as C++ ? If so use extern "C" so the name isn't mangled.

Try writing this in consistent HAL form, I'm not looking to unpack the bit fields and registers.

extern "C" void EXTI2_IRQHandler(void)
{
  /* EXTI line interrupt detected */
  if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_2) != RESET)
  {
     __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_2);
     HAL_GPIO_TogglePin(GPIOI, GPIO_PIN_1);  // Toggle PI1
  }
}
 
 
static void EXTI2_IRQHandler_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure = {0};
 
  /* Enable GPIOI clock */
  __HAL_RCC_GPIOI_CLK_ENABLE();
 
  /* Configure PI.2 pin as input floating */
  GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStructure.Pull = GPIO_NOPULL;
  GPIO_InitStructure.Pin = GPIO_PIN_2;
  HAL_GPIO_Init(GPIOI, &GPIO_InitStructure);
 
  /* Enable and set EXTI lines 2 Interrupt to the lowest priority */
  HAL_NVIC_SetPriority(EXTI2_IRQn, 2, 0);
  HAL_NVIC_EnableIRQ(EXTI2_IRQn);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

3 REPLIES 3

Define "crash", when you stop in the debugger WHERE is it?

If in the Default Handler that would suggest the linker isn't binding the one you have supplied.

Is this using a .CPP or compiled as C++ ? If so use extern "C" so the name isn't mangled.

Try writing this in consistent HAL form, I'm not looking to unpack the bit fields and registers.

extern "C" void EXTI2_IRQHandler(void)
{
  /* EXTI line interrupt detected */
  if(__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_2) != RESET)
  {
     __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_2);
     HAL_GPIO_TogglePin(GPIOI, GPIO_PIN_1);  // Toggle PI1
  }
}
 
 
static void EXTI2_IRQHandler_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure = {0};
 
  /* Enable GPIOI clock */
  __HAL_RCC_GPIOI_CLK_ENABLE();
 
  /* Configure PI.2 pin as input floating */
  GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStructure.Pull = GPIO_NOPULL;
  GPIO_InitStructure.Pin = GPIO_PIN_2;
  HAL_GPIO_Init(GPIOI, &GPIO_InitStructure);
 
  /* Enable and set EXTI lines 2 Interrupt to the lowest priority */
  HAL_NVIC_SetPriority(EXTI2_IRQn, 2, 0);
  HAL_NVIC_EnableIRQ(EXTI2_IRQn);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
DRobe.4
Senior

Clive, you did it! and your approach avoids the messy SYSCFG code which comes in 100 flavours on the internet. Thank you!

dbgarasiya
Senior II

great