Skip to content

Commit a0f71ff

Browse files
bors[bot]yseraf
andauthored
Merge #67
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 <yannick.lanz@wifx.net>
2 parents a6d1f1f + 0d2cebd commit a0f71ff

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Versioning](https://semver.org/spec/v2.0.0.html).
99
## [Unreleased]
1010

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

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

src/ffi.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ pub struct i2c_rdwr_ioctl_data {
153153
mod ioctl {
154154
pub use super::i2c_rdwr_ioctl_data;
155155
pub use super::i2c_smbus_ioctl_data;
156-
use super::{I2C_RDWR, I2C_SLAVE, I2C_SMBUS};
156+
use super::{I2C_PEC, I2C_RDWR, I2C_SLAVE, I2C_SMBUS};
157157

158158
ioctl_write_int_bad!(set_i2c_slave_address, I2C_SLAVE);
159+
ioctl_write_int_bad!(set_smbus_pec, I2C_PEC);
159160
ioctl_write_ptr_bad!(i2c_smbus, I2C_SMBUS, i2c_smbus_ioctl_data);
160161
ioctl_write_ptr_bad!(i2c_rdwr, I2C_RDWR, i2c_rdwr_ioctl_data);
161162
}
@@ -167,6 +168,13 @@ pub fn i2c_set_slave_address(fd: RawFd, slave_address: u16) -> Result<(), nix::E
167168
Ok(())
168169
}
169170

171+
pub fn i2c_set_smbus_pec(fd: RawFd, enable: bool) -> Result<(), nix::Error> {
172+
unsafe {
173+
ioctl::set_smbus_pec(fd, i32::from(enable))?;
174+
}
175+
Ok(())
176+
}
177+
170178
unsafe fn i2c_smbus_access(
171179
fd: RawFd,
172180
read_write: I2CSMBusReadWrite,

src/linux.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub use core::I2CMessage;
2525
pub struct LinuxI2CDevice {
2626
devfile: File,
2727
slave_address: u16,
28+
pec: bool,
2829
}
2930

3031
/// Linux I2C bus
@@ -102,8 +103,10 @@ impl LinuxI2CDevice {
102103
let mut device = LinuxI2CDevice {
103104
devfile: file,
104105
slave_address: 0, // will be set later
106+
pec: false,
105107
};
106108
device.set_slave_address(slave_address)?;
109+
device.set_smbus_pec(false)?;
107110
Ok(device)
108111
}
109112

@@ -123,6 +126,17 @@ impl LinuxI2CDevice {
123126
self.slave_address = slave_address;
124127
Ok(())
125128
}
129+
130+
/// Enable/Disable PEC support for this device
131+
///
132+
/// Used only for SMBus transactions. This request only has an effect if the
133+
/// the adapter has I2C_FUNC_SMBUS_PEC; it is still safe if not, it just
134+
/// doesn't have any effect.
135+
pub fn set_smbus_pec(&mut self, enable: bool) -> Result<(), LinuxI2CError> {
136+
ffi::i2c_set_smbus_pec(self.as_raw_fd(), enable)?;
137+
self.pec = enable;
138+
Ok(())
139+
}
126140
}
127141

128142
impl I2CDevice for LinuxI2CDevice {

0 commit comments

Comments
 (0)