The Rust spidev
seeks to provide full access to the Linux spidev
device in Rust without the need to wrap any C code or directly make
low-level system calls. The documentation for the spidev interace can
be found at https://www.kernel.org/doc/Documentation/spi/spidev.
The following is not an exhaustive demonstration of the Spidev interface but provides a pretty good idea of how to use the library in practice.
```rust extern crate spidev; use std::io; use std::io::prelude::*; use spidev::{Spidev, SpidevOptions, SpidevTransfer, SPIMODE0};
fn createspi() -> io::Result
/// perform half duplex operations using Read and Write traits fn halfduplex(spi: &mut Spidev) -> io::Result<()> { let mut rxbuf = [0u8; 10]; try!(spi.write(&[0x01, 0x02, 0x03])); try!(spi.read(&mut rxbuf)); println!("{:?}", rx_buf); Ok(()) }
/// Perform full duplex operations using Ioctl fn fullduplex(spi: &mut Spidev) -> io::Result<()> { // "write" transfers are also reads at the same time with // the read having the same length as the write let mut transfer = SpidevTransfer::write(&[0x01, 0x02, 0x03]); try!(spi.transfer(&mut transfer)); println!("{:?}", transfer.rxbuf); Ok(()) }
fn main() { let mut spi = createspi().unwrap(); println!("{:?}", halfduplex(&mut spi).unwrap()); println!("{:?}", full_duplex(&mut spi).unwrap()); } ```
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