Search code examples
stm32interrupt-handlingstm32f1

Clearing pending EXTI interrupt in stm32f103


I'm trying to toggle an LED at PC13 by toggling PC14, the problem is that the interrupt handler is kept being called without toggling PC14 and the the pending interrupt is not cleared using EXTI->PR register, nor cleared manually using the debugger. I tried also clearing it in NVIC->ICPR, I'm not sure why there are two registers for clearing the same interrupt.
here is my code
and you can find the header in https://github.com/AymenSekhri/tinyHAL-STM32f103/tree/master/STM32F103-HAL/tinyHAL

/* 
* Description:
*   Toggle LED at C13 whenever C14 goes from HIGH to LOW.
*
*/
#include "tinyHAL/stm32f103_hal.h"
int main(){
    //Enable AFIO clock from RCC
    enablePeripheralClock(Peripheral_AFIO);
    //Enable and configure C13 & C14
    enablePeripheralClock(Peripheral_GPIOC);
    configureGPIO(Peripheral_GPIOC, 13, GPIO_MODE_OUT_50MHZ, GPIO_CONF_OUT_PUSHPULL);
    configureGPIO(Peripheral_GPIOC, 14, GPIO_MODE_IN, GPIO_CONF_IN_PUSHUP_PULLDOWN);

    //Link EXTI14 to C14
    AFIO->EXTICR[3] = (AFIO->EXTICR[3] & ~(0xF<<8)) | 2;
    //Configure inturrput at EXTI14 falling edge
    EXTI->FTSR      |= 1<<14;
    //Unmask interrupt 40 (EXTI10-15)
    EXTI->IMR       |= 1<<14;
    //Set Priority to interrupt 40 (EXTI10-15)
    NVIC->IP[40]    |= 0x10;

    //Enable interrupt 40 (EXTI10-15)
    NVIC->ISER[40>>5] |= (1 << (40&0x1F));
    while(1);
}

void EXTI15_10_IRQHandler(void){
    toggleGPIOBit(Peripheral_GPIOC, 13);
    if (EXTI->PR & (1 << 14)){
        EXTI->PR |= (1 << 14);
    }
    //NVIC->ICPR[40>>5] |= (1 << (40&0x1F));
    __COMPILER_BARRIER();
}

Solution

  • The best solution to get rid of the electronic noise at the pin that (over-)triggers your EXTI is to improve the hardware - but this is the software board, not the electronic one.

    If you had a TIM channel connected to that pin, I would recommend to use it to filter the signal coming in. But I think that PC14 doesn't have a timer.

    The second-best solution (and this is where workarounds have already started!) is to use a timer (the TIM, not its channel), either to establish a periodic time base to sample the pin (by DMA or by an ISR, and feed samples into a software-based filtering...) - or to deactivate the EXTI interrupt in the EXTI ISR, start the timer and re-activate the EXTI interrupt when the timer expired.

    Both of these µC-based approaches are clumsy and clearly inferior to developing a good hardware. This doesn't say that with a "good" hardware you shouldn't add some debouncing or noise protection inside your software!