AnsweredAssumed Answered

Detecting long button press on STM32F3

Question asked by Tony Blake on Apr 20, 2018
Latest reply on Apr 20, 2018 by dhenry

Hi,

 

I'm trying to detect a long button press on an STM32F3.

 

I have a push button connected to a GPIO on the STM32F3 configured with an external interrupt request (EXTI9_5_IRQn). The interrupt handler (HAL_GPIO_EXTI_IRQHandler) is successfully triggered when the button is pressed and it subsequently calls the callback function (HAL_GPIO_EXTI_Callback).

 

I have attempted to detect a long button press by creating a counter variable inside the callback function and incrementing it while the GPIO pin is HIGH (i.e. while the button is pressed). A long button press could therefore be detected when the counter reaches a certain value (i.e. a period of time has elapsed). Unfortunately, the GPIO pin state will not update until the program exits the callback function and I get stuck in an infinite loop like so:

 

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

    uint8_t counter = 0;

    
     if(GPIO_Pin == POWER_DET_GPIO_PIN){
        
          if(POWER_DET_GPIO_PIN_STATE == 1){

 

               while(POWER_DET_GPIO_PIN_STATE){

 

                      counter++; // program gets stuck here

               }

          }

 

         if(counter == someval){

 

                  // long button press detected

         }
     }    
}

 

Can anyone help me out here? Is there a simpler way to detect a long button press?

 

Cheers,

Tony

Outcomes