Skip to content

Only emit inline-assembly when building for AVR #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ include = [
"/README.md",
]

[package.metadata.docs.rs]
all-features = true

[features]
atmega1280 = []
atmega168 = []
Expand All @@ -32,11 +35,9 @@ rt = ["avr-device-macros"]
[dependencies]
bare-metal = "0.2.5"
vcell = "0.1.2"
cfg-if = "0.1.10"

[dependencies.avr-device-macros]
path = "macros/"
version = "=0.2.0"
optional = true

[package.metadata.docs.rs]
all-features = true
24 changes: 21 additions & 3 deletions src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,35 @@
/// No Operation
#[inline(always)]
pub fn nop() {
unsafe { llvm_asm!("nop") }
cfg_if::cfg_if! {
if #[cfg(target_arch = "avr")] {
unsafe { llvm_asm!("nop") }
} else {
unimplemented!()
}
}
}

/// Sleep / Wait For Interrupt
#[inline(always)]
pub fn sleep() {
unsafe { llvm_asm!("sleep") }
cfg_if::cfg_if! {
if #[cfg(target_arch = "avr")] {
unsafe { llvm_asm!("sleep") }
} else {
unimplemented!()
}
}
}

/// Watchdog Reset
#[inline(always)]
pub fn wdr() {
unsafe { llvm_asm!("wdr") }
cfg_if::cfg_if! {
if #[cfg(target_arch = "avr")] {
unsafe { llvm_asm!("wdr") }
} else {
unimplemented!()
}
}
}
59 changes: 33 additions & 26 deletions src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ pub use bare_metal::{CriticalSection, Mutex, Nr};
#[inline]
/// Disables all interrupts
pub fn disable() {
unsafe {
llvm_asm!(
"cli" :::: "volatile"
);
cfg_if::cfg_if! {
if #[cfg(target_arch = "avr")] {
unsafe { llvm_asm!("cli" :::: "volatile") };
} else {
unimplemented!()
}
}
}

Expand All @@ -27,9 +29,13 @@ pub fn disable() {
///
/// - Do not call this function inside an [crate::interrupt::free] critical section
pub unsafe fn enable() {
llvm_asm!(
"sei" :::: "volatile"
);
cfg_if::cfg_if! {
if #[cfg(target_arch = "avr")] {
llvm_asm!("sei" :::: "volatile");
} else {
unimplemented!()
}
}
}

/// Execute closure `f` in an interrupt-free context.
Expand All @@ -39,28 +45,29 @@ pub fn free<F, R>(f: F) -> R
where
F: FnOnce(&CriticalSection) -> R,
{
let sreg: u8;
cfg_if::cfg_if! {
if #[cfg(target_arch = "avr")] {
let sreg: u8;

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

// Disable interrupts
disable();
// Disable interrupts
disable();

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

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

r
r
} else {
let _ = f;
unimplemented!()
}
}
}