Add support for log+defmt again, but better.

This commit is contained in:
Dario Nieuwenhuis 2020-12-01 17:46:56 +01:00
parent 78135a81d9
commit 6f76c0ebcc
22 changed files with 261 additions and 21 deletions

View File

@ -21,7 +21,8 @@ defmt-error = [ ]
[dependencies] [dependencies]
embassy = { version = "0.1.0", path = "../embassy" } embassy = { version = "0.1.0", path = "../embassy" }
defmt = { version = "0.1.2" } defmt = { version = "0.1.3", optional = true }
log = { version = "0.4.11", optional = true }
cortex-m-rt = "0.6.13" cortex-m-rt = "0.6.13"
cortex-m = { version = "0.6.4" } cortex-m = { version = "0.6.4" }
embedded-hal = { version = "0.2.4" } embedded-hal = { version = "0.2.4" }

110
embassy-nrf/src/fmt.rs Normal file
View File

@ -0,0 +1,110 @@
#![macro_use]
#[cfg(all(feature = "defmt", feature = "log"))]
compile_error!("You may not enable both `defmt` and `log` features.");
pub use fmt::*;
#[cfg(feature = "defmt")]
mod fmt {
pub use defmt::{
assert, assert_eq, assert_ne, debug, debug_assert, debug_assert_eq, debug_assert_ne, error,
info, panic, todo, trace, unreachable, unwrap, warn,
};
}
#[cfg(feature = "log")]
mod fmt {
pub use core::{
assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo,
unreachable,
};
pub use log::{debug, error, info, trace, warn};
}
#[cfg(not(any(feature = "defmt", feature = "log")))]
mod fmt {
#![macro_use]
pub use core::{
assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo,
unreachable,
};
#[macro_export]
macro_rules! trace {
($($msg:expr),*) => {
()
};
}
#[macro_export]
macro_rules! debug {
($($msg:expr),*) => {
()
};
}
#[macro_export]
macro_rules! info {
($($msg:expr),*) => {
()
};
}
#[macro_export]
macro_rules! warn {
($($msg:expr),*) => {
()
};
}
#[macro_export]
macro_rules! error {
($($msg:expr),*) => {
()
};
}
}
#[cfg(not(feature = "defmt"))]
#[macro_export]
macro_rules! unwrap {
($arg:expr$(,$msg:expr)*) => {
match $crate::fmt::Try::into_result($arg) {
::core::result::Result::Ok(t) => t,
::core::result::Result::Err(e) => {
panic!($($msg,)*);
}
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct NoneError;
pub trait Try {
type Ok;
type Error;
fn into_result(self) -> Result<Self::Ok, Self::Error>;
}
impl<T> Try for Option<T> {
type Ok = T;
type Error = NoneError;
#[inline]
fn into_result(self) -> Result<T, NoneError> {
self.ok_or(NoneError)
}
}
impl<T, E> Try for Result<T, E> {
type Ok = T;
type Error = E;
#[inline]
fn into_result(self) -> Self {
self
}
}

View File

@ -1,8 +1,8 @@
use crate::fmt::{panic, *};
use core::cell::Cell; use core::cell::Cell;
use core::future::Future; use core::future::Future;
use core::ptr; use core::ptr;
use core::task::{Context, Poll}; use core::task::{Context, Poll};
use defmt::{panic, *};
use embassy::util::Signal; use embassy::util::Signal;
use crate::hal::gpio::{Input, Level, Output, Pin, Port}; use crate::hal::gpio::{Input, Level, Output, Pin, Port};
@ -51,7 +51,8 @@ pub enum OutputChannelPolarity {
Toggle, Toggle,
} }
#[derive(Debug, Copy, Clone, Eq, PartialEq, defmt::Format)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum NewChannelError { pub enum NewChannelError {
NoFreeChannels, NoFreeChannels,
} }

View File

