1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-24 14:03:55 +01:00

drivers/lsm303agr: Use version 0.3 of driver

Note that for the very CPU this driver is used with (nRF52 on the
microbit-v2 board), this currently needs extra workarounds to copy
written data from flash to RAM so that the driver can see it. (Otherwise
it silently writes 00, and then correctly reads 00 from the bus all the
time).
This commit is contained in:
chrysn 2024-01-25 13:56:17 +01:00
parent 790e808deb
commit 96af920a12
3 changed files with 10 additions and 7 deletions

View File

@ -11,7 +11,7 @@ license = "LGPL-2.1-only"
publish = false
[dependencies]
lsm303agr = "^0.2"
lsm303agr = "0.3.0"
riot-wrappers = "^0.8"
# Whatever lsm uses
nb = "*"

View File

@ -1,3 +1,4 @@
USEMODULE += rust_riotmodules
USEMODULE += ztimer_usec
FEATURES_REQUIRED += periph_i2c

View File

@ -1,6 +1,6 @@
#![no_std]
use lsm303agr::{interface, mode, Lsm303agr, AccelOutputDataRate::Hz50};
use lsm303agr::{interface, mode, Lsm303agr, AccelOutputDataRate::Hz50, AccelMode};
use riot_wrappers::{saul, println, i2c, cstr::cstr, mutex::Mutex};
use saul::{Phydat, registration};
@ -59,9 +59,11 @@ fn init() -> Result<(), &'static str> {
for (&i2cdev, (lsm, (reg, reg_mag))) in I2C_DEVICES.iter().zip(lsm.iter_mut().zip(reg.iter_mut().zip(reg_mag.iter_mut()))) {
let mut device = Lsm303agr::new_with_i2c(i2c::I2CDevice::new(i2cdev));
let mut init_clock = riot_wrappers::ztimer::Clock::usec();
device.init()
.map_err(|_| "Device initialization failed")?;
device.set_accel_odr(Hz50)
device.set_accel_mode_and_odr(&mut init_clock, AccelMode::Normal, Hz50)
.map_err(|_| "Device configuration failed")?;
let lsm = lsm.insert(SaulLSM { device: Mutex::new(device) });
@ -90,10 +92,10 @@ impl registration::Drivable for &SaulLSM {
let mut device = self.device.try_lock()
.ok_or(registration::Error)?;
let data = device.accel_data()
let data = device.acceleration()
.map_err(|_| registration::Error)?;
// Data is in the +-2g range by default, which doesn't overflow even the i16 SAUL uses
Ok(Phydat::new(&[data.x as _, data.y as _, data.z as _], Some(saul::Unit::GForce), -3))
Ok(Phydat::new(&[data.x_mg() as _, data.y_mg() as _, data.z_mg() as _], Some(saul::Unit::GForce), -3))
}
}
@ -115,9 +117,9 @@ impl registration::Drivable for MagAspect {
let mut device = self.0.device.try_lock()
.ok_or(registration::Error)?;
let data = nb::block!(device.mag_data())
let data = nb::block!(device.magnetic_field())
.map_err(|_| registration::Error)?;
// Original data is in nanotesla
return Ok(Phydat::fit(&[data.x, data.y, data.z], Some(saul::Unit::T), -9))
return Ok(Phydat::fit(&[data.x_nt(), data.y_nt(), data.z_nt()], Some(saul::Unit::T), -9))
}
}