From 44150c483017c18979e58d8557aac3df031ba47a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Alse=CC=81r?= Date: Tue, 30 Aug 2022 01:18:28 +0200 Subject: [PATCH] impl embedded-hal-async --- embassy-rp/src/spi.rs | 54 ++++++++++++++++++++++++++++++++ examples/rp/src/bin/spi_async.rs | 6 ++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/embassy-rp/src/spi.rs b/embassy-rp/src/spi.rs index a91a1fd1..be639504 100644 --- a/embassy-rp/src/spi.rs +++ b/embassy-rp/src/spi.rs @@ -479,6 +479,60 @@ mod eh1 { } } +cfg_if::cfg_if! { + if #[cfg(all(feature = "unstable-traits", feature = "nightly"))] { + use core::future::Future; + impl<'d, T: Instance> embedded_hal_async::spi::SpiBusFlush for Spi<'d, T, Async> { + type FlushFuture<'a> = impl Future> + 'a where Self: 'a; + + fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { + async { Ok(()) } + } + } + + impl<'d, T: Instance> embedded_hal_async::spi::SpiBusWrite + for Spi<'d, T, Async> + { + type WriteFuture<'a> = impl Future> + 'a where Self: 'a; + + fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { + self.write(data) + } + } + + impl<'d, T: Instance> embedded_hal_async::spi::SpiBusRead + for Spi<'d, T, Async> + { + type ReadFuture<'a> = impl Future> + 'a where Self: 'a; + + fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { + self.read(data) + } + } + + impl<'d, T: Instance> embedded_hal_async::spi::SpiBus + for Spi<'d, T, Async> + { + type TransferFuture<'a> = impl Future> + 'a where Self: 'a; + + fn transfer<'a>(&'a mut self, rx: &'a mut [u8], tx: &'a [u8]) -> Self::TransferFuture<'a> { + self.transfer(rx, tx) + } + + type TransferInPlaceFuture<'a> = impl Future> + 'a where Self: 'a; + + fn transfer_in_place<'a>( + &'a mut self, + words: &'a mut [u8], + ) -> Self::TransferInPlaceFuture<'a> { + let (ptr, len) = crate::dma::slice_ptr_parts(words); + let tx_buffer = unsafe { core::slice::from_raw_parts(ptr as *const _, len) }; + self.transfer(words, tx_buffer) + } + } + } +} + impl<'d, T: Instance, M: Mode> SetConfig for Spi<'d, T, M> { type Config = Config; fn set_config(&mut self, config: &Self::Config) { diff --git a/examples/rp/src/bin/spi_async.rs b/examples/rp/src/bin/spi_async.rs index f21377ed..359ad50e 100644 --- a/examples/rp/src/bin/spi_async.rs +++ b/examples/rp/src/bin/spi_async.rs @@ -4,10 +4,8 @@ use defmt::*; use embassy_executor::Spawner; -use embassy_rp::spi::{Async, Spi}; -use embassy_rp::{gpio, spi}; +use embassy_rp::spi::{Async, Config, Spi}; use embassy_time::{Duration, Timer}; -use gpio::{Level, Output}; use {defmt_rtt as _, panic_probe as _}; #[embassy_executor::main] @@ -19,7 +17,7 @@ async fn main(_spawner: Spawner) { let mosi = p.PIN_11; let clk = p.PIN_10; - let mut spi: Spi<'_, _, Async> = Spi::new(p.SPI1, p.DMA_CH0, p.DMA_CH1, clk, mosi, miso, spi::Config::default()); + let mut spi: Spi<'_, _, Async> = Spi::new(p.SPI1, p.DMA_CH0, p.DMA_CH1, clk, mosi, miso, Config::default()); loop { let tx_buf = [1_u8, 2, 3, 4, 5, 6];