aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2020-12-01 17:46:56 +0100
committerDario Nieuwenhuis <[email protected]>2020-12-01 17:46:56 +0100
commit6f76c0ebccf1d3d7b8712eaa145f6033b277a6ae (patch)
treeb85002c833e5100080050c259754ad7b2945ef60 /embassy-nrf
parent78135a81d96a8bb74207b3f73c9f261aa4561a18 (diff)
Add support for log+defmt again, but better.
Diffstat (limited to 'embassy-nrf')
-rw-r--r--embassy-nrf/Cargo.toml3
-rw-r--r--embassy-nrf/src/fmt.rs110
-rw-r--r--embassy-nrf/src/gpiote.rs5
-rw-r--r--embassy-nrf/src/interrupt.rs3
-rw-r--r--embassy-nrf/src/lib.rs3
-rw-r--r--embassy-nrf/src/qspi.rs2
-rw-r--r--embassy-nrf/src/uarte.rs2
7 files changed, 122 insertions, 6 deletions
diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml
index d1538908d..7fe285c62 100644
--- a/embassy-nrf/Cargo.toml
+++ b/embassy-nrf/Cargo.toml
@@ -21,7 +21,8 @@ defmt-error = [ ]
21[dependencies] 21[dependencies]
22embassy = { version = "0.1.0", path = "../embassy" } 22embassy = { version = "0.1.0", path = "../embassy" }
23 23
24defmt = { version = "0.1.2" } 24defmt = { version = "0.1.3", optional = true }
25log = { version = "0.4.11", optional = true }
25cortex-m-rt = "0.6.13" 26cortex-m-rt = "0.6.13"
26cortex-m = { version = "0.6.4" } 27cortex-m = { version = "0.6.4" }
27embedded-hal = { version = "0.2.4" } 28embedded-hal = { version = "0.2.4" }
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"))]
4compile_error!("You may not enable both `defmt` and `log` features.");
5
6pub use fmt::*;
7
8#[cfg(feature = "defmt")]
9mod 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")]
17mod 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")))]
26mod 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]
72macro_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)]
84pub struct NoneError;
85
86pub trait Try {
87 type Ok;
88 type Error;
89 fn into_result(self) -> Result<Self::Ok, Self::Error>;
90}
91
92impl<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
102impl<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 @@
1use crate::fmt::{panic, *};
1use core::cell::Cell; 2use core::cell::Cell;
2use core::future::Future; 3use core::future::Future;
3use core::ptr; 4use core::ptr;
4use core::task::{Context, Poll}; 5use core::task::{Context, Poll};
5use defmt::{panic, *};
6use embassy::util::Signal; 6use embassy::util::Signal;
7 7
8use crate::hal::gpio::{Input, Level, Output, Pin, Port}; 8use 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))]
55pub enum NewChannelError { 56pub 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;
12pub use crate::pac::Interrupt::*; // needed for cortex-m-rt #[interrupt] 12pub use crate::pac::Interrupt::*; // needed for cortex-m-rt #[interrupt]
13pub use cortex_m::interrupt::{CriticalSection, Mutex}; 13pub 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)]
17pub enum Priority { 18pub 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")]
49pub use nrf52840_hal as hal; 49pub use nrf52840_hal as hal;
50 50
51// This mod MUST go first, so that the others see its macros.
52pub(crate) mod fmt;
53
51pub mod gpiote; 54pub mod gpiote;
52pub mod interrupt; 55pub 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 @@
1use crate::fmt::{assert, assert_eq, panic, *};
1use core::future::Future; 2use core::future::Future;
2use defmt::{assert, assert_eq, panic, *};
3 3
4use crate::hal::gpio::{Output, Pin as GpioPin, Port as GpioPort, PushPull}; 4use crate::hal::gpio::{Output, Pin as GpioPin, Port as GpioPort, PushPull};
5use crate::pac::{Interrupt, QSPI}; 5use 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};
28use embassy::io::{AsyncBufRead, AsyncWrite, Result}; 28use embassy::io::{AsyncBufRead, AsyncWrite, Result};
29use embassy::util::WakerStore; 29use embassy::util::WakerStore;
30 30
31use defmt::{assert, panic, todo, *}; 31use crate::fmt::{assert, panic, todo, *};
32 32
33//use crate::trace; 33//use crate::trace;
34 34