Add support for log+defmt again, but better.
This commit is contained in:
@ -21,7 +21,8 @@ defmt-error = [ ]
|
||||
[dependencies]
|
||||
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 = { version = "0.6.4" }
|
||||
embedded-hal = { version = "0.2.4" }
|
||||
|
110
embassy-nrf/src/fmt.rs
Normal file
110
embassy-nrf/src/fmt.rs
Normal 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
|
||||
}
|
||||
}
|
@ -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,
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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")]
|
||||
|
@ -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};
|
||||
|
@ -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;
|
||||
|
||||
|
Reference in New Issue
Block a user