diff options
| author | Henrik Alsér <[email protected]> | 2022-08-30 01:18:28 +0200 |
|---|---|---|
| committer | Henrik Alsér <[email protected]> | 2022-09-01 15:12:43 +0200 |
| commit | 44150c483017c18979e58d8557aac3df031ba47a (patch) | |
| tree | 10f92e7f1472f9e4c72781056766c9883e602f13 | |
| parent | 07c64d902e001ab0943382e9da35f9280a5533d0 (diff) | |
impl embedded-hal-async
| -rw-r--r-- | embassy-rp/src/spi.rs | 54 | ||||
| -rw-r--r-- | 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 a91a1fd19..be639504f 100644 --- a/embassy-rp/src/spi.rs +++ b/embassy-rp/src/spi.rs | |||
| @@ -479,6 +479,60 @@ mod eh1 { | |||
| 479 | } | 479 | } |
| 480 | } | 480 | } |
| 481 | 481 | ||
| 482 | cfg_if::cfg_if! { | ||
| 483 | if #[cfg(all(feature = "unstable-traits", feature = "nightly"))] { | ||
| 484 | use core::future::Future; | ||
| 485 | impl<'d, T: Instance> embedded_hal_async::spi::SpiBusFlush for Spi<'d, T, Async> { | ||
| 486 | type FlushFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 487 | |||
| 488 | fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a> { | ||
| 489 | async { Ok(()) } | ||
| 490 | } | ||
| 491 | } | ||
| 492 | |||
| 493 | impl<'d, T: Instance> embedded_hal_async::spi::SpiBusWrite<u8> | ||
| 494 | for Spi<'d, T, Async> | ||
| 495 | { | ||
| 496 | type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 497 | |||
| 498 | fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { | ||
| 499 | self.write(data) | ||
| 500 | } | ||
| 501 | } | ||
| 502 | |||
| 503 | impl<'d, T: Instance> embedded_hal_async::spi::SpiBusRead<u8> | ||
| 504 | for Spi<'d, T, Async> | ||
| 505 | { | ||
| 506 | type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 507 | |||
| 508 | fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { | ||
| 509 | self.read(data) | ||
| 510 | } | ||
| 511 | } | ||
| 512 | |||
| 513 | impl<'d, T: Instance> embedded_hal_async::spi::SpiBus<u8> | ||
| 514 | for Spi<'d, T, Async> | ||
| 515 | { | ||
| 516 | type TransferFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 517 | |||
| 518 | fn transfer<'a>(&'a mut self, rx: &'a mut [u8], tx: &'a [u8]) -> Self::TransferFuture<'a> { | ||
| 519 | self.transfer(rx, tx) | ||
| 520 | } | ||
| 521 | |||
| 522 | type TransferInPlaceFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | ||
| 523 | |||
| 524 | fn transfer_in_place<'a>( | ||
| 525 | &'a mut self, | ||
| 526 | words: &'a mut [u8], | ||
| 527 | ) -> Self::TransferInPlaceFuture<'a> { | ||
| 528 | let (ptr, len) = crate::dma::slice_ptr_parts(words); | ||
| 529 | let tx_buffer = unsafe { core::slice::from_raw_parts(ptr as *const _, len) }; | ||
| 530 | self.transfer(words, tx_buffer) | ||
| 531 | } | ||
| 532 | } | ||
| 533 | } | ||
| 534 | } | ||
| 535 | |||
| 482 | impl<'d, T: Instance, M: Mode> SetConfig for Spi<'d, T, M> { | 536 | impl<'d, T: Instance, M: Mode> SetConfig for Spi<'d, T, M> { |
| 483 | type Config = Config; | 537 | type Config = Config; |
| 484 | fn set_config(&mut self, config: &Self::Config) { | 538 | 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 f21377ede..359ad50e7 100644 --- a/examples/rp/src/bin/spi_async.rs +++ b/examples/rp/src/bin/spi_async.rs | |||
| @@ -4,10 +4,8 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::*; | 5 | use defmt::*; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_rp::spi::{Async, Spi}; | 7 | use embassy_rp::spi::{Async, Config, Spi}; |
| 8 | use embassy_rp::{gpio, spi}; | ||
| 9 | use embassy_time::{Duration, Timer}; | 8 | use embassy_time::{Duration, Timer}; |
| 10 | use gpio::{Level, Output}; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | 9 | use {defmt_rtt as _, panic_probe as _}; |
| 12 | 10 | ||
| 13 | #[embassy_executor::main] | 11 | #[embassy_executor::main] |
| @@ -19,7 +17,7 @@ async fn main(_spawner: Spawner) { | |||
| 19 | let mosi = p.PIN_11; | 17 | let mosi = p.PIN_11; |
| 20 | let clk = p.PIN_10; | 18 | let clk = p.PIN_10; |
| 21 | 19 | ||
| 22 | let mut spi: Spi<'_, _, Async> = Spi::new(p.SPI1, p.DMA_CH0, p.DMA_CH1, clk, mosi, miso, spi::Config::default()); | 20 | let mut spi: Spi<'_, _, Async> = Spi::new(p.SPI1, p.DMA_CH0, p.DMA_CH1, clk, mosi, miso, Config::default()); |
| 23 | 21 | ||
| 24 | loop { | 22 | loop { |
| 25 | let tx_buf = [1_u8, 2, 3, 4, 5, 6]; | 23 | let tx_buf = [1_u8, 2, 3, 4, 5, 6]; |
