pub struct KernelDelay {
spin_below_ns: u64,
}
impl Default for KernelDelay {
fn default() -> Self {
KernelDelay::new(100_000)
}
}
impl KernelDelay {
pub const fn new(spin_below_ns: u64) -> Self {
KernelDelay { spin_below_ns }
}
pub const fn spin_below_ns(&self) -> u64 {
self.spin_below_ns
}
}
impl embedded_hal::delay::DelayNs for KernelDelay {
fn delay_ns(&mut self, ns: u32) {
let ns = ns as u64;
if ns < self.spin_below_ns {
let until = crate::time::now()
.wrapping_add(crate::time::ticks_for(core::time::Duration::from_nanos(ns)));
if let Some(start) = crate::arch::cycle_counter() {
let hz = crate::arch::cycle_counter_hz();
let want = (ns as u128 * hz as u128 / 1_000_000_000) as u32;
while crate::arch::cycle_counter()
.unwrap_or(start)
.wrapping_sub(start)
< want
{
core::hint::spin_loop();
}
} else {
let mut n = ns.saturating_mul(4);
while n > 0 {
core::hint::spin_loop();
n -= 1;
}
}
let _ = until;
} else {
crate::sleep(core::time::Duration::from_nanos(ns));
}
}
}
impl embedded_hal_async::delay::DelayNs for KernelDelay {
async fn delay_ns(&mut self, ns: u32) {
crate::exec::sleep(core::time::Duration::from_nanos(ns as u64)).await;
}
}
pub struct Blocking<T>(pub T);
impl<T> Blocking<T> {
pub const fn new(inner: T) -> Self {
Blocking(inner)
}
pub fn into_inner(self) -> T {
self.0
}
}
#[cfg(target_has_atomic = "32")]
mod shared {
use crate::sync::{LockError, LockId, Mutex, MutexGuard};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SharedError<E> {
Bus(E),
LockTimeout,
LockOrder,
}
impl<E> From<LockError> for SharedError<E> {
fn from(e: LockError) -> Self {
match e {
LockError::Timeout => SharedError::LockTimeout,
_ => SharedError::LockOrder,
}
}
}
impl<E: core::fmt::Debug> embedded_hal::i2c::Error for SharedError<E> {
fn kind(&self) -> embedded_hal::i2c::ErrorKind {
embedded_hal::i2c::ErrorKind::Other
}
}
pub struct MutexDevice<BUS, const ID: u32> {
bus: Mutex<BUS>,
timeout_ms: u64,
}
impl<BUS, const ID: u32> MutexDevice<BUS, ID> {
pub const fn new(bus: BUS) -> Self {
MutexDevice {
bus: Mutex::with_id(LockId::new(ID), bus),
timeout_ms: 1000,
}
}
pub const fn with_timeout(bus: BUS, timeout_ms: u64) -> Self {
MutexDevice {
bus: Mutex::with_id(LockId::new(ID), bus),
timeout_ms,
}
}
pub fn lock(&self) -> Result<MutexGuard<'_, BUS>, SharedError<()>> {
Ok(self.bus.try_lock_for(self.timeout_ms)?)
}
}
impl<BUS: embedded_hal::i2c::ErrorType, const ID: u32> embedded_hal::i2c::ErrorType
for MutexDevice<BUS, ID>
{
type Error = SharedError<BUS::Error>;
}
impl<BUS: embedded_hal::i2c::I2c, const ID: u32> embedded_hal::i2c::I2c for MutexDevice<BUS, ID> {
fn transaction(
&mut self,
address: u8,
operations: &mut [embedded_hal::i2c::Operation<'_>],
) -> Result<(), Self::Error> {
let mut guard = self.bus.try_lock_for(self.timeout_ms)?;
guard
.transaction(address, operations)
.map_err(SharedError::Bus)
}
}
}
#[cfg(target_has_atomic = "32")]
pub use shared::{MutexDevice, SharedError};
mod facade {
use super::Blocking;
use crate::exec::block_on;
impl<T: embedded_hal_async::i2c::ErrorType> embedded_hal::i2c::ErrorType for Blocking<T> {
type Error = T::Error;
}
impl<T: embedded_hal_async::i2c::I2c> embedded_hal::i2c::I2c for Blocking<T> {
fn transaction(
&mut self,
address: u8,
operations: &mut [embedded_hal::i2c::Operation<'_>],
) -> Result<(), Self::Error> {
block_on(self.0.transaction(address, operations))
}
}
impl<T: embedded_hal_async::spi::ErrorType> embedded_hal::spi::ErrorType for Blocking<T> {
type Error = T::Error;
}
impl<T, Word> embedded_hal::spi::SpiDevice<Word> for Blocking<T>
where
T: embedded_hal_async::spi::SpiDevice<Word>,
Word: Copy + 'static,
{
fn transaction(
&mut self,
operations: &mut [embedded_hal::spi::Operation<'_, Word>],
) -> Result<(), Self::Error> {
block_on(self.0.transaction(operations))
}
}
}