From fbff6472539c12a7864d37d22e1dca9194980e52 Mon Sep 17 00:00:00 2001 From: Ryan Kurte Date: Mon, 9 Mar 2020 21:54:28 +1300 Subject: [PATCH 1/3] Added transactional SPI interface See: https://github.com/rust-embedded/embedded-hal/pull/178 for work to get here --- src/blocking/spi.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index b1e81bb8a..58680dd0e 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -102,3 +102,52 @@ pub mod write_iter { } } } + +/// Operation for transactional SPI trait +/// +/// This allows composition of SPI operations into a single bus transaction +#[derive(Debug, PartialEq)] +pub enum Operation<'a, W: 'static> { + /// Write data from the provided buffer, discarding read data + Write(&'a [W]), + /// Write data out while reading data into the provided buffer + Transfer(&'a mut [W]), +} + +/// Transactional trait allows multiple actions to be executed +/// as part of a single SPI transaction +pub trait Transactional { + /// Associated error type + type Error; + + /// Execute the provided transactions + fn try_exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error>; +} + +/// Blocking transactional impl over spi::Write and spi::Transfer +pub mod transactional { + use super::{Operation, Transfer, Write}; + + /// Default implementation of `blocking::spi::Transactional` for implementers of + /// `spi::Write` and `spi::Transfer` + pub trait Default {} + + impl super::Transactional for S + where + S: self::Default + Write + Transfer, + W: Copy + Clone, + { + type Error = E; + + fn try_exec<'a>(&mut self, operations: &mut [super::Operation<'a, W>]) -> Result<(), E> { + for op in operations { + match op { + Operation::Write(w) => self.try_write(w)?, + Operation::Transfer(t) => self.try_transfer(t).map(|_| ())?, + } + } + + Ok(()) + } + } +} From db7b4f26e5bb60f7fe5166b6a42d4d23c11125dd Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 25 Jun 2020 11:21:12 +1200 Subject: [PATCH 2/3] fixed default impl --- src/blocking/spi.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index 58680dd0e..b5a0ae211 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -130,11 +130,11 @@ pub mod transactional { /// Default implementation of `blocking::spi::Transactional` for implementers of /// `spi::Write` and `spi::Transfer` - pub trait Default {} + pub trait Default: Write + Transfer {} impl super::Transactional for S where - S: self::Default + Write + Transfer, + S: self::Default + Write + Transfer , W: Copy + Clone, { type Error = E; From c66420be3c90989143d8a86665f1ed802ede40bb Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 27 Oct 2020 11:58:55 +1300 Subject: [PATCH 3/3] apply cargo fmt --- src/blocking/spi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index b5a0ae211..93fad65c1 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -134,7 +134,7 @@ pub mod transactional { impl super::Transactional for S where - S: self::Default + Write + Transfer , + S: self::Default + Write + Transfer, W: Copy + Clone, { type Error = E;