Electrical – Disable interrupt in ISR on STM32F4

interruptsmicrocontrollerstm32stm32f4

Is is possible to disable an interruption during its service routine to prevent it from happening again for some time?

For example:

void TIM2_IRQHandler(void)
{

    if(TIM_GetITStatus(TIM2,TIM_IT_CC1) !=RESET)
    {
        //(some code)

       **TIM_ITConfig(TIM2,TIM_IT_CC1,DISABLE);**
       TIM_ClearITPendingBit(TIM2,TIM_IT_CC1);
    }
}

Best Answer

Yes, you can disable the interrupt source inside the interrupt you're handling.

This is actually a quite common procedure in things like USARTs, that may have interrupts like "TX buffer empty". The interrupt is fired when the software needs to load a new byte. It may check if a (circular) buffer has any remaining bytes. If not, the software needs to disable this interrupt, otherwise the hardware would keep requesting the interrupt for new bytes. Effectively creating an infinite loop (as the "Pending Bit" is immedetialy SET by hardware again) leaving no CPU time for the application.

However, re-enabling the interrupt "after some time" doesn't happen by itself. You could use another timer for this, to create the delay you want. Or write a state machine of some sort inside the timer if you want to use the same time base/phasing. Another solution is to re-enable the interrupt from the main code. To iterate further on the USART example: the software (e.g. 'main' code) can re-enable the TX-empty interrupt once it has a new buffer loaded that needs to be sent.

Related Topic