~ruther/avr-device

bbba98677d5bd679d8075cec448d729abd14f553 — Tray Torrance 3 years ago d1f9379
Return the previous interrupt status upon disabling interrupts

This commit addresses #88 by returning a boolean which reflects the
previous state of the GIE flag upon disabling interrupts. This allows
`critical-section` to implement itself for AVR, while not breaking the
existing `avr-device` API.
1 files changed, 12 insertions(+), 10 deletions(-)

M src/interrupt.rs
M src/interrupt.rs => src/interrupt.rs +12 -10
@@ 12,10 12,19 @@ pub use bare_metal::{CriticalSection, Mutex, Nr};

#[inline]
/// Disables all interrupts
pub fn disable() {
///
/// Returns a bool, reflecting whether interrupts were enabled prior to calling this method.
pub fn disable() -> bool {
    cfg_if::cfg_if! {
        if #[cfg(target_arch = "avr")] {
            // Store current state
            let sreg: u8;
            unsafe { llvm_asm!("in $0,0x3F" :"=r"(sreg) ::: "volatile") };

            // Disable interrupts
            unsafe { llvm_asm!("cli" :::: "volatile") };

            sreg & 0x80 == 0x80
        } else {
            unimplemented!()
        }


@@ 47,20 56,13 @@ where
{
    cfg_if::cfg_if! {
        if #[cfg(target_arch = "avr")] {
            let sreg: u8;

            // Store current state
            unsafe {
                llvm_asm!("in $0,0x3F" :"=r"(sreg) ::: "volatile");
            }

            // Disable interrupts
            disable();
            let interrupts_enabled = disable();

            let r = f(unsafe { &CriticalSection::new() });

            // Restore interrupt state
            if sreg & 0x80 != 0x00 {
            if interrupts_enabled {
                unsafe { enable(); }
            }


Do not follow this link