implmented maybe_async

This commit is contained in:
2026-02-10 12:54:28 +01:00
parent c9b498215f
commit 215999ca8a

View File

@@ -1,13 +1,19 @@
#![no_std]
#[cfg(feature = "sync")]
use embedded_hal::i2c::{self};
use embedded_hal::{
delay::DelayNs,
i2c::{self},
};
use embedded_hal_async::delay::DelayNs;
#[cfg(not(feature = "sync"))]
use embedded_hal_async::i2c::{self};
use embedded_hal_async::{
delay::DelayNs,
i2c::{self},
};
use bitflags::bitflags;
use maybe_async::maybe_async;
const REG_OUT_START: u8 = 0x00;
@@ -241,6 +247,7 @@ where
data_rate: DataRate,
}
#[maybe_async]
impl<I, D> MMC56X3<I, D>
where
I: i2c::I2c,
@@ -257,6 +264,7 @@ where
}
}
#[maybe_async]
pub async fn init(&mut self) -> Result<(), Error<I::Error>> {
self.reset().await?;
@@ -264,6 +272,7 @@ where
}
/// Resets the sensor to an initial state
#[maybe_async]
pub async fn reset(&mut self) -> Result<(), Error<I::Error>> {
self.write_reg_controll_1(Control1RegisterFlags::SW_RESET)
.await?;
@@ -284,6 +293,7 @@ where
}
/// Pulse large currents through the sense coils to clear any offset
#[maybe_async]
pub async fn magnet_set_reset(&mut self) -> Result<(), Error<I::Error>> {
self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::DO_SET)
.await?;
@@ -300,6 +310,7 @@ where
Ok(())
}
#[maybe_async]
pub async fn set_continuous_mode(&mut self, enable: bool) -> Result<(), Error<I::Error>> {
if enable {
if matches!(self.data_rate, DataRate::Unset) {
@@ -323,6 +334,7 @@ where
self.ctrl2.contains(Control2RegisterFlags::CMM_EN)
}
#[maybe_async]
pub async fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error<I::Error>> {
match rate {
DataRate::Hz(hz) => {
@@ -352,11 +364,13 @@ where
/// Set the bandwidth selection bits to adjust the length of the decimation filter. They control the duration
/// of each measurement. They also impacts the maximum data rate.
#[inline]
#[maybe_async]
pub async fn set_bandwidth(&mut self, bandwidth: Bandwidth) -> Result<(), Error<I::Error>> {
self.write_reg_controll_1(bandwidth.to_flags()).await
}
/// Enable or disable the automatic set/reset. This also impacts the maximum data rate.
#[maybe_async]
pub async fn set_auto_set_reset(&mut self, enable: bool) -> Result<(), Error<I::Error>> {
self.ctrl0
.set(Control0RegisterFlags::AUTO_SET_RESET_EN, enable);
@@ -365,6 +379,7 @@ where
}
/// Read temperature in Celcius with steps of 0.8 C
#[maybe_async]
pub async fn read_temperature(&mut self) -> Result<f32, Error<I::Error>> {
if self.is_continuous_mode() {
return Err(Error::NotAvailableInContinuousMode);
@@ -385,6 +400,7 @@ where
}
/// Read the last measurement
#[maybe_async]
pub async fn read_messurement(&mut self) -> Result<MagneticMessurement, Error<I::Error>> {
let mut data = [0u8; 9];
self.read_registers(REG_OUT_START, &mut data).await?;
@@ -411,6 +427,7 @@ where
/// Trigger a new measurement if not in continuous mode.
/// Waits for the measurement to complete.
#[maybe_async]
pub async fn trigger_messurement(&mut self) -> Result<(), Error<I::Error>> {
self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::TAKE_MESSUREMENT_M)
.await?;
@@ -420,10 +437,12 @@ where
}
#[inline]
#[maybe_async]
pub async fn read_product_id(&mut self) -> Result<u8, Error<I::Error>> {
self.read_register(REG_PRODUCT_ID).await
}
#[maybe_async]
async fn wait_for_status_flag(
&mut self,
flag: StatusRegisterFlags,
@@ -440,11 +459,13 @@ where
}
#[inline]
#[maybe_async]
async fn read_reg_temperature(&mut self) -> Result<u8, Error<I::Error>> {
self.read_register(REG_TOUT).await
}
#[inline]
#[maybe_async]
async fn read_reg_status(&mut self) -> Result<StatusRegisterFlags, Error<I::Error>> {
Ok(StatusRegisterFlags::from_bits_truncate(
self.read_register(REG_STATUS1).await?,
@@ -452,11 +473,13 @@ where
}
#[inline]
#[maybe_async]
async fn write_reg_odr(&mut self, data: u8) -> Result<(), Error<I::Error>> {
self.write_register(REG_ODR, data).await
}
#[inline]
#[maybe_async]
async fn write_reg_controll_0(
&mut self,
value: Control0RegisterFlags,
@@ -465,6 +488,7 @@ where
}
#[inline]
#[maybe_async]
async fn write_reg_controll_1(
&mut self,
value: Control1RegisterFlags,
@@ -473,6 +497,7 @@ where
}
#[inline]
#[maybe_async]
async fn write_reg_controll_2(
&mut self,
value: Control2RegisterFlags,
@@ -481,11 +506,13 @@ where
}
#[inline]
#[maybe_async]
async fn write_register(&mut self, reg: u8, value: u8) -> Result<(), Error<I::Error>> {
self.i2c.write(DEFAULT_ADDRESS, &[reg, value]).await?;
Ok(())
}
#[maybe_async]
async fn read_register(&mut self, reg: u8) -> Result<u8, Error<I::Error>> {
let mut data = [0u8; 1];
self.i2c
@@ -495,6 +522,7 @@ where
}
#[inline]
#[maybe_async]
async fn read_registers(&mut self, reg: u8, buffer: &mut [u8]) -> Result<(), Error<I::Error>> {
self.i2c.write_read(DEFAULT_ADDRESS, &[reg], buffer).await?;
Ok(())