cancel
Showing results for 
Search instead for 
Did you mean: 

Timer interrupt handling - multiple flags

jkuek
Associate II
Posted on May 28, 2012 at 07:28

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);

  }

}

1 REPLY 1
Posted on May 28, 2012 at 13:27

Because presumably the coder understood what he expected to see.

Example #1 Interrupts would occur 100+ ms apart, and should be serviceable within that window.

Example #2 That interrupts would, on multiple occasions, fall on the same service window.

Aberrant conditions, or in fact Von Neumann serialization effects (ie signals after test done), would be caught by tail chaining.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..