mpu9250_i2c/
mag.rs

1/*****************************************************************************
2 *                                                                           *
3 *  Copyright 2018 Simon M. Werner                                           *
4 *                                                                           *
5 *  Licensed under the Apache License, Version 2.0 (the "License");          *
6 *  you may not use this file except in compliance with the License.         *
7 *  You may obtain a copy of the License at                                  *
8 *                                                                           *
9 *      http://www.apache.org/licenses/LICENSE-2.0                           *
10 *                                                                           *
11 *  Unless required by applicable law or agreed to in writing, software      *
12 *  distributed under the License is distributed on an "AS IS" BASIS,        *
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 *  See the License for the specific language governing permissions and      *
15 *  limitations under the License.                                           *
16 *                                                                           *
17 *****************************************************************************/
18
19/// The magnetometer address on the i2c bus.  This may not be initially visible.
20pub const ADDRESS: u8 = 0x0c;
21
22#[allow(dead_code)]
23#[allow(non_camel_case_types)]
24#[allow(missing_docs)]
25#[derive(Copy, Clone)]
26pub enum Register {
27  WHO_AM_I = 0x00, // should return 0x48
28  WHO_AM_I_RESPONSE = 0x48,
29  INFO = 0x01,
30  ST1 = 0x02,    // data ready status bit 0
31  XOUT_L = 0x03, // data
32  XOUT_H = 0x04,
33  YOUT_L = 0x05,
34  YOUT_H = 0x06,
35  ZOUT_L = 0x07,
36  ZOUT_H = 0x08,
37  ST2 = 0x09,    // Data overflow bit 3 and data read error status bit 2
38  CNTL = 0x0a, // Power down (0000), single-measurement (0001), self-test (1000) and Fuse ROM (1111) modes on bits 3:0
39  ASTC = 0x0c, // Self test control
40  I2CDIS = 0x0f, // I2C disable
41  ASA_X = 0x10, // Fuse ROM x-axis sensitivity adjustment value
42  ASA_Y = 0x11, // Fuse ROM y-axis sensitivity adjustment value
43  ASA_Z = 0x12,
44}
45
46impl Register {
47  /// Convert to a usable value
48  pub fn addr(&self) -> u8 {
49    *self as u8
50  }
51}
52
53#[allow(dead_code)]
54#[allow(non_camel_case_types)]
55#[allow(missing_docs)]
56#[derive(Copy, Clone)]
57pub enum Ctnl1 {
58  MODE_POWER_DOWN = 0b0000_0000,         // Power-down mode
59  MODE_SINGLE_MEASURE = 0b0000_0001,     // Single measurement mode
60  MODE_CONTINUE_MEASURE_1 = 0b0000_0010, // Continuous measurement mode 1
61  MODE_CONTINUE_MEASURE_2 = 0b0000_0110, // Continuous measurement mode 2
62  MODE_EXT_TRIG_MEASURE = 0b0000_0100,   // External trigger measurement mode
63  MODE_SELF_TEST_MODE = 0b0000_1000,     // Self-test mode
64  MODE_FUSE_ROM_ACCESS = 0b0000_1111,    // Fuse ROM access mode
65  BIT_16 = 0b0001_0000,                  // 16 bit output, otherwise 14 bit output
66  NONE = 0xff,
67}
68
69impl From<u8> for Ctnl1 {
70  fn from(n: u8) -> Ctnl1 {
71    match n {
72      0b0000_0000 => Ctnl1::MODE_POWER_DOWN,
73      0b0000_0001 => Ctnl1::MODE_SINGLE_MEASURE,
74      0b0000_0010 => Ctnl1::MODE_CONTINUE_MEASURE_1,
75      0b0000_0110 => Ctnl1::MODE_CONTINUE_MEASURE_2,
76      0b0000_0100 => Ctnl1::MODE_EXT_TRIG_MEASURE,
77      0b0000_1000 => Ctnl1::MODE_SELF_TEST_MODE,
78      0b0000_1111 => Ctnl1::MODE_FUSE_ROM_ACCESS,
79      0b0001_0000 => Ctnl1::BIT_16,
80      _ => Ctnl1::NONE,
81    }
82  }
83}
84
85impl Ctnl1 {
86  /// Convert to a usable value
87  pub fn addr(&self) -> u8 {
88    *self as u8
89  }
90}