@@ -14,41 +14,22 @@ use std::mem;
1414use std:: ptr;
1515use std:: io:: Cursor ;
1616use std:: os:: unix:: prelude:: * ;
17+ use std:: marker:: PhantomData ;
1718use byteorder:: { NativeEndian , ReadBytesExt , WriteBytesExt } ;
19+ use libc:: c_int;
1820
1921pub type I2CError = nix:: Error ;
2022
21- bitflags ! {
22- struct I2CMsgFlags : u16 {
23- /// this is a ten bit chip address
24- const I2C_M_TEN = 0x0010 ;
25- /// read data, from slave to master
26- const I2C_M_RD = 0x0001 ;
27- /// if I2C_FUNC_PROTOCOL_MANGLING
28- const I2C_M_STOP = 0x8000 ;
29- /// if I2C_FUNC_NOSTART
30- const I2C_M_NOSTART = 0x4000 ;
31- /// if I2C_FUNC_PROTOCOL_MANGLING
32- const I2C_M_REV_DIR_ADDR = 0x2000 ;
33- /// if I2C_FUNC_PROTOCOL_MANGLING
34- const I2C_M_IGNORE_NAK = 0x1000 ;
35- /// if I2C_FUNC_PROTOCOL_MANGLING
36- const I2C_M_NO_RD_ACK = 0x0800 ;
37- /// length will be first received byte
38- const I2C_M_RECV_LEN = 0x0400 ;
39- }
40- }
41-
4223#[ repr( C ) ]
43- struct i2c_msg {
24+ pub struct i2c_msg {
4425 /// slave address
45- addr : u16 ,
26+ pub ( crate ) addr : u16 ,
4627 /// serialized I2CMsgFlags
47- flags : u16 ,
28+ pub ( crate ) flags : u16 ,
4829 /// msg length
49- len : u16 ,
30+ pub ( crate ) len : u16 ,
5031 /// pointer to msg data
51- buf : * mut u8 ,
32+ pub ( crate ) buf : * const u8 ,
5233}
5334
5435bitflags ! {
@@ -163,23 +144,25 @@ pub struct i2c_smbus_ioctl_data {
163144}
164145
165146/// This is the structure as used in the I2C_RDWR ioctl call
147+ // see linux/i2c-dev.h
166148#[ repr( C ) ]
167- struct i2c_rdwr_ioctl_data {
149+ pub struct i2c_rdwr_ioctl_data {
168150 // struct i2c_msg __user *msgs;
169151 msgs : * mut i2c_msg ,
170152 // __u32 nmsgs;
171153 nmsgs : u32 ,
172154}
173155
174156mod ioctl {
175- use super :: { I2C_SLAVE , I2C_SMBUS } ;
157+ use super :: { I2C_SLAVE , I2C_SMBUS , I2C_RDWR } ;
176158 pub use super :: i2c_smbus_ioctl_data;
159+ pub use super :: i2c_rdwr_ioctl_data;
177160
178161 ioctl_write_int_bad ! ( set_i2c_slave_address, I2C_SLAVE ) ;
179162 ioctl_write_ptr_bad ! ( i2c_smbus, I2C_SMBUS , i2c_smbus_ioctl_data) ;
163+ ioctl_write_ptr_bad ! ( i2c_rdwr, I2C_RDWR , i2c_rdwr_ioctl_data) ;
180164}
181165
182-
183166pub fn i2c_set_slave_address ( fd : RawFd , slave_address : u16 ) -> Result < ( ) , nix:: Error > {
184167 unsafe {
185168 ioctl:: set_i2c_slave_address ( fd, slave_address as i32 ) ?;
@@ -429,3 +412,17 @@ pub fn i2c_smbus_process_call_block(fd: RawFd, register: u8, values: &[u8]) -> R
429412 let count = data. block [ 0 ] ;
430413 Ok ( ( & data. block [ 1 ..( count + 1 ) as usize ] ) . to_vec ( ) )
431414}
415+
416+ #[ inline]
417+ pub fn i2c_rdwr ( fd : RawFd , values : & mut [ i2c_msg ] ) -> Result < u32 , I2CError > {
418+ let i2c_data = i2c_rdwr_ioctl_data {
419+ msgs : values. as_mut_ptr ( ) ,
420+ nmsgs : values. len ( ) as u32 ,
421+ } ;
422+
423+ let n;
424+ unsafe {
425+ n = ioctl:: i2c_rdwr ( fd, & i2c_data) ?;
426+ }
427+ Ok ( n as u32 )
428+ }
0 commit comments