diff --git a/Cargo.toml b/Cargo.toml index 57377d4b..7336a287 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ members = [ "embassy", "embassy-nrf", + "anyfmt", "examples", ] diff --git a/anyfmt/Cargo.toml b/anyfmt/Cargo.toml new file mode 100644 index 00000000..3ee7c167 --- /dev/null +++ b/anyfmt/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "anyfmt" +version = "0.1.0" +authors = ["Dario Nieuwenhuis "] +edition = "2018" + + +[dependencies] +defmt = { version = "0.1.0", optional = true } +log = { version = "0.4.11", optional = true } diff --git a/anyfmt/src/lib.rs b/anyfmt/src/lib.rs new file mode 100644 index 00000000..3286cc27 --- /dev/null +++ b/anyfmt/src/lib.rs @@ -0,0 +1,149 @@ +#![no_std] + +pub mod export { + #[cfg(feature = "defmt")] + pub use defmt; + #[cfg(feature = "log")] + pub use log; +} + +#[cfg(feature = "log")] +#[macro_export] +macro_rules! log { + (trace, $($arg:expr),*) => { $crate::export::log::trace!($($arg),*); }; + (debug, $($arg:expr),*) => { $crate::export::log::debug!($($arg),*); }; + (info, $($arg:expr),*) => { $crate::export::log::info!($($arg),*); }; + (warn, $($arg:expr),*) => { $crate::export::log::warn!($($arg),*); }; + (error, $($arg:expr),*) => { $crate::export::log::error!($($arg),*); }; +} + +#[cfg(feature = "defmt")] +#[macro_export] +macro_rules! log { + (trace, $($arg:expr),*) => { $crate::export::defmt::trace!($($arg),*); }; + (debug, $($arg:expr),*) => { $crate::export::defmt::debug!($($arg),*); }; + (info, $($arg:expr),*) => { $crate::export::defmt::info!($($arg),*); }; + (warn, $($arg:expr),*) => { $crate::export::defmt::warn!($($arg),*); }; + (error, $($arg:expr),*) => { $crate::export::defmt::error!($($arg),*); }; +} + +#[cfg(not(any(feature = "log", feature = "defmt")))] +#[macro_export] +macro_rules! log { + ($level:ident, $($arg:expr),*) => {{}}; +} + +#[macro_export] +macro_rules! trace { + ($($arg:expr),*) => (log!(trace, $($arg),*)); +} + +#[macro_export] +macro_rules! debug { + ($($arg:expr),*) => ($crate::log!(debug, $($arg),*)); +} + +#[macro_export] +macro_rules! info { + ($($arg:expr),*) => ($crate::log!(info, $($arg),*)); +} + +#[macro_export] +macro_rules! warn { + ($($arg:expr),*) => ($crate::log!(warn, $($arg),*)); +} + +#[macro_export] +macro_rules! error { + ($($arg:expr),*) => ($crate::log!(error, $($arg),*)); +} + +#[macro_export] +macro_rules! expect { + ($arg:expr, $msg:expr) => { + match $crate::Try::into_result($arg) { + ::core::result::Result::Ok(t) => t, + ::core::result::Result::Err(e) => { + $crate::panic!("{:?}: {:?}", $crate::intern!($msg), e); + } + } + }; +} + +#[cfg(feature = "defmt")] +#[macro_export] +macro_rules! intern { + ($arg:expr) => { + $crate::export::defmt::intern!($arg) + }; +} + +#[cfg(not(feature = "defmt"))] +#[macro_export] +macro_rules! intern { + ($arg:expr) => { + $arg + }; +} + +#[macro_export] +macro_rules! unwrap { + ($arg:expr) => { + expect!($arg, "Unwrap failed") + }; +} + +#[macro_export] +macro_rules! panic { + () => { + $crate::panic!("panic") + }; + ($($arg:expr),*) => {{ + $crate::log!(error, $($arg),*); + ::core::panic!() + }}; +} + +#[macro_export] +macro_rules! assert { + ($cond:expr) => { + $crate::assert!($cond, "assertion failed"); + }; + ($cond:expr, $($arg:expr),*) => { + { + if !$cond { + $crate::panic!($($arg),*); + } + } + }; +} + +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +pub struct NoneError; + +pub trait Try { + type Ok; + type Error; + fn into_result(self) -> Result; +} + +impl Try for Option { + type Ok = T; + type Error = NoneError; + + #[inline] + fn into_result(self) -> Result { + self.ok_or(NoneError) + } +} + +impl Try for Result { + type Ok = T; + type Error = E; + + #[inline] + fn into_result(self) -> Self { + self + } +} diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml index 0019cbfb..6cf23aa7 100644 --- a/embassy-nrf/Cargo.toml +++ b/embassy-nrf/Cargo.toml @@ -5,12 +5,11 @@ authors = ["Dario Nieuwenhuis "] edition = "2018" [features] -defmt-default = [] -defmt-trace = [] -defmt-debug = [] -defmt-info = [] -defmt-warn = [] -defmt-error = [] +defmt-trace = [ ] +defmt-debug = [ ] +defmt-info = [ ] +defmt-warn = [ ] +defmt-error = [ ] 52810 = ["nrf52810-pac", "nrf52810-hal"] 52811 = ["nrf52811-pac"] #, "nrf52811-hal"] @@ -21,14 +20,17 @@ defmt-error = [] [dependencies] embassy = { version = "0.1.0", path = "../embassy" } + +anyfmt = { version = "0.1.0", path = "../anyfmt" } +defmt = { version = "0.1.0", optional = true } + cortex-m-rt = "0.6.12" cortex-m = { version = "0.6.3" } embedded-hal = { version = "0.2.4" } bare-metal = { version = "0.2.0", features = ["const-fn"] } -defmt = "0.1.0" nrf52810-pac = { version = "0.9.0", optional = true } -nrf52811-pac = { version = "0.9.0", optional = true } +nrf52811-pac = { version = "0.9.1", optional = true } nrf52832-pac = { version = "0.9.0", optional = true } nrf52833-pac = { version = "0.9.0", optional = true } nrf52840-pac = { version = "0.9.0", optional = true } diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs index 0b9a33b3..c18db262 100644 --- a/embassy-nrf/src/gpiote.rs +++ b/embassy-nrf/src/gpiote.rs @@ -1,6 +1,6 @@ +use anyfmt::*; use core::cell::Cell; use core::ptr; -use defmt::trace; use embassy::util::Signal; use crate::hal::gpio::{Input, Level, Output, Pin, Port}; @@ -34,7 +34,8 @@ pub enum TaskOutPolarity { Toggle, } -#[derive(defmt::Format)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum NewChannelError { NoFreeChannels, } diff --git a/embassy-nrf/src/interrupt.rs b/embassy-nrf/src/interrupt.rs index 581adff7..8918b13e 100644 --- a/embassy-nrf/src/interrupt.rs +++ b/embassy-nrf/src/interrupt.rs @@ -12,7 +12,8 @@ pub use crate::pac::Interrupt; pub use crate::pac::Interrupt::*; // needed for cortex-m-rt #[interrupt] pub use bare_metal::{CriticalSection, Mutex}; -#[derive(defmt::Format, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[repr(u8)] pub enum Priority { Level0 = 0, diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs index 575405b5..13b06397 100644 --- a/embassy-nrf/src/uarte.rs +++ b/embassy-nrf/src/uarte.rs @@ -28,7 +28,8 @@ pub use uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity}; use embassy::io::{AsyncBufRead, AsyncWrite, Result}; use embassy::util::WakerStore; -use defmt::trace; +use anyfmt::panic; +use anyfmt::*; //use crate::trace; diff --git a/embassy/Cargo.toml b/embassy/Cargo.toml index 7e9ea91d..6c930976 100644 --- a/embassy/Cargo.toml +++ b/embassy/Cargo.toml @@ -6,7 +6,6 @@ edition = "2018" [features] std = ["futures/std"] -defmt-default = [] defmt-trace = [] defmt-debug = [] defmt-info = [] @@ -14,9 +13,12 @@ defmt-warn = [] defmt-error = [] [dependencies] -defmt = "0.1.0" +anyfmt = { version = "0.1.0", path = "../anyfmt" } +defmt = { version = "0.1.0", optional = true } + cortex-m = "0.6.3" futures = { version = "0.3.5", default-features = false } pin-project = { version = "0.4.23", default-features = false } futures-intrusive = { version = "0.3.1", default-features = false } embassy-macros = { version = "0.1.0", path = "../embassy-macros"} + diff --git a/embassy/src/executor/executor.rs b/embassy/src/executor/executor.rs index ff3a8517..4d73cf9f 100644 --- a/embassy/src/executor/executor.rs +++ b/embassy/src/executor/executor.rs @@ -63,7 +63,8 @@ pub struct Task { future: UninitCell, // Valid if STATE_RUNNING } -#[derive(Copy, Clone, Debug, defmt::Format)] +#[derive(Copy, Clone, Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum SpawnError { Busy, } diff --git a/embassy/src/flash.rs b/embassy/src/flash.rs index bf6d5980..ca9fb595 100644 --- a/embassy/src/flash.rs +++ b/embassy/src/flash.rs @@ -1,7 +1,7 @@ - use core::future::Future; -#[derive(defmt::Format, Copy, Clone, Debug, Eq, PartialEq)] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum Error { Failed, AddressMisaligned, @@ -48,4 +48,3 @@ pub trait Flash { /// This is guaranteed to be a power of 2. fn erase_size(&self) -> usize; } - diff --git a/embassy/src/io/error.rs b/embassy/src/io/error.rs index 8fa1d93f..8bad0cdb 100644 --- a/embassy/src/io/error.rs +++ b/embassy/src/io/error.rs @@ -2,7 +2,8 @@ /// /// This list is intended to grow over time and it is not recommended to /// exhaustively match against it. -#[derive(defmt::Format, Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum Error { /// An entity was not found, often a file. NotFound, diff --git a/embassy/src/rand.rs b/embassy/src/rand.rs index bb6cd9d3..dbabb6f2 100644 --- a/embassy/src/rand.rs +++ b/embassy/src/rand.rs @@ -1,4 +1,5 @@ -use crate::util::Dewrap; +use anyfmt::*; + pub trait Rand { fn rand(&self, buf: &mut [u8]); } @@ -10,5 +11,5 @@ pub unsafe fn set_rand(rand: &'static dyn Rand) { } pub fn rand(buf: &mut [u8]) { - unsafe { RAND.dexpect(defmt::intern!("No rand set")).rand(buf) } + unsafe { expect!(RAND, "No rand set").rand(buf) } } diff --git a/embassy/src/time/duration.rs b/embassy/src/time/duration.rs index 604a1750..8135961e 100644 --- a/embassy/src/time/duration.rs +++ b/embassy/src/time/duration.rs @@ -3,7 +3,8 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; use super::TICKS_PER_SECOND; -#[derive(defmt::Format, Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct Duration { pub(crate) ticks: u64, } diff --git a/embassy/src/time/instant.rs b/embassy/src/time/instant.rs index a7f268e1..75098081 100644 --- a/embassy/src/time/instant.rs +++ b/embassy/src/time/instant.rs @@ -5,7 +5,8 @@ use core::ops::{Add, AddAssign, Sub, SubAssign}; use super::TICKS_PER_SECOND; use super::{now, Duration}; -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, defmt::Format)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct Instant { ticks: u64, } diff --git a/embassy/src/time/mod.rs b/embassy/src/time/mod.rs index da2e1c21..a3792e3c 100644 --- a/embassy/src/time/mod.rs +++ b/embassy/src/time/mod.rs @@ -8,7 +8,7 @@ pub use instant::Instant; pub use timer::Timer; pub use traits::*; -use crate::util::Dewrap; +use anyfmt::*; // TODO allow customizing, probably via Cargo features `tick-hz-32768` or something. pub const TICKS_PER_SECOND: u64 = 32768; @@ -20,5 +20,5 @@ pub unsafe fn set_clock(clock: &'static dyn Clock) { } pub(crate) fn now() -> u64 { - unsafe { CLOCK.dexpect(defmt::intern!("No clock set")).now() } + unsafe { expect!(CLOCK, "No clock set").now() } } diff --git a/embassy/src/util/drop_bomb.rs b/embassy/src/util/drop_bomb.rs index 2a995a82..0ef051cf 100644 --- a/embassy/src/util/drop_bomb.rs +++ b/embassy/src/util/drop_bomb.rs @@ -1,3 +1,4 @@ +use anyfmt::panic; use core::mem; pub struct DropBomb { @@ -16,6 +17,6 @@ impl DropBomb { impl Drop for DropBomb { fn drop(&mut self) { - depanic!("boom") + panic!("boom") } } diff --git a/embassy/src/util/macros.rs b/embassy/src/util/macros.rs deleted file mode 100644 index 3773af69..00000000 --- a/embassy/src/util/macros.rs +++ /dev/null @@ -1,34 +0,0 @@ -#![macro_use] - -#[macro_export] -macro_rules! depanic { - ($( $i:expr ),*) => { - { - defmt::error!($( $i ),*); - panic!(); - } - } -} - -#[macro_export] -macro_rules! deassert { - ($cond:expr) => { - deassert!($cond, "assertion failed"); - }; - ($cond:expr, $msg:literal) => { - { - if !$cond { - defmt::error!($msg); - panic!(); - } - } - }; - ($cond:expr, $msg:literal, $( $i:expr ),*) => { - { - if !$cond { - defmt::error!($msg, $( $i ),*); - panic!(); - } - } - }; -} diff --git a/embassy/src/util/mod.rs b/embassy/src/util/mod.rs index 9f44c08c..94745b83 100644 --- a/embassy/src/util/mod.rs +++ b/embassy/src/util/mod.rs @@ -1,74 +1,11 @@ -#![macro_use] - -mod macros; - -mod signal; -pub use signal::*; -mod portal; -pub use portal::*; -mod waker_store; -pub use waker_store::*; mod drop_bomb; -pub use drop_bomb::*; mod forever; +mod portal; +mod signal; +mod waker_store; + +pub use drop_bomb::*; pub use forever::*; - -use defmt::{debug, error, info, intern, trace, warn}; - -pub use macros::*; - -pub trait Dewrap { - /// dewrap = defmt unwrap - fn dewrap(self) -> T; - - /// dexpect = defmt expect - fn dexpect(self, msg: M) -> T; - - fn dewarn(self, msg: M) -> Self; -} - -impl Dewrap for Option { - fn dewrap(self) -> T { - match self { - Some(t) => t, - None => depanic!("unwrap failed: enum is none"), - } - } - - fn dexpect(self, msg: M) -> T { - match self { - Some(t) => t, - None => depanic!("unexpected None: {:?}", msg), - } - } - - fn dewarn(self, msg: M) -> Self { - if self.is_none() { - warn!("{:?} is none", msg); - } - self - } -} - -impl Dewrap for Result { - fn dewrap(self) -> T { - match self { - Ok(t) => t, - Err(e) => depanic!("unwrap failed: {:?}", e), - } - } - - fn dexpect(self, msg: M) -> T { - match self { - Ok(t) => t, - Err(e) => depanic!("unexpected error: {:?}: {:?}", msg, e), - } - } - - fn dewarn(self, msg: M) -> Self { - if let Err(e) = &self { - warn!("{:?} err: {:?}", msg, e); - } - self - } -} +pub use portal::*; +pub use signal::*; +pub use waker_store::*; diff --git a/embassy/src/util/portal.rs b/embassy/src/util/portal.rs index e01968c5..8f5d812f 100644 --- a/embassy/src/util/portal.rs +++ b/embassy/src/util/portal.rs @@ -1,3 +1,4 @@ +use anyfmt::panic; use core::cell::UnsafeCell; use core::future::Future; use core::mem; @@ -27,7 +28,7 @@ impl Portal { unsafe { match *self.state.get() { State::None => {} - State::Running => depanic!("Portall::call() called reentrantly"), + State::Running => panic!("Portall::call() called reentrantly"), State::Waiting(func) => (*func)(val), } } @@ -58,7 +59,7 @@ impl Portal { let state = &mut *self.state.get(); match state { State::None => {} - _ => depanic!("Multiple tasks waiting on same portal"), + _ => panic!("Multiple tasks waiting on same portal"), } *state = State::Waiting(func_ptr); } @@ -110,7 +111,7 @@ impl Portal { let state = &mut *self.state.get(); match *state { State::None => {} - _ => depanic!("Multiple tasks waiting on same portal"), + _ => panic!("Multiple tasks waiting on same portal"), } *state = State::Waiting(func_ptr); } diff --git a/embassy/src/util/signal.rs b/embassy/src/util/signal.rs index 32286a30..78372524 100644 --- a/embassy/src/util/signal.rs +++ b/embassy/src/util/signal.rs @@ -1,3 +1,4 @@ +use anyfmt::panic; use core::cell::UnsafeCell; use core::future::Future; use core::mem; @@ -58,7 +59,7 @@ impl<'a, T: Send> Future for WaitFuture<'a, T> { Poll::Pending } State::Waiting(w) if w.will_wake(cx.waker()) => Poll::Pending, - State::Waiting(_) => depanic!("waker overflow"), + State::Waiting(_) => panic!("waker overflow"), State::Signaled(_) => match mem::replace(state, State::None) { State::Signaled(res) => Poll::Ready(res), _ => unreachable!(), diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 8eb7a5ff..719f7326 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -17,14 +17,17 @@ defmt-error = [] [dependencies] +embassy = { version = "0.1.0", path = "../embassy", features = ["defmt"] } +embassy-nrf = { version = "0.1.0", path = "../embassy-nrf", features = ["defmt", "defmt-trace", "52840"] } +anyfmt = { version = "0.1.0", path = "../anyfmt", features = ["defmt"] } + +defmt = "0.1.0" +defmt-rtt = "0.1.0" + cortex-m = { version = "0.6.3" } cortex-m-rt = "0.6.12" -defmt = "0.1.0" embedded-hal = { version = "0.2.4" } -defmt-rtt = "0.1.0" panic-probe = "0.1.0" nrf52840-hal = { version = "0.11.0" } -embassy = { version = "0.1.0", path = "../embassy" } -embassy-nrf = { version = "0.1.0", path = "../embassy-nrf", features = ["defmt-trace", "52840"] } futures = { version = "0.3.7", default-features = false, features = ["async-await"] } -cortex-m-rtic = { git = "https://github.com/rtic-rs/cortex-m-rtic", branch = "master"} \ No newline at end of file +cortex-m-rtic = { git = "https://github.com/rtic-rs/cortex-m-rtic", branch = "master"} diff --git a/examples/src/bin/gpiote.rs b/examples/src/bin/gpiote.rs index 16b4f06d..d8394155 100644 --- a/examples/src/bin/gpiote.rs +++ b/examples/src/bin/gpiote.rs @@ -15,7 +15,7 @@ use embassy_nrf::gpiote; #[task] async fn run() { - let p = embassy_nrf::pac::Peripherals::take().dewrap(); + let p = unwrap!(embassy_nrf::pac::Peripherals::take()); let port0 = gpio::p0::Parts::new(p.P0); let g = gpiote::Gpiote::new(p.GPIOTE); @@ -24,9 +24,7 @@ async fn run() { let pin1 = port0.p0_11.into_pullup_input().degrade(); let button1 = async { - let ch = g - .new_input_channel(pin1, gpiote::EventPolarity::HiToLo) - .dewrap(); + let ch = unwrap!(g.new_input_channel(pin1, gpiote::EventPolarity::HiToLo)); loop { ch.wait().await; @@ -36,9 +34,7 @@ async fn run() { let pin2 = port0.p0_12.into_pullup_input().degrade(); let button2 = async { - let ch = g - .new_input_channel(pin2, gpiote::EventPolarity::LoToHi) - .dewrap(); + let ch = unwrap!(g.new_input_channel(pin2, gpiote::EventPolarity::LoToHi)); loop { ch.wait().await; @@ -48,9 +44,7 @@ async fn run() { let pin3 = port0.p0_24.into_pullup_input().degrade(); let button3 = async { - let ch = g - .new_input_channel(pin3, gpiote::EventPolarity::Toggle) - .dewrap(); + let ch = unwrap!(g.new_input_channel(pin3, gpiote::EventPolarity::Toggle)); loop { ch.wait().await; @@ -60,9 +54,7 @@ async fn run() { let pin4 = port0.p0_25.into_pullup_input().degrade(); let button4 = async { - let ch = g - .new_input_channel(pin4, gpiote::EventPolarity::Toggle) - .dewrap(); + let ch = unwrap!(g.new_input_channel(pin4, gpiote::EventPolarity::Toggle)); loop { ch.wait().await; @@ -79,8 +71,8 @@ static EXECUTOR: Forever = Forever::new(); fn main() -> ! { info!("Hello World!"); - let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); - executor.spawn(run()).dewrap(); + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev)); + unwrap!(executor.spawn(run())); loop { executor.run(); diff --git a/examples/src/bin/multiprio.rs b/examples/src/bin/multiprio.rs index dbca491e..e73747ac 100644 --- a/examples/src/bin/multiprio.rs +++ b/examples/src/bin/multiprio.rs @@ -120,7 +120,7 @@ static EXECUTOR_HIGH: Forever>> = Forever::n fn main() -> ! { info!("Hello World!"); - let p = embassy_nrf::pac::Peripherals::take().dewrap(); + let p = unwrap!(embassy_nrf::pac::Peripherals::take()); clocks::Clocks::new(p.CLOCK) .enable_ext_hfosc() @@ -132,17 +132,21 @@ fn main() -> ! { unsafe { embassy::time::set_clock(rtc) }; let executor_low = EXECUTOR_LOW.put(TimerExecutor::new(rtc.alarm0(), cortex_m::asm::sev)); - let executor_med = EXECUTOR_MED.put(TimerExecutor::new(rtc.alarm1(), cortex_m::asm::sev)); - let executor_high = EXECUTOR_HIGH.put(TimerExecutor::new(rtc.alarm2(), cortex_m::asm::sev)); + let executor_med = EXECUTOR_MED.put(TimerExecutor::new(rtc.alarm1(), || { + interrupt::pend(interrupt::SWI0_EGU0) + })); + let executor_high = EXECUTOR_HIGH.put(TimerExecutor::new(rtc.alarm2(), || { + interrupt::pend(interrupt::SWI1_EGU1) + })); interrupt::set_priority(interrupt::SWI0_EGU0, interrupt::Priority::Level7); interrupt::set_priority(interrupt::SWI1_EGU1, interrupt::Priority::Level6); interrupt::enable(interrupt::SWI0_EGU0); interrupt::enable(interrupt::SWI1_EGU1); - executor_low.spawn(run_low()).dewrap(); - executor_med.spawn(run_med()).dewrap(); - executor_high.spawn(run_high()).dewrap(); + unwrap!(executor_low.spawn(run_low())); + unwrap!(executor_med.spawn(run_med())); + unwrap!(executor_high.spawn(run_high())); loop { executor_low.run(); diff --git a/examples/src/bin/qspi.rs b/examples/src/bin/qspi.rs index caabac8b..a7f668ca 100644 --- a/examples/src/bin/qspi.rs +++ b/examples/src/bin/qspi.rs @@ -6,6 +6,7 @@ mod example_common; use example_common::*; +use anyfmt::panic; use cortex_m_rt::entry; use nrf52840_hal::gpio; @@ -23,7 +24,7 @@ struct AlignedBuf([u8; 4096]); #[task] async fn run() { - let p = embassy_nrf::pac::Peripherals::take().dewrap(); + let p = unwrap!(embassy_nrf::pac::Peripherals::take()); let port0 = gpio::p0::Parts::new(p.P0); @@ -121,8 +122,8 @@ static EXECUTOR: Forever = Forever::new(); fn main() -> ! { info!("Hello World!"); - let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); - executor.spawn(run()).dewrap(); + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev)); + unwrap!(executor.spawn(run())); loop { executor.run(); diff --git a/examples/src/bin/rtc_async.rs b/examples/src/bin/rtc_async.rs index 5126a2cc..b4ee736b 100644 --- a/examples/src/bin/rtc_async.rs +++ b/examples/src/bin/rtc_async.rs @@ -39,7 +39,7 @@ static EXECUTOR: Forever>> = Forever::new(); fn main() -> ! { info!("Hello World!"); - let p = embassy_nrf::pac::Peripherals::take().dewrap(); + let p = unwrap!(embassy_nrf::pac::Peripherals::take()); clocks::Clocks::new(p.CLOCK) .enable_ext_hfosc() @@ -53,8 +53,8 @@ fn main() -> ! { let executor = EXECUTOR.put(TimerExecutor::new(rtc.alarm0(), cortex_m::asm::sev)); - executor.spawn(run1()).dewrap(); - executor.spawn(run2()).dewrap(); + unwrap!(executor.spawn(run1())); + unwrap!(executor.spawn(run2())); loop { executor.run(); diff --git a/examples/src/bin/rtc_raw.rs b/examples/src/bin/rtc_raw.rs index 4453ecae..ad5fab24 100644 --- a/examples/src/bin/rtc_raw.rs +++ b/examples/src/bin/rtc_raw.rs @@ -18,7 +18,7 @@ static mut RTC: MaybeUninit> = MaybeUninit::uni fn main() -> ! { info!("Hello World!"); - let p = embassy_nrf::pac::Peripherals::take().dewrap(); + let p = unwrap!(embassy_nrf::pac::Peripherals::take()); clocks::Clocks::new(p.CLOCK) .enable_ext_hfosc() diff --git a/examples/src/bin/uart.rs b/examples/src/bin/uart.rs index eeaf5fee..553bbb35 100644 --- a/examples/src/bin/uart.rs +++ b/examples/src/bin/uart.rs @@ -17,7 +17,7 @@ use embassy_nrf::uarte; #[task] async fn run() { - let p = embassy_nrf::pac::Peripherals::take().dewrap(); + let p = unwrap!(embassy_nrf::pac::Peripherals::take()); let port0 = gpio::p0::Parts::new(p.P0); @@ -41,14 +41,14 @@ async fn run() { info!("uarte initialized!"); - u.write_all(b"Hello!\r\n").await.dewrap(); + unwrap!(u.write_all(b"Hello!\r\n").await); info!("wrote hello in uart!"); // Simple demo, reading 8-char chunks and echoing them back reversed. loop { info!("reading..."); let mut buf = [0u8; 8]; - u.read_exact(&mut buf).await.dewrap(); + unwrap!(u.read_exact(&mut buf).await); info!("read done, got {:[u8]}", buf); // Reverse buf @@ -59,7 +59,7 @@ async fn run() { } info!("writing..."); - u.write_all(&buf).await.dewrap(); + unwrap!(u.write_all(&buf).await); info!("write done"); } } @@ -70,8 +70,8 @@ static EXECUTOR: Forever = Forever::new(); fn main() -> ! { info!("Hello World!"); - let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); - executor.spawn(run()).dewrap(); + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev)); + unwrap!(executor.spawn(run())); loop { executor.run(); diff --git a/examples/src/example_common.rs b/examples/src/example_common.rs index 65bfe6bb..1a12fa69 100644 --- a/examples/src/example_common.rs +++ b/examples/src/example_common.rs @@ -4,7 +4,7 @@ use defmt_rtt as _; // global logger use nrf52840_hal as _; use panic_probe as _; -pub use defmt::{info, intern}; +pub use anyfmt::*; use core::sync::atomic::{AtomicUsize, Ordering}; @@ -16,52 +16,3 @@ fn timestamp() -> u64 { COUNT.store(n + 1, Ordering::Relaxed); n as u64 } - -macro_rules! depanic { - ($( $i:expr ),*) => { - { - defmt::error!($( $i ),*); - panic!(); - } - } -} - -pub trait Dewrap { - /// dewrap = defmt unwrap - fn dewrap(self) -> T; - - /// dexpect = defmt expect - fn dexpect(self, msg: M) -> T; -} - -impl Dewrap for Option { - fn dewrap(self) -> T { - match self { - Some(t) => t, - None => depanic!("Dewrap failed: enum is none"), - } - } - - fn dexpect(self, msg: M) -> T { - match self { - Some(t) => t, - None => depanic!("Unexpected None: {:?}", msg), - } - } -} - -impl Dewrap for Result { - fn dewrap(self) -> T { - match self { - Ok(t) => t, - Err(e) => depanic!("Dewrap failed: {:?}", e), - } - } - - fn dexpect(self, msg: M) -> T { - match self { - Ok(t) => t, - Err(e) => depanic!("Unexpected error: {:?}: {:?}", msg, e), - } - } -} diff --git a/test-build.sh b/test-build.sh index 8c93484d..0b905e2c 100755 --- a/test-build.sh +++ b/test-build.sh @@ -2,13 +2,19 @@ set -euxo pipefail -cargo build --target thumbv7em-none-eabihf -p embassy-examples --bins -cargo build --target thumbv7em-none-eabihf -p embassy +# examples +(cd examples; cargo build --target thumbv7em-none-eabihf --bins) -# Build with all feature combinations -cd embassy-nrf -cargo build --target thumbv7em-none-eabihf -p embassy-nrf --features 52810 -#cargo build --target thumbv7em-none-eabihf -p embassy-nrf --features 52811 # nrf52811-hal doesn't exist yet -cargo build --target thumbv7em-none-eabihf -p embassy-nrf --features 52832 -cargo build --target thumbv7em-none-eabihf -p embassy-nrf --features 52833 -cargo build --target thumbv7em-none-eabihf -p embassy-nrf --features 52840 +# embassy +(cd embassy; cargo build --target thumbv7em-none-eabihf) +(cd embassy; cargo build --target thumbv7em-none-eabihf --features defmt,anyfmt/defmt) +(cd embassy; cargo build --target thumbv7em-none-eabihf --features anyfmt/log) + +# embassy-nrf +(cd embassy-nrf; cargo build --target thumbv7em-none-eabihf --features 52810) +#(cd embassy-nrf; cargo build --target thumbv7em-none-eabihf --features 52811) # nrf52811-hal doesn't exist yet +(cd embassy-nrf; cargo build --target thumbv7em-none-eabihf --features 52832) +(cd embassy-nrf; cargo build --target thumbv7em-none-eabihf --features 52833) +(cd embassy-nrf; cargo build --target thumbv7em-none-eabihf --features 52840) + +(cd embassy-nrf; cargo build --target thumbv7em-none-eabihf --features 52840,defmt,embassy/defmt,anyfmt/defmt)