Skip to content

Commit

Permalink
Fixes/Add: Gyroscope SelfTest and FIFO reads
Browse files Browse the repository at this point in the history
* Fix crtl type replaced by ctrl
* Add missing documentation on different fn
* Add Gyroscope SelfTest
* Add private helper for U12.4 format and f32_abs
* Fix FIFO sample count in `read_fifo`
  • Loading branch information
IniterWorker committed Sep 7, 2024
1 parent 8424661 commit 0424495
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 29 deletions.
19 changes: 14 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@ name = "qmi8658"
categories = ["embedded", "no-std"]
version = "0.1.0"
edition = "2021"
authors = [
"Walter Bonetti <[email protected]>"
]
authors = ["Walter Bonetti <[email protected]>"]
license = "MIT OR Apache-2.0"
description = "QMI8658 Sensor/Gyroscope"
keywords = ["no-std", "qmi8658", "embedded", "embedded-hal-driver", "sensor"]
include = ["src/**/*.rs", "crates-io.md", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
include = [
"src/**/*.rs",
"crates-io.md",
"README.md",
"LICENSE-APACHE",
"LICENSE-MIT",
]
repository = "https://github.com/IniterWorker/qmi8658"
readme = "README.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[package.metadata.docs.rs]
targets = [ "thumbv7m-none-eabi", "thumbv7em-none-eabihf" ]
targets = ["thumbv7m-none-eabi", "thumbv7em-none-eabihf"]

[dependencies]
# Embedded HAL abstraction
# We use this layer to abstract hardware i2c/spi
embedded-hal = { version = "1.0.0" }
bitfield = "0.15.0"
log = { version = "0.4", default-features = false, optional = true }

[features]
default = []
loglib = ["log"]

[dev-dependencies.cargo-husky]
version = "1"
Expand Down
138 changes: 138 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ pub mod register {
assert_eq!(data.aodr(), AccelerometerODR::NormalAODR7);
}

#[test]
fn test_struct_bit_range_bis() {
let register: u8 = 0b0000_0111;

let data = Ctrl2Register(register);
assert!(!data.ast());
assert_eq!(data.afs(), AccelerometerFS::FS2G);
assert_eq!(data.aodr(), AccelerometerODR::NormalAODR7);
}

#[test]
fn test_struct_set_bit_range() {
let register_result: u8 = 0b1000_0111;
Expand All @@ -421,6 +431,18 @@ pub mod register {
data.set_aodr(AccelerometerODR::NormalAODR7);
assert_eq!(data.0, register_result);
}

#[test]
fn test_struct_set_bit_range_bis() {
let register_result: u8 = 0b0000_0111;
let register_default: u8 = 0b0000_0000;

let mut data = Ctrl2Register(register_default);
data.set_ast(false);
data.set_afs(AccelerometerFS::FS2G);
data.set_aodr(AccelerometerODR::NormalAODR7);
assert_eq!(data.0, register_result);
}
}
}

Expand Down Expand Up @@ -493,12 +515,120 @@ pub mod register {
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Ctrl3Register(u8);
impl Debug;
#[doc = "Set Gyroscope Self-Test Enable/Disable"]
pub bool, gst, set_gst: 7;
#[doc = "Set Gyroscope Full-scale"]
pub GyroscopeFS, gfs, set_gfs: 6, 4;
#[doc = "Set Gyroscope Output Data Rate (ODR)"]
pub GyroscopeODR, godr, set_godr: 3, 0;
}

#[cfg(test)]
mod test {
use bitfield::{BitRange, BitRangeMut};

use super::Ctrl3Register;
use super::GyroscopeFS;
use super::GyroscopeODR;

#[test]
fn test_gfs_bit_range() {
// Test DPS16
let value_dps16: u8 = 0b0000_0000;
assert_eq!(
<u8 as BitRange<GyroscopeFS>>::bit_range(&value_dps16, 6, 4),
GyroscopeFS::DPS16
);

// Test DPS32
let value_dps32: u8 = 0b0001_0000;
assert_eq!(
<u8 as BitRange<GyroscopeFS>>::bit_range(&value_dps32, 6, 4),
GyroscopeFS::DPS32
);

// Test DPS64
let value_dps64: u8 = 0b0010_0000;
assert_eq!(
<u8 as BitRange<GyroscopeFS>>::bit_range(&value_dps64, 6, 4),
GyroscopeFS::DPS64
);

// Test DPS128
let value_dps128: u8 = 0b0011_0000;
assert_eq!(
<u8 as BitRange<GyroscopeFS>>::bit_range(&value_dps128, 6, 4),
GyroscopeFS::DPS128
);

// Test DPS256
let value_dps256: u8 = 0b0100_0000;
assert_eq!(
<u8 as BitRange<GyroscopeFS>>::bit_range(&value_dps256, 6, 4),
GyroscopeFS::DPS256
);

// Test DPS512
let value_dps512: u8 = 0b0101_0000;
assert_eq!(
<u8 as BitRange<GyroscopeFS>>::bit_range(&value_dps512, 6, 4),
GyroscopeFS::DPS512
);

// Test DPS1024
let value_dps1024: u8 = 0b0110_0000;
assert_eq!(
<u8 as BitRange<GyroscopeFS>>::bit_range(&value_dps1024, 6, 4),
GyroscopeFS::DPS1024
);

// Test DPS2048
let value_dps2048: u8 = 0b0111_0000;
assert_eq!(
<u8 as BitRange<GyroscopeFS>>::bit_range(&value_dps2048, 6, 4),
GyroscopeFS::DPS2048
);
}

#[test]
fn test_gfs_set_bit_range() {
let mut value: u8 = 0;

// Set DPS16
value.set_bit_range(6, 4, GyroscopeFS::DPS16);
assert_eq!(value, 0b0000_0000);

// Set DPS32
value.set_bit_range(6, 4, GyroscopeFS::DPS32);
assert_eq!(value, 0b0001_0000);

// Set DPS32
value.set_bit_range(6, 4, GyroscopeFS::DPS64);
assert_eq!(value, 0b0010_0000);
}

#[test]
fn test_struct_bit_range() {
let register: u8 = 0b1000_0111;

let data = Ctrl3Register(register);
assert!(data.gst());
assert_eq!(data.gfs(), GyroscopeFS::DPS16);
assert_eq!(data.godr(), GyroscopeODR::NormalGORD7);
}

#[test]
fn test_struct_set_bit_range() {
let register_result: u8 = 0b1000_0111;
let register_default: u8 = 0b0000_0000;

let mut data = Ctrl3Register(register_default);
data.set_gst(true);
data.set_gfs(GyroscopeFS::DPS16);
data.set_godr(GyroscopeODR::NormalGORD7);
assert_eq!(data.0, register_result);
}
}
}

pub mod ctrl5 {
Expand Down Expand Up @@ -855,6 +985,14 @@ pub mod register {
let data = SensorDataAvailableAndLockRegister(register);
assert_eq!(data.ctrl9_cmd_done(), Ctrl9CmdDone::Done);
}

#[test]
fn test_cmd_available_read() {
let register: u8 = 0b0000_0001;

let data = SensorDataAvailableAndLockRegister(register);
assert!(data.available());
}
}
}

Expand Down
Loading

0 comments on commit 0424495

Please sign in to comment.