Skip to main content

Module linux

Module linux 

Source
Expand description

Linux backends: the kernel’s I2C, SPI, and GPIO character devices as the bus traits.

On a Raspberry Pi or any Linux single-board computer the buses are files: /dev/i2c-1, /dev/spidev0.0, /dev/gpiochip0. These functions open them as the traits every driver is written against, through linux-embedded-hal, so a driver moves from a scripted test to a gateway by being handed a different bus and nothing else. The kernel drivers have to be enabled first: on a Raspberry Pi that is raspi-config, or dtparam=i2c_arm=on and dtparam=spi=on in config.txt.

A DS18B20 is the one part that should not be bit-banged from a Linux process, because user space cannot hold the microsecond slot timing the bus needs. Enable the kernel’s own w1-gpio driver instead (dtoverlay=w1-gpio) and read the thermometer through the sysfs file it exposes, which the DS18B20 driver in pamoja-sensors does.

§Examples

use pamoja_hal::i2c::I2c;
use pamoja_hal::linux;

// The BME280 on the Raspberry Pi's user I2C bus answers its chip id at 0xD0.
const BME280: u8 = 0x76;
let mut bus = linux::i2c("/dev/i2c-1")?;
let mut id = [0u8; 1];
bus.write_read(BME280, &[0xD0], &mut id)?;
println!("chip id 0x{:02X}", id[0]);

Re-exports§

pub use linux_embedded_hal::gpio_cdev;
pub use linux_embedded_hal::i2cdev;
pub use linux_embedded_hal::spidev;

Structs§

CdevPin
Newtype around gpio_cdev::LineHandle that implements the embedded-hal traits
CdevPinError
Error type wrapping gpio_cdev::errors::Error to implement embedded_hal::digital::Error
Delay
Empty struct that provides delay functionality on top of thread::sleep, and tokio::time::sleep if the async-tokio feature is enabled.
I2CError
Error type wrapping LinuxI2CError to implement embedded_hal::i2c::ErrorKind
I2cdev
Newtype around i2cdev::linux::LinuxI2CDevice that implements the embedded-hal traits
SPIError
Error type wrapping io::Error to implement embedded_hal::spi::ErrorKind
SpidevBus
Spidev wrapper providing the embedded-hal SpiBus trait.
SpidevDevice
Spidev wrapper providing the embedded-hal SpiDevice trait.

Enums§

OpenError
Why a bus or a line could not be opened.

Functions§

delay
Returns the delay that sleeps the process, for pacing a driver on a gateway.
i2c
Opens an I2C adapter, such as /dev/i2c-1.
input
Requests a GPIO line as an input.
output
Requests a GPIO line as an output.
spi
Opens an SPI device, such as /dev/spidev0.0, at a clock mode and speed.