• Stars
    star
    121
  • Rank 293,924 (Top 6 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rust library providing access to spidev devices under Linux

Rust Spidev

Build Status Version License

Documentation

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.

Example/API

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.

extern crate spidev;
use std::io;
use std::io::prelude::*;
use spidev::{Spidev, SpidevOptions, SpidevTransfer, SpiModeFlags};

fn create_spi() -> io::Result<Spidev> {
    let mut spi = Spidev::open("/dev/spidev0.0")?;
    let options = SpidevOptions::new()
         .bits_per_word(8)
         .max_speed_hz(20_000)
         .mode(SpiModeFlags::SPI_MODE_0)
         .build();
    spi.configure(&options)?;
    Ok(spi)
}

/// perform half duplex operations using Read and Write traits
fn half_duplex(spi: &mut Spidev) -> io::Result<()> {
    let mut rx_buf = [0_u8; 10];
    spi.write(&[0x01, 0x02, 0x03])?;
    spi.read(&mut rx_buf)?;
    println!("{:?}", rx_buf);
    Ok(())
}

/// Perform full duplex operations using Ioctl
fn full_duplex(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 tx_buf = [0x01, 0x02, 0x03];
    let mut rx_buf = [0; 3];
    {
        let mut transfer = SpidevTransfer::read_write(&tx_buf, &mut rx_buf);
        spi.transfer(&mut transfer)?;
    }
    println!("{:?}", rx_buf);
    Ok(())
}

fn main() {
    let mut spi = create_spi().unwrap();
    println!("{:?}", half_duplex(&mut spi).unwrap());
    println!("{:?}", full_duplex(&mut spi).unwrap());
}

Features

The following features are implemented and planned for the library:

  • Implement the Read trait
  • Implement the Write trait
  • Support for full-duplex transfers
  • Support for configuring spidev device
  • Support for querying spidev configuration state

Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.46 and up. It might compile with older versions but that may change in any new patch release.

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:

  1. Install rust and cargo
  2. Install an appropriate cross compiler. On an Ubuntu system, this can be done by doing sudo apt-get install g++-arm-linux-gnueabihf.
  3. 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).
  4. Tell cargo how to link by adding the lines below to your ~/.cargo/config file.
  5. 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

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Code of Conduct

Contribution to this crate is organized under the terms of the Rust Code of Conduct, the maintainer of this crate, the Embedded Linux Team, promises to intervene to uphold that code of conduct.

More Repositories

1

rust-raspberrypi-OS-tutorials

📚 Learn to write an embedded OS in Rust 🦀
Rust
13,436
star
2

awesome-embedded-rust

Curated list of resources for Embedded and Low-level development in the Rust programming language
6,173
star
3

embedded-hal

A Hardware Abstraction Layer (HAL) for embedded systems
Rust
1,977
star
4

wg

Coordination repository of the embedded devices Working Group
1,892
star
5

heapless

Heapless, `static` friendly data structures
Rust
1,492
star
6

discovery

Discover the world of microcontrollers through Rust!
Rust
1,458
star
7

book

Documentation on how to use the Rust Programming Language to develop firmware for bare metal (microcontroller) devices
1,083
star
8

riscv

Low level access to RISC-V processors
Rust
820
star
9

cortex-m

Low level access to Cortex-M processors
Rust
801
star
10

cortex-m-quickstart

Template to develop bare metal applications for Cortex-M microcontrollers
Rust
786
star
11

svd2rust

Generate Rust register maps (`struct`s) from SVD files
Rust
685
star
12

cargo-binutils

Cargo subcommands to invoke the LLVM tools shipped with the Rust toolchain
Rust
488
star
13

rust-sysfs-gpio

A Rust Interface to the Linux sysfs GPIO interface (https://www.kernel.org/doc/Documentation/gpio/sysfs.txt)
Rust
383
star
14

cortex-m-rt

Minimal startup / runtime for Cortex-M microcontrollers
Rust
357
star
15

riscv-rt

Minimal runtime / startup for RISC-V CPU's.
Rust
299
star
16

embedded-alloc

A heap allocator for embedded systems
Rust
295
star
17

linux-embedded-hal

Implementation of the `embedded-hal` traits for Linux devices
Rust
233
star
18

gpio-cdev

Rust interface to the Linux GPIO Character Device API (/dev/gpiochip...)
Rust
210
star
19

embedonomicon

How to bootstrap support for a no_std target
Rust
206
star
20

rust-i2cdev

Rust library for interfacing with i2c devices under Linux
Rust
205
star
21

not-yet-awesome-embedded-rust

A collection of items that are not yet awesome in Embedded Rust
120
star
22

cortex-a

Low level access to Cortex-A processors
Rust
120
star
23

bare-metal

Abstractions common to microcontrollers
Rust
114
star
24

meta-rust-bin

Yocto layer for installing Rust toolchain from pre-built binaries
BitBake
102
star
25

critical-section

Pluggable critical section
Rust
97
star
26

gpio-utils

Userspace Utilities for managing GPIOs in Linux
Rust
94
star
27

showcase

Awesome embedded projects by the Rust community!
CSS
91
star
28

nb

Minimal and reusable non-blocking I/O layer
Rust
87
star
29

rust-embedded.github.io

A collection of books and other documents about embedded Rust
84
star
30

blog

The Rust Embedded WG Blog
SCSS
71
star
31

svd

A CMSIS-SVD file parser
Rust
70
star
32

r0

Initialization code ("crt0") written in Rust
Rust
69
star
33

aarch64-cpu

Low level access to processors using the AArch64 execution state.
Rust
65
star
34

svdtools

Python package to handle vendor-supplied, often buggy SVD files.
Rust
60
star
35

debugonomicon

Shell
60
star
36

rust-sysfs-pwm

Linux PWM Access via Sysfs in Rust
Rust
47
star
37

register-rs

Unified interface for type-safe MMIO and CPU register access in Rust
Rust
46
star
38

fixedvec-rs

Heapless vector implementation for Rust
Rust
45
star
39

qemu-exit

Exit QEMU with user-defined code
Rust
40
star
40

cortex-m-semihosting

Semihosting for ARM Cortex-M processors
Rust
40
star
41

msp430

Low level access to MSP430 microcontrollers
Rust
36
star
42

msp430-quickstart

Template to develop bare metal applications for MSP430 microcontrollers
Rust
36
star
43

embedded-dma

Rust
35
star
44

volatile-register

Volatile access to memory mapped hardware registers
Rust
34
star
45

itm

Tool to parse and dump ITM packets
Rust
28
star
46

mutex-trait

Low level API definition of a Mutex
Rust
25
star
47

msp430-rt

Minimal startup / runtime for MSP430 microcontrollers
Rust
15
star
48

patterns

A book capturing different approaches or patterns for solving problems in Embedded Rust
15
star
49

docker-rust-cross

Docker images for testing rust code on many versions and architecures (DEPRECATED)
Shell
13
star
50

panic-semihosting

Report panic messages to the host stderr using semihosting
Rust
12
star
51

cortex-r

Low level access to Cortex-R processors
Shell
11
star
52

arm-dcc

Debug Communication Channel (DCC) API
Rust
9
star
53

rust-embedded-www

The Rust embedded website: http://www.rust-embedded.org
HTML
7
star
54

panic-itm

Log panic messages using the ITM (Instrumentation Trace Macrocell)
Rust
6
star
55

rust-embedded-provisioning

Terraform provisioning for Rust Embedded infrastructure
HCL
2
star
56

template

Rust
1
star
57

discovery-mb2

Rust Discovery Book for BBC micro::bit v2
Rust
1
star