@ -12,7 +12,8 @@ pub use crate::pac::Interrupt;
pub use crate::pac::Interrupt::*; // needed for cortex-m-rt #[interrupt] pub use crate::pac::Interrupt::*; // needed for cortex-m-rt #[interrupt]
pub use cortex_m::interrupt::{CriticalSection, Mutex}; pub use cortex_m::interrupt::{CriticalSection, Mutex};
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, defmt::Format)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)] #[repr(u8)]
pub enum Priority { pub enum Priority {
Level0 = 0, Level0 = 0,

View File

@ -48,6 +48,9 @@ pub use nrf52833_hal as hal;
#[cfg(feature = "52840")] #[cfg(feature = "52840")]
pub use nrf52840_hal as hal; pub use nrf52840_hal as hal;
// This mod MUST go first, so that the others see its macros.
pub(crate) mod fmt;
pub mod gpiote; pub mod gpiote;
pub mod interrupt; pub mod interrupt;
#[cfg(feature = "52840")] #[cfg(feature = "52840")]

View File

@ -1,5 +1,5 @@
use crate::fmt::{assert, assert_eq, panic, *};
use core::future::Future; use core::future::Future;
use defmt::{assert, assert_eq, panic, *};
use crate::hal::gpio::{Output, Pin as GpioPin, Port as GpioPort, PushPull}; use crate::hal::gpio::{Output, Pin as GpioPin, Port as GpioPort, PushPull};
use crate::pac::{Interrupt, QSPI}; use crate::pac::{Interrupt, QSPI};

View File

@ -28,7 +28,7 @@ pub use uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity};
use embassy::io::{AsyncBufRead, AsyncWrite, Result}; use embassy::io::{AsyncBufRead, AsyncWrite, Result};
use embassy::util::WakerStore; use embassy::util::WakerStore;
use defmt::{assert, panic, todo, *}; use crate::fmt::{assert, panic, todo, *};
//use crate::trace; //use crate::trace;

View File

@ -13,7 +13,8 @@ defmt-warn = []
defmt-error = [] defmt-error = []
[dependencies] [dependencies]
defmt = { version = "0.1.0" } defmt = { version = "0.1.3", optional = true }
log = { version = "0.4.11", optional = true }
cortex-m = "0.6.4" cortex-m = "0.6.4"
futures = { version = "0.3.5", default-features = false } futures = { version = "0.3.5", default-features = false }

View File

@ -63,7 +63,8 @@ pub struct Task<F: Future + 'static> {
future: UninitCell<F>, // Valid if STATE_RUNNING future: UninitCell<F>, // Valid if STATE_RUNNING
} }
#[derive(Copy, Clone, Debug, defmt::Format)] #[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SpawnError { pub enum SpawnError {
Busy, Busy,
} }

View File

@ -1,6 +1,7 @@
use core::future::Future; use core::future::Future;
#[derive(Copy, Clone, Debug, Eq, PartialEq, defmt::Format)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error { pub enum Error {
Failed, Failed,
AddressMisaligned, AddressMisaligned,

110
embassy/src/fmt.rs Normal file
View File

