mirror of
https://codeberg.org/ral/rwedid.git
synced 2024-08-16 09:59:49 +02:00
Write to bus in chunks of 8 bytes a time
This commit is contained in:
parent
522c63762e
commit
68c7193ffd
1 changed files with 21 additions and 10 deletions
31
src/i2c.rs
31
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(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue