From 58d503a77da73204f097fae1f1a2a1ce2cf90600 Mon Sep 17 00:00:00 2001 From: sodo Date: Sun, 10 Dec 2023 01:45:24 +0900 Subject: add avr support --- embassy-executor/src/arch/avr.rs | 60 ++++++++++++++++++++++++++++++++++++++++ embassy-executor/src/lib.rs | 3 +- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 embassy-executor/src/arch/avr.rs (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/arch/avr.rs b/embassy-executor/src/arch/avr.rs new file mode 100644 index 000000000..2c715f297 --- /dev/null +++ b/embassy-executor/src/arch/avr.rs @@ -0,0 +1,60 @@ +#[cfg(feature = "executor-interrupt")] +compile_error!("`executor-interrupt` is not supported with `arch-avr`."); + +#[cfg(feature = "executor-thread")] +pub use thread::*; +#[cfg(feature = "executor-thread")] +mod thread { + use core::marker::PhantomData; + + pub use embassy_executor_macros::main_avr as main; + + use crate::{raw, Spawner}; + + #[export_name = "__pender"] + fn __pender(_context: *mut ()) {} + + /// avr Executor + pub struct Executor { + inner: raw::Executor, + not_send: PhantomData<*mut ()>, + } + + impl Executor { + /// Create a new Executor. + pub fn new() -> Self { + Self { + inner: raw::Executor::new(core::ptr::null_mut()), + not_send: PhantomData, + } + } + + /// Run the executor. + /// + /// The `init` closure is called with a [`Spawner`] that spawns tasks on + /// this executor. Use it to spawn the initial task(s). After `init` returns, + /// the executor starts running the tasks. + /// + /// To spawn more tasks later, you may keep copies of the [`Spawner`] (it is `Copy`), + /// for example by passing it as an argument to the initial tasks. + /// + /// This function requires `&'static mut self`. This means you have to store the + /// Executor instance in a place where it'll live forever and grants you mutable + /// access. There's a few ways to do this: + /// + /// - a [StaticCell](https://docs.rs/static_cell/latest/static_cell/) (safe) + /// - a `static mut` (unsafe) + /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) + /// + /// This function never returns. + pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! { + init(self.inner.spawner()); + + loop { + unsafe { + self.inner.poll(); + } + } + } + } +} diff --git a/embassy-executor/src/lib.rs b/embassy-executor/src/lib.rs index 4c6900a6d..834ebf16a 100644 --- a/embassy-executor/src/lib.rs +++ b/embassy-executor/src/lib.rs @@ -20,9 +20,10 @@ macro_rules! check_at_most_one { check_at_most_one!(@amo [$($f)*] [$($f)*] []); }; } -check_at_most_one!("arch-cortex-m", "arch-riscv32", "arch-std", "arch-wasm",); +check_at_most_one!("arch-avr", "arch-cortex-m", "arch-riscv32", "arch-std", "arch-wasm",); #[cfg(feature = "_arch")] +#[cfg_attr(feature = "arch-avr", path = "arch/avr.rs")] #[cfg_attr(feature = "arch-cortex-m", path = "arch/cortex_m.rs")] #[cfg_attr(feature = "arch-riscv32", path = "arch/riscv32.rs")] #[cfg_attr(feature = "arch-std", path = "arch/std.rs")] -- cgit From 6bbc316312b2ba372ea05c52924d99bc4fb5382a Mon Sep 17 00:00:00 2001 From: Barnaby Walters Date: Fri, 22 Dec 2023 19:05:16 +0100 Subject: [embassy-executor] improved documentation * Feature auto-documentation * Task arena sizes in a
list * Non-documented comment explaining turbowakers with see-also link Further improvements: * Are the task-arena-size-* numbers sizes in bytes? or something else? * Task arena section could benefit from advice about how to choose a suitable size --- embassy-executor/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/lib.rs b/embassy-executor/src/lib.rs index 4c6900a6d..eea118ade 100644 --- a/embassy-executor/src/lib.rs +++ b/embassy-executor/src/lib.rs @@ -3,6 +3,9 @@ #![doc = include_str!("../README.md")] #![warn(missing_docs)] +//! ## Feature flags +#![doc = document_features::document_features!(feature_label = r#"{feature}"#)] + // This mod MUST go first, so that the others see its macros. pub(crate) mod fmt; -- cgit From b7cd7952c890f585ff876c622482534e5d58d4a4 Mon Sep 17 00:00:00 2001 From: sodo Date: Mon, 1 Jan 2024 21:18:30 +0900 Subject: avr: support sleep --- embassy-executor/src/arch/avr.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/arch/avr.rs b/embassy-executor/src/arch/avr.rs index 2c715f297..fa6afe762 100644 --- a/embassy-executor/src/arch/avr.rs +++ b/embassy-executor/src/arch/avr.rs @@ -8,11 +8,16 @@ mod thread { use core::marker::PhantomData; pub use embassy_executor_macros::main_avr as main; + use portable_atomic::{AtomicBool, Ordering}; use crate::{raw, Spawner}; + static SIGNAL_WORK_THREAD_MODE: AtomicBool = AtomicBool::new(false); + #[export_name = "__pender"] - fn __pender(_context: *mut ()) {} + fn __pender(_context: *mut ()) { + SIGNAL_WORK_THREAD_MODE.store(true, Ordering::SeqCst); + } /// avr Executor pub struct Executor { @@ -52,7 +57,11 @@ mod thread { loop { unsafe { - self.inner.poll(); + if SIGNAL_WORK_THREAD_MODE.swap(false, Ordering::SeqCst) { + self.inner.poll(); + } else { + avr_device::asm::sleep(); + } } } } -- cgit From b0071c5070f6b4c932e703f2539c2eef74f09338 Mon Sep 17 00:00:00 2001 From: sodo Date: Tue, 2 Jan 2024 14:48:27 +0900 Subject: avr: sleep fix --- embassy-executor/src/arch/avr.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/arch/avr.rs b/embassy-executor/src/arch/avr.rs index fa6afe762..11e81ed9a 100644 --- a/embassy-executor/src/arch/avr.rs +++ b/embassy-executor/src/arch/avr.rs @@ -57,10 +57,13 @@ mod thread { loop { unsafe { + avr_device::interrupt::disable(); if SIGNAL_WORK_THREAD_MODE.swap(false, Ordering::SeqCst) { - self.inner.poll(); - } else { + avr_device::interrupt::enable(); avr_device::asm::sleep(); + } else { + avr_device::interrupt::enable(); + self.inner.poll(); } } } -- cgit From 01dbe9278357296fe9fb99f5e6923e80067c4a98 Mon Sep 17 00:00:00 2001 From: sodo Date: Wed, 3 Jan 2024 12:35:07 +0900 Subject: fix --- embassy-executor/src/arch/avr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/arch/avr.rs b/embassy-executor/src/arch/avr.rs index 11e81ed9a..70085d04d 100644 --- a/embassy-executor/src/arch/avr.rs +++ b/embassy-executor/src/arch/avr.rs @@ -58,7 +58,7 @@ mod thread { loop { unsafe { avr_device::interrupt::disable(); - if SIGNAL_WORK_THREAD_MODE.swap(false, Ordering::SeqCst) { + if !SIGNAL_WORK_THREAD_MODE.swap(false, Ordering::SeqCst) { avr_device::interrupt::enable(); avr_device::asm::sleep(); } else { -- cgit From f0606da9adc8032cc92c06c0661b385742459fc8 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Thu, 11 Jan 2024 22:47:05 +0100 Subject: time: split queue driver too, don't reexport drivers. --- embassy-executor/src/raw/mod.rs | 27 +++++++++++++-------------- embassy-executor/src/raw/timer_queue.rs | 12 +++++------- 2 files changed, 18 insertions(+), 21 deletions(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/raw/mod.rs b/embassy-executor/src/raw/mod.rs index b16a1c7c3..3f00be4a8 100644 --- a/embassy-executor/src/raw/mod.rs +++ b/embassy-executor/src/raw/mod.rs @@ -30,9 +30,7 @@ use core::ptr::NonNull; use core::task::{Context, Poll}; #[cfg(feature = "integrated-timers")] -use embassy_time::driver::{self, AlarmHandle}; -#[cfg(feature = "integrated-timers")] -use embassy_time::Instant; +use embassy_time_driver::{self, AlarmHandle}; #[cfg(feature = "rtos-trace")] use rtos_trace::trace; @@ -50,7 +48,7 @@ pub(crate) struct TaskHeader { poll_fn: SyncUnsafeCell>, #[cfg(feature = "integrated-timers")] - pub(crate) expires_at: SyncUnsafeCell, + pub(crate) expires_at: SyncUnsafeCell, #[cfg(feature = "integrated-timers")] pub(crate) timer_queue_item: timer_queue::TimerQueueItem, } @@ -123,7 +121,7 @@ impl TaskStorage { poll_fn: SyncUnsafeCell::new(None), #[cfg(feature = "integrated-timers")] - expires_at: SyncUnsafeCell::new(Instant::from_ticks(0)), + expires_at: SyncUnsafeCell::new(0), #[cfg(feature = "integrated-timers")] timer_queue_item: timer_queue::TimerQueueItem::new(), }, @@ -164,7 +162,7 @@ impl TaskStorage { this.raw.state.despawn(); #[cfg(feature = "integrated-timers")] - this.raw.expires_at.set(Instant::MAX); + this.raw.expires_at.set(u64::MAX); } Poll::Pending => {} } @@ -328,7 +326,7 @@ pub(crate) struct SyncExecutor { impl SyncExecutor { pub(crate) fn new(pender: Pender) -> Self { #[cfg(feature = "integrated-timers")] - let alarm = unsafe { unwrap!(driver::allocate_alarm()) }; + let alarm = unsafe { unwrap!(embassy_time_driver::allocate_alarm()) }; Self { run_queue: RunQueue::new(), @@ -377,18 +375,19 @@ impl SyncExecutor { /// Same as [`Executor::poll`], plus you must only call this on the thread this executor was created. pub(crate) unsafe fn poll(&'static self) { #[cfg(feature = "integrated-timers")] - driver::set_alarm_callback(self.alarm, Self::alarm_callback, self as *const _ as *mut ()); + embassy_time_driver::set_alarm_callback(self.alarm, Self::alarm_callback, self as *const _ as *mut ()); #[allow(clippy::never_loop)] loop { #[cfg(feature = "integrated-timers")] - self.timer_queue.dequeue_expired(Instant::now(), wake_task_no_pend); + self.timer_queue + .dequeue_expired(embassy_time_driver::now(), wake_task_no_pend); self.run_queue.dequeue_all(|p| { let task = p.header(); #[cfg(feature = "integrated-timers")] - task.expires_at.set(Instant::MAX); + task.expires_at.set(u64::MAX); if !task.state.run_dequeue() { // If task is not running, ignore it. This can happen in the following scenario: @@ -418,7 +417,7 @@ impl SyncExecutor { // If this is already in the past, set_alarm might return false // In that case do another poll loop iteration. let next_expiration = self.timer_queue.next_expiration(); - if driver::set_alarm(self.alarm, next_expiration.as_ticks()) { + if embassy_time_driver::set_alarm(self.alarm, next_expiration) { break; } } @@ -568,8 +567,8 @@ pub fn wake_task_no_pend(task: TaskRef) { struct TimerQueue; #[cfg(feature = "integrated-timers")] -impl embassy_time::queue::TimerQueue for TimerQueue { - fn schedule_wake(&'static self, at: Instant, waker: &core::task::Waker) { +impl embassy_time_queue_driver::TimerQueue for TimerQueue { + fn schedule_wake(&'static self, at: u64, waker: &core::task::Waker) { let task = waker::task_from_waker(waker); let task = task.header(); unsafe { @@ -580,7 +579,7 @@ impl embassy_time::queue::TimerQueue for TimerQueue { } #[cfg(feature = "integrated-timers")] -embassy_time::timer_queue_impl!(static TIMER_QUEUE: TimerQueue = TimerQueue); +embassy_time_queue_driver::timer_queue_impl!(static TIMER_QUEUE: TimerQueue = TimerQueue); #[cfg(feature = "rtos-trace")] impl rtos_trace::RtosTraceOSCallbacks for Executor { diff --git a/embassy-executor/src/raw/timer_queue.rs b/embassy-executor/src/raw/timer_queue.rs index 59a3b43f5..94a5f340b 100644 --- a/embassy-executor/src/raw/timer_queue.rs +++ b/embassy-executor/src/raw/timer_queue.rs @@ -1,7 +1,5 @@ use core::cmp::min; -use embassy_time::Instant; - use super::TaskRef; use crate::raw::util::SyncUnsafeCell; @@ -30,7 +28,7 @@ impl TimerQueue { pub(crate) unsafe fn update(&self, p: TaskRef) { let task = p.header(); - if task.expires_at.get() != Instant::MAX { + if task.expires_at.get() != u64::MAX { if task.state.timer_enqueue() { task.timer_queue_item.next.set(self.head.get()); self.head.set(Some(p)); @@ -38,18 +36,18 @@ impl TimerQueue { } } - pub(crate) unsafe fn next_expiration(&self) -> Instant { - let mut res = Instant::MAX; + pub(crate) unsafe fn next_expiration(&self) -> u64 { + let mut res = u64::MAX; self.retain(|p| { let task = p.header(); let expires = task.expires_at.get(); res = min(res, expires); - expires != Instant::MAX + expires != u64::MAX }); res } - pub(crate) unsafe fn dequeue_expired(&self, now: Instant, on_task: impl Fn(TaskRef)) { + pub(crate) unsafe fn dequeue_expired(&self, now: u64, on_task: impl Fn(TaskRef)) { self.retain(|p| { let task = p.header(); if task.expires_at.get() <= now { -- cgit From 5f36108896d909ed990a587941d74e0488bcd190 Mon Sep 17 00:00:00 2001 From: xgroleau🐢 Date: Tue, 6 Feb 2024 10:38:48 -0500 Subject: fix: rtos-usage time missing --- embassy-executor/src/raw/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/raw/mod.rs b/embassy-executor/src/raw/mod.rs index 3f00be4a8..fbc0481c2 100644 --- a/embassy-executor/src/raw/mod.rs +++ b/embassy-executor/src/raw/mod.rs @@ -588,7 +588,7 @@ impl rtos_trace::RtosTraceOSCallbacks for Executor { } #[cfg(feature = "integrated-timers")] fn time() -> u64 { - Instant::now().as_micros() + embassy_time::Instant::now().as_millis() } #[cfg(not(feature = "integrated-timers"))] fn time() -> u64 { -- cgit From d48620d58f588936a5c74840063fe422764b749f Mon Sep 17 00:00:00 2001 From: xgroleau🐢 Date: Tue, 6 Feb 2024 15:50:28 -0500 Subject: fix: compilation for rtos trace --- embassy-executor/src/raw/mod.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/raw/mod.rs b/embassy-executor/src/raw/mod.rs index fbc0481c2..3d221c94b 100644 --- a/embassy-executor/src/raw/mod.rs +++ b/embassy-executor/src/raw/mod.rs @@ -581,6 +581,15 @@ impl embassy_time_queue_driver::TimerQueue for TimerQueue { #[cfg(feature = "integrated-timers")] embassy_time_queue_driver::timer_queue_impl!(static TIMER_QUEUE: TimerQueue = TimerQueue); +#[cfg(all(feature = "rtos-trace", feature = "integrated-timers"))] +const fn gcd(a: u64, b: u64) -> u64 { + if b == 0 { + a + } else { + gcd(b, a % b) + } +} + #[cfg(feature = "rtos-trace")] impl rtos_trace::RtosTraceOSCallbacks for Executor { fn task_list() { @@ -588,7 +597,8 @@ impl rtos_trace::RtosTraceOSCallbacks for Executor { } #[cfg(feature = "integrated-timers")] fn time() -> u64 { - embassy_time::Instant::now().as_millis() + const GCD_1M: u64 = gcd(embassy_time_driver::TICK_HZ, 1_000_000); + embassy_time_driver::now() * (1_000_00 / GCD_1M) / (embassy_time_driver::TICK_HZ / GCD_1M); } #[cfg(not(feature = "integrated-timers"))] fn time() -> u64 { -- cgit From 09613e90de92ba43974796efec13e38adf4c3ac8 Mon Sep 17 00:00:00 2001 From: xgroleau🐢 Date: Thu, 8 Feb 2024 09:01:07 -0500 Subject: fix: missing 0 --- embassy-executor/src/raw/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/raw/mod.rs b/embassy-executor/src/raw/mod.rs index 3d221c94b..3c9407d39 100644 --- a/embassy-executor/src/raw/mod.rs +++ b/embassy-executor/src/raw/mod.rs @@ -598,7 +598,7 @@ impl rtos_trace::RtosTraceOSCallbacks for Executor { #[cfg(feature = "integrated-timers")] fn time() -> u64 { const GCD_1M: u64 = gcd(embassy_time_driver::TICK_HZ, 1_000_000); - embassy_time_driver::now() * (1_000_00 / GCD_1M) / (embassy_time_driver::TICK_HZ / GCD_1M); + embassy_time_driver::now() * (1_000_000 / GCD_1M) / (embassy_time_driver::TICK_HZ / GCD_1M); } #[cfg(not(feature = "integrated-timers"))] fn time() -> u64 { -- cgit From 262518cfe5c303034f71393367914bec221c71be Mon Sep 17 00:00:00 2001 From: xgroleau🐢 Date: Thu, 8 Feb 2024 09:02:07 -0500 Subject: fix: removed trailing comma --- embassy-executor/src/raw/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/raw/mod.rs b/embassy-executor/src/raw/mod.rs index 3c9407d39..3d5e3ab9f 100644 --- a/embassy-executor/src/raw/mod.rs +++ b/embassy-executor/src/raw/mod.rs @@ -598,7 +598,7 @@ impl rtos_trace::RtosTraceOSCallbacks for Executor { #[cfg(feature = "integrated-timers")] fn time() -> u64 { const GCD_1M: u64 = gcd(embassy_time_driver::TICK_HZ, 1_000_000); - embassy_time_driver::now() * (1_000_000 / GCD_1M) / (embassy_time_driver::TICK_HZ / GCD_1M); + embassy_time_driver::now() * (1_000_000 / GCD_1M) / (embassy_time_driver::TICK_HZ / GCD_1M) } #[cfg(not(feature = "integrated-timers"))] fn time() -> u64 { -- cgit From 2c42463205dae7e38535fb18f58e872df99e125a Mon Sep 17 00:00:00 2001 From: Zheng Li <875543533@qq.com> Date: Sat, 2 Mar 2024 00:21:56 +0100 Subject: executor: remove portable-atomic for riscv. --- embassy-executor/src/arch/riscv32.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/arch/riscv32.rs b/embassy-executor/src/arch/riscv32.rs index c56f502d3..01e63a9fd 100644 --- a/embassy-executor/src/arch/riscv32.rs +++ b/embassy-executor/src/arch/riscv32.rs @@ -6,9 +6,9 @@ pub use thread::*; #[cfg(feature = "executor-thread")] mod thread { use core::marker::PhantomData; + use core::sync::atomic::{AtomicBool, Ordering}; pub use embassy_executor_macros::main_riscv as main; - use portable_atomic::{AtomicBool, Ordering}; use crate::{raw, Spawner}; -- cgit From 3d842dac85a4ea21519f56d4ec6342b528805b8a Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 20 Mar 2024 14:53:19 +0100 Subject: fmt: disable "unused" warnings. --- embassy-executor/src/fmt.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/fmt.rs b/embassy-executor/src/fmt.rs index 78e583c1c..2ac42c557 100644 --- a/embassy-executor/src/fmt.rs +++ b/embassy-executor/src/fmt.rs @@ -1,5 +1,5 @@ #![macro_use] -#![allow(unused_macros)] +#![allow(unused)] use core::fmt::{Debug, Display, LowerHex}; @@ -229,7 +229,6 @@ impl Try for Result { } } -#[allow(unused)] pub(crate) struct Bytes<'a>(pub &'a [u8]); impl<'a> Debug for Bytes<'a> { -- cgit From eca9aac194580956c851e42565546e5fc50d8070 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 20 Mar 2024 14:54:25 +0100 Subject: Fix warnings in recent nightly. --- embassy-executor/src/raw/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'embassy-executor/src') diff --git a/embassy-executor/src/raw/mod.rs b/embassy-executor/src/raw/mod.rs index 3d5e3ab9f..d9ea5c005 100644 --- a/embassy-executor/src/raw/mod.rs +++ b/embassy-executor/src/raw/mod.rs @@ -30,7 +30,7 @@ use core::ptr::NonNull; use core::task::{Context, Poll}; #[cfg(feature = "integrated-timers")] -use embassy_time_driver::{self, AlarmHandle}; +use embassy_time_driver::AlarmHandle; #[cfg(feature = "rtos-trace")] use rtos_trace::trace; -- cgit