LSM9DS0
A platform-agnostic Rust driver for the ST LSM9DS0 9-axis IMU (3D accelerometer, 3D gyroscope, 3D magnetometer).
Built on embedded-hal-async traits for I2C and SPI communication.
Features
- I2C and SPI interface support
- Configurable sensor ranges and data rates
- Temperature sensor
- FIFO support
- Interrupt configuration
no_stdcompatible
Usage
Add the dependency to your Cargo.toml:
[]
= "0.1"
I2C
use ;
use Delay;
let interface = init;
let config = new
.with_gyro_enabled
.with_gyro_data_rate
.with_accel_data_rate
.with_mag_mode
.with_temperature_enabled
.with_auto_calibration;
let mut imu = new_with_config;
imu.init.await?;
let = imu.read_gyro.await?; // DegreesPerSecond
let = imu.read_accel.await?; // GForce
let = imu.read_mag.await?; // Gauss
let temp = imu.read_temp.await?; // Celsius
SPI
The LSM9DS0 has separate chip selects for the gyroscope and accelerometer/magnetometer. Provide two SpiDevice instances:
use ;
use Delay;
let interface = init;
let mut imu = new;
imu.init.await?;
Configuration
Sensor parameters can be set at initialization or changed at runtime:
use ;
use Delay;
// At initialization
let config = new
.with_gyro_scale
.with_accel_scale
.with_mag_scale;
// At runtime
imu.set_gyro_scale.await?;
imu.set_accel_data_rate.await?;
// Calibrate the gyroscope and accelerometer during runtime
imu.calibrate_bias.await?;
Blocking I2C / SPI
This driver is built on embedded-hal-async traits, but it works with blocking buses too. The
async methods on the driver still require .await, but if the underlying bus is blocking, they
complete immediately with no executor overhead.
You need two things:
- An adapter that wraps your blocking
embedded_hal::i2c::I2c(orSpiDevice) to satisfy the async trait. The async fn just calls the blocking method and returns — no actual suspension:
;
- A trivial executor like
embassy_futures::block_onto run the async code synchronously:
use block_on;
let interface = init;
let mut imu = new_with_config;
block_on;
See the examples/rp2040/src/bin/simple-i2c-blocking.rs example for a complete working implementation.
Default Configuration
The driver's default configuration matches the device's power-on-reset register values from the datasheet, with two exceptions where the actual hardware defaults differ from the documented values:
| Register | Datasheet | Actual |
|---|---|---|
| CTRL_REG7_XM | 0x02 | 0x03 |
| INT_CTRL_REG_M | 0x00 | 0xE8 |
Examples
See the examples/ directory for platform-specific examples, including RP2040.