diff options
| author | Dario Nieuwenhuis <[email protected]> | 2020-12-01 17:46:56 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2020-12-01 17:46:56 +0100 |
| commit | 6f76c0ebccf1d3d7b8712eaa145f6033b277a6ae (patch) | |
| tree | b85002c833e5100080050c259754ad7b2945ef60 /embassy-nrf/src | |
| parent | 78135a81d96a8bb74207b3f73c9f261aa4561a18 (diff) | |
Add support for log+defmt again, but better.
Diffstat (limited to 'embassy-nrf/src')
| -rw-r--r-- | embassy-nrf/src/fmt.rs | 110 | ||||
| -rw-r--r-- | embassy-nrf/src/gpiote.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/interrupt.rs | 3 | ||||
| -rw-r--r-- | embassy-nrf/src/lib.rs | 3 | ||||
| -rw-r--r-- | embassy-nrf/src/qspi.rs | 2 | ||||
| -rw-r--r-- | embassy-nrf/src/uarte.rs | 2 |
6 files changed, 120 insertions, 5 deletions
diff --git a/embassy-nrf/src/fmt.rs b/embassy-nrf/src/fmt.rs new file mode 100644 index 000000000..93dda67d8 --- /dev/null +++ b/embassy-nrf/src/fmt.rs | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | #![macro_use] | ||
| 2 | |||
| 3 | #[cfg(all(feature = "defmt", feature = "log"))] | ||
| 4 | compile_error!("You may not enable both `defmt` and `log` features."); | ||
| 5 | |||
| 6 | pub use fmt::*; | ||
| 7 | |||
| 8 | #[cfg(feature = "defmt")] | ||
| 9 | mod fmt { | ||
| 10 | pub use defmt::{ | ||
| 11 | assert, assert_eq, assert_ne, debug, debug_assert, debug_assert_eq, debug_assert_ne, error, | ||
| 12 | info, panic, todo, trace, unreachable, unwrap, warn, | ||
| 13 | }; | ||
| 14 | } | ||
| 15 | |||
| 16 | #[cfg(feature = "log")] | ||
| 17 | mod fmt { | ||
| 18 | pub use core::{ | ||
| 19 | assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo, | ||
| 20 | unreachable, | ||
| 21 | }; | ||
| 22 | pub use log::{debug, error, info, trace, warn}; | ||
| 23 | } | ||
| 24 | |||
| 25 | #[cfg(not(any(feature = "defmt", feature = "log")))] | ||
| 26 | mod fmt { | ||
| 27 | #![macro_use] | ||
| 28 | |||
| 29 | pub use core::{ | ||
| 30 | assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo, | ||
| 31 | unreachable, | ||
| 32 | }; | ||
| 33 | |||
| 34 | #[macro_export] | ||
| 35 | macro_rules! trace { | ||
| 36 | ($($msg:expr),*) => { | ||
| 37 | () | ||
| 38 | }; | ||
| 39 | } | ||
| 40 | |||
| 41 | #[macro_export] | ||
| 42 | macro_rules! debug { | ||
| 43 | ($($msg:expr),*) => { | ||
| 44 | () | ||
| 45 | }; | ||
| 46 | } | ||
| 47 | |||
| 48 | #[macro_export] | ||
| 49 | macro_rules! info { | ||
| 50 | ($($msg:expr),*) => { | ||
| 51 | () | ||
| 52 | }; | ||
| 53 | } | ||
| 54 | |||
| 55 | #[macro_export] | ||
| 56 | macro_rules! warn { | ||
| 57 | ($($msg:expr),*) => { | ||
| 58 | () | ||
| 59 | }; | ||
| 60 | } | ||
| 61 | |||
| 62 | #[macro_export] | ||
| 63 | macro_rules! error { | ||
| 64 | ($($msg:expr),*) => { | ||
| 65 | () | ||
| 66 | }; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | #[cfg(not(feature = "defmt"))] | ||
| 71 | #[macro_export] | ||
| 72 | macro_rules! unwrap { | ||
| 73 | ($arg:expr$(,$msg:expr)*) => { | ||
| 74 | match $crate::fmt::Try::into_result($arg) { | ||
| 75 | ::core::result::Result::Ok(t) => t, | ||
| 76 | ::core::result::Result::Err(e) => { | ||
| 77 | panic!($($msg,)*); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] | ||
| 84 | pub struct NoneError; | ||
| 85 | |||
| 86 | pub trait Try { | ||
| 87 | type Ok; | ||
| 88 | type Error; | ||
| 89 | fn into_result(self) -> Result<Self::Ok, Self::Error>; | ||
| 90 | } | ||
| 91 | |||
| 92 | impl<T> Try for Option<T> { | ||
| 93 | type Ok = T; | ||
| 94 | type Error = NoneError; | ||
| 95 | |||
| 96 | #[inline] | ||
| 97 | fn into_result(self) -> Result<T, NoneError> { | ||
| 98 | self.ok_or(NoneError) | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | impl<T, E> Try for Result<T, E> { | ||
| 103 | type Ok = T; | ||
| 104 | type Error = E; | ||
| 105 | |||
| 106 | #[inline] | ||
| 107 | fn into_result(self) -> Self { | ||
| 108 | self | ||
| 109 | } | ||
| 110 | } | ||
diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs index bb15cca66..069e9140b 100644 --- a/embassy-nrf/src/gpiote.rs +++ b/embassy-nrf/src/gpiote.rs | |||
| @@ -1,8 +1,8 @@ | |||
| 1 | use crate::fmt::{panic, *}; | ||
| 1 | use core::cell::Cell; | 2 | use core::cell::Cell; |
| 2 | use core::future::Future; | 3 | use core::future::Future; |
| 3 | use core::ptr; | 4 | use core::ptr; |
| 4 | use core::task::{Context, Poll}; | 5 | use core::task::{Context, Poll}; |
| 5 | use defmt::{panic, *}; | ||
| 6 | use embassy::util::Signal; | 6 | use embassy::util::Signal; |
| 7 | 7 | ||
| 8 | use crate::hal::gpio::{Input, Level, Output, Pin, Port}; | 8 | use crate::hal::gpio::{Input, Level, Output, Pin, Port}; |
| @@ -51,7 +51,8 @@ pub enum OutputChannelPolarity { | |||
| 51 | Toggle, | 51 | Toggle, |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | #[derive(Debug, Copy, Clone, Eq, PartialEq, defmt::Format)] | 54 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 55 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 55 | pub enum NewChannelError { | 56 | pub enum NewChannelError { |
| 56 | NoFreeChannels, | 57 | NoFreeChannels, |
| 57 | } | 58 | } |
diff --git a/embassy-nrf/src/interrupt.rs b/embassy-nrf/src/interrupt.rs index 384426ea2..17fc9ab34 100644 --- a/embassy-nrf/src/interrupt.rs +++ b/embassy-nrf/src/interrupt.rs | |||
| @@ -12,7 +12,8 @@ pub use crate::pac::Interrupt; | |||
| 12 | pub use crate::pac::Interrupt::*; // needed for cortex-m-rt #[interrupt] | 12 | pub use crate::pac::Interrupt::*; // needed for cortex-m-rt #[interrupt] |
| 13 | pub use cortex_m::interrupt::{CriticalSection, Mutex}; | 13 | pub use cortex_m::interrupt::{CriticalSection, Mutex}; |
| 14 | 14 | ||
| 15 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, defmt::Format)] | 15 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] |
| 16 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 16 | #[repr(u8)] | 17 | #[repr(u8)] |
| 17 | pub enum Priority { | 18 | pub enum Priority { |
| 18 | Level0 = 0, | 19 | Level0 = 0, |
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs index 8efd6c568..7b3368d66 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs | |||
| @@ -48,6 +48,9 @@ pub use nrf52833_hal as hal; | |||
| 48 | #[cfg(feature = "52840")] | 48 | #[cfg(feature = "52840")] |
| 49 | pub use nrf52840_hal as hal; | 49 | pub use nrf52840_hal as hal; |
| 50 | 50 | ||
| 51 | // This mod MUST go first, so that the others see its macros. | ||
| 52 | pub(crate) mod fmt; | ||
| 53 | |||
| 51 | pub mod gpiote; | 54 | pub mod gpiote; |
| 52 | pub mod interrupt; | 55 | pub mod interrupt; |
| 53 | #[cfg(feature = "52840")] | 56 | #[cfg(feature = "52840")] |
diff --git a/embassy-nrf/src/qspi.rs b/embassy-nrf/src/qspi.rs index 75cbe7ddc..79fc7029c 100644 --- a/embassy-nrf/src/qspi.rs +++ b/embassy-nrf/src/qspi.rs | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | use crate::fmt::{assert, assert_eq, panic, *}; | ||
| 1 | use core::future::Future; | 2 | use core::future::Future; |
| 2 | use defmt::{assert, assert_eq, panic, *}; | ||
| 3 | 3 | ||
| 4 | use crate::hal::gpio::{Output, Pin as GpioPin, Port as GpioPort, PushPull}; | 4 | use crate::hal::gpio::{Output, Pin as GpioPin, Port as GpioPort, PushPull}; |
| 5 | use crate::pac::{Interrupt, QSPI}; | 5 | use crate::pac::{Interrupt, QSPI}; |
diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs index 0f2030c98..3a33e7597 100644 --- a/embassy-nrf/src/uarte.rs +++ b/embassy-nrf/src/uarte.rs | |||
| @@ -28,7 +28,7 @@ pub use uarte0::{baudrate::BAUDRATE_A as Baudrate, config::PARITY_A as Parity}; | |||
| 28 | use embassy::io::{AsyncBufRead, AsyncWrite, Result}; | 28 | use embassy::io::{AsyncBufRead, AsyncWrite, Result}; |
| 29 | use embassy::util::WakerStore; | 29 | use embassy::util::WakerStore; |
| 30 | 30 | ||
| 31 | use defmt::{assert, panic, todo, *}; | 31 | use crate::fmt::{assert, panic, todo, *}; |
| 32 | 32 | ||
| 33 | //use crate::trace; | 33 | //use crate::trace; |
| 34 | 34 | ||
