diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-03-08 01:51:45 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-03-08 01:51:45 +0100 |
| commit | 16e00669aeae310451adbd1773db29cc70c9dc5f (patch) | |
| tree | 2f511983d6efa69c5055e9aad888ce0899624cd0 | |
| parent | f922cf1609c9baebf8dead87161df215f35df449 (diff) | |
| parent | 15c3e78408a2835003b9d7dee0b7fbae544d10ba (diff) | |
Merge pull request #73 from thalesfragoso/split-util
Move nRF's util into a separate crate
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | embassy-extras/Cargo.toml | 19 | ||||
| -rw-r--r-- | embassy-extras/src/fmt.rs | 119 | ||||
| -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.toml | 1 | ||||
| -rw-r--r-- | embassy-nrf/src/buffered_uarte.rs | 8 | ||||
| -rw-r--r-- | embassy-nrf/src/lib.rs | 1 | ||||
| -rw-r--r-- | embassy-nrf/src/qspi.rs | 3 | ||||
| -rw-r--r-- | embassy-nrf/src/spim.rs | 2 | ||||
| -rw-r--r-- | embassy-nrf/src/uarte.rs | 6 |
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] | ||
| 2 | name = "embassy-extras" | ||
| 3 | version = "0.1.0" | ||
| 4 | authors = ["Dario Nieuwenhuis <[email protected]>"] | ||
| 5 | edition = "2018" | ||
| 6 | |||
| 7 | [features] | ||
| 8 | defmt-trace = [ ] | ||
| 9 | defmt-debug = [ ] | ||
| 10 | defmt-info = [ ] | ||
| 11 | defmt-warn = [ ] | ||
| 12 | defmt-error = [ ] | ||
| 13 | |||
| 14 | [dependencies] | ||
| 15 | embassy = { version = "0.1.0", path = "../embassy" } | ||
| 16 | |||
| 17 | defmt = { version = "0.2.0", optional = true } | ||
| 18 | log = { version = "0.4.11", optional = true } | ||
| 19 | cortex-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"))] | ||
| 5 | compile_error!("You may not enable both `defmt` and `log` features."); | ||
| 6 | |||
| 7 | pub use fmt::*; | ||
| 8 | |||
| 9 | #[cfg(feature = "defmt")] | ||
| 10 | mod 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")] | ||
| 18 | mod 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")))] | ||
| 27 | mod 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] | ||
| 73 | macro_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)] | ||
| 93 | pub struct NoneError; | ||
| 94 | |||
| 95 | pub trait Try { | ||
| 96 | type Ok; | ||
| 97 | type Error; | ||
| 98 | fn into_result(self) -> Result<Self::Ok, Self::Error>; | ||
| 99 | } | ||
| 100 | |||
| 101 | impl<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 | |||
| 111 | impl<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. | ||
| 4 | pub(crate) mod fmt; | ||
| 5 | |||
| 1 | pub mod peripheral; | 6 | pub mod peripheral; |
| 2 | pub mod ring_buffer; | 7 | pub 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; | |||
| 4 | use core::pin::Pin; | 4 | use core::pin::Pin; |
| 5 | use core::sync::atomic::{compiler_fence, Ordering}; | 5 | use core::sync::atomic::{compiler_fence, Ordering}; |
| 6 | 6 | ||
| 7 | use embassy::interrupt::InterruptExt; | 7 | use embassy::interrupt::{Interrupt, InterruptExt}; |
| 8 | 8 | ||
| 9 | use crate::fmt::{assert, *}; | 9 | use crate::fmt::assert; |
| 10 | use crate::interrupt::Interrupt; | ||
| 11 | 10 | ||
| 12 | pub trait PeripheralState { | 11 | pub 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] |
| 22 | embassy = { version = "0.1.0", path = "../embassy" } | 22 | embassy = { version = "0.1.0", path = "../embassy" } |
| 23 | embassy-extras = {version = "0.1.0", path = "../embassy-extras" } | ||
| 23 | 24 | ||
| 24 | defmt = { version = "0.2.0", optional = true } | 25 | defmt = { version = "0.2.0", optional = true } |
| 25 | log = { version = "0.4.11", optional = true } | 26 | log = { 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}; | |||
| 13 | use embassy::interrupt::InterruptExt; | 13 | use embassy::interrupt::InterruptExt; |
| 14 | use embassy::io::{AsyncBufRead, AsyncWrite, Result}; | 14 | use embassy::io::{AsyncBufRead, AsyncWrite, Result}; |
| 15 | use embassy::util::WakerRegistration; | 15 | use embassy::util::WakerRegistration; |
| 16 | use embassy_extras::low_power_wait_until; | ||
| 17 | use embassy_extras::peripheral::{PeripheralMutex, PeripheralState}; | ||
| 18 | use embassy_extras::ring_buffer::RingBuffer; | ||
| 16 | use embedded_hal::digital::v2::OutputPin; | 19 | use embedded_hal::digital::v2::OutputPin; |
| 17 | 20 | ||
| 21 | use crate::fmt::*; | ||
| 18 | use crate::hal::ppi::ConfigurablePpi; | 22 | use crate::hal::ppi::ConfigurablePpi; |
| 19 | use crate::interrupt::{self, Interrupt}; | 23 | use crate::interrupt::{self, Interrupt}; |
| 20 | use crate::pac; | 24 | use crate::pac; |
| 21 | use crate::util::peripheral::{PeripheralMutex, PeripheralState}; | ||
| 22 | use crate::util::ring_buffer::RingBuffer; | ||
| 23 | use 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 |
| 26 | pub use crate::hal::uarte::Pins; | 27 | pub 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. |
| 92 | pub(crate) mod fmt; | 92 | pub(crate) mod fmt; |
| 93 | pub(crate) mod util; | ||
| 94 | 93 | ||
| 95 | pub mod buffered_uarte; | 94 | pub mod buffered_uarte; |
| 96 | pub mod gpiote; | 95 | pub 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; | |||
| 2 | use core::pin::Pin; | 2 | use core::pin::Pin; |
| 3 | use core::task::Poll; | 3 | use core::task::Poll; |
| 4 | 4 | ||
| 5 | use embassy_extras::peripheral::{PeripheralMutex, PeripheralState}; | ||
| 6 | |||
| 5 | use crate::fmt::{assert, assert_eq, *}; | 7 | use crate::fmt::{assert, assert_eq, *}; |
| 6 | use crate::hal::gpio::{Output, Pin as GpioPin, PushPull}; | 8 | use crate::hal::gpio::{Output, Pin as GpioPin, PushPull}; |
| 7 | use crate::interrupt::{self}; | 9 | use crate::interrupt::{self}; |
| @@ -11,7 +13,6 @@ pub use crate::pac::qspi::ifconfig0::ADDRMODE_A as AddressMode; | |||
| 11 | pub use crate::pac::qspi::ifconfig0::PPSIZE_A as WritePageSize; | 13 | pub use crate::pac::qspi::ifconfig0::PPSIZE_A as WritePageSize; |
| 12 | pub use crate::pac::qspi::ifconfig0::READOC_A as ReadOpcode; | 14 | pub use crate::pac::qspi::ifconfig0::READOC_A as ReadOpcode; |
| 13 | pub use crate::pac::qspi::ifconfig0::WRITEOC_A as WriteOpcode; | 15 | pub use crate::pac::qspi::ifconfig0::WRITEOC_A as WriteOpcode; |
| 14 | use 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; | |||
| 3 | use core::sync::atomic::{compiler_fence, Ordering}; | 3 | use core::sync::atomic::{compiler_fence, Ordering}; |
| 4 | use core::task::Poll; | 4 | use core::task::Poll; |
| 5 | use embassy::util::WakerRegistration; | 5 | use embassy::util::WakerRegistration; |
| 6 | use embassy_extras::peripheral::{PeripheralMutex, PeripheralState}; | ||
| 6 | use futures::future::poll_fn; | 7 | use futures::future::poll_fn; |
| 7 | 8 | ||
| 8 | use crate::interrupt::{self, Interrupt}; | 9 | use crate::interrupt::{self, Interrupt}; |
| 9 | use crate::util::peripheral::{PeripheralMutex, PeripheralState}; | ||
| 10 | use crate::{pac, slice_in_ram_or}; | 10 | use crate::{pac, slice_in_ram_or}; |
| 11 | 11 | ||
| 12 | pub use crate::hal::spim::{ | 12 | pub 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, *}; | |||
| 16 | use crate::hal::pac; | 16 | use crate::hal::pac; |
| 17 | use crate::hal::prelude::*; | 17 | use crate::hal::prelude::*; |
| 18 | use crate::hal::target_constants::EASY_DMA_SIZE; | 18 | use crate::hal::target_constants::EASY_DMA_SIZE; |
| 19 | use crate::interrupt; | ||
| 19 | use crate::interrupt::Interrupt; | 20 | use crate::interrupt::Interrupt; |
| 20 | use crate::{interrupt, util}; | ||
| 21 | 21 | ||
| 22 | pub use crate::hal::uarte::Pins; | 22 | pub 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 | } |
