[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [avr-libc-dev] New Atomic.h header?
From: |
Ron |
Subject: |
RE: [avr-libc-dev] New Atomic.h header? |
Date: |
Mon, 8 Jan 2007 18:22:41 +1100 |
> Hi Dean,
>
> thanks for your suggestion, and for getting here to the
> mailing list with these. I hope others might also state
> their opinions about it, as I think this issue occasionally
> came up as a request in some discussions.
I have used these two for some time now.
static inline STATE_REG SaveState(void)
{
STATE_REG flags = SREG; // get processor state
cli(); // turn off interrupts
return flags; // return machine state
}
static inline void RestoreState(STATE_REG ps)
{
SREG = ps; // restore previous flag state
}
STATE_REG is a typedef'd unsigned char. Usage is:
void Function(void)
{
STATE_REG state = SaveState();
.
.
RestoreState(state);
}
This is just a more formal expression of Jorg's suggestion. SaveState
compiles (-Os) to:
IN Rxx,0x3F
CLI
and RestoreState to:
OUT 0x3f,Rxx
where the compiler manages the initial contents of Rxx throughout the
function to ensure the OUT uses the same value (the OUT register may not
be the IN register, but its contents have been preserved).
Ron