From 8e2ada9dd6c17347bbfe618d0a26f7f787821f9e Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 10 Jan 2020 10:27:17 +1300 Subject: [PATCH 01/15] added rough transactional traits for SPI and I2C --- Cargo.toml | 2 +- src/blocking/i2c.rs | 25 +++++++++++++++++++++++++ src/blocking/spi.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0fc6ab3f7..d8a65177e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ repository = "https://github.com/rust-embedded/embedded-hal" version = "0.2.3" [dependencies.nb] -version = "0.1.1" +version = "0.1.2" [dev-dependencies] stm32f3 = { version = "0.8", features = ["stm32f303", "rt"] } diff --git a/src/blocking/i2c.rs b/src/blocking/i2c.rs index 57192cac8..5b5405769 100644 --- a/src/blocking/i2c.rs +++ b/src/blocking/i2c.rs @@ -127,3 +127,28 @@ pub trait WriteIterRead { where B: IntoIterator; } + +/// Actions for transactional I2C trait +/// +/// This allows composition of I2C operations into a single bus transaction +#[cfg(feature = "unproven")] +#[derive(Debug, PartialEq)] +pub enum Actions<'a> { + /// Read data into the provided buffer, write data undefined + Read(&'a mut [u8]), + /// Write data from the provided buffer, discarding read data + Write(&'a [u8]), +} + +/// Transactional trait allows multiple actions to be executed +/// as part of a single I2C transaction +#[cfg(feature = "unproven")] +pub trait Transactional { + /// Associated error type + type Error; + + /// Execute the provided actions + fn exec(&mut self, addr: u16, actions: &[Actions]) -> Result<(), Self::Error>; +} + + diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index 90dc27afb..202151c7b 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -104,3 +104,31 @@ pub mod write_iter { } } } + + +/// Actions for transactional SPI trait +/// +/// This allows composition of SPI operations into a single bus transaction +#[cfg(feature = "unproven")] +#[derive(Debug, PartialEq)] +pub enum Actions<'a> { + /// Read data into the provided buffer, write data undefined + Read(&'a mut [u8]), + /// Write data from the provided buffer, discarding read data + Write(&'a [u8]), + /// Transfer data from the provided buffer, overwriting the buffer with read data + Transfer(&'a mut [u8]), +} + +/// Transactional trait allows multiple actions to be executed +/// as part of a single SPI transaction +#[cfg(feature = "unproven")] +pub trait Transactional { + /// Associated error type + type Error; + + /// Execute the provided actions + fn exec(&mut self, actions: &[Actions]) -> Result<(), Self::Error>; +} + + From a9777467370df5c6c22a9cf83961e0c3fcd252dc Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 10 Jan 2020 10:38:10 +1300 Subject: [PATCH 02/15] fix docs for i2c::Action --- src/blocking/i2c.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/blocking/i2c.rs b/src/blocking/i2c.rs index 5b5405769..5aac1c536 100644 --- a/src/blocking/i2c.rs +++ b/src/blocking/i2c.rs @@ -134,9 +134,9 @@ pub trait WriteIterRead { #[cfg(feature = "unproven")] #[derive(Debug, PartialEq)] pub enum Actions<'a> { - /// Read data into the provided buffer, write data undefined + /// Read data into the provided buffer Read(&'a mut [u8]), - /// Write data from the provided buffer, discarding read data + /// Write data from the provided buffer Write(&'a [u8]), } @@ -147,8 +147,8 @@ pub trait Transactional { /// Associated error type type Error; - /// Execute the provided actions - fn exec(&mut self, addr: u16, actions: &[Actions]) -> Result<(), Self::Error>; + /// Execute the provided actions against the provided I2C address + fn exec(&mut self, addr: u8, actions: &[Actions]) -> Result<(), Self::Error>; } From fb0a26aef6c1a38ea2d5633f7743c3a3e99ac64b Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 10 Jan 2020 14:17:32 +1300 Subject: [PATCH 03/15] revert nb version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d8a65177e..0fc6ab3f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ repository = "https://github.com/rust-embedded/embedded-hal" version = "0.2.3" [dependencies.nb] -version = "0.1.2" +version = "0.1.1" [dev-dependencies] stm32f3 = { version = "0.8", features = ["stm32f303", "rt"] } From dcdbf1b16aeba7c590df55a2ab12a0cdb8abee1f Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 10 Jan 2020 14:23:40 +1300 Subject: [PATCH 04/15] cargo fmt --- src/blocking/i2c.rs | 6 ++---- src/blocking/spi.rs | 7 ++----- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/blocking/i2c.rs b/src/blocking/i2c.rs index 5aac1c536..e8f9ed24a 100644 --- a/src/blocking/i2c.rs +++ b/src/blocking/i2c.rs @@ -129,7 +129,7 @@ pub trait WriteIterRead { } /// Actions for transactional I2C trait -/// +/// /// This allows composition of I2C operations into a single bus transaction #[cfg(feature = "unproven")] #[derive(Debug, PartialEq)] @@ -146,9 +146,7 @@ pub enum Actions<'a> { pub trait Transactional { /// Associated error type type Error; - + /// Execute the provided actions against the provided I2C address fn exec(&mut self, addr: u8, actions: &[Actions]) -> Result<(), Self::Error>; } - - diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index 202151c7b..348b73922 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -105,9 +105,8 @@ pub mod write_iter { } } - /// Actions for transactional SPI trait -/// +/// /// This allows composition of SPI operations into a single bus transaction #[cfg(feature = "unproven")] #[derive(Debug, PartialEq)] @@ -126,9 +125,7 @@ pub enum Actions<'a> { pub trait Transactional { /// Associated error type type Error; - + /// Execute the provided actions fn exec(&mut self, actions: &[Actions]) -> Result<(), Self::Error>; } - - From d6e9f35f53f784e2392b8f51c607039714c44345 Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 11 Jan 2020 11:57:44 +1300 Subject: [PATCH 05/15] remove spi::Actions::Read --- src/blocking/spi.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index 348b73922..a564673d4 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -111,11 +111,9 @@ pub mod write_iter { #[cfg(feature = "unproven")] #[derive(Debug, PartialEq)] pub enum Actions<'a> { - /// Read data into the provided buffer, write data undefined - Read(&'a mut [u8]), /// Write data from the provided buffer, discarding read data Write(&'a [u8]), - /// Transfer data from the provided buffer, overwriting the buffer with read data + /// Transfer data from the provided buffer, overwriting the output buffer with read data Transfer(&'a mut [u8]), } From ee1080758fe35f60367f86c75a8acd2f9c7f914e Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 22 Jan 2020 08:47:23 +1300 Subject: [PATCH 06/15] fix mutability of actions --- src/blocking/i2c.rs | 2 +- src/blocking/spi.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/blocking/i2c.rs b/src/blocking/i2c.rs index e8f9ed24a..071defd60 100644 --- a/src/blocking/i2c.rs +++ b/src/blocking/i2c.rs @@ -148,5 +148,5 @@ pub trait Transactional { type Error; /// Execute the provided actions against the provided I2C address - fn exec(&mut self, addr: u8, actions: &[Actions]) -> Result<(), Self::Error>; + fn exec(&mut self, addr: u8, actions: &mut [Actions]) -> Result<(), Self::Error>; } diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index a564673d4..33b040e3e 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -125,5 +125,5 @@ pub trait Transactional { type Error; /// Execute the provided actions - fn exec(&mut self, actions: &[Actions]) -> Result<(), Self::Error>; + fn exec(&mut self, actions: &mut [Actions]) -> Result<(), Self::Error>; } From f3792c29ba271bf8afbc51282eace9feeabf998b Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 22 Jan 2020 09:24:08 +1300 Subject: [PATCH 07/15] Updated to AsMut --- src/blocking/i2c.rs | 3 ++- src/blocking/spi.rs | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/blocking/i2c.rs b/src/blocking/i2c.rs index 071defd60..02ecaf41d 100644 --- a/src/blocking/i2c.rs +++ b/src/blocking/i2c.rs @@ -148,5 +148,6 @@ pub trait Transactional { type Error; /// Execute the provided actions against the provided I2C address - fn exec(&mut self, addr: u8, actions: &mut [Actions]) -> Result<(), Self::Error>; + fn exec<'a, A>(&mut self, address: u8, actions: A) -> Result<(), Self::Error> + where A: AsMut<[Actions<'a>]>; } diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index 33b040e3e..386eda070 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -113,8 +113,8 @@ pub mod write_iter { pub enum Actions<'a> { /// Write data from the provided buffer, discarding read data Write(&'a [u8]), - /// Transfer data from the provided buffer, overwriting the output buffer with read data - Transfer(&'a mut [u8]), + /// Write data from one buffer while reading into the second buffer + WriteRead(&'a [u8], &'a mut [u8]), } /// Transactional trait allows multiple actions to be executed @@ -125,5 +125,6 @@ pub trait Transactional { type Error; /// Execute the provided actions - fn exec(&mut self, actions: &mut [Actions]) -> Result<(), Self::Error>; + fn exec<'a, A>(&mut self, actions: A) -> Result<(), Self::Error> + where A: AsMut<[Actions<'a>]>; } From 80894b1aebd68460de9c6327145f7e822a58e4ce Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 22 Jan 2020 09:29:17 +1300 Subject: [PATCH 08/15] Rename to transaction --- src/blocking/i2c.rs | 8 ++++---- src/blocking/spi.rs | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/blocking/i2c.rs b/src/blocking/i2c.rs index 02ecaf41d..3aa4762af 100644 --- a/src/blocking/i2c.rs +++ b/src/blocking/i2c.rs @@ -128,12 +128,12 @@ pub trait WriteIterRead { B: IntoIterator; } -/// Actions for transactional I2C trait +/// Transaction for transactional I2C trait /// /// This allows composition of I2C operations into a single bus transaction #[cfg(feature = "unproven")] #[derive(Debug, PartialEq)] -pub enum Actions<'a> { +pub enum Transaction<'a> { /// Read data into the provided buffer Read(&'a mut [u8]), /// Write data from the provided buffer @@ -148,6 +148,6 @@ pub trait Transactional { type Error; /// Execute the provided actions against the provided I2C address - fn exec<'a, A>(&mut self, address: u8, actions: A) -> Result<(), Self::Error> - where A: AsMut<[Actions<'a>]>; + fn exec<'a, T>(&mut self, address: u8, transactions: T) -> Result<(), Self::Error> + where T: AsMut<[Transaction<'a>]>; } diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index 386eda070..38a0b19f8 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -105,12 +105,12 @@ pub mod write_iter { } } -/// Actions for transactional SPI trait +/// Transactions for transactional SPI trait /// /// This allows composition of SPI operations into a single bus transaction #[cfg(feature = "unproven")] #[derive(Debug, PartialEq)] -pub enum Actions<'a> { +pub enum Transaction<'a> { /// Write data from the provided buffer, discarding read data Write(&'a [u8]), /// Write data from one buffer while reading into the second buffer @@ -124,7 +124,7 @@ pub trait Transactional { /// Associated error type type Error; - /// Execute the provided actions - fn exec<'a, A>(&mut self, actions: A) -> Result<(), Self::Error> - where A: AsMut<[Actions<'a>]>; + /// Execute the provided transactions + fn exec<'a, T>(&mut self, transactions: T) -> Result<(), Self::Error> + where T: AsMut<[Transaction<'a>]>; } From f69e354951983a2756cd7e7154e51a382548be94 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 22 Jan 2020 11:17:28 +1300 Subject: [PATCH 09/15] cargo fmt pass --- src/blocking/i2c.rs | 3 ++- src/blocking/spi.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/blocking/i2c.rs b/src/blocking/i2c.rs index 3aa4762af..a4b419888 100644 --- a/src/blocking/i2c.rs +++ b/src/blocking/i2c.rs @@ -149,5 +149,6 @@ pub trait Transactional { /// Execute the provided actions against the provided I2C address fn exec<'a, T>(&mut self, address: u8, transactions: T) -> Result<(), Self::Error> - where T: AsMut<[Transaction<'a>]>; + where + T: AsMut<[Transaction<'a>]>; } diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index 38a0b19f8..892940ad9 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -126,5 +126,6 @@ pub trait Transactional { /// Execute the provided transactions fn exec<'a, T>(&mut self, transactions: T) -> Result<(), Self::Error> - where T: AsMut<[Transaction<'a>]>; + where + T: AsMut<[Transaction<'a>]>; } From 187e41af9c47f9864b71d18aacaa584f6a98f3e3 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 22 Jan 2020 11:27:43 +1300 Subject: [PATCH 10/15] Rename transaction to operation --- src/blocking/i2c.rs | 8 ++++---- src/blocking/spi.rs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/blocking/i2c.rs b/src/blocking/i2c.rs index a4b419888..4b6a56360 100644 --- a/src/blocking/i2c.rs +++ b/src/blocking/i2c.rs @@ -128,12 +128,12 @@ pub trait WriteIterRead { B: IntoIterator; } -/// Transaction for transactional I2C trait +/// Operation for transactional I2C trait /// /// This allows composition of I2C operations into a single bus transaction #[cfg(feature = "unproven")] #[derive(Debug, PartialEq)] -pub enum Transaction<'a> { +pub enum Operation<'a> { /// Read data into the provided buffer Read(&'a mut [u8]), /// Write data from the provided buffer @@ -148,7 +148,7 @@ pub trait Transactional { type Error; /// Execute the provided actions against the provided I2C address - fn exec<'a, T>(&mut self, address: u8, transactions: T) -> Result<(), Self::Error> + fn exec<'a, O>(&mut self, address: u8, operations: O) -> Result<(), Self::Error> where - T: AsMut<[Transaction<'a>]>; + O: AsMut<[Operation<'a>]>; } diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index 892940ad9..10aa79dd3 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -105,12 +105,12 @@ pub mod write_iter { } } -/// Transactions for transactional SPI trait +/// Operation for transactional SPI trait /// /// This allows composition of SPI operations into a single bus transaction #[cfg(feature = "unproven")] #[derive(Debug, PartialEq)] -pub enum Transaction<'a> { +pub enum Operation<'a> { /// Write data from the provided buffer, discarding read data Write(&'a [u8]), /// Write data from one buffer while reading into the second buffer @@ -125,7 +125,7 @@ pub trait Transactional { type Error; /// Execute the provided transactions - fn exec<'a, T>(&mut self, transactions: T) -> Result<(), Self::Error> + fn exec<'a, O>(&mut self, operations: O) -> Result<(), Self::Error> where - T: AsMut<[Transaction<'a>]>; + O: AsMut<[Operation<'a>]>; } From 47df2eb8ad1a1f6f667310d4848c7ad62b6774b8 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 28 Jan 2020 09:59:54 +1300 Subject: [PATCH 11/15] make spi::Transactional generic over word lengths --- src/blocking/spi.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index 10aa79dd3..7a35c34c4 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -110,22 +110,22 @@ pub mod write_iter { /// This allows composition of SPI operations into a single bus transaction #[cfg(feature = "unproven")] #[derive(Debug, PartialEq)] -pub enum Operation<'a> { +pub enum Operation<'a, W> { /// Write data from the provided buffer, discarding read data - Write(&'a [u8]), + Write(&'a [W]), /// Write data from one buffer while reading into the second buffer - WriteRead(&'a [u8], &'a mut [u8]), + WriteRead(&'a [W], &'a mut [W]), } /// Transactional trait allows multiple actions to be executed /// as part of a single SPI transaction #[cfg(feature = "unproven")] -pub trait Transactional { +pub trait Transactional { /// Associated error type type Error; /// Execute the provided transactions fn exec<'a, O>(&mut self, operations: O) -> Result<(), Self::Error> where - O: AsMut<[Operation<'a>]>; + O: AsMut<[Operation<'a, W>]>; } From 371251e03142f969d671b28f563efa695c5bc64a Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 28 Jan 2020 10:02:46 +1300 Subject: [PATCH 12/15] added static bounds to word type --- 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 7a35c34c4..aeaf78de2 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -110,7 +110,7 @@ pub mod write_iter { /// This allows composition of SPI operations into a single bus transaction #[cfg(feature = "unproven")] #[derive(Debug, PartialEq)] -pub enum Operation<'a, W> { +pub enum Operation<'a, W: 'static> { /// Write data from the provided buffer, discarding read data Write(&'a [W]), /// Write data from one buffer while reading into the second buffer @@ -120,7 +120,7 @@ pub enum Operation<'a, W> { /// Transactional trait allows multiple actions to be executed /// as part of a single SPI transaction #[cfg(feature = "unproven")] -pub trait Transactional { +pub trait Transactional { /// Associated error type type Error; From 3a3b8e530a67e1f2fade1dcfaacea91057bd96dc Mon Sep 17 00:00:00 2001 From: Ryan Kurte Date: Thu, 27 Feb 2020 19:46:45 +1100 Subject: [PATCH 13/15] swap to single buffer for blocking::spi::Operation::WriteRead --- 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 aeaf78de2..d1645e7c9 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -114,7 +114,7 @@ pub enum Operation<'a, W: 'static> { /// Write data from the provided buffer, discarding read data Write(&'a [W]), /// Write data from one buffer while reading into the second buffer - WriteRead(&'a [W], &'a mut [W]), + WriteRead(&'a mut [W]), } /// Transactional trait allows multiple actions to be executed From 795aa8e4791738d54a5989fd1bdf958609595133 Mon Sep 17 00:00:00 2001 From: ryan Date: Wed, 4 Mar 2020 16:04:20 +1300 Subject: [PATCH 14/15] moved blocking::spi::Transactional to operation slice to allow generic traits --- src/blocking/spi.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index d1645e7c9..411007c38 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -125,7 +125,5 @@ pub trait Transactional { type Error; /// Execute the provided transactions - fn exec<'a, O>(&mut self, operations: O) -> Result<(), Self::Error> - where - O: AsMut<[Operation<'a, W>]>; + fn exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error>; } From 6e3038545728e0a703445f4cac2ade2359a63277 Mon Sep 17 00:00:00 2001 From: Ryan Kurte Date: Mon, 9 Mar 2020 21:52:32 +1300 Subject: [PATCH 15/15] removed generic bound from blocking::spi::Transactional --- src/blocking/i2c.rs | 4 +--- src/blocking/spi.rs | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/blocking/i2c.rs b/src/blocking/i2c.rs index 4b6a56360..e98ad3816 100644 --- a/src/blocking/i2c.rs +++ b/src/blocking/i2c.rs @@ -148,7 +148,5 @@ pub trait Transactional { type Error; /// Execute the provided actions against the provided I2C address - fn exec<'a, O>(&mut self, address: u8, operations: O) -> Result<(), Self::Error> - where - O: AsMut<[Operation<'a>]>; + fn exec<'a>(&mut self, address: u8, operations: &mut[Operation<'a>]) -> Result<(), Self::Error>; } diff --git a/src/blocking/spi.rs b/src/blocking/spi.rs index 411007c38..83e3e0f8d 100644 --- a/src/blocking/spi.rs +++ b/src/blocking/spi.rs @@ -113,7 +113,7 @@ pub mod write_iter { pub enum Operation<'a, W: 'static> { /// Write data from the provided buffer, discarding read data Write(&'a [W]), - /// Write data from one buffer while reading into the second buffer + /// Write data out while reading data into the provided buffer WriteRead(&'a mut [W]), }