Skip to main content

Subsection 9.2.6 Part 5: The Handler (Which Line Fired, and How to Clear It)

Twelve lines share EXTI4_15_IRQHandler, so it is the programmer’s job to determine, inside the ISR, what it was called for. The EXTI records each detected falling edge in the falling edge pending register, EXTI_FPR1, one bit per line (Figure 9.2.14). To check whether EXTI4 caused the interrupt we would therefore check bit 4 of EXTI_FPR1.
It is also the programmer’s job to clear this bit if it was set. Recall that we have encountered two different ways to clear a status bit: TIM14’s flag was cleared by writing a 0, and the ADC’s flags were cleared by writing a 1. What decided which was the access type printed under the bit in the reference manual (Subsection 8.3.2).

Activity 9.2.3. Predict the Clear.

(a)

The reference manual calls bit 4 of the EXTI_FPR1 register FPIF4. To clear it, do you write a 1 to that bit or a 0?

(b)

What would you look up to find out? Name the document and the section. Then determine which part of the register description you would read.
The access type under every FPIF bit is rc_w1: “Each bit is cleared by writing 1 into it.” So this flag behaves like the ADC’s flags and unlike the timer’s, and the clearing mask carries a 1 at our bit and 0s everywhere else. Recall that we need to assign the mask, and not compound-assign it, for the reason we established with the timer. On a write-1-to-clear register the read returns a 1 at every flag that is already set, and writing those 1s back would clear every one of them, including lines the handler was not called for. Assign the single-bit mask instead.
Figure 9.2.14. EXTI_FPR1, the falling edge pending register (RM0490 §12.5.5, offset 0x010). One bit per line, FPIF0 through FPIF15: hardware sets the bit when a falling edge is detected on that line, and the access type rc_w1 under every bit means software clears it by writing a 1. Writing a 0 has no effect. This is the same convention as the ADC’s flags and the opposite of TIM14_SR.
The handler therefore checks the pending flag to find out which line fired, then clears that flag, sets a flag of its own, and leaves. It has the same shape as the timer handler, with a test in front of it:
// Interrupt service routine for the User button
void EXTI4_15_IRQHandler(void){
    __disable_irq();                      // disable global interrupts

    if (EXTI->FPR1 & EXTI_FPR1_FPIF4) {   // a falling-edge event happened on EXTI4
        EXTI->FPR1 = EXTI_FPR1_FPIF4;     // Clear the pending flag by writing 1
        buttonPushed = 1;
    }
    __enable_irq();                       // Enable global interrupts
}
An ISR that returned without clearing FPR1 would be re-entered the moment it returned, forever: the pending bit is still set, so the request is still outstanding. The symptom is a program that appears to hang, and it is worth recognizing because the cause is one missing line.
The variable it sets is shared between the handler and main(), so it carries the keyword whose reason we saw with the timer: no call to this function appears anywhere in the program, so the compiler has no evidence the variable ever changes and is entitled to assume it does not (Subsection 8.3.6):
volatile int buttonPushed;    // written by the ISR, read by main()