Rust Spidev
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.
Example/API
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.
extern crate i2cdev;
use *;
use thread;
const NUNCHUCK_SLAVE_ADDR: u16 = 0x52;
// real code should not use unwrap() so liberally
In addition to the Read/Write traits, the following methods are available via the I2CSMBus trait:
Features
The following features are implemented and planned for the library:
- Implement the Read trait
- Implement the Write trait
- Implement SMBus Methods
- Add Tests/Example for SMBus Methods
- Add Support for Non-SMBus ioctl methods
- Add examples for non-smbus ioctl methods
- Unit Testing
Cross Compiling
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:
- Install rust and cargo
- Install an appropriate cross compiler. On an Ubuntu system, this
can be done by doing
sudo apt-get install g++-arm-linux-gnueabihf
. - Build or install rust for your target. This is necessary in order to have libstd available for your target. For arm-linux-gnueabihf, you can find binaries at https://github.com/japaric/ruststrap. With this approach or building it yourself, you will need to copy the ${rust}/lib/rustlib/arm-unknown-linux-gnueabihf to your system rust library folder (it is namespaced by triple, so it shouldn't break anything).
- Tell cargo how to link by adding the lines below to your ~/.cargo/config file.
- Run your build
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"
License
Copyright (c) 2015, Paul Osborne ospbau@gmail.com
Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or http://www.apache.org/license/LICENSE-2.0> or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. This file may not be copied, modified, or distributed except according to those terms.