stm32f3-discovery icon indicating copy to clipboard operation
stm32f3-discovery copied to clipboard

Not receiving any magnetometer data

Open christopherhex opened this issue 3 years ago • 7 comments

Hi!

I'm learning embedded rust and currently trying to control the compass leds of the STM32F3 discovery board using the magnetometer data. I was able to setup everything and I get logs from the accelerometer, but not from the magnetometer.

I used the example code from examples/compass.rs and I'm running rust 1.53.0. Any ideas on what I might be overlooking?

Accel:I16x3 { x: -128, y: -896, z: 16512 }; Mag:I16x3 { x: 0, y: 0, z: 0 }
Accel:I16x3 { x: -320, y: -704, z: 16512 }; Mag:I16x3 { x: 0, y: 0, z: 0 }
Accel:I16x3 { x: -192, y: -576, z: 16448 }; Mag:I16x3 { x: 0, y: 0, z: 0 }
Accel:I16x3 { x: -128, y: -768, z: 16512 }; Mag:I16x3 { x: 0, y: 0, z: 0 }
Accel:I16x3 { x: -256, y: -704, z: 16384 }; Mag:I16x3 { x: 0, y: 0, z: 0 }
Accel:I16x3 { x: -320, y: -768, z: 16512 }; Mag:I16x3 { x: 0, y: 0, z: 0 }

Thanks!

christopherhex avatar Jul 04 '21 13:07 christopherhex

Further investigation seems to reveal that there are different compass modules being used for the discovery board. The latest PCB revision (rev. E) seems to include a LSM303AGR compass instead of LSM303DLHC. Probably there is some incombatibility in the I2C registers?

christopherhex avatar Jul 04 '21 14:07 christopherhex

Probably. I’d have to dig into the datasheets to be sure.

https://www.st.com/resource/en/datasheet/DM00027543.pdf https://www.st.com/resource/en/datasheet/lsm303agr.pdf

If we had a driver for the new IMU, we could create a feature gate to switch which driver gets exposed. I don’t have the hardware to do it myself, but would happily accept a PR adding such a feature gate. I can dig into the datasheets later to see if I can provide any guidance.

rubberduck203 avatar Jul 04 '21 15:07 rubberduck203

I verified that the default I2C bus addresses are the same for both devices, which is good. Rules out one possible difference.

rubberduck203 avatar Jul 05 '21 14:07 rubberduck203

I haven’t looked into it yet, but it seems there’s an existing crate for the new device.

https://crates.io/crates/lsm303agr

rubberduck203 avatar Jul 05 '21 15:07 rubberduck203

The accelerator registers are nearly identical, ignoring the changes to the temperature sensor and a few niceties. The magnometer registers are completely different.

On the LSM303DLHC, the registers are in the address range 00-0C while on the LSM303AGR they are in the range 45-6D.

This explains why the accelerometer works, but the magnometer does not. I attached a quick and dirty spreadsheet showing the difference.

LSM303RegisterComparrison.xlsx

rubberduck203 avatar Jul 05 '21 15:07 rubberduck203

Hey @rubberduck203, thanks for the quick response and guidance! 🙏

I'll try to use the other crate and see how it might be integrated in your code later this week and provide a PR for it.

christopherhex avatar Jul 05 '21 20:07 christopherhex

The quickest path is probably adding a feature gate that changes the Compass implementation at compile time.

https://doc.rust-lang.org/cargo/reference/features.html

I’m thinking something like

[dependencies]
lsm303ldhc = { version = "0.2.0”, optional = true }
lsm303agr = { version = "0.x.x”, optional = true }

[features]
default = ["revD"]
revD = [“lsm303dlhc”]
revE = [“lsm303agr”]

Then we can have two different compasses behind a #[cfg(feature = "driverName")] annotation.

rubberduck203 avatar Jul 05 '21 21:07 rubberduck203