1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#![deny(missing_docs)]
#![doc(html_root_url = "http://arcnmx.github.io/i2c-linux-rs/")]
#[macro_use]
extern crate bitflags;
extern crate resize_slice;
extern crate i2c_linux_sys as i2c;
#[cfg(feature = "i2c")]
extern crate i2c as i2c_gen;
#[cfg(feature = "udev")]
extern crate udev;
use std::time::Duration;
use std::path::Path;
use std::os::unix::io::{AsRawFd, IntoRawFd, FromRawFd, RawFd};
use std::io::{self, Read, Write};
use std::fs::{File, OpenOptions};
use std::{mem, cmp, iter};
use resize_slice::ResizeSlice;
pub use i2c::{SmbusReadWrite as ReadWrite, Functionality};
#[cfg(feature = "udev")]
mod enumerate;
#[cfg(feature = "udev")]
pub use enumerate::Enumerator;
#[cfg(feature = "i2c")]
mod i2c_impl;
pub enum Message<'a> {
Read {
address: u16,
data: &'a mut [u8],
flags: ReadFlags,
},
Write {
address: u16,
data: &'a [u8],
flags: WriteFlags,
},
}
impl<'a> Message<'a> {
pub fn len(&self) -> usize {
match *self {
Message::Read { ref data, .. } => data.len(),
Message::Write { ref data, .. } => data.len(),
}
}
pub fn address(&self) -> u16 {
match *self {
Message::Read { address, .. } => address,
Message::Write { address, .. } => address,
}
}
}
bitflags! {
#[derive(Default)]
pub struct ReadFlags: u16 {
const TENBIT_ADDR = i2c::I2C_M_TEN;
const RECEIVE_LEN = i2c::I2C_M_RECV_LEN;
const NACK = i2c::I2C_M_NO_RD_ACK;
const REVERSE_RW = i2c::I2C_M_REV_DIR_ADDR;
const NO_START = i2c::I2C_M_NOSTART;
const STOP = i2c::I2C_M_STOP;
}
}
bitflags! {
#[derive(Default)]
pub struct WriteFlags: u16 {
const TENBIT_ADDR = i2c::I2C_M_TEN;
const IGNORE_NACK = i2c::I2C_M_IGNORE_NAK;
const REVERSE_RW = i2c::I2C_M_REV_DIR_ADDR;
const NO_START = i2c::I2C_M_NOSTART;
const STOP = i2c::I2C_M_STOP;
}
}
pub struct I2c<I> {
inner: I,
address: Option<u16>,
address_10bit: bool,
functionality: Option<Functionality>,
}
impl I2c<File> {
pub fn from_path<P: AsRef<Path>>(p: P) -> io::Result<Self> {
OpenOptions::new().read(true).write(true)
.open(p).map(Self::new)
}
}
impl<I> I2c<I> {
pub fn new(device: I) -> Self {
I2c {
inner: device,
address: None,
address_10bit: false,
functionality: None,
}
}
pub fn into_inner(self) -> I {
self.inner
}
pub fn inner_ref(&self) -> &I {
&self.inner
}
pub fn inner_mut(&mut self) -> &mut I {
&mut self.inner
}
}
impl<I: AsRawFd> AsRawFd for I2c<I> {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}
impl<I: IntoRawFd> IntoRawFd for I2c<I> {
fn into_raw_fd(self) -> RawFd {
self.inner.into_raw_fd()
}
}
impl FromRawFd for I2c<File> {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
Self::new(File::from_raw_fd(fd))
}
}
impl<I: AsRawFd> I2c<I> {
fn update_functionality(&mut self) -> Option<Functionality> {
if let Some(func) = self.functionality.clone() {
Some(func)
} else {
let functionality = self.i2c_functionality().ok();
self.functionality = functionality.clone();
functionality
}
}
pub fn i2c_set_retries(&self, value: usize) -> io::Result<()> {
i2c::i2c_set_retries(self.as_raw_fd(), value)
}
pub fn i2c_set_timeout(&self, duration: Duration) -> io::Result<()> {
let value = duration.as_secs() as usize * 1000 + duration.subsec_nanos() as usize / 1000000;
i2c::i2c_set_timeout_ms(self.as_raw_fd(), value as _)
}
pub fn smbus_set_slave_address(&mut self, address: u16, tenbit: bool) -> io::Result<()> {
if let Some(func) = self.update_functionality() {
if func.contains(Functionality::TENBIT_ADDR) || tenbit {
i2c::i2c_set_slave_address_10bit(self.as_raw_fd(), tenbit)?;
}
}
let res = i2c::i2c_set_slave_address(self.as_raw_fd(), address, false);
if res.is_ok() {
self.address = Some(address);
self.address_10bit = tenbit;
}
res
}
pub fn smbus_set_pec(&self, pec: bool) -> io::Result<()> {
i2c::i2c_pec(self.as_raw_fd(), pec)
}
pub fn i2c_functionality(&self) -> io::Result<Functionality> {
i2c::i2c_get_functionality(self.as_raw_fd())
}
pub fn i2c_transfer_flags(&self) -> io::Result<(ReadFlags, WriteFlags)> {
let func = self.i2c_functionality()?;
let (mut read, mut write) = (ReadFlags::empty(), WriteFlags::empty());
if func.contains(Functionality::PROTOCOL_MANGLING) {
read.set(ReadFlags::NACK, true);
read.set(ReadFlags::REVERSE_RW, true);
read.set(ReadFlags::STOP, true);
write.set(WriteFlags::IGNORE_NACK, true);
write.set(WriteFlags::REVERSE_RW, true);
write.set(WriteFlags::STOP, true);
}
if func.contains(Functionality::NO_START) {
read.set(ReadFlags::NO_START, true);
write.set(WriteFlags::NO_START, true);
}
if func.contains(Functionality::TENBIT_ADDR) {
read.set(ReadFlags::TENBIT_ADDR, true);
write.set(WriteFlags::TENBIT_ADDR, true);
}
Ok((read, write))
}
pub fn i2c_transfer(&mut self, messages: &mut [Message]) -> io::Result<()> {
let mut message_buffer: [i2c::i2c_msg; i2c::I2C_RDWR_IOCTL_MAX_MSGS] = unsafe {
mem::uninitialized()
};
assert!(messages.len() <= message_buffer.len());
message_buffer.iter_mut().zip(messages.iter_mut())
.for_each(|(out, msg)| *out = match *msg {
Message::Read { address, ref mut data, flags } => i2c::i2c_msg {
addr: address,
flags: i2c::Flags::from_bits_truncate(flags.bits()) | i2c::Flags::RD,
len: data.len() as _,
buf: data.as_mut_ptr(),
},
Message::Write { address, ref data, flags } => i2c::i2c_msg {
addr: address,
flags: i2c::Flags::from_bits_truncate(flags.bits()),
len: data.len() as _,
buf: data.as_ptr() as *mut _,
},
});
let res = unsafe {
i2c::i2c_rdwr(self.as_raw_fd(), &mut message_buffer[..messages.len()])?;
};
message_buffer.iter().zip(messages.iter_mut())
.for_each(|(msg, out)| match *out {
Message::Read { ref mut data, .. } => data.resize_to(msg.len as usize),
Message::Write { .. } => (),
});
Ok(res)
}
pub fn smbus_write_quick(&mut self, value: ReadWrite) -> io::Result<()> {
i2c::i2c_smbus_write_quick(self.as_raw_fd(), value)
}
pub fn smbus_read_byte(&mut self) -> io::Result<u8> {
i2c::i2c_smbus_read_byte(self.as_raw_fd())
}
pub fn smbus_write_byte(&mut self, value: u8) -> io::Result<()> {
i2c::i2c_smbus_write_byte(self.as_raw_fd(), value)
}
pub fn smbus_read_byte_data(&mut self, command: u8) -> io::Result<u8> {
i2c::i2c_smbus_read_byte_data(self.as_raw_fd(), command)
}
pub fn smbus_write_byte_data(&mut self, command: u8, value: u8) -> io::Result<()> {
i2c::i2c_smbus_write_byte_data(self.as_raw_fd(), command, value)
}
pub fn smbus_read_word_data(&mut self, command: u8) -> io::Result<u16> {
i2c::i2c_smbus_read_word_data(self.as_raw_fd(), command)
}
pub fn smbus_write_word_data(&mut self, command: u8, value: u16) -> io::Result<()> {
i2c::i2c_smbus_write_word_data(self.as_raw_fd(), command, value)
}
pub fn smbus_process_call(&mut self, command: u8, value: u16) -> io::Result<u16> {
i2c::i2c_smbus_process_call(self.as_raw_fd(), command, value)
}
pub fn smbus_read_block_data(&mut self, command: u8, value: &mut [u8]) -> io::Result<usize> {
let len = cmp::min(value.len(), i2c::I2C_SMBUS_BLOCK_MAX);
i2c::i2c_smbus_read_block_data(self.as_raw_fd(), command, &mut value[..len])
}
pub fn smbus_write_block_data(&mut self, command: u8, value: &[u8]) -> io::Result<()> {
i2c::i2c_smbus_write_block_data(self.as_raw_fd(), command, value)
}
pub fn smbus_block_process_call(&mut self, command: u8, write: &[u8], read: &mut [u8]) -> io::Result<usize> {
let read_len = cmp::min(read.len(), i2c::I2C_SMBUS_BLOCK_MAX);
i2c::i2c_smbus_block_process_call(self.as_raw_fd(), command, write, &mut read[..read_len])
}
pub fn i2c_read_block_data(&mut self, command: u8, value: &mut [u8]) -> io::Result<usize> {
if let Some(func) = self.update_functionality() {
if !func.contains(Functionality::SMBUS_READ_I2C_BLOCK) || value.len() > i2c::I2C_SMBUS_BLOCK_MAX {
if func.contains(Functionality::I2C) {
if let Some(address) = self.address {
let mut msgs = [
Message::Write {
address: address,
data: &[command],
flags: if self.address_10bit { WriteFlags::TENBIT_ADDR } else { WriteFlags::default() },
},
Message::Read {
address: address,
data: value,
flags: if self.address_10bit { ReadFlags::TENBIT_ADDR } else { ReadFlags::default() },
},
];
return self.i2c_transfer(&mut msgs)
.map(|_| msgs[1].len())
}
}
}
}
let len = cmp::min(value.len(), i2c::I2C_SMBUS_BLOCK_MAX);
i2c::i2c_smbus_read_i2c_block_data(self.as_raw_fd(), command, &mut value[..len])
}
pub fn i2c_write_block_data(&mut self, command: u8, value: &[u8]) -> io::Result<()> {
if let Some(func) = self.update_functionality() {
if !func.contains(Functionality::SMBUS_WRITE_I2C_BLOCK) || value.len() > i2c::I2C_SMBUS_BLOCK_MAX {
if func.contains(Functionality::I2C) {
if let Some(address) = self.address {
let flags = if self.address_10bit { WriteFlags::TENBIT_ADDR } else { WriteFlags::default() };
return if func.contains(Functionality::NO_START) {
self.i2c_transfer(&mut [
Message::Write {
address: address,
data: &[command],
flags: flags,
},
Message::Write {
address: address,
data: value,
flags: flags | WriteFlags::NO_START,
},
])
} else {
self.i2c_transfer(&mut [
Message::Write {
address: address,
data: &iter::once(command).chain(value.iter().cloned()).collect::<Vec<_>>(),
flags: flags,
},
])
}
} else {
}
}
}
}
i2c::i2c_smbus_write_i2c_block_data(self.as_raw_fd(), command, value)
}
}
impl<I: Read> Read for I2c<I> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}
impl<I: Write> Write for I2c<I> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}