Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
5fee51f
IGNORE_NACK for wake
codearranger Aug 10, 2023
5c63e99
Update transport.rs
codearranger Aug 10, 2023
1f4fba1
Update transport.rs
codearranger Aug 10, 2023
8e0d7da
Let's try NO_STOP too
codearranger Aug 10, 2023
d05a440
NO_START
codearranger Aug 10, 2023
957d9fe
get port.i2c_functionality
codearranger Aug 10, 2023
84a43dc
use gpio wake
codearranger Aug 10, 2023
2837cfe
add rppal
codearranger Aug 10, 2023
5d7fae5
Update transport.rs
codearranger Aug 10, 2023
85caae6
Update transport.rs
codearranger Aug 10, 2023
98aac25
Update transport.rs
codearranger Aug 10, 2023
debf339
Update Cargo.toml
codearranger Aug 10, 2023
a4bb22e
Update transport.rs
codearranger Aug 10, 2023
1fa3576
Update Cargo.toml
codearranger Aug 10, 2023
4dc3207
Update transport.rs
codearranger Aug 10, 2023
e41f3f8
Update transport.rs
codearranger Aug 10, 2023
76b02ea
Update transport.rs
codearranger Aug 10, 2023
c03f9ac
Update transport.rs
codearranger Aug 10, 2023
b6eb669
Update transport.rs
codearranger Aug 10, 2023
4b4e091
Update transport.rs
codearranger Aug 10, 2023
0599d9a
Update transport.rs
codearranger Aug 10, 2023
3509a0a
Update transport.rs
codearranger Aug 10, 2023
3d4c0f9
Update transport.rs
codearranger Aug 10, 2023
bb040c2
Update transport.rs
codearranger Aug 10, 2023
c5e9d2d
Update transport.rs
codearranger Aug 10, 2023
0b137a1
Update transport.rs
codearranger Aug 10, 2023
504caba
Update transport.rs
codearranger Aug 10, 2023
ef87a3b
Update transport.rs
codearranger Aug 10, 2023
8cf2032
check for raspberry pi
codearranger Aug 11, 2023
bd4ca14
add lazy_static = "1.4.0"
codearranger Aug 11, 2023
cad9cf7
use GW_SCL_PIN and GW_SDA_PIN env vars
codearranger Aug 11, 2023
ca0bf03
Update transport.rs
codearranger Aug 11, 2023
ddae1b6
made rppal a feature
codearranger Aug 13, 2023
43771b2
use feature flag
codearranger Aug 13, 2023
168436d
Update transport.rs
codearranger Aug 13, 2023
4594a09
Update transport.rs
codearranger Aug 13, 2023
cce3e46
Update Cargo.toml
codearranger Aug 13, 2023
dd35a28
Update transport.rs
codearranger Aug 13, 2023
15d7b53
Update transport.rs
codearranger Aug 13, 2023
0996c77
Update transport.rs
codearranger Aug 13, 2023
33b2111
Update transport.rs
codearranger Aug 13, 2023
dcdc665
Update Cargo.toml
codearranger Aug 13, 2023
4ac68c9
Update Cargo.toml
codearranger Aug 13, 2023
7791706
Update Cargo.toml
codearranger Aug 13, 2023
b150b1d
Update Cargo.toml
codearranger Aug 13, 2023
bc6d92e
Update transport.rs
codearranger Aug 13, 2023
665ac9f
Update transport.rs
codearranger Aug 13, 2023
84e1fd3
Update transport.rs
codearranger Aug 13, 2023
7a6abce
skip nonce
codearranger Aug 14, 2023
5347257
don't skip nonce
codearranger Aug 14, 2023
3d57278
Merge branch 'helium:main' into joecryptotoo/wake_fix
codearranger Aug 15, 2023
169e4d1
added wake duration config option
codearranger Aug 30, 2023
454ea2d
Update ecc.rs
codearranger Aug 30, 2023
d9d8703
wake_duration
codearranger Aug 30, 2023
8a9fb5b
wake_duration
codearranger Aug 30, 2023
bdd7c89
i2c_set_retries and i2c_set_timeout
codearranger Sep 1, 2023
9bc710d
port.i2c_set_retries(RECV_RETRIES as usize);
codearranger Sep 1, 2023
7779335
Update transport.rs
codearranger Sep 1, 2023
28a76d2
Send sleep command on read timeout
onicolaos Sep 10, 2023
70ffe06
Merge pull request #1 from onicolaos/sleep_on_read_timeout
codearranger Sep 10, 2023
1a37498
fix off by one and add last resort wait
mawdegroot Sep 12, 2023
f2f3330
only idle on success
mawdegroot Sep 12, 2023
3e7d77e
Merge pull request #2 from mawdegroot/mg/fix-off-by-one
codearranger Sep 13, 2023
5fa17f7
Revert "Merge pull request #2 from mawdegroot/mg/fix-off-by-one"
codearranger Sep 14, 2023
01975f7
Revert "Merge pull request #1 from onicolaos/sleep_on_read_timeout"
codearranger Sep 14, 2023
8317dbf
Merge pull request #3 from helium/main
codearranger Sep 14, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ bitfield = "0"
serde = { version = "1", features = ["derive"] }
serde_derive = "1"
thiserror = "1"
rppal = { version = "0.14.1", optional = true }
lazy_static = "1.4.0"

