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

@ -13,7 +13,8 @@ defmt-warn = []
defmt-error = []
[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"
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
}
#[derive(Copy, Clone, Debug, defmt::Format)]
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SpawnError {
Busy,
}

View File

@ -1,6 +1,7 @@
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 {
Failed,
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
/// 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 {
/// An entity was not found, often a file.
NotFound,

View File

@ -4,6 +4,9 @@
#![feature(const_fn)]
#![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 flash;
pub mod io;

View File

@ -1,4 +1,4 @@
use defmt::*;
use crate::fmt::*;
pub trait Rand {
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;
#[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(crate) ticks: u64,
}

View File

@ -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,
}

View File

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

View File

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

View File

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

View File

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