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

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::future::Future;
use core::ptr;
use core::task::{Context, Poll};
use defmt::{panic, *};
use embassy::util::Signal;
use crate::hal::gpio::{Input, Level, Output, Pin, Port};
@ -51,7 +51,8 @@ pub enum OutputChannelPolarity {
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 {
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 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)]
pub enum Priority {
Level0 = 0,

View File

@ -48,6 +48,9 @@ pub use nrf52833_hal as hal;
#[cfg(feature = "52840")]
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 interrupt;
#[cfg(feature = "52840")]

View File

@ -1,5 +1,5 @@
use crate::fmt::{assert, assert_eq, panic, *};
use core::future::Future;
use defmt::{assert, assert_eq, panic, *};
use crate::hal::gpio::{Output, Pin as GpioPin, Port as GpioPort, PushPull};
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::util::WakerStore;
use defmt::{assert, panic, todo, *};
use crate::fmt::{assert, panic, todo, *};
//use crate::trace;