Skip to content

Commit 411956e

Browse files
committed
added smbus_read_i2c_block_data
1 parent a3b0125 commit 411956e

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
name = "i2cdev"
44
build = "build.rs"
5-
version = "0.3.0"
5+
version = "0.3.1"
66
authors = ["Paul Osborne <[email protected]>"]
77
license = "MIT/Apache-2.0"
88
repository = "https://github.com/rust-embedded/rust-i2cdev"

src/core.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ pub trait I2CDevice {
9090
/// count bytes read from the device.
9191
fn smbus_read_block_data(&mut self, register: u8) -> Result<Vec<u8>, Self::Error>;
9292

93+
/// Read a block of up to 32 bytes from a device
94+
///
95+
/// Uses read_i2c_block_data instead read_block_data.
96+
fn smbus_read_i2c_block_data(&mut self, register: u8, len: u8) -> Result<Vec<u8>, Self::Error>;
97+
9398
/// Write a block of up to 32 bytes to a device
9499
///
95100
/// The opposite of the Block Read command, this writes up to 32 bytes to

src/ffi.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,23 @@ pub fn i2c_smbus_read_block_data(fd: RawFd, register: u8) -> Result<Vec<u8>, I2C
338338
Ok((&data.block[1..(count + 1) as usize]).to_vec())
339339
}
340340

341+
pub fn i2c_smbus_read_i2c_block_data(fd: RawFd, register: u8, len: u8) -> Result<Vec<u8>, I2CError> {
342+
let mut data = i2c_smbus_data::empty();
343+
data.block[0] = len;
344+
try!(unsafe {
345+
i2c_smbus_access(fd,
346+
I2CSMBusReadWrite::I2C_SMBUS_READ,
347+
register,
348+
I2CSMBusSize::I2C_SMBUS_I2C_BLOCK_DATA,
349+
&mut data)
350+
});
351+
352+
// create a vector from the data in the block starting at byte
353+
// 1 and ending after count bytes after that
354+
let count = data.block[0];
355+
Ok((&data.block[1..(count + 1) as usize]).to_vec())
356+
}
357+
341358
#[inline]
342359
pub fn i2c_smbus_write_block_data(fd: RawFd, register: u8, values: &[u8]) -> Result<(), I2CError> {
343360
let mut data = i2c_smbus_data::empty();

src/linux.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ impl I2CDevice for LinuxI2CDevice {
196196
ffi::i2c_smbus_read_block_data(self.as_raw_fd(), register).map_err(From::from)
197197
}
198198

199+
/// Read a block of up to 32 bytes from a device via i2c_smbus_i2c_read_block_data
200+
fn smbus_read_i2c_block_data(&mut self, register: u8, len: u8) -> Result<Vec<u8>, LinuxI2CError> {
201+
ffi::i2c_smbus_read_i2c_block_data(self.as_raw_fd(), register, len).map_err(From::from)
202+
}
203+
199204
/// Write a block of up to 32 bytes to a device
200205
///
201206
/// The opposite of the Block Read command, this writes up to 32 bytes to

src/mock.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,8 @@ impl I2CDevice for MockI2CDevice {
9191
fn smbus_process_block(&mut self, _register: u8, _values: &[u8]) -> I2CResult<()> {
9292
unimplemented!()
9393
}
94+
95+
fn smbus_read_i2c_block_data(&mut self, register: u8, len: u8) -> I2CResult<Vec<u8>> {
96+
unimplemented!()
97+
}
9498
}

0 commit comments

Comments
 (0)