Skip to content

Commit

Permalink
Merge #67
Browse files Browse the repository at this point in the history
67: Add PEC management support for SMBus compatible controller. r=eldruin a=hilt0n

Thanks for the review!

Some cleaning and updated the changelog.
r? `@eldruin`

Co-authored-by: Yannick Lanz <[email protected]>
  • Loading branch information
bors[bot] and hilt0n authored Nov 3, 2021
2 parents a6d1f1f + 0d2cebd commit a0f71ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]

- Updated nix to allow both version `0.22` or `0.23`.
- Add PEC support for SMBus compatible adapters

## [v0.5.0] - 2021-09-21

Expand Down
10 changes: 9 additions & 1 deletion src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ pub struct i2c_rdwr_ioctl_data {
mod ioctl {
pub use super::i2c_rdwr_ioctl_data;
pub use super::i2c_smbus_ioctl_data;
use super::{I2C_RDWR, I2C_SLAVE, I2C_SMBUS};
use super::{I2C_PEC, I2C_RDWR, I2C_SLAVE, I2C_SMBUS};

ioctl_write_int_bad!(set_i2c_slave_address, I2C_SLAVE);
ioctl_write_int_bad!(set_smbus_pec, I2C_PEC);
ioctl_write_ptr_bad!(i2c_smbus, I2C_SMBUS, i2c_smbus_ioctl_data);
ioctl_write_ptr_bad!(i2c_rdwr, I2C_RDWR, i2c_rdwr_ioctl_data);
}
Expand All @@ -167,6 +168,13 @@ pub fn i2c_set_slave_address(fd: RawFd, slave_address: u16) -> Result<(), nix::E
Ok(())
}

pub fn i2c_set_smbus_pec(fd: RawFd, enable: bool) -> Result<(), nix::Error> {
unsafe {
ioctl::set_smbus_pec(fd, i32::from(enable))?;
}
Ok(())
}

unsafe fn i2c_smbus_access(
fd: RawFd,
read_write: I2CSMBusReadWrite,
Expand Down
14 changes: 14 additions & 0 deletions src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub use core::I2CMessage;
pub struct LinuxI2CDevice {
devfile: File,
slave_address: u16,
pec: bool,
}

/// Linux I2C bus
Expand Down Expand Up @@ -102,8 +103,10 @@ impl LinuxI2CDevice {
let mut device = LinuxI2CDevice {
devfile: file,
slave_address: 0, // will be set later
pec: false,
};
device.set_slave_address(slave_address)?;
device.set_smbus_pec(false)?;
Ok(device)
}

Expand All @@ -123,6 +126,17 @@ impl LinuxI2CDevice {
self.slave_address = slave_address;
Ok(())
}

/// Enable/Disable PEC support for this device
///
/// Used only for SMBus transactions. This request only has an effect if the
/// the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just
/// doesn't have any effect.
pub fn set_smbus_pec(&mut self, enable: bool) -> Result<(), LinuxI2CError> {
ffi::i2c_set_smbus_pec(self.as_raw_fd(), enable)?;
self.pec = enable;
Ok(())
}
}

impl I2CDevice for LinuxI2CDevice {
Expand Down

0 comments on commit a0f71ff

Please sign in to comment.