Merge pull request #73 from thalesfragoso/split-util

Move nRF's util into a separate crate
This commit is contained in:
Dario Nieuwenhuis 2021-03-08 01:51:45 +01:00 committed by GitHub
commit 16e00669ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 157 additions and 13 deletions

View File

@ -8,6 +8,7 @@ members = [
"embassy-nrf-examples",
"embassy-stm32f4-examples",
"embassy-macros",
"embassy-extras",
]
# embassy-std enables std-only features. Since Cargo resolves all features

19
embassy-extras/Cargo.toml Normal file
View File

@ -0,0 +1,19 @@
[package]
name = "embassy-extras"
version = "0.1.0"
authors = ["Dario Nieuwenhuis <dirbaio@dirbaio.net>"]
edition = "2018"
[features]
defmt-trace = [ ]
defmt-debug = [ ]
defmt-info = [ ]
defmt-warn = [ ]
defmt-error = [ ]
[dependencies]
embassy = { version = "0.1.0", path = "../embassy" }
defmt = { version = "0.2.0", optional = true }
log = { version = "0.4.11", optional = true }
cortex-m = "0.7.1"

119
embassy-extras/src/fmt.rs Normal file
View File

@ -0,0 +1,119 @@
#![macro_use]
#![allow(clippy::module_inception)]
#[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) => {
match $crate::fmt::Try::into_result($arg) {
::core::result::Result::Ok(t) => t,
::core::result::Result::Err(e) => {
::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e);
}
}
};
($arg:expr, $($msg:expr),+ $(,)? ) => {
match $crate::fmt::Try::into_result($arg) {
::core::result::Result::Ok(t) => t,
::core::result::Result::Err(e) => {
::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e);
}
}
}
}
#[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
}
}

View File

@ -1,3 +1,8 @@
#![no_std]
// This mod MUST go first, so that the others see its macros.
pub(crate) mod fmt;
pub mod peripheral;
pub mod ring_buffer;

View File

@ -4,10 +4,9 @@ use core::mem::MaybeUninit;
use core::pin::Pin;
use core::sync::atomic::{compiler_fence, Ordering};
use embassy::interrupt::InterruptExt;
use embassy::interrupt::{Interrupt, InterruptExt};
use crate::fmt::{assert, *};
use crate::interrupt::Interrupt;
use crate::fmt::assert;
pub trait PeripheralState {
type Interrupt: Interrupt;

View File

@ -20,6 +20,7 @@ defmt-error = [ ]
[dependencies]
embassy = { version = "0.1.0", path = "../embassy" }
embassy-extras = {version = "0.1.0", path = "../embassy-extras" }
defmt = { version = "0.2.0", optional = true }
log = { version = "0.4.11", optional = true }

View File

@ -13,14 +13,15 @@ use core::task::{Context, Poll};
use embassy::interrupt::InterruptExt;
use embassy::io::{AsyncBufRead, AsyncWrite, Result};
use embassy::util::WakerRegistration;
use embassy_extras::low_power_wait_until;
use embassy_extras::peripheral::{PeripheralMutex, PeripheralState};
use embassy_extras::ring_buffer::RingBuffer;
use embedded_hal::digital::v2::OutputPin;
use crate::fmt::*;
use crate::hal::ppi::ConfigurablePpi;
use crate::interrupt::{self, Interrupt};
use crate::pac;
use crate::util::peripheral::{PeripheralMutex, PeripheralState};
use crate::util::ring_buffer::RingBuffer;
use crate::{fmt::*, util::low_power_wait_until};
// Re-export SVD variants to allow user to directly set values
pub use crate::hal::uarte::Pins;
@ -116,7 +117,6 @@ impl<'a, U: Instance, T: TimerInstance, P1: ConfigurablePpi, P2: ConfigurablePpi
}
});
// Enable UARTE instance
uarte.enable.write(|w| w.enable().enabled());

View File

@ -90,7 +90,6 @@ pub(crate) fn slice_in_ram_or<T>(slice: &[u8], err: T) -> Result<(), T> {
// This mod MUST go first, so that the others see its macros.
pub(crate) mod fmt;
pub(crate) mod util;
pub mod buffered_uarte;
pub mod gpiote;

View File

@ -2,6 +2,8 @@ use core::future::Future;
use core::pin::Pin;
use core::task::Poll;
use embassy_extras::peripheral::{PeripheralMutex, PeripheralState};
use crate::fmt::{assert, assert_eq, *};
use crate::hal::gpio::{Output, Pin as GpioPin, PushPull};
use crate::interrupt::{self};
@ -11,7 +13,6 @@ pub use crate::pac::qspi::ifconfig0::ADDRMODE_A as AddressMode;
pub use crate::pac::qspi::ifconfig0::PPSIZE_A as WritePageSize;
pub use crate::pac::qspi::ifconfig0::READOC_A as ReadOpcode;
pub use crate::pac::qspi::ifconfig0::WRITEOC_A as WriteOpcode;
use crate::util::peripheral::{PeripheralMutex, PeripheralState};
// TODO
// - config:

View File

@ -3,10 +3,10 @@ use core::pin::Pin;
use core::sync::atomic::{compiler_fence, Ordering};
use core::task::Poll;
use embassy::util::WakerRegistration;
use embassy_extras::peripheral::{PeripheralMutex, PeripheralState};
use futures::future::poll_fn;
use crate::interrupt::{self, Interrupt};
use crate::util::peripheral::{PeripheralMutex, PeripheralState};
use crate::{pac, slice_in_ram_or};
pub use crate::hal::spim::{

View File

@ -16,8 +16,8 @@ use crate::fmt::{assert, *};
use crate::hal::pac;
use crate::hal::prelude::*;
use crate::hal::target_constants::EASY_DMA_SIZE;
use crate::interrupt;
use crate::interrupt::Interrupt;
use crate::{interrupt, util};
pub use crate::hal::uarte::Pins;
// Re-export SVD variants to allow user to directly set values.
@ -45,7 +45,7 @@ where
/// Creates the interface to a UARTE instance.
/// Sets the baud rate, parity and assigns the pins to the UARTE peripheral.
///
/// # Unsafe
/// # Safety
///
/// The returned API is safe unless you use `mem::forget` (or similar safe mechanisms)
/// on stack allocated buffers which which have been passed to [`send()`](Uarte::send)
@ -327,7 +327,7 @@ where
.tasks_stoprx
.write(|w| unsafe { w.bits(1) });
util::low_power_wait_until(|| T::state().rx_done.signaled())
embassy_extras::low_power_wait_until(|| T::state().rx_done.signaled())
}
}
}