STM32 GPIO External interrupt triggers but state does not change
Hey everyone!
I'm trying to detect the rotations of my rotary encoder. I used external interrupts to detect when the state of the pin changes. My problem is that it seems like when the encoder bounces the interrupt gets triggered, but the state doesn't change. How can that be? The only thing I can think of is that when I read the pin with HAL_GPIO_ReadPin inside the interrupt it has already changed. Can that happen? I would assume that the contents of the IDR are locked while the interrupt is running, but I could find any information about this.
Here is the minimal code from my project.
uint8_t get_rotary_state() {
GPIO_PinState A_state = HAL_GPIO_ReadPin(ENCODER_A_GPIO_Port, ENCODER_A_Pin);
GPIO_PinState B_state = HAL_GPIO_ReadPin(ENCODER_B_GPIO_Port, ENCODER_B_Pin);
return (B_state << 1) + A_state;
}
uint8_t rotary_prev_state = 0;
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_pin) {
if (GPIO_pin == ENCODER_A_Pin || GPIO_pin == ENCODER_B_Pin) {
uint8_t rotary_current_state = get_rotary_state();
// This can happen.... somehow
if (rotary_current_state == rotary_prev_state) {
}
rotary_prev_state = rotary_current_state;
}
}
