avr-chat
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-chat] Need critical section; how to implement semaphore without


From: Matthew MacClary
Subject: Re: [avr-chat] Need critical section; how to implement semaphore without disabling interrupt
Date: Sun, 29 May 2005 12:50:34 -0700
User-agent: Mutt/1.4.2.1i

On Sun, May 29, 2005 at 06:03:48PM +0200, J?rgen Birkler wrote:
> Is there a way to implement a semaphore without disabling the interrupt?
> 
> Recall that some CPU (HC11?) have some exchange instruction that read and 
> write to to a memory location in one atomonous instruction.

    This isn't fancy but you could just use a volatile uint8_t flag.
Inside any potentially conflicting interrupts you then need to check
the state of the flag before preceeding.  In the main loop, once you
get past the write to the 8 bit flag, you know that the interrupt
routines that check the flag won't execute user code.

volatile uint8_t flag;

SIGNAL(...) {
  if (flag != LOCKED) {
    /* user code */
  }
}

int main () {
  flag = UNLOCKED;

  /* code ... */

  flag = LOCKED;
  /* critical section */
  flag = UNLOCKED;

  /* more code ...*/
}

    Another alternative to disabling all interrupts is to just disable
the interrupt enable bits for the potentially conflicting interrupts.

-Matt




reply via email to

[Prev in Thread] Current Thread [Next in Thread]