cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to getting interrupt on rising edge

JPanc.1
Associate II

I am using STM32F103C8T6 and STM32CubeMX and TrueSTUDIO for development purpose.

I am trying to get interrupt on rising edge and in ISR I wrote LED blinking code.

I gave high input for rising edge detection but not getting LED blink.

My code as follows...

static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
 
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
 
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(SD_CS_GPIO_Port, SD_CS_Pin, GPIO_PIN_SET);
 
  /*Configure GPIO pin : LED_Pin */
  GPIO_InitStruct.Pin = LED_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
 
  /*Configure GPIO pin : SD_CS_Pin */
  GPIO_InitStruct.Pin = SD_CS_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  HAL_GPIO_Init(SD_CS_GPIO_Port, &GPIO_InitStruct);
 
  /*Configure GPIO pin : ADXL345_INT_Pin */
  GPIO_InitStruct.Pin = ADXL345_INT_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(ADXL345_INT_GPIO_Port, &GPIO_InitStruct);
 
  /*Configure GPIO pin : RTC_INT_Pin */
  GPIO_InitStruct.Pin = RTC_INT_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(RTC_INT_GPIO_Port, &GPIO_InitStruct);
 
  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI4_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI4_IRQn);
 
  HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 1);
  HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
 
}
 
/****************************************************************/
/***************************ISR for GPIO***************************/
 
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(GPIO_Pin);
  /* NOTE: This function Should not be modified, when the callback is needed,
           the HAL_GPIO_EXTI_Callback could be implemented in the user file
   */
  if(GPIO_Pin == ADXL345_INT_Pin)
  {
	  HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin,0);
	  HAL_Delay(500);
	  HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin,1);
	  HAL_Delay(500);
  }
}

5 REPLIES 5
Bob S
Principal

What DO you get? Do you ever enter the callback function? Do you get into the callback but with a different pin? And should we presume that the ADXL345 interrupt pin is pin 4 on one of the GPIO ports (ex. PA4, PB4, etc.)?

Note that calling HAL_Delay() inside an interrupt callback will not work unless the SYSTICK interrupt has a higher priority than your EXTI interrupt. If not, HAL_Delay() will never return because it is waiting for a global variable to be incremented by the SYSTICK interrupt handler, which is blocked by your interrupt.

ADXL345 pin is connected to PB4

Led just turns off when giving interrupt but not turn off after delay

I think it is a hal delay problem.

but if I want to blink led than what should I do???

Declare a global variable like "volatile bool gotADXLInt = false;" (and you need the "volatile" qualifier). Then in your callback, set that to true. In your main code loop, check for gotADXLInt true and flash the LED.

JPanc.1
Associate II

Still not getting detect rising edge...

Is any strep wrong?

RomainR.
ST Employee

Hello jay28pancha1,

As explain by Bob, remove for loop and HAL_Delay into HAL_GPIO_EXTI_Callback. Declare a global volatile boolean variable, and manage the toggle in your main loop .

IRQ_Handler and Callback function must be short without any overhead instruction code or software latency.

Then, can you try to configure GPIO_InitStruct.Pull = GPIO_PULLDOWN;

/*Configure GPIO pin : ADXL345_INT_Pin */
  GPIO_InitStruct.Pin = ADXL345_INT_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(ADXL345_INT_GPIO_Port, &GPIO_InitStruct);

And place a breakpoint in stm32f1xx_it.c at line GPIO_EXTI_IRQHandler. Check if you get this line when you apply a rising egde on PB4.

/**
  * @brief  This function handles External line 4 interrupt request.
  * @param  None
  * @retval None
  */
void EXTI4_IRQHandler(void)
{
  HAL_GPIO_EXTI_IRQHandler(GPIO_Pin);
}

Then continue debug (step out) you should enter into the callback function.

It should work. Share your code if not.

BR

rrom

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.