@ -0,0 +1,110 @@
#![macro_use]
#[cfg(all(feature = "defmt", feature = "log"))]
compile_error!("You may not enable both `defmt` and `log` features.");
pub use fmt::*;
#[cfg(feature = "defmt")]
mod fmt {
pub use defmt::{
assert, assert_eq, assert_ne, debug, debug_assert, debug_assert_eq, debug_assert_ne, error,
info, panic, todo, trace, unreachable, unwrap, warn,
};
}
#[cfg(feature = "log")]
mod fmt {
pub use core::{
assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo,
unreachable,
};
pub use log::{debug, error, info, trace, warn};
}
#[cfg(not(any(feature = "defmt", feature = "log")))]
mod fmt {
#![macro_use]
pub use core::{
assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo,
unreachable,
};
#[macro_export]
macro_rules! trace {
($($msg:expr),*) => {
()
};
}
#[macro_export]
macro_rules! debug {
($($msg:expr),*) => {
()
};
}
#[macro_export]
macro_rules! info {
($($msg:expr),*) => {
()
};
}
#[macro_export]
macro_rules! warn {
($($msg:expr),*) => {
()
};
}
#[macro_export]
macro_rules! error {
($($msg:expr),*) => {
()
};
}
}
#[cfg(not(feature = "defmt"))]
#[macro_export]
macro_rules! unwrap {
($arg:expr$(,$msg:expr)*) => {
match $crate::fmt::Try::into_result($arg) {
::core::result::Result::Ok(t) => t,
::core::result::Result::Err(e) => {
panic!($($msg,)*);
}
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct NoneError;
pub trait Try {
type Ok;
type Error;
fn into_result(self) -> Result<Self::Ok, Self::Error>;
}
impl<T> Try for Option<T> {
type Ok = T;
type Error = NoneError;
#[inline]
fn into_result(self) -> Result<T, NoneError> {
self.ok_or(NoneError)
}
}
impl<T, E> Try for Result<T, E> {
type Ok = T;
type Error = E;
#[inline]
fn into_result(self) -> Self {
self
}
}

View File

@ -2,7 +2,8 @@
/// ///
/// This list is intended to grow over time and it is not recommended to /// This list is intended to grow over time and it is not recommended to
/// exhaustively match against it. /// exhaustively match against it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, defmt::Format)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error { pub enum Error {
/// An entity was not found, often a file. /// An entity was not found, often a file.
NotFound, NotFound,

View File

@ -4,6 +4,9 @@
#![feature(const_fn)] #![feature(const_fn)]
#![feature(const_fn_fn_ptr_basics)] #![feature(const_fn_fn_ptr_basics)]
// This mod MUST go first, so that the others see its macros.
pub(crate) mod fmt;
pub mod executor; pub mod executor;
pub mod flash; pub mod flash;
pub mod io; pub mod io;

View File

@ -1,4 +1,4 @@
use defmt::*; use crate::fmt::*;
pub trait Rand { pub trait Rand {
fn rand(&self, buf: &mut [u8]); fn rand(&self, buf: &mut [u8]);

View File

@ -3,7 +3,8 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use super::TICKS_PER_SECOND; use super::TICKS_PER_SECOND;
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, defmt::Format)] #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Duration { pub struct Duration {
pub(crate) ticks: u64, pub(crate) ticks: u64,
} }

View File

@ -5,7 +5,8 @@ use core::ops::{Add, AddAssign, Sub, SubAssign};
use super::TICKS_PER_SECOND; use super::TICKS_PER_SECOND;
use super::{now, Duration}; 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 { pub struct Instant {
ticks: u64, ticks: u64,
} }

View File

@ -8,7 +8,7 @@ pub use instant::Instant;
pub use timer::Timer; pub use timer::Timer;
pub use traits::*; pub use traits::*;
use defmt::*; use crate::fmt::*;
// TODO allow customizing, probably via Cargo features `tick-hz-32768` or something. // TODO allow customizing, probably via Cargo features `tick-hz-32768` or something.
pub const TICKS_PER_SECOND: u64 = 32768; pub const TICKS_PER_SECOND: u64 = 32768;

View File

@ -1,5 +1,5 @@
use crate::fmt::panic;
use core::mem; use core::mem;
use defmt::panic;
pub struct DropBomb { pub struct DropBomb {
_private: (), _private: (),

View File

@ -1,8 +1,8 @@
use crate::fmt::panic;
use core::cell::UnsafeCell; use core::cell::UnsafeCell;
use core::future::Future; use core::future::Future;
use core::mem; use core::mem;
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
use defmt::panic;
use crate::util::*; use crate::util::*;

View File

@ -1,8 +1,8 @@
use crate::fmt::panic;
use core::cell::UnsafeCell; use core::cell::UnsafeCell;
use core::future::Future; use core::future::Future;
use core::mem; use core::mem;
use core::task::{Context, Poll, Waker}; use core::task::{Context, Poll, Waker};
use defmt::panic;
pub struct Signal<T> { pub struct Signal<T> {
state: UnsafeCell<State<T>>, state: UnsafeCell<State<T>>,

View File

@ -17,14 +17,14 @@ defmt-error = []
[dependencies] [dependencies]
embassy = { version = "0.1.0", path = "../embassy", features = ["defmt-trace"] } embassy = { version = "0.1.0", path = "../embassy", features = ["defmt", "defmt-trace"] }
embassy-nrf = { version = "0.1.0", path = "../embassy-nrf", features = ["defmt-trace", "52840"] } embassy-nrf = { version = "0.1.0", path = "../embassy-nrf", features = ["defmt", "defmt-trace", "52840"] }
defmt = "0.1.2" defmt = "0.1.3"
defmt-rtt = "0.1.0" defmt-rtt = "0.1.0"
cortex-m = { version = "0.6.3" } cortex-m = { version = "0.6.3" }
cortex-m-rt = "0.6.12" cortex-m-rt = "0.6.13"
embedded-hal = { version = "0.2.4" } embedded-hal = { version = "0.2.4" }
panic-probe = "0.1.0" panic-probe = "0.1.0"
nrf52840-hal = { version = "0.12.0" } nrf52840-hal = { version = "0.12.0" }

View File

@ -7,6 +7,8 @@ set -euxo pipefail
# embassy # embassy
(cd embassy; cargo build --target thumbv7em-none-eabihf) (cd embassy; cargo build --target thumbv7em-none-eabihf)
(cd embassy; cargo build --target thumbv7em-none-eabihf --features log)
(cd embassy; cargo build --target thumbv7em-none-eabihf --features defmt)
# embassy-nrf # embassy-nrf
(cd embassy-nrf; cargo build --target thumbv7em-none-eabi --features 52810) (cd embassy-nrf; cargo build --target thumbv7em-none-eabi --features 52810)
@ -15,3 +17,6 @@ set -euxo pipefail
(cd embassy-nrf; cargo build --target thumbv7em-none-eabihf --features 52833) (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)
(cd embassy-nrf; cargo build --target thumbv7em-none-eabihf --features 52840,log)
(cd embassy-nrf; cargo build --target thumbv7em-none-eabihf --features 52840,defmt)