Statistics
| Branch: | Tag: | Revision:

amiro-os / include / amiro / magneto / hmc5883l.hpp @ 58fe0e0b

History | View | Annotate | Download (3.324 KB)

1 58fe0e0b Thomas Schöpping
#ifndef HMC5883L_H_
2
#define HMC5883L_H_
3
4
#include <amiro/bus/i2c/I2CParams.hpp>
5
6
namespace amiro {
7
8
class I2CDriver;
9
10
/**
11
 * HMC5883L Magnetometer
12
 * \todo Interrupt Support, self-test
13
 */
14
class HMC5883L : public chibios_rt::BaseStaticThread<256> {
15
16
 public:
17
18
  struct registers {
19
    uint8_t ctrlA;
20
    uint8_t ctrlB;
21
    uint8_t mode;
22
    uint8_t xMsb;
23
    uint8_t xLsb;
24
    uint8_t zMsb;
25
    uint8_t zLsb;
26
    uint8_t yMsb;
27
    uint8_t yLsb;
28
    uint8_t status;
29
    uint8_t idA;
30
    uint8_t idB;
31
    uint8_t idC;
32
  }__attribute__((packed));
33
34
  struct HMC5883LConfig {
35
    uint8_t ctrlA;
36
    uint8_t ctrlB;
37
    uint8_t mode;
38
  };
39
40
  enum {
41
    SLA = 0x1E
42
  };
43
44
  enum {
45
    MA_AVG1 = 0x00,
46
    MA_AVG2 = 0x20,
47
    MA_AVG4 = 0x40,
48
    MA_AVG8 = 0x60,
49
  };
50
51
  enum {
52
    DO_0_5_HZ = 0x00,
53
    DO_1_HZ = 0x04,
54
    DO_2_HZ = 0x08,
55
    DO_5_HZ = 0x0C,
56
    DO_10_HZ = 0x10,
57
    DO_20_HZ = 0x14,
58
    DO_50_HZ = 0x18,
59
  };
60
61
  enum {
62
    MS_NORMAL = 0x00,
63
    MS_BIASPOS = 0x01,
64
    MS_BIASNEG = 0x02,
65
  };
66
67
  enum {
68
    GN_0_GA = 0x00,
69
    GN_1_GA = 0x20,
70
    GN_2_GA = 0x40,
71
    GN_3_GA = 0x60,
72
    GN_4_GA = 0x80,
73
    GN_5_GA = 0xA0,
74
    GN_6_GA = 0xC0,
75
    GN_7_GA = 0xE0,
76
  };
77
78
  enum {
79
    MD_CONTCV = 0x00,
80
    MD_SINGCV = 0x01,
81
    MD_IDLE = 0x02,
82
    MD_SLEEP = 0x03,
83
  };
84
85
  enum {
86
    HS_DISABLE = 0x00,
87
    HS_ENABLE = 0x80,
88
  };
89
90
  enum {
91
    SR_REN = 0x04,
92
    SR_LOCK = 0x02,
93
    SR_RDY = 0x01,
94
  };
95
96
  enum {
97
    ID_IRA = 'H',
98
    ID_IRB = '4',
99
    ID_IRC = '3',
100
  };
101
102
  enum {
103
    MAGNETO_AVG = 10
104
  };
105
106
  /**
107
   * Return types of getCheck()
108
   */
109
  enum {
110
    CHECK_OK = 0x00u,
111
    CHECK_FAIL = 0x01u,
112
  };
113
114
 public:
115
  HMC5883L(I2CDriver *driver, const HMC5883LConfig *config);
116
  virtual ~HMC5883L();
117
118
  msg_t configure(HMC5883LConfig* config);
119
120
  chibios_rt::EvtSource* getEventSource();
121
122
  /**
123
   * Check the presence of the magnetometer by reading
124
   * the identifier register and comparing it to the standard
125
   * value
126
   */
127
  uint8_t getCheck();
128
129
  /**
130
   * Top view of the AMiRo with charger in the back (F:Front, B:Back).
131
   * Z is pointing into the ground (Apply right-hand-rule):
132
   *    ___________
133
   *   /    F      \
134
   *  /     X       \
135
   * |      |        |
136
   * |      Z---Y    |
137
   * |               |
138
   *  \             /
139
   *   \____B______/
140
   */
141
  enum {
142
    AXIS_X = 0x00u,
143
    AXIS_Y = 0x02u,
144
    AXIS_Z = 0x01u,
145
  };
146
147
  /**
148
   * Return the magnetization in LSB for the given axis.
149
   *
150
   * @param axis can be one of the axis [AXIS_X | AXIS_Y | AXIS_Z]
151
   *
152
   * @return Measured magnetization in in LSB for the given axis
153
   */
154
  int16_t getMagnetization(const uint8_t axis);
155
156
  /**
157
   * Return the magnetization in µGauss for the given axis.
158
   *
159
   * @param axis can be one of the axis [AXIS_X | AXIS_Y | AXIS_Z]
160
   *
161
   * @return Measured magnetization in µGauss for the given axis
162
   */
163
  int32_t getMagnetizationGauss(const uint8_t axis);
164
165
 protected:
166
  virtual msg_t main(void);
167
168
 private:
169
170
  /**
171
   * Update the sensor values by reading accessing
172
   * the sensor over I2C
173
   */
174
  inline void updateSensorData();
175
176
  /**
177
   * Writes the config to the HMC5883L
178
   *
179
   * @return I2C write return message
180
   */
181
  msg_t writeConf();
182
183
  /**
184
   * Stores the three orientations x,z,y in LSB
185
   */
186
  int16_t data[3];
187
188
  I2CDriver *driver;
189
  const HMC5883LConfig *config;
190
  chibios_rt::EvtSource eventSource;
191
  I2CTxParams txParams;
192
};
193
194
}
195
196
#endif /* HMC5883L_H_ */