Last active
June 4, 2021 21:40
-
-
Save timokroeger/f025ef590557f1cd6c2e9c7ab30442d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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