[Top][All Lists]

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

Re: [avr-gcc-list] generic queue library for AVR GCC?

From: E. Weddington
Subject: Re: [avr-gcc-list] generic queue library for AVR GCC?
Date: Mon, 15 Nov 2004 16:42:42 -0700
User-agent: Mozilla Thunderbird 0.7.3 (Windows/20040803)

Rusty Wright wrote:

Eric, wouldn't it be safer to disable interrupts before you copy the
SREG?  I.e., isn't it possible for an interrupt to occur between the
copy of SREG and the cli?
Yes, it's possible. But no, you don't want to disable interrupts before saving SREG.

The whole point of saving SREG and restoring SREG is to restore the state of global interrupts. Remember, when you clear global interrupts (CLI) it changes a flag (bit) in the SREG register. This is why you want to save the SREG register *before* doing the cli(). When you restore the SREG register, you restore the state of the global interrupts after the "critical section" is done, *regardless of whether the global interrupts were enabled or disabled*.

For example, you do a critical section like:

// critical section

However, this enables global interrupts after the critical section. The problem is: were global interrupts enabled, or disabled before the cli() and the critical section? Saving and restoring the status register handles both cases:

sreg = SREG;
// critical section
SREG = sreg;

It doesn't matter whether there is an interrupt between the sreg = SREG; and the cli();. The point is that the state, no matter what it is, has been preseved and subsequently restored.

Also, regarding the push/pop, most c compilers store local variables
on the stack so with your method you'll end up pushing and popping

You're absolutely right. I misspoke. My main point was really that one does not have to do this in inline assembly, that it can be done just as well in pure C.


PS, Don't forget to respond to the *list* and not just personally.

reply via email to

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