From 5fee51fe0893bb418b69f328d0ff5e5b8647e56f Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Wed, 9 Aug 2023 22:06:38 -0700 Subject: [PATCH 01/62] IGNORE_NACK for wake --- src/transport.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/transport.rs b/src/transport.rs index f614440..8f8af0a 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -91,6 +91,22 @@ impl I2cTransport { Ok(()) } + fn send_wake(&mut self, wake_delay: Duration) -> Result { + // Number of times you want to send 0x00 + let num_zeros_to_send = 3; // You can change this to the desired number + let zeros = vec![0x00; num_zeros_to_send]; + + let write_msg = i2c_linux::Message::Write { + address: 0, // The address to send the wake command + data: &zeros, + flags: i2c_linux::WriteFlags::IGNORE_NACK, // Using IGNORE_NACK flag + }; + + self.port.i2c_transfer(&mut [write_msg])?; + thread::sleep(wake_delay); + Ok(()) + } + fn send_idle(&mut self) { let _ = self.send_buf(self.address, &[0x02]); } From 5c63e994e63b9a4d04f898570375ea37baff0a2c Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Wed, 9 Aug 2023 22:10:15 -0700 Subject: [PATCH 02/62] Update transport.rs --- src/transport.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 8f8af0a..0c358fa 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -85,12 +85,6 @@ impl I2cTransport { Ok(Self { port, address }) } - fn send_wake(&mut self, wake_delay: Duration) -> Result { - let _ = self.send_buf(0, &[0x00]); - thread::sleep(wake_delay); - Ok(()) - } - fn send_wake(&mut self, wake_delay: Duration) -> Result { // Number of times you want to send 0x00 let num_zeros_to_send = 3; // You can change this to the desired number From 1f4fba13f6d280cbd0c29fca0fdb9cfbf1cfca9e Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Wed, 9 Aug 2023 22:13:35 -0700 Subject: [PATCH 03/62] Update transport.rs --- src/transport.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport.rs b/src/transport.rs index 0c358fa..16527d0 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -100,7 +100,7 @@ impl I2cTransport { thread::sleep(wake_delay); Ok(()) } - + fn send_idle(&mut self) { let _ = self.send_buf(self.address, &[0x02]); } From 8e0d7dab4da53f85480435b9c2a575d82c4d992d Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Wed, 9 Aug 2023 23:13:09 -0700 Subject: [PATCH 04/62] Let's try NO_STOP too --- src/transport.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport.rs b/src/transport.rs index 16527d0..85eed7b 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -93,7 +93,7 @@ impl I2cTransport { let write_msg = i2c_linux::Message::Write { address: 0, // The address to send the wake command data: &zeros, - flags: i2c_linux::WriteFlags::IGNORE_NACK, // Using IGNORE_NACK flag + flags: i2c_linux::WriteFlags::IGNORE_NACK | i2c_linux::WriteFlags::NO_STOP, // Using IGNORE_NACK and NO_STOP flags }; self.port.i2c_transfer(&mut [write_msg])?; From d05a4406b81e775a469cf1903c886dd32a2ca3a3 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Wed, 9 Aug 2023 23:18:56 -0700 Subject: [PATCH 05/62] NO_START --- src/transport.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport.rs b/src/transport.rs index 85eed7b..31dc457 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -93,7 +93,7 @@ impl I2cTransport { let write_msg = i2c_linux::Message::Write { address: 0, // The address to send the wake command data: &zeros, - flags: i2c_linux::WriteFlags::IGNORE_NACK | i2c_linux::WriteFlags::NO_STOP, // Using IGNORE_NACK and NO_STOP flags + flags: i2c_linux::WriteFlags::IGNORE_NACK | i2c_linux::WriteFlags::NO_START, // Using IGNORE_NACK and NO_START flags }; self.port.i2c_transfer(&mut [write_msg])?; From 957d9fee2d934a9fe68c18bd14ac8e5201f83d89 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Wed, 9 Aug 2023 23:46:49 -0700 Subject: [PATCH 06/62] get port.i2c_functionality --- src/transport.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/transport.rs b/src/transport.rs index 31dc457..0e430eb 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -86,6 +86,10 @@ impl I2cTransport { } fn send_wake(&mut self, wake_delay: Duration) -> Result { + + let f = self.port.i2c_functionality()?; + println!("Supported functionality: {:?}", f); + // Number of times you want to send 0x00 let num_zeros_to_send = 3; // You can change this to the desired number let zeros = vec![0x00; num_zeros_to_send]; From 84a43dc754edff605c8c3873c59bda6ea7524fb6 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 01:10:08 -0700 Subject: [PATCH 07/62] use gpio wake --- src/transport.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 0e430eb..3396146 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,5 +1,6 @@ use bytes::{BufMut, BytesMut}; use std::{fs::File, thread, time::Duration}; +use rppal::gpio::{Gpio, Mode}; use crate::constants::{ ATCA_I2C_COMMAND_FLAG, ATCA_RSP_SIZE_MAX, ATCA_SWI_COMMAND_FLAG, ATCA_SWI_IDLE_FLAG, @@ -87,20 +88,23 @@ impl I2cTransport { fn send_wake(&mut self, wake_delay: Duration) -> Result { - let f = self.port.i2c_functionality()?; - println!("Supported functionality: {:?}", f); - - // Number of times you want to send 0x00 - let num_zeros_to_send = 3; // You can change this to the desired number - let zeros = vec![0x00; num_zeros_to_send]; + // Create a new Gpio instance + let gpio = Gpio::new()?; - let write_msg = i2c_linux::Message::Write { - address: 0, // The address to send the wake command - data: &zeros, - flags: i2c_linux::WriteFlags::IGNORE_NACK | i2c_linux::WriteFlags::NO_START, // Using IGNORE_NACK and NO_START flags - }; + // Get the SDA and SCL pins + let mut sda_pin = gpio.get(2)?.into_output(); + let mut scl_pin = gpio.get(3)?.into_output(); - self.port.i2c_transfer(&mut [write_msg])?; + // Set the SDA and SCL pins low + sda_pin.set_low(); + scl_pin.set_low(); + + // Hold them low for 60 microseconds + thread::sleep(Duration::from_micros(60)); + + // Switch the pins back to Alt0 mode (i2c) + sda_pin.set_mode(Mode::Alt0); + scl_pin.set_mode(Mode::Alt0); thread::sleep(wake_delay); Ok(()) } From 2837cfe3a2802bb37e8c98cf7a1ac75e7c1bdb0c Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 01:12:37 -0700 Subject: [PATCH 08/62] add rppal --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 23f8a40..a477304 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ bitfield = "0" serde = { version = "1", features = ["derive"] } serde_derive = "1" thiserror = "1" +rppal = "0.14.1" From 5d7fae5aa117710fe88df2b693d6797c15211f43 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 01:24:36 -0700 Subject: [PATCH 09/62] Update transport.rs --- src/transport.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 3396146..6addaa7 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -91,9 +91,13 @@ impl I2cTransport { // Create a new Gpio instance let gpio = Gpio::new()?; - // Get the SDA and SCL pins - let mut sda_pin = gpio.get(2)?.into_output(); - let mut scl_pin = gpio.get(3)?.into_output(); + // Retrieve the SDA and SCL pins as general pins + let mut sda_pin = gpio.get(2)?; + let mut scl_pin = gpio.get(3)?; + + // Set the SDA and SCL pins to Output mode + sda_pin.set_mode(Mode::Output); + scl_pin.set_mode(Mode::Output); // Set the SDA and SCL pins low sda_pin.set_low(); From 85caae692a1b1cf8b3606ba60075214f6a858274 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 01:30:49 -0700 Subject: [PATCH 10/62] Update transport.rs --- src/transport.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 6addaa7..2b1b9fc 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -91,13 +91,9 @@ impl I2cTransport { // Create a new Gpio instance let gpio = Gpio::new()?; - // Retrieve the SDA and SCL pins as general pins - let mut sda_pin = gpio.get(2)?; - let mut scl_pin = gpio.get(3)?; - - // Set the SDA and SCL pins to Output mode - sda_pin.set_mode(Mode::Output); - scl_pin.set_mode(Mode::Output); + // Retrieve the SDA and SCL pins as output pins + let mut sda_pin = gpio.get(2)?.into_output(); + let mut scl_pin = gpio.get(3)?.into_output(); // Set the SDA and SCL pins low sda_pin.set_low(); From 98aac255a0de8f3371376d968f480da96d64a4ff Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 01:35:59 -0700 Subject: [PATCH 11/62] Update transport.rs --- src/transport.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 2b1b9fc..6912559 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -103,8 +103,8 @@ impl I2cTransport { thread::sleep(Duration::from_micros(60)); // Switch the pins back to Alt0 mode (i2c) - sda_pin.set_mode(Mode::Alt0); - scl_pin.set_mode(Mode::Alt0); + sda_pin.into_io; + scl_pin.into_io; thread::sleep(wake_delay); Ok(()) } From debf33994f8fb85ab71e693110432fb5631f8ef0 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 01:43:09 -0700 Subject: [PATCH 12/62] Update Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a477304..4b5560a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,4 @@ bitfield = "0" serde = { version = "1", features = ["derive"] } serde_derive = "1" thiserror = "1" -rppal = "0.14.1" +rppal = { version = "0.14.1", features = ["hal"] } From a4bb22e82e67a3d2dd1ae3bd71370a6442decf5f Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 01:46:21 -0700 Subject: [PATCH 13/62] Update transport.rs --- src/transport.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 6912559..865d540 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -103,8 +103,8 @@ impl I2cTransport { thread::sleep(Duration::from_micros(60)); // Switch the pins back to Alt0 mode (i2c) - sda_pin.into_io; - scl_pin.into_io; + // sda_pin.into_io; + // scl_pin.into_io; thread::sleep(wake_delay); Ok(()) } From 1fa3576eb7f5c332d0e4b21dcc808a90d632968a Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 01:52:38 -0700 Subject: [PATCH 14/62] Update Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 4b5560a..a477304 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,4 @@ bitfield = "0" serde = { version = "1", features = ["derive"] } serde_derive = "1" thiserror = "1" -rppal = { version = "0.14.1", features = ["hal"] } +rppal = "0.14.1" From 4dc3207646139182802f570e0fafb3465d2056f7 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 02:04:01 -0700 Subject: [PATCH 15/62] Update transport.rs --- src/transport.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 865d540..7d599d3 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -86,14 +86,13 @@ impl I2cTransport { Ok(Self { port, address }) } - fn send_wake(&mut self, wake_delay: Duration) -> Result { - + fn send_wake(&mut self, wake_delay: Duration) -> Result<(), error::Error> { // Create a new Gpio instance - let gpio = Gpio::new()?; + let gpio = Gpio::new().map_err(|e| error::Error::from(e))?; // Retrieve the SDA and SCL pins as output pins - let mut sda_pin = gpio.get(2)?.into_output(); - let mut scl_pin = gpio.get(3)?.into_output(); + let mut sda_pin = gpio.get(2)?.into_output().map_err(|e| error::Error::from(e))?; + let mut scl_pin = gpio.get(3)?.into_output().map_err(|e| error::Error::from(e))?; // Set the SDA and SCL pins low sda_pin.set_low(); @@ -101,10 +100,7 @@ impl I2cTransport { // Hold them low for 60 microseconds thread::sleep(Duration::from_micros(60)); - - // Switch the pins back to Alt0 mode (i2c) - // sda_pin.into_io; - // scl_pin.into_io; + thread::sleep(wake_delay); Ok(()) } From e41f3f89b3c32edc642364bb2c370fe84a7b55fd Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 02:10:58 -0700 Subject: [PATCH 16/62] Update transport.rs --- src/transport.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 7d599d3..a7605b1 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -86,13 +86,13 @@ impl I2cTransport { Ok(Self { port, address }) } - fn send_wake(&mut self, wake_delay: Duration) -> Result<(), error::Error> { + fn send_wake(&mut self, wake_delay: Duration) -> Result { // Create a new Gpio instance - let gpio = Gpio::new().map_err(|e| error::Error::from(e))?; + let gpio = Gpio::new().map_err(Error::from)?; // Retrieve the SDA and SCL pins as output pins - let mut sda_pin = gpio.get(2)?.into_output().map_err(|e| error::Error::from(e))?; - let mut scl_pin = gpio.get(3)?.into_output().map_err(|e| error::Error::from(e))?; + let mut sda_pin = gpio.get(2)?.into_output().map_err(Error::from)?; + let mut scl_pin = gpio.get(3)?.into_output().map_err(Error::from)?; // Set the SDA and SCL pins low sda_pin.set_low(); From 76b02ea5e0a771c0153c0e1785fc930e5e5ff76b Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 09:20:01 -0700 Subject: [PATCH 17/62] Update transport.rs --- src/transport.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index a7605b1..ae1ecb7 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -88,11 +88,11 @@ impl I2cTransport { fn send_wake(&mut self, wake_delay: Duration) -> Result { // Create a new Gpio instance - let gpio = Gpio::new().map_err(Error::from)?; + let gpio = Gpio::new()?; // Retrieve the SDA and SCL pins as output pins - let mut sda_pin = gpio.get(2)?.into_output().map_err(Error::from)?; - let mut scl_pin = gpio.get(3)?.into_output().map_err(Error::from)?; + let mut sda_pin = gpio.get(2)?.into_output(); + let mut scl_pin = gpio.get(3)?.into_output(); // Set the SDA and SCL pins low sda_pin.set_low(); From c03f9ac93ed3e9b4c9b7125b0a7d053ca8c571cf Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 09:23:29 -0700 Subject: [PATCH 18/62] Update transport.rs --- src/transport.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport.rs b/src/transport.rs index ae1ecb7..d89a9a6 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -86,7 +86,7 @@ impl I2cTransport { Ok(Self { port, address }) } - fn send_wake(&mut self, wake_delay: Duration) -> Result { + fn send_wake(&mut self, wake_delay: Duration) -> Result<(), Box> { // Create a new Gpio instance let gpio = Gpio::new()?; From b6eb6696aadf4ac6e0829c28590491e86c3a7ada Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 09:30:16 -0700 Subject: [PATCH 19/62] Update transport.rs --- src/transport.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport.rs b/src/transport.rs index d89a9a6..ae1ecb7 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -86,7 +86,7 @@ impl I2cTransport { Ok(Self { port, address }) } - fn send_wake(&mut self, wake_delay: Duration) -> Result<(), Box> { + fn send_wake(&mut self, wake_delay: Duration) -> Result { // Create a new Gpio instance let gpio = Gpio::new()?; From 4b4e091639ad94e769895b0e6b11593191a14305 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 09:37:17 -0700 Subject: [PATCH 20/62] Update transport.rs --- src/transport.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/transport.rs b/src/transport.rs index ae1ecb7..f5669de 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -41,6 +41,15 @@ impl From for TransportProtocol { } } +impl From for Error { + fn from(err: rppal::gpio::Error) -> Self { + // Here, you'll need to map or convert the `rppal::gpio::Error` into an appropriate variant or representation of your custom `Error` type + // This will depend on how your custom `Error` type is structured + // As an example, you might have something like this: + Error::GpioError(err) + } +} + impl TransportProtocol { pub fn send_wake(&mut self, wake_delay: Duration) -> Result { match self { From 0599d9afb25af03aa3f9aa71db5ed8da4eb584d4 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 09:49:32 -0700 Subject: [PATCH 21/62] Update transport.rs --- src/transport.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport.rs b/src/transport.rs index f5669de..6b4f698 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -46,7 +46,7 @@ impl From for Error { // Here, you'll need to map or convert the `rppal::gpio::Error` into an appropriate variant or representation of your custom `Error` type // This will depend on how your custom `Error` type is structured // As an example, you might have something like this: - Error::GpioError(err) + Error::SerialPort(err) } } From 3509a0ab5e2789a39a92cdc5726e9bba842a84a9 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 09:58:25 -0700 Subject: [PATCH 22/62] Update transport.rs --- src/transport.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/transport.rs b/src/transport.rs index 6b4f698..82daa3c 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -46,7 +46,8 @@ impl From for Error { // Here, you'll need to map or convert the `rppal::gpio::Error` into an appropriate variant or representation of your custom `Error` type // This will depend on how your custom `Error` type is structured // As an example, you might have something like this: - Error::SerialPort(err) + //Error::SerialPort(err) + Error::timeout() } } From 3d4c0f9599d38fe9447df80a26cd97a0eeac7920 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 10:09:43 -0700 Subject: [PATCH 23/62] Update transport.rs --- src/transport.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/transport.rs b/src/transport.rs index 82daa3c..76c44a6 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -110,6 +110,9 @@ impl I2cTransport { // Hold them low for 60 microseconds thread::sleep(Duration::from_micros(60)); + + sda_pin.set_high(); + scl_pin.set_high(); thread::sleep(wake_delay); Ok(()) From bb040c2f9e06d76b1a56e5145d1ef15c0573cefd Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 10:13:36 -0700 Subject: [PATCH 24/62] Update transport.rs --- src/transport.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/transport.rs b/src/transport.rs index 76c44a6..8548377 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -113,6 +113,9 @@ impl I2cTransport { sda_pin.set_high(); scl_pin.set_high(); + + sda_pin.set_mode(Mode::Alt0); + scl_pin.set_mode(Mode::Alt0); thread::sleep(wake_delay); Ok(()) From c5e9d2dda746d04e7cdef0d36e835b491f3135c6 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 10:19:21 -0700 Subject: [PATCH 25/62] Update transport.rs --- src/transport.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 8548377..fe2dd8a 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,6 +1,7 @@ use bytes::{BufMut, BytesMut}; use std::{fs::File, thread, time::Duration}; use rppal::gpio::{Gpio, Mode}; +use rppal::i2c::I2c; use crate::constants::{ ATCA_I2C_COMMAND_FLAG, ATCA_RSP_SIZE_MAX, ATCA_SWI_COMMAND_FLAG, ATCA_SWI_IDLE_FLAG, @@ -114,8 +115,8 @@ impl I2cTransport { sda_pin.set_high(); scl_pin.set_high(); - sda_pin.set_mode(Mode::Alt0); - scl_pin.set_mode(Mode::Alt0); + // Switch back to i2c mode + let mut i2c = I2c::new()?; thread::sleep(wake_delay); Ok(()) From 0b137a13742d82a99fb53cb6adec4b1fb7e40e46 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 10:31:29 -0700 Subject: [PATCH 26/62] Update transport.rs --- src/transport.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index fe2dd8a..d5e8185 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,7 +1,6 @@ use bytes::{BufMut, BytesMut}; use std::{fs::File, thread, time::Duration}; use rppal::gpio::{Gpio, Mode}; -use rppal::i2c::I2c; use crate::constants::{ ATCA_I2C_COMMAND_FLAG, ATCA_RSP_SIZE_MAX, ATCA_SWI_COMMAND_FLAG, ATCA_SWI_IDLE_FLAG, @@ -102,10 +101,14 @@ impl I2cTransport { let gpio = Gpio::new()?; // Retrieve the SDA and SCL pins as output pins - let mut sda_pin = gpio.get(2)?.into_output(); - let mut scl_pin = gpio.get(3)?.into_output(); + let mut sda_pin = gpio.get(2); + let mut scl_pin = gpio.get(3); - // Set the SDA and SCL pins low + // Set pin mode to output + sda_pin.set_mode(Mode::Output); + scl_pin.set_mode(Mode::Output); + + // Send the wake pulse sda_pin.set_low(); scl_pin.set_low(); @@ -114,9 +117,6 @@ impl I2cTransport { sda_pin.set_high(); scl_pin.set_high(); - - // Switch back to i2c mode - let mut i2c = I2c::new()?; thread::sleep(wake_delay); Ok(()) From 504caba5577d34ccbb359028bcdb4c8adc4a6617 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 10:39:43 -0700 Subject: [PATCH 27/62] Update transport.rs --- src/transport.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index d5e8185..3f36a5d 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -101,12 +101,8 @@ impl I2cTransport { let gpio = Gpio::new()?; // Retrieve the SDA and SCL pins as output pins - let mut sda_pin = gpio.get(2); - let mut scl_pin = gpio.get(3); - - // Set pin mode to output - sda_pin.set_mode(Mode::Output); - scl_pin.set_mode(Mode::Output); + let mut sda_pin = gpio.get(2)?.into_output(); + let mut scl_pin = gpio.get(3)?.into_output(); // Send the wake pulse sda_pin.set_low(); @@ -117,6 +113,8 @@ impl I2cTransport { sda_pin.set_high(); scl_pin.set_high(); + + gpio.close(); thread::sleep(wake_delay); Ok(()) From ef87a3bc3b3271619ed4a240ed7ec336b5e07291 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 10 Aug 2023 10:48:11 -0700 Subject: [PATCH 28/62] Update transport.rs --- src/transport.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/transport.rs b/src/transport.rs index 3f36a5d..9c0fe7a 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -114,7 +114,9 @@ impl I2cTransport { sda_pin.set_high(); scl_pin.set_high(); - gpio.close(); + // Drop pins + drop(sda_pin); + drop(scl_pin); thread::sleep(wake_delay); Ok(()) From 8cf20322dbdabcdc4e5524d3bb47a4d1225a8c7d Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:37:26 -0700 Subject: [PATCH 29/62] check for raspberry pi --- src/transport.rs | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 9c0fe7a..de50ae2 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,6 +1,8 @@ use bytes::{BufMut, BytesMut}; use std::{fs::File, thread, time::Duration}; use rppal::gpio::{Gpio, Mode}; +use rppal::system::DeviceInfo; +use lazy_static::lazy_static; use crate::constants::{ ATCA_I2C_COMMAND_FLAG, ATCA_RSP_SIZE_MAX, ATCA_SWI_COMMAND_FLAG, ATCA_SWI_IDLE_FLAG, @@ -16,6 +18,11 @@ const RECV_RETRIES: u8 = 3; const SWI_DEFAULT_BAUDRATE: u32 = 230_400; const SWI_WAKE_BAUDRATE: u32 = 115_200; const SWI_BIT_SEND_DELAY: Duration = Duration::from_micros(45); + +lazy_static! { + static ref IS_RASPI: bool = rppal::system::DeviceInfo::new().is_ok(); +} + pub struct I2cTransport { port: I2c, address: u16, @@ -97,27 +104,30 @@ impl I2cTransport { } fn send_wake(&mut self, wake_delay: Duration) -> Result { - // Create a new Gpio instance - let gpio = Gpio::new()?; + if *IS_RASPI { + // Create a new Gpio instance + let gpio = Gpio::new()?; - // Retrieve the SDA and SCL pins as output pins - let mut sda_pin = gpio.get(2)?.into_output(); - let mut scl_pin = gpio.get(3)?.into_output(); + // Retrieve the SDA and SCL pins as output pins + let mut sda_pin = gpio.get(2)?.into_output(); + let mut scl_pin = gpio.get(3)?.into_output(); - // Send the wake pulse - sda_pin.set_low(); - scl_pin.set_low(); + // Send the wake pulse + sda_pin.set_low(); + scl_pin.set_low(); - // Hold them low for 60 microseconds - thread::sleep(Duration::from_micros(60)); + // Hold them low for 60 microseconds + thread::sleep(Duration::from_micros(60)); - sda_pin.set_high(); - scl_pin.set_high(); + sda_pin.set_high(); + scl_pin.set_high(); - // Drop pins - drop(sda_pin); - drop(scl_pin); - + // Drop pins + drop(sda_pin); + drop(scl_pin); + } else { + let _ = self.send_buf(0, &[0x00]); + } thread::sleep(wake_delay); Ok(()) } From bd4ca14ed6650d0272a75f31b8bd37d39a4184e1 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:37:41 -0700 Subject: [PATCH 30/62] add lazy_static = "1.4.0" --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index a477304..0979710 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,4 @@ serde = { version = "1", features = ["derive"] } serde_derive = "1" thiserror = "1" rppal = "0.14.1" +lazy_static = "1.4.0" From cad9cf7ba5b5e00ae877ca764eb87ea8cfe57df1 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Fri, 11 Aug 2023 12:01:15 -0700 Subject: [PATCH 31/62] use GW_SCL_PIN and GW_SDA_PIN env vars --- src/transport.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index de50ae2..2d9edfd 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,5 +1,5 @@ use bytes::{BufMut, BytesMut}; -use std::{fs::File, thread, time::Duration}; +use std::{fs::File, thread, time::Duration, env}; use rppal::gpio::{Gpio, Mode}; use rppal::system::DeviceInfo; use lazy_static::lazy_static; @@ -13,6 +13,9 @@ use crate::{Error, Result}; use i2c_linux::I2c; use serialport::{ClearBuffer, SerialPort}; +const DEFAULT_SCL_PIN: u8 = 3; // Replace with your default SCL pin +const DEFAULT_SDA_PIN: u8 = 2; // Replace with your default SDA pin + const RECV_RETRY_WAIT: Duration = Duration::from_millis(4); const RECV_RETRIES: u8 = 3; const SWI_DEFAULT_BAUDRATE: u32 = 230_400; @@ -105,12 +108,23 @@ impl I2cTransport { fn send_wake(&mut self, wake_delay: Duration) -> Result { if *IS_RASPI { + + let scl_pin_number: u8 = env::var("GW_SCL_PIN") + .unwrap_or_else(|_| DEFAULT_SCL_PIN.to_string()) + .parse() + .unwrap_or(DEFAULT_SCL_PIN); + + let sda_pin_number: u8 = env::var("GW_SDA_PIN") + .unwrap_or_else(|_| DEFAULT_SDA_PIN.to_string()) + .parse() + .unwrap_or(DEFAULT_SDA_PIN); + // Create a new Gpio instance let gpio = Gpio::new()?; // Retrieve the SDA and SCL pins as output pins - let mut sda_pin = gpio.get(2)?.into_output(); - let mut scl_pin = gpio.get(3)?.into_output(); + let mut sda_pin = gpio.get(sda_pin_number)?.into_output(); + let mut scl_pin = gpio.get(scl_pin_number)?.into_output(); // Send the wake pulse sda_pin.set_low(); From ca0bf03103a0e673aa82210cb24ea3e0189e9192 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Fri, 11 Aug 2023 12:26:11 -0700 Subject: [PATCH 32/62] Update transport.rs --- src/transport.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 2d9edfd..489447d 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,7 +1,6 @@ use bytes::{BufMut, BytesMut}; use std::{fs::File, thread, time::Duration, env}; -use rppal::gpio::{Gpio, Mode}; -use rppal::system::DeviceInfo; +use rppal::{gpio::Gpio, gpio::Mode, system::DeviceInfo}; use lazy_static::lazy_static; use crate::constants::{ From ddae1b662875bc8d76de77411f65d3b5cbeb0c42 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 21:44:29 -0700 Subject: [PATCH 33/62] made rppal a feature --- Cargo.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 0979710..dfb82ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,6 @@ serde_derive = "1" thiserror = "1" rppal = "0.14.1" lazy_static = "1.4.0" + +[features] +rppal = [] From 43771b2bbb445d0cfefd24851d4c2021baf3804f Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 21:48:45 -0700 Subject: [PATCH 34/62] use feature flag --- src/transport.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/transport.rs b/src/transport.rs index 489447d..843fe79 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,7 +1,8 @@ use bytes::{BufMut, BytesMut}; use std::{fs::File, thread, time::Duration, env}; -use rppal::{gpio::Gpio, gpio::Mode, system::DeviceInfo}; use lazy_static::lazy_static; +#[cfg(feature = "rppal")] +use rppal::{gpio::Gpio, gpio::Mode, system::DeviceInfo}; use crate::constants::{ ATCA_I2C_COMMAND_FLAG, ATCA_RSP_SIZE_MAX, ATCA_SWI_COMMAND_FLAG, ATCA_SWI_IDLE_FLAG, From 168436d7e0f0867f7d6061a49e9f22a72d6cbf85 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 21:59:19 -0700 Subject: [PATCH 35/62] Update transport.rs --- src/transport.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 843fe79..e718300 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -22,10 +22,11 @@ const SWI_DEFAULT_BAUDRATE: u32 = 230_400; const SWI_WAKE_BAUDRATE: u32 = 115_200; const SWI_BIT_SEND_DELAY: Duration = Duration::from_micros(45); -lazy_static! { - static ref IS_RASPI: bool = rppal::system::DeviceInfo::new().is_ok(); +if cfg!(feature = "rppal") { + lazy_static! { + static ref IS_RASPI: bool = rppal::system::DeviceInfo::new().is_ok(); + } } - pub struct I2cTransport { port: I2c, address: u16, @@ -107,7 +108,7 @@ impl I2cTransport { } fn send_wake(&mut self, wake_delay: Duration) -> Result { - if *IS_RASPI { + if cfg!(feature = "rppal") && *IS_RASPI { let scl_pin_number: u8 = env::var("GW_SCL_PIN") .unwrap_or_else(|_| DEFAULT_SCL_PIN.to_string()) From 4594a0933770e2fd6ec8f47df01e9c157f9ebc2f Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 22:03:49 -0700 Subject: [PATCH 36/62] Update transport.rs --- src/transport.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index e718300..0e93714 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -22,11 +22,12 @@ const SWI_DEFAULT_BAUDRATE: u32 = 230_400; const SWI_WAKE_BAUDRATE: u32 = 115_200; const SWI_BIT_SEND_DELAY: Duration = Duration::from_micros(45); -if cfg!(feature = "rppal") { - lazy_static! { - static ref IS_RASPI: bool = rppal::system::DeviceInfo::new().is_ok(); - } -} +#[cfg(feature = "rppal")] +lazy_static! { static ref IS_RASPI: bool = rppal::system::DeviceInfo::new().is_ok(); } + +#[cfg(feature = "default")] +lazy_static! { static ref IS_RASPI: bool = false; } + pub struct I2cTransport { port: I2c, address: u16, @@ -108,7 +109,7 @@ impl I2cTransport { } fn send_wake(&mut self, wake_delay: Duration) -> Result { - if cfg!(feature = "rppal") && *IS_RASPI { + if *IS_RASPI { let scl_pin_number: u8 = env::var("GW_SCL_PIN") .unwrap_or_else(|_| DEFAULT_SCL_PIN.to_string()) From cce3e46fa11663c733275de8375667dc724b9818 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 22:05:29 -0700 Subject: [PATCH 37/62] Update Cargo.toml --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index dfb82ce..c539742 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,5 @@ rppal = "0.14.1" lazy_static = "1.4.0" [features] +default = [] rppal = [] From dd35a28d9aa0a256687ce378744b6b7ed3f396af Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 22:08:50 -0700 Subject: [PATCH 38/62] Update transport.rs --- src/transport.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport.rs b/src/transport.rs index 0e93714..5e61ec5 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -25,7 +25,7 @@ const SWI_BIT_SEND_DELAY: Duration = Duration::from_micros(45); #[cfg(feature = "rppal")] lazy_static! { static ref IS_RASPI: bool = rppal::system::DeviceInfo::new().is_ok(); } -#[cfg(feature = "default")] +#[cfg(not(feature = "rppal"))] lazy_static! { static ref IS_RASPI: bool = false; } pub struct I2cTransport { From 15d7b53941d0ff965edc7a4b65755274e3b4a1ac Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 22:51:37 -0700 Subject: [PATCH 39/62] Update transport.rs --- src/transport.rs | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 5e61ec5..594de5f 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -121,26 +121,31 @@ impl I2cTransport { .parse() .unwrap_or(DEFAULT_SDA_PIN); - // Create a new Gpio instance - let gpio = Gpio::new()?; + #[cfg(feature = "rppal")] + { + // Create a new Gpio instance + let gpio = Gpio::new()?; - // Retrieve the SDA and SCL pins as output pins - let mut sda_pin = gpio.get(sda_pin_number)?.into_output(); - let mut scl_pin = gpio.get(scl_pin_number)?.into_output(); + // Retrieve the SDA and SCL pins as output pins + let mut sda_pin = gpio.get(sda_pin_number)?.into_output(); + let mut scl_pin = gpio.get(scl_pin_number)?.into_output(); - // Send the wake pulse - sda_pin.set_low(); - scl_pin.set_low(); + // Send the wake pulse + sda_pin.set_low(); + scl_pin.set_low(); - // Hold them low for 60 microseconds - thread::sleep(Duration::from_micros(60)); + // Hold them low for 60 microseconds + thread::sleep(Duration::from_micros(60)); - sda_pin.set_high(); - scl_pin.set_high(); + sda_pin.set_high(); + scl_pin.set_high(); - // Drop pins - drop(sda_pin); - drop(scl_pin); + // Drop pins + drop(sda_pin); + drop(scl_pin); + } else { + let _ = self.send_buf(0, &[0x00]); + } } else { let _ = self.send_buf(0, &[0x00]); } From 0996c7721b430d52d8d2a05bccb140394298c49e Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 22:52:55 -0700 Subject: [PATCH 40/62] Update transport.rs --- src/transport.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 594de5f..236b3bb 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -143,9 +143,8 @@ impl I2cTransport { // Drop pins drop(sda_pin); drop(scl_pin); - } else { - let _ = self.send_buf(0, &[0x00]); - } + #[cfg(not(feature = "rppal"))] + { let _ = self.send_buf(0, &[0x00]); } } else { let _ = self.send_buf(0, &[0x00]); } From 33b21117884d045b0d449ef7d6d58388c99a6648 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 22:55:15 -0700 Subject: [PATCH 41/62] Update transport.rs --- src/transport.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 236b3bb..bad4782 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -143,8 +143,11 @@ impl I2cTransport { // Drop pins drop(sda_pin); drop(scl_pin); - #[cfg(not(feature = "rppal"))] - { let _ = self.send_buf(0, &[0x00]); } + } + + #[cfg(not(feature = "rppal"))] + { let _ = self.send_buf(0, &[0x00]); } + } else { let _ = self.send_buf(0, &[0x00]); } From dcdc6654a082cc9f0f40ed98c682be351518b276 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 23:31:58 -0700 Subject: [PATCH 42/62] Update Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c539742..d0736a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ bitfield = "0" serde = { version = "1", features = ["derive"] } serde_derive = "1" thiserror = "1" -rppal = "0.14.1" +rppal = { version = "0.14.1", optional = true } lazy_static = "1.4.0" [features] From 4ac68c9469df71ca545c94eb00d050af90dccf45 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 23:33:15 -0700 Subject: [PATCH 43/62] Update Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d0736a9..a2cd120 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,4 +22,4 @@ lazy_static = "1.4.0" [features] default = [] -rppal = [] +rppal = ["rppal"] From 7791706a34bd479a2aad3c66fd2acfb63b24551e Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 23:33:38 -0700 Subject: [PATCH 44/62] Update Cargo.toml --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index a2cd120..00c7e7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,5 +21,4 @@ rppal = { version = "0.14.1", optional = true } lazy_static = "1.4.0" [features] -default = [] rppal = ["rppal"] From b150b1d3781f185486f28568d0c7a5e3ad2196b6 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 23:35:18 -0700 Subject: [PATCH 45/62] Update Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 00c7e7a..39436e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,4 @@ rppal = { version = "0.14.1", optional = true } lazy_static = "1.4.0" [features] -rppal = ["rppal"] +raspi = ["rppal"] From bc6d92e5b98c6b649dee38c069df79343bb89781 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 23:38:10 -0700 Subject: [PATCH 46/62] Update transport.rs --- src/transport.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index bad4782..2616c5d 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,7 +1,7 @@ use bytes::{BufMut, BytesMut}; use std::{fs::File, thread, time::Duration, env}; use lazy_static::lazy_static; -#[cfg(feature = "rppal")] +#[cfg(feature = "raspi")] use rppal::{gpio::Gpio, gpio::Mode, system::DeviceInfo}; use crate::constants::{ @@ -22,10 +22,10 @@ const SWI_DEFAULT_BAUDRATE: u32 = 230_400; const SWI_WAKE_BAUDRATE: u32 = 115_200; const SWI_BIT_SEND_DELAY: Duration = Duration::from_micros(45); -#[cfg(feature = "rppal")] +#[cfg(feature = "raspi")] lazy_static! { static ref IS_RASPI: bool = rppal::system::DeviceInfo::new().is_ok(); } -#[cfg(not(feature = "rppal"))] +#[cfg(not(feature = "raspi"))] lazy_static! { static ref IS_RASPI: bool = false; } pub struct I2cTransport { @@ -121,7 +121,7 @@ impl I2cTransport { .parse() .unwrap_or(DEFAULT_SDA_PIN); - #[cfg(feature = "rppal")] + #[cfg(feature = "raspi")] { // Create a new Gpio instance let gpio = Gpio::new()?; @@ -145,7 +145,7 @@ impl I2cTransport { drop(scl_pin); } - #[cfg(not(feature = "rppal"))] + #[cfg(not(feature = "raspi"))] { let _ = self.send_buf(0, &[0x00]); } } else { From 665ac9f63faa596b7df973d0f6eabfb2ef638829 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 23:39:18 -0700 Subject: [PATCH 47/62] Update transport.rs --- src/transport.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 2616c5d..7402405 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -53,13 +53,13 @@ impl From for TransportProtocol { } } -impl From for Error { - fn from(err: rppal::gpio::Error) -> Self { - // Here, you'll need to map or convert the `rppal::gpio::Error` into an appropriate variant or representation of your custom `Error` type - // This will depend on how your custom `Error` type is structured - // As an example, you might have something like this: - //Error::SerialPort(err) - Error::timeout() +#[cfg(feature = "raspi")] +{ + impl From for Error { + fn from(err: rppal::gpio::Error) -> Self { + + Error::timeout() + } } } From 84e1fd32dd36b3ce79b77a8aebe6ee3fe083c788 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Sat, 12 Aug 2023 23:46:11 -0700 Subject: [PATCH 48/62] Update transport.rs --- src/transport.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 7402405..7d83c68 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -54,12 +54,9 @@ impl From for TransportProtocol { } #[cfg(feature = "raspi")] -{ - impl From for Error { - fn from(err: rppal::gpio::Error) -> Self { - - Error::timeout() - } +impl From for Error { + fn from(err: rppal::gpio::Error) -> Self { + Error::timeout() } } From 7a6abcecefc6922a38555afab3984fa9dee09695 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Mon, 14 Aug 2023 01:39:04 -0700 Subject: [PATCH 49/62] skip nonce --- src/ecc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ecc.rs b/src/ecc.rs index 32a7148..1edf5f0 100644 --- a/src/ecc.rs +++ b/src/ecc.rs @@ -208,12 +208,12 @@ impl Ecc { pub fn sign(&mut self, key_slot: u8, data: &[u8]) -> Result { let digest = Sha256::digest(data); - let _ = self.send_command_retries( +/* let _ = self.send_command_retries( &EccCommand::nonce(DataBuffer::MessageDigest, Bytes::copy_from_slice(&digest)), true, false, 1, - )?; + )?;*/ self.send_command_retries( &EccCommand::sign(DataBuffer::MessageDigest, key_slot), false, From 534725738728886897bb470738a1b710673be0f6 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Mon, 14 Aug 2023 01:42:33 -0700 Subject: [PATCH 50/62] don't skip nonce --- src/ecc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ecc.rs b/src/ecc.rs index 1edf5f0..32a7148 100644 --- a/src/ecc.rs +++ b/src/ecc.rs @@ -208,12 +208,12 @@ impl Ecc { pub fn sign(&mut self, key_slot: u8, data: &[u8]) -> Result { let digest = Sha256::digest(data); -/* let _ = self.send_command_retries( + let _ = self.send_command_retries( &EccCommand::nonce(DataBuffer::MessageDigest, Bytes::copy_from_slice(&digest)), true, false, 1, - )?;*/ + )?; self.send_command_retries( &EccCommand::sign(DataBuffer::MessageDigest, key_slot), false, From 169e4d1069021f1b07254342a319a80dd70ed874 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Wed, 30 Aug 2023 02:08:51 -0700 Subject: [PATCH 51/62] added wake duration config option --- src/ecc.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ecc.rs b/src/ecc.rs index 32a7148..2aa4a89 100644 --- a/src/ecc.rs +++ b/src/ecc.rs @@ -38,6 +38,7 @@ pub struct EccCommandDuration { pub genkey: u32, pub sign: u32, pub ecdh: u32, + pub wake: u32, } impl EccConfig { @@ -81,6 +82,7 @@ impl EccConfig { genkey: 59_000, sign: 62_000, ecdh: 28_000, + wake: 60, }, } } From 454ea2d5d1f960736649860c6a64b01b5d2e958f Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Wed, 30 Aug 2023 08:18:22 -0700 Subject: [PATCH 52/62] Update ecc.rs --- src/ecc.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ecc.rs b/src/ecc.rs index 2aa4a89..6fef01e 100644 --- a/src/ecc.rs +++ b/src/ecc.rs @@ -65,6 +65,7 @@ impl EccConfig { genkey: 85_000, sign: 80_000, ecdh: 42_000, + wake: 60, }, } } From d9d8703b13df82dc9e9fe2d0d78e7c1fcb6dc0e9 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Wed, 30 Aug 2023 08:39:35 -0700 Subject: [PATCH 53/62] wake_duration --- src/ecc.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ecc.rs b/src/ecc.rs index 6fef01e..be92562 100644 --- a/src/ecc.rs +++ b/src/ecc.rs @@ -265,6 +265,7 @@ impl Ecc { let mut buf = BytesMut::with_capacity(ATCA_CMD_SIZE_MAX as usize); let delay = self.config.command_duration(command); let wake_delay = Duration::from_micros(self.config.wake_delay as u64); + let wake_duration = Duration::from_micros(self.config.durations.wake as u64); for retry in 0..retries { buf.clear(); @@ -272,7 +273,7 @@ impl Ecc { command.bytes_into(&mut buf); if wake { - self.transport.send_wake(wake_delay)?; + self.transport.send_wake(wake_delay, wake_duration)?; } if let Err(_err) = self.transport.send_recv_buf(delay, &mut buf) { From 8a9fb5bcf45511f80205cc1a32604fe55a7e7954 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Wed, 30 Aug 2023 08:43:29 -0700 Subject: [PATCH 54/62] wake_duration --- src/transport.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index df13994..ddd8c84 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -61,9 +61,9 @@ impl From for Error { } impl TransportProtocol { - pub fn send_wake(&mut self, wake_delay: Duration) -> Result { + pub fn send_wake(&mut self, wake_delay: Duration, wake_duration: Duration) -> Result { match self { - Self::I2c(i2c_handle) => i2c_handle.send_wake(wake_delay), + Self::I2c(i2c_handle) => i2c_handle.send_wake(wake_delay, wake_duration), Self::Swi(swi_handle) => swi_handle.send_wake(wake_delay), } } @@ -105,7 +105,7 @@ impl I2cTransport { Ok(Self { port, address }) } - fn send_wake(&mut self, wake_delay: Duration) -> Result { + fn send_wake(&mut self, wake_delay: Duration, wake_duration: Duration) -> Result { if *IS_RASPI { let scl_pin_number: u8 = env::var("GW_SCL_PIN") @@ -132,7 +132,7 @@ impl I2cTransport { scl_pin.set_low(); // Hold them low for 60 microseconds - thread::sleep(Duration::from_micros(60)); + thread::sleep(wake_duration); sda_pin.set_high(); scl_pin.set_high(); From bdd7c89da377bfe900bfe852c2c5a0cf59797ae6 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 31 Aug 2023 20:30:13 -0700 Subject: [PATCH 55/62] i2c_set_retries and i2c_set_timeout --- src/transport.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/transport.rs b/src/transport.rs index ddd8c84..853b630 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -101,6 +101,8 @@ impl I2cTransport { pub fn new(path: &str, address: u16) -> Result { let mut port = I2c::from_path(path)?; port.smbus_set_slave_address(address, false)?; + port.i2c_set_retries(RECV_RETRIES); + port.i2c_set_timeout(RECV_RETRY_WAIT); Ok(Self { port, address }) } From 9bc710d2115ff407c79d15dac81c817b4e092e74 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 31 Aug 2023 20:32:06 -0700 Subject: [PATCH 56/62] port.i2c_set_retries(RECV_RETRIES as usize); --- src/transport.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport.rs b/src/transport.rs index 853b630..29ba56f 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -101,7 +101,7 @@ impl I2cTransport { pub fn new(path: &str, address: u16) -> Result { let mut port = I2c::from_path(path)?; port.smbus_set_slave_address(address, false)?; - port.i2c_set_retries(RECV_RETRIES); + port.i2c_set_retries(RECV_RETRIES as usize); port.i2c_set_timeout(RECV_RETRY_WAIT); Ok(Self { port, address }) From 77793356443f023331a1cd7f985c58794b246be3 Mon Sep 17 00:00:00 2001 From: joecryptotoo <80373433+joecryptotoo@users.noreply.github.com> Date: Thu, 31 Aug 2023 20:52:24 -0700 Subject: [PATCH 57/62] Update transport.rs --- src/transport.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 29ba56f..fce36ad 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -101,8 +101,8 @@ impl I2cTransport { pub fn new(path: &str, address: u16) -> Result { let mut port = I2c::from_path(path)?; port.smbus_set_slave_address(address, false)?; - port.i2c_set_retries(RECV_RETRIES as usize); - port.i2c_set_timeout(RECV_RETRY_WAIT); + // port.i2c_set_retries(RECV_RETRIES as usize); + // port.i2c_set_timeout(RECV_RETRY_WAIT); Ok(Self { port, address }) } From 28a76d22d2d7d46a9af9744079ab59e7999ad334 Mon Sep 17 00:00:00 2001 From: onicolaos <59395917+onicolaos@users.noreply.github.com> Date: Sun, 10 Sep 2023 08:25:52 +0200 Subject: [PATCH 58/62] Send sleep command on read timeout The chip will be occasionally stuck in a state where it will not respond. Sending a sleep command when exhausted of read retries will recover on next wake command --- src/transport.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/transport.rs b/src/transport.rs index 09b7129..174ca57 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -132,6 +132,8 @@ impl I2cTransport { } let count = buf[0] as usize; if count == 0xff { + // Sleep the chip to clear the SRAM when the maximum error read retries have been exhausted + self.send_sleep(); return Err(Error::timeout()); } buf.truncate(count); From 1a374985c8fbb88d2d13314d36a2c2d929c0b11a Mon Sep 17 00:00:00 2001 From: mawdegroot <73519916+mawdegroot@users.noreply.github.com> Date: Tue, 12 Sep 2023 11:38:31 +0200 Subject: [PATCH 59/62] fix off by one and add last resort wait --- src/ecc.rs | 6 +++--- src/transport.rs | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ecc.rs b/src/ecc.rs index 32a7148..77256fe 100644 --- a/src/ecc.rs +++ b/src/ecc.rs @@ -212,13 +212,13 @@ impl Ecc { &EccCommand::nonce(DataBuffer::MessageDigest, Bytes::copy_from_slice(&digest)), true, false, - 1, + 0, )?; self.send_command_retries( &EccCommand::sign(DataBuffer::MessageDigest, key_slot), false, true, - 1, + 0, ) } @@ -263,7 +263,7 @@ impl Ecc { let delay = self.config.command_duration(command); let wake_delay = Duration::from_micros(self.config.wake_delay as u64); - for retry in 0..retries { + for retry in 0..=retries { buf.clear(); buf.put_u8(self.transport.put_command_flag()); command.bytes_into(&mut buf); diff --git a/src/transport.rs b/src/transport.rs index 09b7129..d768173 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -11,6 +11,7 @@ use i2c_linux::I2c; use serialport::{ClearBuffer, SerialPort}; const RECV_RETRY_WAIT: Duration = Duration::from_millis(4); +const RECV_RETRY_WAIT_LAST_RESORT: Duration = Duration::from_millis(40); const RECV_RETRIES: u8 = 10; const SWI_DEFAULT_BAUDRATE: u32 = 230_400; const SWI_WAKE_BAUDRATE: u32 = 115_200; @@ -119,7 +120,10 @@ impl I2cTransport { fn recv_buf(&mut self, buf: &mut BytesMut) -> Result { buf.resize(ATCA_RSP_SIZE_MAX as usize, 0); buf[0] = 0xff; - for _retry in 0..RECV_RETRIES { + for retry in 0..=RECV_RETRIES { + if retry == RECV_RETRIES { + thread::sleep(RECV_RETRY_WAIT_LAST_RESORT); + } let msg = i2c_linux::Message::Read { address: self.address, data: buf, From f2f3330c71090755192d300a21dcdaeaada49aa2 Mon Sep 17 00:00:00 2001 From: mawdegroot <73519916+mawdegroot@users.noreply.github.com> Date: Tue, 12 Sep 2023 11:48:09 +0200 Subject: [PATCH 60/62] only idle on success --- src/ecc.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/ecc.rs b/src/ecc.rs index 77256fe..8d176f4 100644 --- a/src/ecc.rs +++ b/src/ecc.rs @@ -273,25 +273,26 @@ impl Ecc { } if let Err(_err) = self.transport.send_recv_buf(delay, &mut buf) { - if retry == retries { - // Sleep the chip to clear the SRAM when the maximum error retries have been exhausted - self.transport.send_sleep(); - break; - } else { - continue; - } + continue; } let response = EccResponse::from_bytes(&buf[..])?; - if idle { - self.transport.send_idle(); - } + match response { - EccResponse::Data(bytes) => return Ok(bytes), + EccResponse::Data(bytes) => { + if idle { + self.transport.send_idle(); + } + return Ok(bytes); + } EccResponse::Error(err) if err.is_recoverable() && retry < retries => continue, - EccResponse::Error(err) => return Err(Error::ecc(err)), + EccResponse::Error(err) => { + self.transport.send_sleep(); + return Err(Error::ecc(err)); + } } } + self.transport.send_sleep(); Err(Error::timeout()) } } From 5fa17f707da5fc1b75c1864ba36afb3b45703e44 Mon Sep 17 00:00:00 2001 From: joecryptotoo Date: Wed, 13 Sep 2023 18:06:37 -0700 Subject: [PATCH 61/62] Revert "Merge pull request #2 from mawdegroot/mg/fix-off-by-one" This reverts commit 3e7d77edae1b5b1628d0980758536abbd6303760, reversing changes made to 70ffe06d5f8a47d96c6572b66a728d4854a5fe63. --- src/ecc.rs | 31 +++++++++++++++---------------- src/transport.rs | 6 +----- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/ecc.rs b/src/ecc.rs index 5039b01..be92562 100644 --- a/src/ecc.rs +++ b/src/ecc.rs @@ -215,13 +215,13 @@ impl Ecc { &EccCommand::nonce(DataBuffer::MessageDigest, Bytes::copy_from_slice(&digest)), true, false, - 0, + 1, )?; self.send_command_retries( &EccCommand::sign(DataBuffer::MessageDigest, key_slot), false, true, - 0, + 1, ) } @@ -267,7 +267,7 @@ impl Ecc { let wake_delay = Duration::from_micros(self.config.wake_delay as u64); let wake_duration = Duration::from_micros(self.config.durations.wake as u64); - for retry in 0..=retries { + for retry in 0..retries { buf.clear(); buf.put_u8(self.transport.put_command_flag()); command.bytes_into(&mut buf); @@ -277,26 +277,25 @@ impl Ecc { } if let Err(_err) = self.transport.send_recv_buf(delay, &mut buf) { - continue; + if retry == retries { + // Sleep the chip to clear the SRAM when the maximum error retries have been exhausted + self.transport.send_sleep(); + break; + } else { + continue; + } } let response = EccResponse::from_bytes(&buf[..])?; - + if idle { + self.transport.send_idle(); + } match response { - EccResponse::Data(bytes) => { - if idle { - self.transport.send_idle(); - } - return Ok(bytes); - } + EccResponse::Data(bytes) => return Ok(bytes), EccResponse::Error(err) if err.is_recoverable() && retry < retries => continue, - EccResponse::Error(err) => { - self.transport.send_sleep(); - return Err(Error::ecc(err)); - } + EccResponse::Error(err) => return Err(Error::ecc(err)), } } - self.transport.send_sleep(); Err(Error::timeout()) } } diff --git a/src/transport.rs b/src/transport.rs index 6614715..9bf9ec8 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -17,7 +17,6 @@ const DEFAULT_SCL_PIN: u8 = 3; // Replace with your default SCL pin const DEFAULT_SDA_PIN: u8 = 2; // Replace with your default SDA pin const RECV_RETRY_WAIT: Duration = Duration::from_millis(4); -const RECV_RETRY_WAIT_LAST_RESORT: Duration = Duration::from_millis(40); const RECV_RETRIES: u8 = 10; const SWI_DEFAULT_BAUDRATE: u32 = 230_400; const SWI_WAKE_BAUDRATE: u32 = 115_200; @@ -183,10 +182,7 @@ impl I2cTransport { fn recv_buf(&mut self, buf: &mut BytesMut) -> Result { buf.resize(ATCA_RSP_SIZE_MAX as usize, 0); buf[0] = 0xff; - for retry in 0..=RECV_RETRIES { - if retry == RECV_RETRIES { - thread::sleep(RECV_RETRY_WAIT_LAST_RESORT); - } + for _retry in 0..RECV_RETRIES { let msg = i2c_linux::Message::Read { address: self.address, data: buf, From 01975f7a7beb6a542aa31ac30b914b0ebe0a12c5 Mon Sep 17 00:00:00 2001 From: joecryptotoo Date: Wed, 13 Sep 2023 18:07:25 -0700 Subject: [PATCH 62/62] Revert "Merge pull request #1 from onicolaos/sleep_on_read_timeout" This reverts commit 70ffe06d5f8a47d96c6572b66a728d4854a5fe63, reversing changes made to 77793356443f023331a1cd7f985c58794b246be3. --- src/transport.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/transport.rs b/src/transport.rs index 9bf9ec8..fce36ad 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -195,8 +195,6 @@ impl I2cTransport { } let count = buf[0] as usize; if count == 0xff { - // Sleep the chip to clear the SRAM when the maximum error read retries have been exhausted - self.send_sleep(); return Err(Error::timeout()); } buf.truncate(count);