[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[avr-libc-dev] Disable/Restore all Interrupts
From: |
Sam de Jongh Hepworth |
Subject: |
[avr-libc-dev] Disable/Restore all Interrupts |
Date: |
Thu, 4 Sep 2003 00:18:57 +0200 |
Hi
avr-libc is great! I have writte two macros that help disable and restore all
interrupts. I think the cli(); and sei(); macros are a little dangerous to use,
please consider this;
Before this code is executed all interrupts has been disabled. Now the
following code is executed;
-- BEGIN --------------------------------
cli();
// do some work with all interrupts disabled...
sei();
-- END --------------------------------
After executing the code all interrupts will be enabled! But they where
disabled before the code was executed :-(
My solution to this problem is;
-- BEGIN --------------------------------
#define DISABLE_INTERRUPTS() {ubyte _save_int = SREG & flag(7); cli();
#define RESTORE_INTERRUPTS() (SREG |= _save_int);}
DISABLE_INTERRUPTS();
// do some work with all interrupts disabled...
ENABLE_INTERRUPTS();
-- END --------------------------------
The DISABLE_INTERRUPTS macro save the current state of SREG bit 7 and then
disabled interrupts. The RESTORE_INTERRUPTS macro restore SREG bit 7. Nice!
Best regards,
Sam
- [avr-libc-dev] Disable/Restore all Interrupts,
Sam de Jongh Hepworth <=