feature parity with cpp lib
This commit is contained in:
175
src/lib.rs
175
src/lib.rs
@@ -9,15 +9,8 @@ use embedded_hal_async::i2c::{self};
|
|||||||
|
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
|
|
||||||
const REG_XOUT_0: u8 = 0x00;
|
const REG_OUT_START: u8 = 0x00;
|
||||||
const REG_XOUT_1: u8 = 0x01;
|
|
||||||
const REG_YOUT_0: u8 = 0x02;
|
|
||||||
const REG_YOUT_1: u8 = 0x03;
|
|
||||||
const REG_ZOUT_0: u8 = 0x04;
|
|
||||||
const REG_ZOUT_1: u8 = 0x05;
|
|
||||||
const REG_XOUT_2: u8 = 0x06;
|
|
||||||
const REG_YOUT_2: u8 = 0x07;
|
|
||||||
const REG_ZOUT_2: u8 = 0x08;
|
|
||||||
const REG_TOUT: u8 = 0x09;
|
const REG_TOUT: u8 = 0x09;
|
||||||
const REG_STATUS1: u8 = 0x18;
|
const REG_STATUS1: u8 = 0x18;
|
||||||
const REG_ODR: u8 = 0x1A;
|
const REG_ODR: u8 = 0x1A;
|
||||||
@@ -129,6 +122,18 @@ bitflags! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Control1RegisterFlags {
|
||||||
|
/// Transform bandwidth to bitflags
|
||||||
|
fn flag_for_bandwidth(bandwidth: Bandwidth) -> Self {
|
||||||
|
match bandwidth {
|
||||||
|
Bandwidth::Bw6_6ms => Self::empty(),
|
||||||
|
Bandwidth::Bw3_5ms => Self::BANDWIDTH_0,
|
||||||
|
Bandwidth::Bw2_0ms => Self::BANDWIDTH_1,
|
||||||
|
Bandwidth::Bw1_2ms => Self::BANDWIDTH_0 | Self::BANDWIDTH_1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
/// Flags for the control 2 register
|
/// Flags for the control 2 register
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
@@ -150,6 +155,7 @@ bitflags! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// All 3 Axis from a measurement
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct MagneticMessurement {
|
pub struct MagneticMessurement {
|
||||||
/// X-Axis in µT
|
/// X-Axis in µT
|
||||||
@@ -162,17 +168,50 @@ pub struct MagneticMessurement {
|
|||||||
pub z: f32,
|
pub z: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// At what rate
|
/// At what rate to output data
|
||||||
pub enum DataRate {
|
pub enum DataRate {
|
||||||
|
Unset,
|
||||||
Hz(u8),
|
Hz(u8),
|
||||||
Max1000Hz,
|
Max1000Hz,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adjust the length of the decimation filter. They control the duration of each measurement.
|
||||||
|
/// Note: X/Y/Z channel measurements are taken sequentially. Delay Time among those
|
||||||
|
/// measurements is 1/3 of the Measurement Time defined as the bandwidth.
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
enum Bandwidth {
|
||||||
|
/// 6.6ms
|
||||||
|
Bw6_6ms,
|
||||||
|
|
||||||
|
/// 3.5ms
|
||||||
|
Bw3_5ms,
|
||||||
|
|
||||||
|
/// 2ms
|
||||||
|
Bw2_0ms,
|
||||||
|
|
||||||
|
/// 1.2ms
|
||||||
|
Bw1_2ms,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Bandwidth {
|
||||||
|
fn to_flags(self) -> Control1RegisterFlags {
|
||||||
|
Control1RegisterFlags::flag_for_bandwidth(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error<E> {
|
pub enum Error<E> {
|
||||||
|
/// I2C Error
|
||||||
I2c(E),
|
I2c(E),
|
||||||
|
|
||||||
|
/// Waiting for a status flag timed out
|
||||||
Timeout,
|
Timeout,
|
||||||
|
|
||||||
|
/// This function is not available in continuous mode
|
||||||
NotAvailableInContinuousMode,
|
NotAvailableInContinuousMode,
|
||||||
|
|
||||||
|
/// You need to set a data rate before using this function
|
||||||
|
NoDataRateSet,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E> From<E> for Error<E> {
|
impl<E> From<E> for Error<E> {
|
||||||
@@ -189,7 +228,10 @@ where
|
|||||||
{
|
{
|
||||||
i2c: I,
|
i2c: I,
|
||||||
delay: D,
|
delay: D,
|
||||||
is_continuous_mode: bool,
|
ctrl0: Control0RegisterFlags,
|
||||||
|
ctrl1: Control1RegisterFlags,
|
||||||
|
ctrl2: Control2RegisterFlags,
|
||||||
|
data_rate: DataRate,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I, D> MMC56X3<I, D>
|
impl<I, D> MMC56X3<I, D>
|
||||||
@@ -201,7 +243,10 @@ where
|
|||||||
Self {
|
Self {
|
||||||
i2c,
|
i2c,
|
||||||
delay,
|
delay,
|
||||||
is_continuous_mode: false,
|
ctrl0: Control0RegisterFlags::empty(),
|
||||||
|
ctrl1: Control1RegisterFlags::empty(),
|
||||||
|
ctrl2: Control2RegisterFlags::empty(),
|
||||||
|
data_rate: DataRate::Unset,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,6 +262,10 @@ where
|
|||||||
.await?;
|
.await?;
|
||||||
self.delay.delay_ms(20).await; // According to the datasheet power on time is 20ms
|
self.delay.delay_ms(20).await; // According to the datasheet power on time is 20ms
|
||||||
|
|
||||||
|
self.ctrl0 = Control0RegisterFlags::empty();
|
||||||
|
self.ctrl1 = Control1RegisterFlags::empty();
|
||||||
|
self.ctrl2 = Control2RegisterFlags::empty();
|
||||||
|
|
||||||
self.magnet_set_reset().await?;
|
self.magnet_set_reset().await?;
|
||||||
self.set_continuous_mode(false).await?;
|
self.set_continuous_mode(false).await?;
|
||||||
|
|
||||||
@@ -225,54 +274,85 @@ where
|
|||||||
|
|
||||||
/// Pulse large currents through the sense coils to clear any offset
|
/// Pulse large currents through the sense coils to clear any offset
|
||||||
pub async fn magnet_set_reset(&mut self) -> Result<(), Error<I::Error>> {
|
pub async fn magnet_set_reset(&mut self) -> Result<(), Error<I::Error>> {
|
||||||
self.write_reg_controll_0(Control0RegisterFlags::DO_SET)
|
self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::DO_SET)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
self.delay.delay_ns(375).await; // According to the datasheet this is how long it takes.
|
self.delay.delay_ns(375).await; // According to the datasheet this is how long it takes.
|
||||||
self.write_reg_controll_0(Control0RegisterFlags::empty())
|
|
||||||
|
self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::DO_RESET)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
self.delay.delay_ns(375).await;
|
||||||
|
|
||||||
|
// No need to undo sets. Bits are self clearing.
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_continuous_mode(&mut self, enable: bool) -> Result<(), Error<I::Error>> {
|
pub async fn set_continuous_mode(&mut self, enable: bool) -> Result<(), Error<I::Error>> {
|
||||||
|
if matches!(self.data_rate, DataRate::Unset) {
|
||||||
|
return Err(Error::NoDataRateSet);
|
||||||
|
}
|
||||||
|
|
||||||
if enable {
|
if enable {
|
||||||
self.write_reg_controll_0(Control0RegisterFlags::CMM_FRE_EN)
|
self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::CMM_FRE_EN)
|
||||||
.await?;
|
.await?;
|
||||||
self.write_reg_controll_2(Control2RegisterFlags::CMM_EN)
|
|
||||||
.await?;
|
self.ctrl2.insert(Control2RegisterFlags::CMM_EN);
|
||||||
self.is_continuous_mode = true;
|
self.write_reg_controll_2(self.ctrl2).await?;
|
||||||
} else {
|
} else {
|
||||||
self.write_reg_controll_2(Control2RegisterFlags::empty())
|
self.ctrl2.remove(Control2RegisterFlags::CMM_EN);
|
||||||
.await?;
|
self.write_reg_controll_2(self.ctrl2).await?;
|
||||||
self.is_continuous_mode = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check if the CMM bit is set
|
||||||
|
pub fn is_continuous_mode(&self) -> bool {
|
||||||
|
self.ctrl2.contains(Control2RegisterFlags::CMM_EN)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error<I::Error>> {
|
pub async fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error<I::Error>> {
|
||||||
match rate {
|
match rate {
|
||||||
DataRate::Hz(hz) => {
|
DataRate::Hz(hz) => {
|
||||||
self.write_reg_odr(hz).await?;
|
self.write_reg_odr(hz).await?;
|
||||||
self.write_reg_controll_2(Control2RegisterFlags::empty())
|
self.ctrl2.remove(Control2RegisterFlags::HPOWER);
|
||||||
.await?;
|
self.write_reg_controll_2(self.ctrl2).await?;
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
DataRate::Max1000Hz => {
|
DataRate::Max1000Hz => {
|
||||||
self.write_reg_odr(255).await?;
|
self.write_reg_odr(255).await?;
|
||||||
self.write_reg_controll_2(Control2RegisterFlags::HPOWER)
|
self.ctrl2.insert(Control2RegisterFlags::HPOWER);
|
||||||
.await?;
|
self.write_reg_controll_2(self.ctrl2).await?;
|
||||||
Ok(())
|
}
|
||||||
|
DataRate::Unset => {
|
||||||
|
self.write_reg_odr(0).await?;
|
||||||
|
if !matches!(self.data_rate, DataRate::Unset) {
|
||||||
|
self.ctrl2.remove(Control2RegisterFlags::HPOWER);
|
||||||
|
self.write_reg_controll_2(self.ctrl2).await?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.data_rate = rate;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the bandwidth selection bits to adjust the length of the decimation filter. They control the duration
|
||||||
|
/// of each measurement.
|
||||||
|
#[inline]
|
||||||
|
async fn set_bandwidth(&mut self, bandwidth: Bandwidth) -> Result<(), Error<I::Error>> {
|
||||||
|
self.write_reg_controll_1(bandwidth.to_flags()).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read temperature in Celcius with steps of 0.8 C
|
/// Read temperature in Celcius with steps of 0.8 C
|
||||||
pub async fn read_temperature(&mut self) -> Result<f32, Error<I::Error>> {
|
pub async fn read_temperature(&mut self) -> Result<f32, Error<I::Error>> {
|
||||||
if self.is_continuous_mode {
|
if self.is_continuous_mode() {
|
||||||
return Err(Error::NotAvailableInContinuousMode);
|
return Err(Error::NotAvailableInContinuousMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.write_reg_controll_0(Control0RegisterFlags::TAKE_MESSUREMENT_T)
|
self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::TAKE_MESSUREMENT_T)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
self.wait_for_status_flag(StatusRegisterFlags::MESSUREMENT_T_DONE)
|
self.wait_for_status_flag(StatusRegisterFlags::MESSUREMENT_T_DONE)
|
||||||
@@ -286,9 +366,10 @@ where
|
|||||||
Ok(temperature)
|
Ok(temperature)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Read the last measurement
|
||||||
pub async fn read_messurement(&mut self) -> Result<MagneticMessurement, Error<I::Error>> {
|
pub async fn read_messurement(&mut self) -> Result<MagneticMessurement, Error<I::Error>> {
|
||||||
let mut data = [0u8; 9];
|
let mut data = [0u8; 9];
|
||||||
self.read_registers(REG_XOUT_0, &mut data).await?;
|
self.read_registers(REG_OUT_START, &mut data).await?;
|
||||||
|
|
||||||
let x = ((data[0] as u32) << 12) | ((data[1] as u32) << 4) | ((data[6] as u32) >> 4);
|
let x = ((data[0] as u32) << 12) | ((data[1] as u32) << 4) | ((data[6] as u32) >> 4);
|
||||||
let y = ((data[2] as u32) << 12) | ((data[3] as u32) << 4) | ((data[7] as u32) >> 4);
|
let y = ((data[2] as u32) << 12) | ((data[3] as u32) << 4) | ((data[7] as u32) >> 4);
|
||||||
@@ -303,26 +384,24 @@ where
|
|||||||
// Apply resolution. At 20 Bit mode.
|
// Apply resolution. At 20 Bit mode.
|
||||||
const RESOLUTION: f32 = 0.00625;
|
const RESOLUTION: f32 = 0.00625;
|
||||||
|
|
||||||
let fx: f32 = x as f32 * RESOLUTION;
|
let x: f32 = x as f32 * RESOLUTION;
|
||||||
let fy: f32 = y as f32 * RESOLUTION;
|
let y: f32 = y as f32 * RESOLUTION;
|
||||||
let fz: f32 = z as f32 * RESOLUTION;
|
let z: f32 = z as f32 * RESOLUTION;
|
||||||
|
|
||||||
Ok(MagneticMessurement {
|
Ok(MagneticMessurement { x, y, z })
|
||||||
x: fx,
|
|
||||||
y: fy,
|
|
||||||
z: fz,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Trigger a new measurement if not in continuous mode.
|
||||||
|
/// Waits for the measurement to complete.
|
||||||
pub async fn trigger_messurement(&mut self) -> Result<(), Error<I::Error>> {
|
pub async fn trigger_messurement(&mut self) -> Result<(), Error<I::Error>> {
|
||||||
self.write_reg_controll_0(Control0RegisterFlags::TAKE_MESSUREMENT_M)
|
self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::TAKE_MESSUREMENT_M)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
self.wait_for_status_flag(StatusRegisterFlags::MESSUREMENT_M_DONE)
|
self.wait_for_status_flag(StatusRegisterFlags::MESSUREMENT_M_DONE)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
pub async fn read_product_id(&mut self) -> Result<u8, Error<I::Error>> {
|
pub async fn read_product_id(&mut self) -> Result<u8, Error<I::Error>> {
|
||||||
self.read_register(REG_PRODUCT_ID).await
|
self.read_register(REG_PRODUCT_ID).await
|
||||||
}
|
}
|
||||||
@@ -342,24 +421,24 @@ where
|
|||||||
Err(Error::Timeout)
|
Err(Error::Timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
async fn read_reg_temperature(&mut self) -> Result<u8, Error<I::Error>> {
|
async fn read_reg_temperature(&mut self) -> Result<u8, Error<I::Error>> {
|
||||||
self.read_register(REG_TOUT).await
|
self.read_register(REG_TOUT).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
async fn read_reg_status(&mut self) -> Result<StatusRegisterFlags, Error<I::Error>> {
|
async fn read_reg_status(&mut self) -> Result<StatusRegisterFlags, Error<I::Error>> {
|
||||||
Ok(StatusRegisterFlags::from_bits_truncate(
|
Ok(StatusRegisterFlags::from_bits_truncate(
|
||||||
self.read_register(REG_STATUS1).await?,
|
self.read_register(REG_STATUS1).await?,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
async fn write_reg_odr(&mut self, data: u8) -> Result<(), Error<I::Error>> {
|
async fn write_reg_odr(&mut self, data: u8) -> Result<(), Error<I::Error>> {
|
||||||
self.write_register(REG_ODR, data).await
|
self.write_register(REG_ODR, data).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
async fn write_reg_controll_0(
|
async fn write_reg_controll_0(
|
||||||
&mut self,
|
&mut self,
|
||||||
value: Control0RegisterFlags,
|
value: Control0RegisterFlags,
|
||||||
@@ -367,7 +446,7 @@ where
|
|||||||
self.write_register(REG_CONTROL0, value.bits()).await
|
self.write_register(REG_CONTROL0, value.bits()).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
async fn write_reg_controll_1(
|
async fn write_reg_controll_1(
|
||||||
&mut self,
|
&mut self,
|
||||||
value: Control1RegisterFlags,
|
value: Control1RegisterFlags,
|
||||||
@@ -375,7 +454,7 @@ where
|
|||||||
self.write_register(REG_CONTROL1, value.bits()).await
|
self.write_register(REG_CONTROL1, value.bits()).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
async fn write_reg_controll_2(
|
async fn write_reg_controll_2(
|
||||||
&mut self,
|
&mut self,
|
||||||
value: Control2RegisterFlags,
|
value: Control2RegisterFlags,
|
||||||
@@ -383,7 +462,7 @@ where
|
|||||||
self.write_register(REG_CONTROL2, value.bits()).await
|
self.write_register(REG_CONTROL2, value.bits()).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
async fn write_register(&mut self, reg: u8, value: u8) -> Result<(), Error<I::Error>> {
|
async fn write_register(&mut self, reg: u8, value: u8) -> Result<(), Error<I::Error>> {
|
||||||
self.i2c.write(DEFAULT_ADDRESS, &[reg, value]).await?;
|
self.i2c.write(DEFAULT_ADDRESS, &[reg, value]).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -397,7 +476,7 @@ where
|
|||||||
Ok(data[0])
|
Ok(data[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline]
|
||||||
async fn read_registers(&mut self, reg: u8, buffer: &mut [u8]) -> Result<(), Error<I::Error>> {
|
async fn read_registers(&mut self, reg: u8, buffer: &mut [u8]) -> Result<(), Error<I::Error>> {
|
||||||
self.i2c.write_read(DEFAULT_ADDRESS, &[reg], buffer).await?;
|
self.i2c.write_read(DEFAULT_ADDRESS, &[reg], buffer).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user