> 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



_______________________________________________
AVR-libc-dev mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/avr-libc-dev

Reply via email to