Skip to content
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

Add support for asm!() #97

Merged
merged 1 commit into from
Apr 26, 2022
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ cfg-if = "0.1.10"
path = "macros/"
version = "=0.3.2"
optional = true

[build-dependencies]
rustversion = "1.0"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The version on `crates.io` is pre-built. The following is only necessary when t
You need to have [atdf2svd][] (= 0.3.1), [svd2rust][] (= 0.19), [form][] (>= 0.8), [rustfmt][](for the *nightly* toolchain) and [svdtools][] (>= 0.1.9) installed:
```bash
cargo install atdf2svd --version 0.3.1
cargo install svd2rust --version 0.19
cargo install svd2rust --version 0.19.0
Patryk27 marked this conversation as resolved.
Show resolved Hide resolved
cargo install form
rustup component add --toolchain nightly rustfmt
pip3 install --user svdtools
Expand Down
16 changes: 16 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn main() {
println!("cargo:rerun-if-changed=build.rs");

maybe_enable_asm();
}

#[rustversion::before(1.59.0)]
fn maybe_enable_asm() {
//
}

#[rustversion::since(1.59.0)]
fn maybe_enable_asm() {
// https://github.com/rust-lang/rust/pull/92816
println!("cargo:rustc-cfg=avr_device_asm_macro");
}
15 changes: 12 additions & 3 deletions src/asm.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
//! Assembly instructions

#[cfg(all(target_arch = "avr", avr_device_asm_macro))]
use core::arch::asm;

/// No Operation
#[inline(always)]
pub fn nop() {
cfg_if::cfg_if! {
if #[cfg(target_arch = "avr")] {
if #[cfg(all(target_arch = "avr", avr_device_asm_macro))] {
unsafe { asm!("nop") }
} else if #[cfg(target_arch = "avr")] {
unsafe { llvm_asm!("nop") }
} else {
unimplemented!()
Expand All @@ -16,7 +21,9 @@ pub fn nop() {
#[inline(always)]
pub fn sleep() {
cfg_if::cfg_if! {
if #[cfg(target_arch = "avr")] {
if #[cfg(all(target_arch = "avr", avr_device_asm_macro))] {
unsafe { asm!("sleep") }
} else if #[cfg(target_arch = "avr")] {
unsafe { llvm_asm!("sleep") }
} else {
unimplemented!()
Expand All @@ -28,7 +35,9 @@ pub fn sleep() {
#[inline(always)]
pub fn wdr() {
cfg_if::cfg_if! {
if #[cfg(target_arch = "avr")] {
if #[cfg(all(target_arch = "avr", avr_device_asm_macro))] {
unsafe { asm!("wdr") }
} else if #[cfg(target_arch = "avr")] {
unsafe { llvm_asm!("wdr") }
} else {
unimplemented!()
Expand Down
24 changes: 22 additions & 2 deletions src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,31 @@

pub use bare_metal::{CriticalSection, Mutex, Nr};

#[cfg(all(target_arch = "avr", avr_device_asm_macro))]
use core::arch::asm;

#[inline]
/// Disables all interrupts
///
/// 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")] {
if #[cfg(all(target_arch = "avr", avr_device_asm_macro))] {
// Store current state
let sreg: u8;

unsafe {
asm!(
"in {sreg}, 0x3F",
sreg = out(reg) sreg,
)
};

// Disable interrupts
unsafe { asm!("cli") };

sreg & 0x80 == 0x80
} else if #[cfg(target_arch = "avr")] {
// Store current state
let sreg: u8;
unsafe { llvm_asm!("in $0,0x3F" :"=r"(sreg) ::: "volatile") };
Expand All @@ -39,7 +57,9 @@ pub fn disable() -> bool {
/// - Do not call this function inside an [crate::interrupt::free] critical section
pub unsafe fn enable() {
cfg_if::cfg_if! {
if #[cfg(target_arch = "avr")] {
if #[cfg(all(target_arch = "avr", avr_device_asm_macro))] {
asm!("sei");
} else if #[cfg(target_arch = "avr")] {
llvm_asm!("sei" :::: "volatile");
} else {
unimplemented!()
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
//! * `attiny861`
//! * `attiny88`
#![no_std]
#![feature(llvm_asm)]
#![cfg_attr(avr_device_asm_macro, feature(asm_experimental_arch))]
#![cfg_attr(not(avr_device_asm_macro), feature(llvm_asm))]

pub mod asm;
pub mod interrupt;
Expand Down Expand Up @@ -167,10 +168,10 @@ pub use crate::devices::atmega644;
pub use crate::devices::atmega8;
#[cfg(feature = "atmega8u2")]
pub use crate::devices::atmega8u2;
#[cfg(feature = "attiny167")]
pub use crate::devices::attiny167;
#[cfg(feature = "attiny1614")]
pub use crate::devices::attiny1614;
#[cfg(feature = "attiny167")]
pub use crate::devices::attiny167;
#[cfg(feature = "attiny202")]
pub use crate::devices::attiny202;
#[cfg(feature = "attiny2313")]
Expand Down