Skip to content

Instantly share code, notes, and snippets.

@timokroeger
Last active June 4, 2021 21:40
Show Gist options
  • Select an option

  • Save timokroeger/f025ef590557f1cd6c2e9c7ab30442d2 to your computer and use it in GitHub Desktop.

Select an option

Save timokroeger/f025ef590557f1cd6c2e9c7ab30442d2 to your computer and use it in GitHub Desktop.
use core::future::Future;
use embassy_nrf::{gpio, spim};
use embedded_hal::digital::v2::OutputPin;
use gpio::{AnyPin, Output};
pub struct SpiWrapper<Spim>
where
Spim: spim::Instance,
{
spim: Spim,
irq: Spim::Interrupt,
sck: AnyPin,
miso: AnyPin,
mosi: AnyPin,
config: spim::Config,
cs: Output<'static, AnyPin>,
}
impl<Spim> SpiWrapper<Spim>
where
Spim: spim::Instance,
{
pub fn new(
spim: Spim,
irq: Spim::Interrupt,
sck: AnyPin,
miso: AnyPin,
mosi: AnyPin,
config: spim::Config,
cs: AnyPin,
) -> Self {
let cs = Output::new(cs, gpio::Level::High, gpio::OutputDrive::Standard);
Self {
spim,
irq,
sck,
miso,
mosi,
config,
cs,
}
}
pub async fn transaction<T, F>(&mut self, f: impl FnOnce(&mut spim::Spim<'_, Spim>) -> F)
where
F: Future<Output = T>,
{
let _ = self.cs.set_low();
let mut spi = spim::Spim::new(
&mut self.spim,
&mut self.irq,
&mut self.sck,
&mut self.miso,
&mut self.mosi,
&self.config,
);
f(&mut spi).await;
let _ = self.cs.set_high();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment