Skip to content

Commit

Permalink
Gyro fixes and tests, expose all types
Browse files Browse the repository at this point in the history
  • Loading branch information
rland93 committed Mar 12, 2024
1 parent 713ac9a commit 68ed784
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
targets: x86_64-unknown-linux-gnu
targets: x86_64-unknown-linux-gnu,thumbv7em-none-eabihf
components: rustfmt

- run: cargo doc
Expand All @@ -65,7 +65,7 @@ jobs:
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.75.0
targets: x86_64-unknown-linux-gnu
targets: x86_64-unknown-linux-gnu,thumbv7em-none-eabihf
components: clippy

- run: cargo clippy --all-targets
Expand All @@ -76,7 +76,7 @@ jobs:
strategy:
matrix:
rust: [stable]
TARGET: [x86_64-unknown-linux-gnu, x86_64-unknown-linux-musl]
TARGET: [thumbv7em-none-eabihf]

steps:
- uses: actions/checkout@v4
Expand All @@ -101,6 +101,8 @@ jobs:

- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
targets: x86_64-unknown-linux-gnu,thumbv7em-none-eabihf

- name: Run cargo-tarpaulin
run: cargo tarpaulin --out Lcov -- --test-threads 1
Expand Down
52 changes: 39 additions & 13 deletions examples/stm32f411.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,7 @@ fn main() -> ! {
// initialize the sensor
let mut sensor = Bmi088::new_with_i2c(i2c1, false, false);

// choose accelerometer or gyroscope test.
let acc_test = true;

while acc_test {
// TEST: Power on reset
// --------------------
info!("Reset...");
sensor.soft_reset().unwrap();
cortex_m::asm::delay(500_000);

loop {
// TEST: successful read of chip ID
// --------------------------------
info!("Reading chip ID");
Expand Down Expand Up @@ -125,9 +116,44 @@ fn main() -> ! {
let status = sensor.acc_status().unwrap();
debug!("Status register: status={:?}", status);

info!("END TEST-----------------------------");
info!("Chip ID");
let id = sensor.gyro_chipid().unwrap();
debug!("Chip ID: {:x}", id);

info!("Bandwidth");
let bw = bmi088::GyroBandwidth::Hz32;
sensor.gyro_bandwidth_write(bw).unwrap();
let readback_bw = sensor.gyro_bandwidth_read().unwrap();
assert!(bw == readback_bw);

info!("Range");
let range = bmi088::GyroRange::Dps2000;
sensor.gyro_range_write(range).unwrap();
let readback_range = sensor.gyro_range_read().unwrap();
assert!(range == readback_range);

info!("Power mode");
let power = bmi088::GyroPowerMode::Normal;
sensor.gyro_power_mode_write(power).unwrap();
let readback_power = sensor.gyro_power_mode_read().unwrap();
assert!(power == readback_power);

info!("Data ready interrupt mapping");
let pin_active = bmi088::PinActive::ActiveHigh;
let pin_behavior = bmi088::PinBehavior::PushPull;

sensor
.gyro_conf_int3_write(pin_active, pin_behavior)
.unwrap();
let (rb_active, rb_beh) = sensor.gyro_conf_int3_read().unwrap();
assert!(pin_active == rb_active);
assert!(pin_behavior == rb_beh);

cortex_m::asm::delay(500_000);
info!("Reading gyroscope data");
let values = sensor.gyro_read_rate().unwrap();
debug!(
"Gyroscope data: [{:?} {:?} {:?}]",
values.x, values.y, values.z
);
}
panic!();
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ mod types;
pub use crate::interface::Addr;
pub use crate::types::{
AccBandwidth, AccConf, AccDataRate, AccPowerConf, AccPowerEnable, AccRange,
AccelerometerConfig, ErrCode, Error, Sensor3DData,
AccelerometerConfig, ErrCode, Error, GyroBandwidth, GyroDrdyMap, GyroPowerMode, GyroRange,
IntConfiguration, IntPin, PinActive, PinBehavior, Sensor3DData,
};

/// BMI088 device object.
Expand Down
4 changes: 3 additions & 1 deletion src/sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,9 @@ where
pub fn gyro_bandwidth_read(&mut self) -> Result<GyroBandwidth, Error<CommE>> {
let reg = GyroRegisters::GYRO_BANDWIDTH as u8;
let data = self.iface.read_register_gyro(reg)?;
let bw = GyroBandwidth::try_from(data).map_err(|_| Error::InvalidInputData)?;
// Note: bit #7 is read-only and always ‚1‘, but has no function and can safely be ignored.
let data_masked = data & 0b0111_1111;
let bw = GyroBandwidth::try_from(data_masked).map_err(|_| Error::InvalidInputData)?;
Ok(bw)
}

Expand Down
10 changes: 10 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,16 @@ pub struct AccelerometerConfig {
pub acc_range: AccRange,
}

/// Input pin configuration
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum IntPin {
/// Input
Input = 0b0001_0000,
/// Output
Output = 0b0000_1000,
}

/// Pin behavior configuration
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PinBehavior {
/// Push-pull
Expand All @@ -218,13 +221,16 @@ pub enum PinBehavior {
OpenDrain = 0b0000_0100,
}

/// Pin active configuration
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PinActive {
/// Active high
ActiveHigh = 0b0000_0000,
/// Active low
ActiveLow = 0b0000_0010,
}

/// Struct to hold the configuration of an input pin
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct IntConfiguration {
/// Input or output
Expand Down Expand Up @@ -310,6 +316,7 @@ pub enum AccDrdyMap {
Int1Int2 = 0b0100_0100,
}

/// Gyroscope range configuration
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GyroRange {
/// ±2000°/s
Expand Down Expand Up @@ -339,6 +346,7 @@ impl TryFrom<u8> for GyroRange {
}
}

/// Gyroscope bandwidth configuration
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GyroBandwidth {
/// 532 Hz, ODR 2000 Hz
Expand Down Expand Up @@ -377,6 +385,7 @@ impl TryFrom<u8> for GyroBandwidth {
}
}

/// Gyroscope power mode
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GyroPowerMode {
/// Normal mode
Expand All @@ -400,6 +409,7 @@ impl TryFrom<u8> for GyroPowerMode {
}
}

/// Gyroscope data ready interrupt mapping
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GyroDrdyMap {
/// Map data ready to neither pin
Expand Down

0 comments on commit 68ed784

Please sign in to comment.