From 68c7193ffd1c8ad922ec4eefbbff5b9a2b686f93 Mon Sep 17 00:00:00 2001 From: Ral Date: Sun, 4 Jun 2023 03:04:16 +0200 Subject: [PATCH] Write to bus in chunks of 8 bytes a time --- src/i2c.rs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/i2c.rs b/src/i2c.rs index 2ec011f..b2d8938 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -1,6 +1,9 @@ use i2cdev::core::*; use i2cdev::linux::{LinuxI2CDevice, LinuxI2CMessage}; +use std::thread; +use std::time::Duration; + pub fn read_from_bus( i2c_device: &str, slave_address: u16, @@ -28,15 +31,23 @@ pub fn write_to_bus( data: &[u8], ) -> Result<(), std::io::Error> { let mut dev = LinuxI2CDevice::new(i2c_device, slave_address)?; - let mut msgs = [ - // Address to write to - LinuxI2CMessage::write(&[0x00]), - // Write data to chip - LinuxI2CMessage::write(data), - ]; - match dev.transfer(&mut msgs) { - Ok(_) => Ok(()), - Err(e) => Err(e.into()), - } + // Write 8 bytes at a time + for (i, chunk) in data.chunks_exact(8).into_iter().enumerate() { + + // Memory address + let memory_address : u8 = (i as u8) * 8; + let mut msgs = [ + // Address to write to + LinuxI2CMessage::write(&[memory_address]), + // Write data to chip + LinuxI2CMessage::write(&chunk), + ]; + + dev.transfer(&mut msgs)?; + + thread::sleep(Duration::from_millis(20)); + }; + + Ok(()) }