Expand description
This is a platform agnostic Rust driver for the KXCJ9 and KXCJB ultra-low-power
tri-axis accelerometers (up to +/-16g) using the embedded-hal
traits.
This driver allows you to:
- Enable/disable the device. See
enable()
. - Read the acceleration measurement. See
read()
. - Read the unscaled acceleration measurement. See
read_unscaled()
. - Set resolution. See
set_resolution()
. - Set output data rate. See
set_output_data_rate()
. - Set +/- G range. See
set_scale()
. - Read
WHO_AM_I
register. Seewho_am_i()
. - Perform a software reset. See
reset()
. - Run a communication self-test. See
communication_self_test()
. - Enable/disable MEMS self-test function. See
enable_mems_self_test()
. - Interrupt support:
- Enable/disable new acceleration data ready interrupt. See
enable_data_ready_interrupt()
. - Enable/disable and configure wake-up motion detected interrupt. See
enable_wake_up_interrupt()
. - Enable/disable physical interrupt pin. See
enable_interrupt_pin()
. - Set physical interrupt pin polarity. See
set_interrupt_pin_polarity()
. - Set physical interrupt pin latching behavior. See
set_interrupt_pin_latching()
. - Check if any interrupt has happened. See
has_interrupt_happened()
. - Clear interrupts. See
clear_interrupts()
. - Read interrupt source information. See
read_interrupt_info()
.
- Enable/disable new acceleration data ready interrupt. See
§The devices
The KXCJ9 is a high-performance, ultra-low-power, tri-axis accelerometer designed for mobile applications. It offers our best power performance along with an embedded wake-up feature, Fast-mode I²C and up to 14-bit resolution. The KXCJ9 sensor offers improved shock, reflow, and temperature performance, and the ASIC has internal voltage regulators that allow operation from 1.8 V to 3.6 V within the specified product performance.
The KXCJB is the thinnest tri-axis accelerometer available on the market today. This ultra-thin 3x3x0.45mm low-power accelerometer is also one of our most full-featured products. The KXCJB offers up to 14-bit resolution for greater precision. User-selectable parameters include ± 2g, 4g or 8g ranges and Output Data Rates (ODR) with programmable low-pass filter. The KXCJB also features the Kionix XAC sense element, our most advanced sense element, for outstanding stability over temperature, shock and post-reflow performance.
The communication is done through an I2C bidirectional bus.
Datasheet:
Application Note:
§Usage examples (see also examples folder)
To use this driver, import this crate and an embedded_hal
implementation,
then instantiate the appropriate device.
Please find additional examples using hardware in this repository: driver-examples
§Read acceleration in G
extern crate kxcj9;
extern crate linux_embedded_hal as hal;
use kxcj9::{Kxcj9, SlaveAddr};
let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let address = SlaveAddr::default();
let mut sensor = Kxcj9::new_kxcj9_1018(dev, address);
sensor.enable().unwrap();
loop {
let acc = sensor.read().unwrap();
println!("X: {:2}, Y: {:2}, Z: {:2}", acc.x, acc.y, acc.z);
}
§Select high resolution
extern crate kxcj9;
extern crate linux_embedded_hal as hal;
use kxcj9::{Kxcj9, Resolution, SlaveAddr};
let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Kxcj9::new_kxcj9_1018(dev, SlaveAddr::default());
sensor.enable().unwrap();
sensor.set_resolution(Resolution::High).unwrap();
// with this settings measurements are taken with 12-bit resolution
§Select +/-16g scale
extern crate kxcj9;
extern crate linux_embedded_hal as hal;
use kxcj9::{GScale16, Kxcj9, SlaveAddr};
let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Kxcj9::new_kxcj9_1018(dev, SlaveAddr::default());
sensor.enable().unwrap();
sensor.set_scale(GScale16::G16FP).unwrap();
// with this settings measurements are taken with 14-bit resolution
§Select 200Hz output data rate
extern crate kxcj9;
extern crate linux_embedded_hal as hal;
use kxcj9::{Kxcj9, OutputDataRate, SlaveAddr};
let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Kxcj9::new_kxcj9_1018(dev, SlaveAddr::default());
sensor.enable().unwrap();
sensor.set_output_data_rate(OutputDataRate::Hz200).unwrap();
§Configure and enable wake-up interrupt
extern crate kxcj9;
extern crate linux_embedded_hal as hal;
use kxcj9::{
Kxcj9, SlaveAddr, WakeUpInterruptConfig, WakeUpOutputDataRate,
WakeUpTriggerMotion,
};
let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Kxcj9::new_kxcj9_1008(dev, SlaveAddr::default());
let config = WakeUpInterruptConfig {
trigger_motion: WakeUpTriggerMotion::default(),
data_rate: WakeUpOutputDataRate::Hz3_125,
fault_count: 3,
threshold: 0.5, // G
};
// 0.5g acceleration must be present for 0.96s to trigger interrupt
sensor.enable_wake_up_interrupt(config).unwrap();
§Perform a software reset and wait for it to finish
extern crate kxcj9;
extern crate linux_embedded_hal as hal;
#[macro_use(block)]
extern crate nb;
use kxcj9::{Kxcj9, OutputDataRate, SlaveAddr};
let dev = hal::I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Kxcj9::new_kxcj9_1018(dev, SlaveAddr::default());
block!(sensor.reset());
Re-exports§
pub use scaled_device::ScaledDevice;
pub use device_impl::GScaleConfig;
pub use device_impl::MeasurementBits;
Structs§
- Interrupt
Info - Interrupt source information
- Kxcj9
- KXCJ9/KXCJB device driver
- Measurement
- Acceleration measurement scaled to configured G range
- Unscaled
Measurement - Unscaled acceleration measurement
- Wake
UpInterrupt Config - Wake-up interrupt configuration
- Wake
UpTrigger Motion - Wake-up interrupt trigger motion
Enums§
- Error
- All possible errors in this crate
- GScale8
- KXCJ9-1008 G scale (up to +/-8g)
- GScale16
- KXCJ9-1018 G scale (up to +/-16g)
- Interrupt
PinLatching - Physical interrupt pin latching behavior
- Interrupt
PinPolarity - Physical interrupt pin polarity
- Output
Data Rate - Output data rate
- Resolution
- Measurement resolution
- Slave
Addr - Possible slave addresses
- Wake
UpOutput Data Rate - Output data rate for wake-up motion detection