This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

timer interrupt disable & enable - pending interrupts

Hi,

working with a LPC1758 I´ve installed a timer. In the interrupt routine I've several values which will be updated according to the current state. Is it the right approach to disable the timer interrupt getting the current value of these values? After enabling the timer again, will I also be able to catch a timer interrupt which occurred during the interrupt disable time? I couldn't found any useful information in the lpc17xx user manual.

volatile unsigned char flag1 = false;
volatile unsigned char flag2 = false;
volatile unsigned char flag1_value = 0;
volatile unsigned char flag2_value = 0;

void TIMER0_IRQHandler (void)
{
/* value 1 */
  if(flag1_value)
  {
    flag1_value--;
    if(!flag1_value)
        flag1= true;
  }
/* value 2 */
  if(flag2_value)
  {
    flag2_value--;
    if(!flag2_value)
        flag2= true;
  }
LPC_TIM0->IR = 0x3<<0; /* clear interrupt flag */
}

void SetValue(unsigned char flag, unsigned char value)
{
  NVIC_DisableIRQ(TIMER0_IRQn);
  switch(flag)
  {
   case 1:
     flag1_value = value;
     break;
   case 2:
     flag2_value = value;
     break;
  }

  NVIC_EnableIRQ(TIMER0_IRQn);
}

unsigned char GetValueFlag(unsigned char flag)
{
  unsigned char rtn = 0x00;
  NVIC_DisableIRQ(TIMER0_IRQn);
  switch(flag)
  {
   case 1:
    rtn = flag1;
    flag1 = false;
    break;
  case 2:
    rtn = flag2;
    flag2 = false;
    break;
  }
  NVIC_EnableIRQ(TIMER0_IRQn);
  return rtn;
}

best regards
Lars

  • Your sample code fails to show any logic that would explain why you want to disable the timer interrupt in your function.

    I don't even understand why you have a function for returning the flag value - it's a volatile variable that you can have your source code check in any place where the current value is relevant to know.

    Stopping interrupts is only meaningful if the interrupt handler modifies multiple values and you have code outside the interrupt handler that needs to get a snapshot copy of these values, i.e. you want to make sure that you don't get an interrupt that makes further changes while your main loop code is busy copying all values.

    But a main loop that only looks at a single value do never need any interrupt lock as long as that single value is small enough that the processor can perform atomic reads.