Compare commits

..

5 Commits

10 changed files with 45 additions and 11 deletions

View File

@@ -2,12 +2,19 @@
name = "mmc56x3" name = "mmc56x3"
version = "0.1.0" version = "0.1.0"
edition = "2024" 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] [dependencies]
embedded-hal-async = { version = "1.0.0" } embedded-hal-async = { version = "1.0.0" }
maybe-async = { version = "0.2.10" } maybe-async = { version = "0.2.10" }
embedded-hal = { version = "1.0.0", optional = true } embedded-hal = { version = "1.0.0", optional = true }
bitflags = { version = "2.10.0", default-features = false } bitflags = { version = "2.10.0" }
[features] [features]

View File

@@ -5,3 +5,4 @@ Based on the [arduino driver](https://github.com/adafruit/Adafruit_MMC56x3).
# Usage # 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). 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.

View File

@@ -39,20 +39,20 @@ async fn main(spawner: Spawner) {
let sda = p.PIN_4; let sda = p.PIN_4;
let scl = p.PIN_5; let scl = p.PIN_5;
let i2c = I2c::new_async(p.I2C0, scl, sda, Irqs, i2c::Config::default()); let i2c = I2c::new_blocking(p.I2C0, scl, sda, i2c::Config::default());
let mut device = MMC56X3::new(i2c, Delay); let mut device = MMC56X3::new(i2c, Delay);
if let Err(e) = device.init().await { if let Err(e) = device.init() {
error!("Failed to init {:?}", e); error!("Failed to init {:?}", e);
reboot().await; reboot().await;
}; };
if let Err(e) = device.set_data_rate(mmc56x3::DataRate::Max1000Hz).await { if let Err(e) = device.set_data_rate(mmc56x3::DataRate::Max1000Hz) {
error!("Failed to set data rate {:?}", e); error!("Failed to set data rate {:?}", e);
reboot().await; reboot().await;
} }
if let Err(e) = device.set_continuous_mode(true).await { if let Err(e) = device.set_continuous_mode(true) {
error!("Failed to set_continuous_mode {:?}", e); error!("Failed to set_continuous_mode {:?}", e);
reboot().await; reboot().await;
} }
@@ -67,7 +67,7 @@ async fn main(spawner: Spawner) {
// .await // .await
// .expect("Failed to trigger trigger_messurement"); // .expect("Failed to trigger trigger_messurement");
match device.read_messurement().await { match device.read_messurement() {
Ok(d) => info!("Got: {:?}", d), Ok(d) => info!("Got: {:?}", d),
Err(e) => error!("Error: {:?}", e), Err(e) => error!("Error: {:?}", e),
} }

View File

@@ -1,13 +1,19 @@
#![no_std] #![no_std]
#[cfg(feature = "sync")] #[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"))] #[cfg(not(feature = "sync"))]
use embedded_hal_async::i2c::{self}; use embedded_hal_async::{
delay::DelayNs,
i2c::{self},
};
use bitflags::bitflags; use bitflags::bitflags;
use maybe_async::maybe_async;
const REG_OUT_START: u8 = 0x00; const REG_OUT_START: u8 = 0x00;
@@ -21,8 +27,6 @@ const REG_PRODUCT_ID: u8 = 0x39;
const DEFAULT_ADDRESS: u8 = 0x30; const DEFAULT_ADDRESS: u8 = 0x30;
const DEVICE_ID: u8 = 0x10;
bitflags! { bitflags! {
/// Flags for status_1 register /// Flags for status_1 register
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -241,6 +245,7 @@ where
data_rate: DataRate, data_rate: DataRate,
} }
#[maybe_async]
impl<I, D> MMC56X3<I, D> impl<I, D> MMC56X3<I, D>
where where
I: i2c::I2c, I: i2c::I2c,
@@ -257,6 +262,7 @@ where
} }
} }
#[maybe_async]
pub async fn init(&mut self) -> Result<(), Error<I::Error>> { pub async fn init(&mut self) -> Result<(), Error<I::Error>> {
self.reset().await?; self.reset().await?;
@@ -264,6 +270,7 @@ where
} }
/// Resets the sensor to an initial state /// Resets the sensor to an initial state
#[maybe_async]
pub async fn reset(&mut self) -> Result<(), Error<I::Error>> { pub async fn reset(&mut self) -> Result<(), Error<I::Error>> {
self.write_reg_controll_1(Control1RegisterFlags::SW_RESET) self.write_reg_controll_1(Control1RegisterFlags::SW_RESET)
.await?; .await?;
@@ -284,6 +291,7 @@ where
} }
/// Pulse large currents through the sense coils to clear any offset /// 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>> { pub async fn magnet_set_reset(&mut self) -> Result<(), Error<I::Error>> {
self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::DO_SET) self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::DO_SET)
.await?; .await?;
@@ -300,6 +308,7 @@ where
Ok(()) Ok(())
} }
#[maybe_async]
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 enable { if enable {
if matches!(self.data_rate, DataRate::Unset) { if matches!(self.data_rate, DataRate::Unset) {
@@ -323,6 +332,7 @@ where
self.ctrl2.contains(Control2RegisterFlags::CMM_EN) self.ctrl2.contains(Control2RegisterFlags::CMM_EN)
} }
#[maybe_async]
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) => {
@@ -352,11 +362,13 @@ where
/// Set the bandwidth selection bits to adjust the length of the decimation filter. They control the duration /// 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. /// of each measurement. They also impacts the maximum data rate.
#[inline] #[inline]
#[maybe_async]
pub async fn set_bandwidth(&mut self, bandwidth: Bandwidth) -> Result<(), Error<I::Error>> { pub async fn set_bandwidth(&mut self, bandwidth: Bandwidth) -> Result<(), Error<I::Error>> {
self.write_reg_controll_1(bandwidth.to_flags()).await self.write_reg_controll_1(bandwidth.to_flags()).await
} }
/// Enable or disable the automatic set/reset. This also impacts the maximum data rate. /// 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>> { pub async fn set_auto_set_reset(&mut self, enable: bool) -> Result<(), Error<I::Error>> {
self.ctrl0 self.ctrl0
.set(Control0RegisterFlags::AUTO_SET_RESET_EN, enable); .set(Control0RegisterFlags::AUTO_SET_RESET_EN, enable);
@@ -365,6 +377,7 @@ where
} }
/// Read temperature in Celcius with steps of 0.8 C /// Read temperature in Celcius with steps of 0.8 C
#[maybe_async]
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);
@@ -385,6 +398,7 @@ where
} }
/// Read the last measurement /// Read the last measurement
#[maybe_async]
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_OUT_START, &mut data).await?; self.read_registers(REG_OUT_START, &mut data).await?;
@@ -411,6 +425,7 @@ where
/// Trigger a new measurement if not in continuous mode. /// Trigger a new measurement if not in continuous mode.
/// Waits for the measurement to complete. /// Waits for the measurement to complete.
#[maybe_async]
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(self.ctrl0 | Control0RegisterFlags::TAKE_MESSUREMENT_M) self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::TAKE_MESSUREMENT_M)
.await?; .await?;
@@ -420,10 +435,12 @@ where
} }
#[inline] #[inline]
#[maybe_async]
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
} }
#[maybe_async]
async fn wait_for_status_flag( async fn wait_for_status_flag(
&mut self, &mut self,
flag: StatusRegisterFlags, flag: StatusRegisterFlags,
@@ -440,11 +457,13 @@ where
} }
#[inline] #[inline]
#[maybe_async]
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] #[inline]
#[maybe_async]
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?,
@@ -452,11 +471,13 @@ where
} }
#[inline] #[inline]
#[maybe_async]
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] #[inline]
#[maybe_async]
async fn write_reg_controll_0( async fn write_reg_controll_0(
&mut self, &mut self,
value: Control0RegisterFlags, value: Control0RegisterFlags,
@@ -465,6 +486,7 @@ where
} }
#[inline] #[inline]
#[maybe_async]
async fn write_reg_controll_1( async fn write_reg_controll_1(
&mut self, &mut self,
value: Control1RegisterFlags, value: Control1RegisterFlags,
@@ -473,6 +495,7 @@ where
} }
#[inline] #[inline]
#[maybe_async]
async fn write_reg_controll_2( async fn write_reg_controll_2(
&mut self, &mut self,
value: Control2RegisterFlags, value: Control2RegisterFlags,
@@ -481,11 +504,13 @@ where
} }
#[inline] #[inline]
#[maybe_async]
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(())
} }
#[maybe_async]
async fn read_register(&mut self, reg: u8) -> Result<u8, Error<I::Error>> { async fn read_register(&mut self, reg: u8) -> Result<u8, Error<I::Error>> {
let mut data = [0u8; 1]; let mut data = [0u8; 1];
self.i2c self.i2c
@@ -495,6 +520,7 @@ where
} }
#[inline] #[inline]
#[maybe_async]
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(())