mpu9250/
ak8963.rs

1//! AK8963, I2C magnetometer
2
3use hal::blocking::delay::DelayMs;
4
5// I2C slave address
6pub const I2C_ADDRESS: u8 = 0x0c;
7pub const R: u8 = 1 << 7;
8pub const W: u8 = 0 << 7;
9
10#[allow(dead_code)]
11#[allow(non_camel_case_types)]
12#[derive(Clone, Copy)]
13pub enum Register {
14    WHO_AM_I = 0x00, // should return 0x48
15    INFO = 0x01,
16    ST1 = 0x02,    // data ready status bit 0
17    XOUT_L = 0x03, // data
18    XOUT_H = 0x04,
19    YOUT_L = 0x05,
20    YOUT_H = 0x06,
21    ZOUT_L = 0x07,
22    ZOUT_H = 0x08,
23    ST2 = 0x09, // Data overflow bit 3 and data read error status bit 2
24    CNTL1 = 0x0A, /* Power down (0000), single-measurement (0001), self-test
25                 * (1000) and Fuse ROM (1111) modes on bits 3:0 */
26    CNTL2 = 0x0B,  // Control 2:
27    ASTC = 0x0C,   // Self test control
28    I2CDIS = 0x0F, // I2C disable
29    ASAX = 0x10,   // Fuse ROM x-axis sensitivity adjustment value
30    ASAY = 0x11,   // Fuse ROM y-axis sensitivity adjustment value
31    ASAZ = 0x12,   // Fuse ROM z-axis sensitivity adjustment value
32}
33
34impl Register {
35    pub fn addr(&self) -> u8 {
36        *self as u8
37    }
38}
39
40/// Decribes a type that can communicate with the
41/// MPU's on-board magnetometer, the AK8963
42pub trait AK8963 {
43    /// Associated error type
44    type Error;
45
46    /// Initialize the AK8963
47    ///
48    /// It may not make sense to call this more than once. However, it is
49    /// absolutely necessary to call it at least once if you need the
50    /// magnetometer
51    fn init<D: DelayMs<u8>>(&mut self,
52                            delay: &mut D)
53                            -> Result<(), Self::Error>;
54
55    /// Perform final initialization. Invoked after acquiring the magnetomter's
56    /// calibration values and setting the sampling rate and resolution.
57    fn finalize<D: DelayMs<u8>>(&mut self,
58                                _: &mut D)
59                                -> Result<(), Self::Error> {
60        Ok(())
61    }
62
63    /// Read a magnetometer's register
64    fn read(&mut self, reg: Register) -> Result<u8, Self::Error>;
65
66    /// Write to a magnetometer's register
67    fn write(&mut self, reg: Register, value: u8) -> Result<(), Self::Error>;
68
69    /// Read the magnetometer's X,Y,Z triplet
70    fn read_xyz(&mut self, buffer: &mut [u8; 7]) -> Result<(), Self::Error>;
71}