aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-03-08 01:51:45 +0100
committerGitHub <[email protected]>2021-03-08 01:51:45 +0100
commit16e00669aeae310451adbd1773db29cc70c9dc5f (patch)
tree2f511983d6efa69c5055e9aad888ce0899624cd0
parentf922cf1609c9baebf8dead87161df215f35df449 (diff)
parent15c3e78408a2835003b9d7dee0b7fbae544d10ba (diff)
Merge pull request #73 from thalesfragoso/split-util
Move nRF's util into a separate crate
-rw-r--r--Cargo.toml1
-rw-r--r--embassy-extras/Cargo.toml19
-rw-r--r--embassy-extras/src/fmt.rs119
-rw-r--r--embassy-extras/src/lib.rs (renamed from embassy-nrf/src/util/mod.rs)5
-rw-r--r--embassy-extras/src/peripheral.rs (renamed from embassy-nrf/src/util/peripheral.rs)5
-rw-r--r--embassy-extras/src/ring_buffer.rs (renamed from embassy-nrf/src/util/ring_buffer.rs)0
-rw-r--r--embassy-nrf/Cargo.toml1
-rw-r--r--embassy-nrf/src/buffered_uarte.rs8
-rw-r--r--embassy-nrf/src/lib.rs1
-rw-r--r--embassy-nrf/src/qspi.rs3
-rw-r--r--embassy-nrf/src/spim.rs2
-rw-r--r--embassy-nrf/src/uarte.rs6
12 files changed, 157 insertions, 13 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 80ae93745..069437317 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,7 @@ members = [
8 "embassy-nrf-examples", 8 "embassy-nrf-examples",
9 "embassy-stm32f4-examples", 9 "embassy-stm32f4-examples",
10 "embassy-macros", 10 "embassy-macros",
11 "embassy-extras",
11] 12]
12 13
13# embassy-std enables std-only features. Since Cargo resolves all features 14# embassy-std enables std-only features. Since Cargo resolves all features
diff --git a/embassy-extras/Cargo.toml b/embassy-extras/Cargo.toml
new file mode 100644
index 000000000..3c42b5c2f
--- /dev/null
+++ b/embassy-extras/Cargo.toml
@@ -0,0 +1,19 @@
1[package]
2name = "embassy-extras"
3version = "0.1.0"
4authors = ["Dario Nieuwenhuis <[email protected]>"]
5edition = "2018"
6
7[features]
8defmt-trace = [ ]
9defmt-debug = [ ]
10defmt-info = [ ]
11defmt-warn = [ ]
12defmt-error = [ ]
13
14[dependencies]
15embassy = { version = "0.1.0", path = "../embassy" }
16
17defmt = { version = "0.2.0", optional = true }
18log = { version = "0.4.11", optional = true }
19cortex-m = "0.7.1"
diff --git a/embassy-extras/src/fmt.rs b/embassy-extras/src/fmt.rs
new file mode 100644
index 000000000..1be1057a7
--- /dev/null
+++ b/embassy-extras/src/fmt.rs
@@ -0,0 +1,119 @@
1#![macro_use]
2#![allow(clippy::module_inception)]
3
4#[cfg(all(feature = "defmt", feature = "log"))]
5compile_error!("You may not enable both `defmt` and `log` features.");
6
7pub use fmt::*;
8
9#[cfg(feature = "defmt")]
10mod fmt {
11 pub use defmt::{
12 assert, assert_eq, assert_ne, debug, debug_assert, debug_assert_eq, debug_assert_ne, error,
13 info, panic, todo, trace, unreachable, unwrap, warn,
14 };
15}
16
17#[cfg(feature = "log")]
18mod fmt {
19 pub use core::{
20 assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo,
21 unreachable,
22 };
23 pub use log::{debug, error, info, trace, warn};
24}
25
26#[cfg(not(any(feature = "defmt", feature = "log")))]
27mod fmt {
28 #![macro_use]
29
30 pub use core::{
31 assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo,
32 unreachable,
33 };
34
35 #[macro_export]
36 macro_rules! trace {
37 ($($msg:expr),+ $(,)?) => {
38 ()
39 };
40 }
41
42 #[macro_export]
43 macro_rules! debug {
44 ($($msg:expr),+ $(,)?) => {
45 ()
46 };
47 }
48
49 #[macro_export]
50 macro_rules! info {
51 ($($msg:expr),+ $(,)?) => {
52 ()
53 };
54 }
55
56 #[macro_export]
57 macro_rules! warn {
58 ($($msg:expr),+ $(,)?) => {
59 ()
60 };
61 }
62
63 #[macro_export]
64 macro_rules! error {
65 ($($msg:expr),+ $(,)?) => {
66 ()
67 };
68 }
69}
70
71#[cfg(not(feature = "defmt"))]
72#[macro_export]
73macro_rules! unwrap {
74 ($arg:expr) => {
75 match $crate::fmt::Try::into_result($arg) {
76 ::core::result::Result::Ok(t) => t,
77 ::core::result::Result::Err(e) => {
78 ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e);
79 }
80 }
81 };
82 ($arg:expr, $($msg:expr),+ $(,)? ) => {
83 match $crate::fmt::Try::into_result($arg) {
84 ::core::result::Result::Ok(t) => t,
85 ::core::result::Result::Err(e) => {
86 ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e);
87 }
88 }
89 }
90}
91
92#[derive(Debug, Copy, Clone, Eq, PartialEq)]
93pub struct NoneError;
94
95pub trait Try {
96 type Ok;
97 type Error;
98 fn into_result(self) -> Result<Self::Ok, Self::Error>;
99}
100
101impl<T> Try for Option<T> {
102 type Ok = T;
103 type Error = NoneError;
104
105 #[inline]
106 fn into_result(self) -> Result<T, NoneError> {
107 self.ok_or(NoneError)
108 }
109}
110
111impl<T, E> Try for Result<T, E> {
112 type Ok = T;
113 type Error = E;
114
115 #[inline]
116 fn into_result(self) -> Self {
117 self
118 }
119}
diff --git a/embassy-nrf/src/util/mod.rs b/embassy-extras/src/lib.rs
index cf3306545..4a95173cf 100644
--- a/embassy-nrf/src/util/mod.rs
+++ b/embassy-extras/src/lib.rs
@@ -1,3 +1,8 @@
1#![no_std]
2
3// This mod MUST go first, so that the others see its macros.
4pub(crate) mod fmt;
5
1pub mod peripheral; 6pub mod peripheral;
2pub mod ring_buffer; 7pub mod ring_buffer;
3 8
diff --git a/embassy-nrf/src/util/peripheral.rs b/embassy-extras/src/peripheral.rs
index bb88f0820..e9357969d 100644
--- a/embassy-nrf/src/util/peripheral.rs
+++ b/embassy-extras/src/peripheral.rs
@@ -4,10 +4,9 @@ use core::mem::MaybeUninit;
4use core::pin::Pin; 4use core::pin::Pin;
5use core::sync::atomic::{compiler_fence, Ordering}; 5use core::sync::atomic::{compiler_fence, Ordering};
6 6
7use embassy::interrupt::InterruptExt; 7use embassy::interrupt::{Interrupt, InterruptExt};
8 8
9use crate::fmt::{assert, *}; 9use crate::fmt::assert;
10use crate::interrupt::Interrupt;
11 10
12pub trait PeripheralState { 11pub trait PeripheralState {
13 type Interrupt: Interrupt; 12 type Interrupt: Interrupt;
diff --git a/embassy-nrf/src/util/ring_buffer.rs b/embassy-extras/src/ring_buffer.rs
index f2b9f7359..f2b9f7359 100644
--- a/embassy-nrf/src/util/ring_buffer.rs
+++ b/embassy-extras/src/ring_buffer.rs
diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml
index 419b7f33f..026e815f7 100644
--- a/embassy-nrf/Cargo.toml
+++ b/embassy-nrf/Cargo.toml
@@ -20,6 +20,7 @@ defmt-error = [ ]
20 20
21[dependencies] 21[dependencies]
22embassy = { version = "0.1.0", path = "../embassy" } 22embassy = { version = "0.1.0", path = "../embassy" }
23embassy-extras = {version = "0.1.0", path = "../embassy-extras" }
23 24
24defmt = { version = "0.2.0", optional = true } 25defmt = { version = "0.2.0", optional = true }
25log = { version = "0.4.11", optional = true } 26log = { version = "0.4.11", optional = true }
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs
index 848687f7b..b1366cd9e 100644
--- a/embassy-nrf/src/buffered_uarte.rs
+++ b/embassy-nrf/src/buffered_uarte.rs
@@ -13,14 +13,15 @@ use core::task::{Context, Poll};
13use embassy::interrupt::InterruptExt; 13use embassy::interrupt::InterruptExt;
14use embassy::io::{AsyncBufRead, AsyncWrite, Result}; 14use embassy::io::{AsyncBufRead, AsyncWrite, Result};
15use embassy::util::WakerRegistration; 15use embassy::util::WakerRegistration;
16use embassy_extras::low_power_wait_until;
17use embassy_extras::peripheral::{PeripheralMutex, PeripheralState};
18use embassy_extras::ring_buffer::RingBuffer;
16use embedded_hal::digital::v2::OutputPin; 19use embedded_hal::digital::v2::OutputPin;
17 20
21use crate::fmt::*;
18use crate::hal::ppi::ConfigurablePpi; 22use crate::hal::ppi::ConfigurablePpi;
19use crate::interrupt::{self, Interrupt}; 23use crate::interrupt::{self, Interrupt};
20use crate::pac; 24use crate::pac;
21use crate::util::peripheral::{PeripheralMutex, PeripheralState};
22use crate::util::ring_buffer::RingBuffer;
23use crate::{fmt::*, util::low_power_wait_until};
24 25
25// Re-export SVD variants to allow user to directly set values 26// Re-export SVD variants to allow user to directly set values
26pub use crate::hal::uarte::Pins; 27pub use crate::hal::uarte::Pins;
@@ -116,7 +117,6 @@ impl<'a, U: Instance, T: TimerInstance, P1: ConfigurablePpi, P2: ConfigurablePpi
116 } 117 }
117 }); 118 });
118 119
119
120 // Enable UARTE instance 120 // Enable UARTE instance
121 uarte.enable.write(|w| w.enable().enabled()); 121 uarte.enable.write(|w| w.enable().enabled());
122 122
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs
index 3de6299e9..bb37ec367 100644
--- a/embassy-nrf/src/lib.rs
+++ b/embassy-nrf/src/lib.rs
@@ -90,7 +90,6 @@ pub(crate) fn slice_in_ram_or<T>(slice: &[u8], err: T) -> Result<(), T> {
90 90
91// This mod MUST go first, so that the others see its macros. 91// This mod MUST go first, so that the others see its macros.
92pub(crate) mod fmt; 92pub(crate) mod fmt;
93pub(crate) mod util;
94 93
95pub mod buffered_uarte; 94pub mod buffered_uarte;
96pub mod gpiote; 95pub mod gpiote;
diff --git a/embassy-nrf/src/qspi.rs b/embassy-nrf/src/qspi.rs
index 1dc178f26..f6e8175fa 100644
--- a/embassy-nrf/src/qspi.rs
+++ b/embassy-nrf/src/qspi.rs
@@ -2,6 +2,8 @@ use core::future::Future;
2use core::pin::Pin; 2use core::pin::Pin;
3use core::task::Poll; 3use core::task::Poll;
4 4
5use embassy_extras::peripheral::{PeripheralMutex, PeripheralState};
6
5use crate::fmt::{assert, assert_eq, *}; 7use crate::fmt::{assert, assert_eq, *};
6use crate::hal::gpio::{Output, Pin as GpioPin, PushPull}; 8use crate::hal::gpio::{Output, Pin as GpioPin, PushPull};
7use crate::interrupt::{self}; 9use crate::interrupt::{self};
@@ -11,7 +13,6 @@ pub use crate::pac::qspi::ifconfig0::ADDRMODE_A as AddressMode;
11pub use crate::pac::qspi::ifconfig0::PPSIZE_A as WritePageSize; 13pub use crate::pac::qspi::ifconfig0::PPSIZE_A as WritePageSize;
12pub use crate::pac::qspi::ifconfig0::READOC_A as ReadOpcode; 14pub use crate::pac::qspi::ifconfig0::READOC_A as ReadOpcode;
13pub use crate::pac::qspi::ifconfig0::WRITEOC_A as WriteOpcode; 15pub use crate::pac::qspi::ifconfig0::WRITEOC_A as WriteOpcode;
14use crate::util::peripheral::{PeripheralMutex, PeripheralState};
15 16
16// TODO 17// TODO
17// - config: 18// - config:
diff --git a/embassy-nrf/src/spim.rs b/embassy-nrf/src/spim.rs
index b72329631..4921bae3b 100644
--- a/embassy-nrf/src/spim.rs
+++ b/embassy-nrf/src/spim.rs
@@ -3,10 +3,10 @@ use core::pin::Pin;
3use core::sync::atomic::{compiler_fence, Ordering}; 3use core::sync::atomic::{compiler_fence, Ordering};
4use core::task::Poll; 4use core::task::Poll;
5use embassy::util::WakerRegistration; 5use embassy::util::WakerRegistration;
6use embassy_extras::peripheral::{PeripheralMutex, PeripheralState};
6use futures::future::poll_fn; 7use futures::future::poll_fn;
7 8
8use crate::interrupt::{self, Interrupt}; 9use crate::interrupt::{self, Interrupt};
9use crate::util::peripheral::{PeripheralMutex, PeripheralState};
10use crate::{pac, slice_in_ram_or}; 10use crate::{pac, slice_in_ram_or};
11 11
12pub use crate::hal::spim::{ 12pub use crate::hal::spim::{
diff --git a/embassy-nrf/src/uarte.rs b/embassy-nrf/src/uarte.rs
index aea01f95c..b5e4da862 100644
--- a/embassy-nrf/src/uarte.rs
+++ b/embassy-nrf/src/uarte.rs
@@ -16,8 +16,8 @@ use crate::fmt::{assert, *};
16use crate::hal::pac; 16use crate::hal::pac;
17use crate::hal::prelude::*; 17use crate::hal::prelude::*;
18use crate::hal::target_constants::EASY_DMA_SIZE; 18use crate::hal::target_constants::EASY_DMA_SIZE;
19use crate::interrupt;
19use crate::interrupt::Interrupt; 20use crate::interrupt::Interrupt;
20use crate::{interrupt, util};
21 21
22pub use crate::hal::uarte::Pins; 22pub use crate::hal::uarte::Pins;
23// Re-export SVD variants to allow user to directly set values. 23// Re-export SVD variants to allow user to directly set values.
@@ -45,7 +45,7 @@ where
45 /// Creates the interface to a UARTE instance. 45 /// Creates the interface to a UARTE instance.
46 /// Sets the baud rate, parity and assigns the pins to the UARTE peripheral. 46 /// Sets the baud rate, parity and assigns the pins to the UARTE peripheral.
47 /// 47 ///
48 /// # Unsafe 48 /// # Safety
49 /// 49 ///
50 /// The returned API is safe unless you use `mem::forget` (or similar safe mechanisms) 50 /// The returned API is safe unless you use `mem::forget` (or similar safe mechanisms)
51 /// on stack allocated buffers which which have been passed to [`send()`](Uarte::send) 51 /// on stack allocated buffers which which have been passed to [`send()`](Uarte::send)
@@ -327,7 +327,7 @@ where
327 .tasks_stoprx 327 .tasks_stoprx
328 .write(|w| unsafe { w.bits(1) }); 328 .write(|w| unsafe { w.bits(1) });
329 329
330 util::low_power_wait_until(|| T::state().rx_done.signaled()) 330 embassy_extras::low_power_wait_until(|| T::state().rx_done.signaled())
331 } 331 }
332 } 332 }
333} 333}