Compare commits
3 Commits
00d196c8f1
...
9ab2e180d0
| Author | SHA1 | Date | |
|---|---|---|---|
|
9ab2e180d0
|
|||
|
215999ca8a
|
|||
|
c9b498215f
|
@@ -2,12 +2,19 @@
|
||||
name = "mmc56x3"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license = "Apache-2.0"
|
||||
description = "Driver for the MMC56x3"
|
||||
readme = "README.md"
|
||||
repository = "https://git.kapelle.org/niklas/mmc56x3-rust"
|
||||
homepage = "https://git.kapelle.org/niklas/mmc56x3-rust"
|
||||
keywords = ["no_std", "embedded"]
|
||||
|
||||
|
||||
[dependencies]
|
||||
embedded-hal-async = { version = "1.0.0" }
|
||||
maybe-async = { version = "0.2.10" }
|
||||
embedded-hal = { version = "1.0.0", optional = true }
|
||||
bitflags = { version = "2.10.0", default-features = false }
|
||||
bitflags = { version = "2.10.0" }
|
||||
|
||||
|
||||
[features]
|
||||
|
||||
@@ -5,3 +5,4 @@ Based on the [arduino driver](https://github.com/adafruit/Adafruit_MMC56x3).
|
||||
# Usage
|
||||
|
||||
See [examples](./example). Also make sure to check the [datasheet](https://cdn-learn.adafruit.com/assets/assets/000/113/957/original/MMC5603NJ_RevB_7-12-18.pdf).
|
||||
If you want to use the synchronous version then enable the sync feature flag.
|
||||
|
||||
34
src/lib.rs
34
src/lib.rs
@@ -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(())
|
||||
|
||||
Reference in New Issue
Block a user