AnsweredAssumed Answered

Timer interrupt handling - multiple flags

Question asked by JK on May 28, 2012
Latest reply on May 28, 2012 by Clive One
How should multiple interrupt flags be handled in the timer ISRs?

1. Handle all flags at the same time?
2. Handle a single flag?

My code originally did #2, but I found sometimes I would get into a state where an interrupt was enabled and the flag was set but the ISR wasn't being called.

The ST examples do a mixture of both. The F103 OCInactive example uses if...else if....:

void TIM2_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
  {
    /* Clear TIM2 Capture Compare1 interrupt pending bit*/
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

    /* PC.06 turnoff after 1000 ms */
    GPIO_ResetBits(GPIOC, GPIO_Pin_6);
  }
  else if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
  {
    /* Clear TIM2 Capture Compare2 interrupt pending bit*/
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);

    /* PC.07 turnoff after 500 ms */
    GPIO_ResetBits(GPIOC, GPIO_Pin_7);
  }
  else if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)
  {
    /* Clear TIM2 Capture Compare3 interrupt pending bit*/
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);

    /* PC.08 turnoff after 250 ms */
    GPIO_ResetBits(GPIOC, GPIO_Pin_8);
  }
  else
  {
    /* Clear TIM2 Capture Compare4 interrupt pending bit*/
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);

    /* PC.09 turnoff after 125 ms */
    GPIO_ResetBits(GPIOC, GPIO_Pin_9);
  }
}


whereas the OCToggle example uses if...if:

void TIM3_IRQHandler(void)
{
  /* TIM3_CH1 toggling with frequency = 183.1 Hz */
  if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1 );
    capture = TIM_GetCapture1(TIM3);
    TIM_SetCompare1(TIM3, capture + CCR1_Val );
  }

  /* TIM3_CH2 toggling with frequency = 366.2 Hz */
  if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
    capture = TIM_GetCapture2(TIM3);
    TIM_SetCompare2(TIM3, capture + CCR2_Val);
  }

  /* TIM3_CH3 toggling with frequency = 732.4 Hz */
  if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
    capture = TIM_GetCapture3(TIM3);
    TIM_SetCompare3(TIM3, capture + CCR3_Val);
  }

  /* TIM3_CH4 toggling with frequency = 1464.8 Hz */
  if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
    capture = TIM_GetCapture4(TIM3);
    TIM_SetCompare4(TIM3, capture + CCR4_Val);
  }
}

Outcomes