[features]
raspi = ["rppal"]
6 changes: 5 additions & 1 deletion src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct EccCommandDuration {
pub genkey: u32,
pub sign: u32,
pub ecdh: u32,
pub wake: u32,
}

impl EccConfig {
Expand All @@ -64,6 +65,7 @@ impl EccConfig {
genkey: 85_000,
sign: 80_000,
ecdh: 42_000,
wake: 60,
},
}
}
Expand All @@ -81,6 +83,7 @@ impl EccConfig {
genkey: 59_000,
sign: 62_000,
ecdh: 28_000,
wake: 60,
},
}
}
Expand Down Expand Up @@ -262,14 +265,15 @@ 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();
buf.put_u8(self.transport.put_command_flag());
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) {
Expand Down
73 changes: 68 additions & 5 deletions src/transport.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use bytes::{BufMut, BytesMut};
use std::{fs::File, thread, time::Duration};
use std::{fs::File, thread, time::Duration, env};
use lazy_static::lazy_static;
#[cfg(feature = "raspi")]
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,
Expand All @@ -10,11 +13,21 @@ 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 = 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 = "raspi")]
lazy_static! { static ref IS_RASPI: bool = rppal::system::DeviceInfo::new().is_ok(); }

#[cfg(not(feature = "raspi"))]
lazy_static! { static ref IS_RASPI: bool = false; }

pub struct I2cTransport {
port: I2c<File>,
address: u16,
Expand All @@ -40,10 +53,17 @@ impl From<SwiTransport> for TransportProtocol {
}
}

#[cfg(feature = "raspi")]
impl From<rppal::gpio::Error> for Error {
fn from(err: rppal::gpio::Error) -> Self {
Error::timeout()
}
}

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),
}
}
Expand Down Expand Up @@ -81,12 +101,55 @@ impl I2cTransport {
pub fn new(path: &str, address: u16) -> Result<Self> {
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);

Ok(Self { port, address })
}

fn send_wake(&mut self, wake_delay: Duration) -> Result {
let _ = self.send_buf(0, &[0x00]);
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")
.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);

#[cfg(feature = "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(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();

// Hold them low for 60 microseconds
thread::sleep(wake_duration);

sda_pin.set_high();
scl_pin.set_high();

// Drop pins
drop(sda_pin);
drop(scl_pin);
}

#[cfg(not(feature = "raspi"))]
{ let _ = self.send_buf(0, &[0x00]); }

} else {
let _ = self.send_buf(0, &[0x00]);
}
thread::sleep(wake_delay);
Ok(())
}
Expand Down