Electronic – Is it really necessary to manually clear the XMEGA timer overflow interrupt flag

atmel-studiocxmega

As some of you may know, Atmel provides a software framework (mainly as part of Atmel Studio) that provides drivers and examples and is updated on a more or less regular basis.

In a recent update they explicitly point out, that it is important to manually clear the overflow interrupt flag in the interrupt callback function.

 // * \subsection xmega_tc_qs_ovf_setup_code Example code
 // *
 // * Add a callback function that will be executed when the overflow interrupt
 // * trigger.
 // * \code
 static void my_callback(void)
 {
    // User code to execute when the overflow occurs here

    // THIS WAS ADDED IN LAST UPDATE
    // Important to clear Interrupt Flag
    tc_clear_overflow(&TCC0);
    // THIS WAS ADDED IN LAST UPDATE

 }
 //\endcode

According to the XMEGAA data sheet:

OVFIF is automatically cleared when the corresponding interrupt vector is executed. The flag can also be cleared by
writing a one to its bit location.

Is there a scenario/reason where manually clearing the flag may be required?

Best Answer

Is there a scenario/reason where manually clearing the flag may be required?

Not sure about the ASF, but there are cases where you need to cancel any pending interrupt. For instance, when (re-)configuring a timer you may want to disable interrupts, modify the timer, and cancel any timer interrupts which may have occurred in the mean time, before re-enabling interrupts.

If you don't even have an overflow ISR you can still poll the OVIF to detect overflow, and reset the flag to arm it for the next overflow.