The Rust i2cdev
crate seeks to provide full access to the Linux i2cdev
driver interface in Rust without the need to wrap any C code or directly make
low-level system calls. The documentation for the i2cdev interace can
be found at https://www.kernel.org/doc/Documentation/i2c/dev-interface and
in the lm-sensors projects.
The source includes an example of using the library to talk to a Wii Nunchuck (which has an i2c interface). Go View the Example Example.
Here's a real quick example showing the guts of how you create a device and start talking to it... This device only requires basic functions (read/write) which are done via the Read/Write traits.
```rust extern crate i2cdev; use i2cdev::*; use std::thread;
const NUNCHUCKSLAVEADDR: u16 = 0x52;
// real code should not use unwrap() so liberally fn i2cfun() -> Result<(), I2CError> { let mut dev = try!(I2CDevice::new("/dev/i2c-1", NUNCHUCKSLAVEADDR)); self.i2cdev.writeall(&[0xF0, 0x55]).unwrap(); self.i2cdev.writeall(&[0xFB, 0x00]).unwrap(); thread::sleepms(100); loop { let mut buf: [u8: 6] = [0: 6]; self.i2cdev.writeall(&[0x00]).unwrap(); thread::sleep_ms(10); self.i2cdev.read(&mut buf).unwrap(); println!("Reading: {:?}", buf); } } ```
In addition to the Read/Write traits, the following methods are available via the I2CSMBus trait:
rust
pub trait I2CSMBus {
fn smbus_write_quick(&self, bit: bool) -> Result<(), Error>;
fn smbus_read_byte(&self) -> Result<u8, Error>;
fn smbus_write_byte(&self, value: u8) -> Result<(), Error>;
fn smbus_read_byte_data(&self, register: u8) -> Result<u8, Error>;
fn smbus_write_byte_data(&self, register: u8, value: u8) -> Result<(), Error>;
fn smbus_read_word_data(&self, register: u8) -> Result<u16, Error>;
fn smbus_write_word_data(&self, register: u8, value: u16) -> Result<(), Error>;
fn smbus_process_word(&self, register: u8, value: u16) -> Result<u16, Error>;
fn smbus_read_block_data(&self, register: u8) -> Result<Vec<u8>, Error>;
fn smbus_write_block_data(&self, register: u8, values: &[u8]) -> Result<(), Error>;
fn smbus_process_block(&self, register: u8, values: &[u8]) -> Result<(), Error>;
}
The following features are implemented and planned for the library:
Most likely, the machine you are running on is not your development machine (although it could be). In those cases, you will need to cross-compile. The following basic instructions should work for the raspberry pi or beaglebone black:
sudo apt-get install g++-arm-linux-gnueabihf
.cargo build --target=arm-unknown-linux-gnueabi
.The following snippet added to my ~/.cargo/config worked for me:
[target.arm-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
Copyright (c) 2015, Paul Osborne ospbau@gmail.com
Licensed under the Apache License, Version 2.0