From 18f6b8ad689cb87f81b572d5131a09e33fc9857d Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Thu, 4 Mar 2021 21:40:54 +0100 Subject: [PATCH] thumbv6m support with emulated atomics --- .github/workflows/rust.yml | 3 ++- .vscode/settings.json | 2 ++ ci.sh | 1 + embassy-macros/src/lib.rs | 8 ++++---- embassy/Cargo.toml | 3 ++- embassy/build.rs | 29 +++++++++++++++++++++++++++++ embassy/src/executor/mod.rs | 2 +- embassy/src/executor/raw.rs | 2 +- embassy/src/executor/run_queue.rs | 2 +- embassy/src/executor/timer_queue.rs | 2 +- embassy/src/interrupt.rs | 5 ++++- embassy/src/lib.rs | 1 + embassy/src/util/forever.rs | 3 ++- 13 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 embassy/build.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 00641612..cdfe354c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -18,8 +18,9 @@ jobs: - uses: actions-rs/toolchain@v1 with: toolchain: nightly - target: thumbv7em-none-eabi components: rust-src override: true + - name: install + run: rustup target add thumbv7em-none-eabi thumbv6m-none-eabi - name: Build run: ./ci.sh diff --git a/.vscode/settings.json b/.vscode/settings.json index 19efb137..f34a7db4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,8 @@ "rust-analyzer.cargo.allFeatures": false, "rust-analyzer.checkOnSave.allFeatures": false, "rust-analyzer.checkOnSave.allTargets": false, + "rust-analyzer.cargo.target": "thumbv6m-none-eabi", + "rust-analyzer.checkOnSave.target": "thumbv6m-none-eabi", "rust-analyzer.procMacro.enable": true, "rust-analyzer.cargo.loadOutDirsFromCheck": true, "rust-analyzer.cargo.target": "thumbv7em-none-eabi", diff --git a/ci.sh b/ci.sh index d556fb31..b6036cac 100755 --- a/ci.sh +++ b/ci.sh @@ -9,6 +9,7 @@ set -euxo pipefail (cd embassy; cargo build --target thumbv7em-none-eabi) (cd embassy; cargo build --target thumbv7em-none-eabi --features log) (cd embassy; cargo build --target thumbv7em-none-eabi --features defmt) +(cd embassy; cargo build --target thumbv6m-none-eabi --features defmt) # embassy-nrf diff --git a/embassy-macros/src/lib.rs b/embassy-macros/src/lib.rs index 8276f0bb..5b834ff1 100644 --- a/embassy-macros/src/lib.rs +++ b/embassy-macros/src/lib.rs @@ -157,17 +157,17 @@ pub fn interrupt_take(item: TokenStream) -> TokenStream { static HANDLER: ::embassy::interrupt::Handler; } - let func = HANDLER.func.load(::core::sync::atomic::Ordering::Acquire); - let ctx = HANDLER.ctx.load(::core::sync::atomic::Ordering::Acquire); + let func = HANDLER.func.load(::embassy::atomic::Ordering::Acquire); + let ctx = HANDLER.ctx.load(::embassy::atomic::Ordering::Acquire); if !func.is_null() { let func: fn(*mut ()) = ::core::mem::transmute(func); func(ctx) } } - static TAKEN: ::core::sync::atomic::AtomicBool = ::core::sync::atomic::AtomicBool::new(false); + static TAKEN: ::embassy::atomic::AtomicBool = ::embassy::atomic::AtomicBool::new(false); - if TAKEN.compare_exchange(false, true, ::core::sync::atomic::Ordering::AcqRel, ::core::sync::atomic::Ordering::Acquire).is_err() { + if TAKEN.compare_exchange(false, true, ::embassy::atomic::Ordering::AcqRel, ::embassy::atomic::Ordering::Acquire).is_err() { panic!("IRQ Already taken"); } diff --git a/embassy/Cargo.toml b/embassy/Cargo.toml index 1b8e2a07..98c607c6 100644 --- a/embassy/Cargo.toml +++ b/embassy/Cargo.toml @@ -18,8 +18,9 @@ defmt = { version = "0.2.0", optional = true } log = { version = "0.4.11", optional = true } cortex-m = "0.7.1" -futures = { version = "0.3.5", default-features = false } +futures = { version = "0.3.5", default-features = false, features = [ "cfg-target-has-atomic", "unstable" ] } pin-project = { version = "1.0.2", default-features = false } embassy-macros = { version = "0.1.0", path = "../embassy-macros"} embassy-traits = { version = "0.1.0", path = "../embassy-traits"} +atomic-polyfill = { version = "0.1.0" } diff --git a/embassy/build.rs b/embassy/build.rs new file mode 100644 index 00000000..6fe82b44 --- /dev/null +++ b/embassy/build.rs @@ -0,0 +1,29 @@ +use std::env; + +fn main() { + let target = env::var("TARGET").unwrap(); + + if target.starts_with("thumbv6m-") { + println!("cargo:rustc-cfg=cortex_m"); + println!("cargo:rustc-cfg=armv6m"); + } else if target.starts_with("thumbv7m-") { + println!("cargo:rustc-cfg=cortex_m"); + println!("cargo:rustc-cfg=armv7m"); + } else if target.starts_with("thumbv7em-") { + println!("cargo:rustc-cfg=cortex_m"); + println!("cargo:rustc-cfg=armv7m"); + println!("cargo:rustc-cfg=armv7em"); // (not currently used) + } else if target.starts_with("thumbv8m.base") { + println!("cargo:rustc-cfg=cortex_m"); + println!("cargo:rustc-cfg=armv8m"); + println!("cargo:rustc-cfg=armv8m_base"); + } else if target.starts_with("thumbv8m.main") { + println!("cargo:rustc-cfg=cortex_m"); + println!("cargo:rustc-cfg=armv8m"); + println!("cargo:rustc-cfg=armv8m_main"); + } + + if target.ends_with("-eabihf") { + println!("cargo:rustc-cfg=has_fpu"); + } +} diff --git a/embassy/src/executor/mod.rs b/embassy/src/executor/mod.rs index 5f016f70..7a6caf70 100644 --- a/embassy/src/executor/mod.rs +++ b/embassy/src/executor/mod.rs @@ -4,7 +4,6 @@ use core::future::Future; use core::marker::PhantomData; use core::pin::Pin; use core::ptr::NonNull; -use core::sync::atomic::Ordering; use core::task::{Context, Poll}; use core::{mem, ptr}; @@ -16,6 +15,7 @@ mod util; mod waker; use self::util::UninitCell; +use crate::atomic::Ordering; use crate::fmt::panic; use crate::interrupt::{Interrupt, InterruptExt}; use crate::time::Alarm; diff --git a/embassy/src/executor/raw.rs b/embassy/src/executor/raw.rs index 1f7e48f5..0c947080 100644 --- a/embassy/src/executor/raw.rs +++ b/embassy/src/executor/raw.rs @@ -3,13 +3,13 @@ use core::cmp::min; use core::marker::PhantomData; use core::ptr; use core::ptr::NonNull; -use core::sync::atomic::{AtomicU32, Ordering}; use core::task::Waker; use super::run_queue::{RunQueue, RunQueueItem}; use super::timer_queue::{TimerQueue, TimerQueueItem}; use super::util::UninitCell; use super::waker; +use crate::atomic::{AtomicU32, Ordering}; use crate::time::{Alarm, Instant}; /// Task is spawned (has a future) diff --git a/embassy/src/executor/run_queue.rs b/embassy/src/executor/run_queue.rs index 397d7122..fb7c65e0 100644 --- a/embassy/src/executor/run_queue.rs +++ b/embassy/src/executor/run_queue.rs @@ -1,8 +1,8 @@ use core::ptr; use core::ptr::NonNull; -use core::sync::atomic::{AtomicPtr, Ordering}; use super::raw::Task; +use crate::atomic::{AtomicPtr, Ordering}; pub(crate) struct RunQueueItem { next: AtomicPtr, diff --git a/embassy/src/executor/timer_queue.rs b/embassy/src/executor/timer_queue.rs index 38c23757..bd043aeb 100644 --- a/embassy/src/executor/timer_queue.rs +++ b/embassy/src/executor/timer_queue.rs @@ -2,9 +2,9 @@ use core::cell::Cell; use core::cmp::min; use core::ptr; use core::ptr::NonNull; -use core::sync::atomic::Ordering; use super::raw::{Task, STATE_TIMER_QUEUED}; +use crate::atomic::{AtomicPtr, Ordering}; use crate::time::Instant; pub(crate) struct TimerQueueItem { diff --git a/embassy/src/interrupt.rs b/embassy/src/interrupt.rs index ba25ec8e..6491a57d 100644 --- a/embassy/src/interrupt.rs +++ b/embassy/src/interrupt.rs @@ -1,7 +1,8 @@ use core::ptr; -use core::sync::atomic::{AtomicPtr, Ordering}; use cortex_m::peripheral::NVIC; +use crate::atomic::{AtomicBool, AtomicPtr, Ordering}; + pub use embassy_macros::interrupt_declare as declare; pub use embassy_macros::interrupt_take as take; @@ -45,6 +46,7 @@ pub trait InterruptExt: Interrupt { fn set_handler_context(&self, ctx: *mut ()); fn enable(&self); fn disable(&self); + #[cfg(not(armv6m))] fn is_active(&self) -> bool; fn is_enabled(&self) -> bool; fn is_pending(&self) -> bool; @@ -83,6 +85,7 @@ impl InterruptExt for T { } #[inline] + #[cfg(not(armv6m))] fn is_active(&self) -> bool { NVIC::is_active(NrWrap(self.number())) } diff --git a/embassy/src/lib.rs b/embassy/src/lib.rs index 5e98736f..b7ed4e70 100644 --- a/embassy/src/lib.rs +++ b/embassy/src/lib.rs @@ -16,3 +16,4 @@ pub mod time; pub mod util; pub use embassy_traits as traits; +pub use atomic_polyfill as atomic; diff --git a/embassy/src/util/forever.rs b/embassy/src/util/forever.rs index 9b1f8bb3..fac3c260 100644 --- a/embassy/src/util/forever.rs +++ b/embassy/src/util/forever.rs @@ -1,6 +1,7 @@ use core::cell::UnsafeCell; use core::mem::MaybeUninit; -use core::sync::atomic::{AtomicBool, Ordering}; + +use crate::atomic::{AtomicBool, Ordering}; pub struct Forever { used: AtomicBool,