Blame view

RIOT/drivers/include/mpu9150.h 11 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  /*
   * Copyright (C) 2015 Freie Universität Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @defgroup    drivers_mpu9150 MPU-9150 accelerometer/magnetometer/gyroscope
   * @ingroup     drivers_sensors
   * @brief       Device driver interface for the MPU-9150
   * @{
   *
   * @file
   * @brief       Device driver interface for the MPU-9150
   *
   * @author      Fabian Nack <nack@inf.fu-berlin.de>
   */
  
  #ifndef MPU9150_H
  #define MPU9150_H
  
  #include "periph/i2c.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /**
   * @name    Sample rate macro definitions
   * @{
   */
  #define MPU9150_MIN_SAMPLE_RATE     (4)
  #define MPU9150_MAX_SAMPLE_RATE     (1000)
  #define MPU9150_DEFAULT_SAMPLE_RATE (50)
  #define MPU9150_MIN_COMP_SMPL_RATE  (1)
  #define MPU9150_MAX_COMP_SMPL_RATE  (100)
  /** @} */
  
  /**
   * @name    Power Management 1 register macros
   * @{
   */
  #define MPU9150_PWR_WAKEUP          (0x00)
  #define MPU9150_PWR_PLL             (0x01)
  #define MPU9150_PWR_RESET           (0x80)
  /** @} */
  
  /**
   * @name    Power Management 2 register macros
   * @{
   */
  #define MPU9150_PWR_GYRO            (0x07)
  #define MPU9150_PWR_ACCEL           (0x38)
  /** @} */
  
  /**
   * @name    Sleep times in microseconds
   * @{
   */
  #define MPU9150_COMP_MODE_SLEEP_US  (1000)
  #define MPU9150_BYPASS_SLEEP_US     (3000)
  #define MPU9150_PWR_CHANGE_SLEEP_US (50000)
  #define MPU9150_RESET_SLEEP_US      (100000)
  /** @} */
  
  /**
   * @name    MPU-9150 compass operating modes and reg values
   * @{
   */
  #define MPU9150_COMP_POWER_DOWN     (0x00)
  #define MPU9150_COMP_SINGLE_MEASURE (0x01)
  #define MPU9150_COMP_SELF_TEST      (0x08)
  #define MPU9150_COMP_FUSE_ROM       (0x0F)
  #define MPU9150_COMP_WHOAMI_ANSWER  (0x48)
  /** @} */
  
  /**
   * @brief   Power enum values
   */
  typedef enum {
      MPU9150_SENSOR_PWR_OFF = 0x00,
      MPU9150_SENSOR_PWR_ON = 0x01,
  } mpu9150_pwr_t;
  
  /**
   * @brief   Possible MPU-9150 hardware addresses (wiring specific)
   */
  typedef enum {
      MPU9150_HW_ADDR_HEX_68 = 0x68,
      MPU9150_HW_ADDR_HEX_69 = 0x69,
  } mpu9150_hw_addr_t;
  
  /**
   * @brief   Possible compass addresses (wiring specific)
   */
  typedef enum {
      MPU9150_COMP_ADDR_HEX_0C = 0x0C,
      MPU9150_COMP_ADDR_HEX_0D = 0x0D,
      MPU9150_COMP_ADDR_HEX_0E = 0x0E,
      MPU9150_COMP_ADDR_HEX_0F = 0x0F,
  } mpu9150_comp_addr_t;
  
  /**
   * @brief   Possible full scale ranges for the gyroscope
   */
  typedef enum {
      MPU9150_GYRO_FSR_250DPS = 0x00,
      MPU9150_GYRO_FSR_500DPS = 0x01,
      MPU9150_GYRO_FSR_1000DPS = 0x02,
      MPU9150_GYRO_FSR_2000DPS = 0x03,
  } mpu9150_gyro_ranges_t;
  
  /**
   * @brief   Possible full scale ranges for the accelerometer
   */
  typedef enum {
      MPU9150_ACCEL_FSR_2G = 0x00,
      MPU9150_ACCEL_FSR_4G = 0x01,
      MPU9150_ACCEL_FSR_8G = 0x02,
      MPU9150_ACCEL_FSR_16G = 0x03,
  } mpu9150_accel_ranges_t;
  
  /**
   * @brief   Possible low pass filter values
   */
  typedef enum {
      MPU9150_FILTER_188HZ = 0x01,
      MPU9150_FILTER_98HZ = 0x02,
      MPU9150_FILTER_42HZ = 0x03,
      MPU9150_FILTER_20HZ = 0x04,
      MPU9150_FILTER_10HZ = 0x05,
      MPU9150_FILTER_5HZ = 0x06,
  } mpu9150_lpf_t;
  
  /**
   * @brief   MPU-9150 result vector struct
   */
  typedef struct {
      int16_t x_axis;             /**< X-Axis measurement result */
      int16_t y_axis;             /**< Y-Axis measurement result */
      int16_t z_axis;             /**< Z-Axis measurement result */
  } mpu9150_results_t;
  
  /**
   * @brief   Configuration struct for the MPU-9150 sensor
   */
  typedef struct {
      mpu9150_pwr_t accel_pwr;            /**< Accel power status (on/off) */
      mpu9150_pwr_t gyro_pwr;             /**< Gyro power status (on/off) */
      mpu9150_pwr_t compass_pwr;          /**< Compass power status (on/off) */
      mpu9150_gyro_ranges_t gyro_fsr;     /**< Configured gyro full-scale range */
      mpu9150_accel_ranges_t accel_fsr;   /**< Configured accel full-scale range */
      uint16_t sample_rate;               /**< Configured sample rate for accel and gyro */
      uint8_t compass_sample_rate;        /**< Configured compass sample rate */
      uint8_t compass_x_adj;              /**< Compass X-Axis sensitivity adjustment value */
      uint8_t compass_y_adj;              /**< Compass Y-Axis sensitivity adjustment value */
      uint8_t compass_z_adj;              /**< Compass Z-Axis sensitivity adjustment value */
  } mpu9150_status_t;
  
  /**
   * @brief   Device descriptor for the MPU-9150 sensor
   */
  typedef struct {
      i2c_t i2c_dev;              /**< I2C device which is used */
      uint8_t hw_addr;            /**< Hardware address of the MPU-9150 */
      uint8_t comp_addr;          /**< Address of the MPU-9150s compass */
      mpu9150_status_t conf;      /**< Device configuration */
  } mpu9150_t;
  
  /**
   * @brief   Initialize the given MPU9150 device
   *
   * @param[out] dev          Initialized device descriptor of MPU9150 device
   * @param[in]  i2c          I2C bus the sensor is connected to
   * @param[in]  hw_addr      The device's address on the I2C bus
   * @param[in]  comp_addr    The compass address on the I2C bus
   *
   * @return                  0 on success
   * @return                  -1 if given I2C is not enabled in board config
   */
  int mpu9150_init(mpu9150_t *dev, i2c_t i2c, mpu9150_hw_addr_t hw_addr,
          mpu9150_comp_addr_t comp_addr);
  
  /**
   * @brief   Enable or disable accelerometer power
   *
   * @param[in] dev           Device descriptor of MPU9150 device
   * @param[in] pwr_conf      Target power setting: PWR_ON or PWR_OFF
   *
   * @return                  0 on success
   * @return                  -1 if given I2C is not enabled in board config
   */
  int mpu9150_set_accel_power(mpu9150_t *dev, mpu9150_pwr_t pwr_conf);
  
  /**
   * @brief   Enable or disable gyroscope power
   *
   * @param[in] dev           Device descriptor of MPU9150 device
   * @param[in] pwr_conf      Target power setting: PWR_ON or PWR_OFF
   *
   * @return                  0 on success
   * @return                  -1 if given I2C is not enabled in board config
   */
  int mpu9150_set_gyro_power(mpu9150_t *dev, mpu9150_pwr_t pwr_conf);
  
  /**
   * @brief   Enable or disable compass power
   *
   * @param[in] dev           Device descriptor of MPU9150 device
   * @param[in] pwr_conf      Target power setting: PWR_ON or PWR_OFF
   *
   * @return                  0 on success
   * @return                  -1 if given I2C is not enabled in board config
   */
  int mpu9150_set_compass_power(mpu9150_t *dev, mpu9150_pwr_t pwr_conf);
  
  /**
   * @brief   Read angular speed values from the given MPU9150 device, returned in dps
   *
   * The raw gyroscope data is read from the sensor and normalized with respect to
   * the configured gyroscope full-scale range.
   *
   * @param[in]  dev          Device descriptor of MPU9150 device to read from
   * @param[out] output       Result vector in dps per axis
   *
   * @return                  0 on success
   * @return                  -1 if device's I2C is not enabled in board config
   * @return                  -2 if gyro full-scale range is configured wrong
   */
  int mpu9150_read_gyro(const mpu9150_t *dev, mpu9150_results_t *output);
  
  /**
   * @brief   Read acceleration values from the given MPU9150 device, returned in mG
   *
   * The raw acceleration data is read from the sensor and normalized with respect to
   * the configured accelerometer full-scale range.
   *
   * @param[in]  dev          Device descriptor of MPU9150 device to read from
   * @param[out] output       Result vector in mG per axis
   *
   * @return                  0 on success
   * @return                  -1 if device's I2C is not enabled in board config
   * @return                  -2 if accel full-scale range is configured wrong
   */
  int mpu9150_read_accel(const mpu9150_t *dev, mpu9150_results_t *output);
  
  /**
   * @brief   Read magnetic field values from the given MPU9150 device, returned in mikroT
   *
   * The raw compass data is read from the sensor and normalized with respect to
   * the compass full-scale range (which can not be configured).
   *
   * @param[in]  dev          Device descriptor of MPU9150 device to read from
   * @param[out] output       Result vector in mikroT per axis
   *
   * @return                  0 on success
   * @return                  -1 if device's I2C is not enabled in board config
   */
  int mpu9150_read_compass(const mpu9150_t *dev, mpu9150_results_t *output);
  
  /**
   * @brief   Read temperature value from the given MPU9150 device, returned in m°C
   *
   * @note
   * The measured temperature is slightly higher than the real room temperature.
   * Tests showed that the offset varied around 2-3 °C (but no warranties here).
   *
   * @param[in] dev           Device descriptor of MPU9150 device to read from
   * @param[out] output       Temperature in m°C
   *
   * @return                  0 on success
   * @return                  -1 if device's I2C is not enabled in board config
   */
  int mpu9150_read_temperature(const mpu9150_t *dev, int32_t *output);
  
  /**
   * @brief   Set the full-scale range for raw gyroscope data
   *
   * @param[in] dev           Device descriptor of MPU9150 device
   * @param[in] fsr           Target full-scale range
   *
   * @return                  0 on success
   * @return                  -1 if device's I2C is not enabled in board config
   * @return                  -2 if given full-scale target value is not valid
   */
  int mpu9150_set_gyro_fsr(mpu9150_t *dev, mpu9150_gyro_ranges_t fsr);
  
  /**
   * @brief   Set the full-scale range for raw accelerometer data
   *
   * @param[in] dev           Device descriptor of MPU9150 device
   * @param[in] fsr           Target full-scale range
   *
   * @return                  0 on success
   * @return                  -1 if device's I2C is not enabled in board config
   * @return                  -2 if given full-scale target value is not valid
   */
  int mpu9150_set_accel_fsr(mpu9150_t *dev, mpu9150_accel_ranges_t fsr);
  
  /**
   * @brief   Set the rate at which the gyroscope and accelerometer data is sampled
   *
   * Sample rate can be chosen between 4 Hz and 1kHz. The actual set value might
   * slightly differ. If necessary, check the actual set value in the device's
   * config member afterwards.
   *
   * @param[in] dev           Device descriptor of MPU9150 device
   * @param[in] rate          Target sample rate in Hz
   *
   * @return                  0 on success
   * @return                  -1 if device's I2C is not enabled in board config
   * @return                  -2 if given target sample rate is not valid
   */
  int mpu9150_set_sample_rate(mpu9150_t *dev, uint16_t rate);
  
  /**
   * @brief   Set the rate at which the compass data is sampled
   *
   * Sample rate can be chosen between 1 Hz and 100 Hz but has to be a fraction
   * of the configured accel/gyro sample rate. The actual set value might
   * slightly differ. If necessary, check the actual set value in the device's
   * config member afterwards.
   *
   * @param[in] dev           Device descriptor of MPU9150 device
   * @param[in] rate          Target sample rate in Hz
   *
   * @return                  0 on success
   * @return                  -1 if device's I2C is not enabled in board config
   * @return                  -2 if given target sample rate is not valid
   */
  int mpu9150_set_compass_sample_rate(mpu9150_t *dev, uint8_t rate);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* MPU9150_H */
  /** @} */