COSA
An Object-Oriented Platform for Arduino Programming
MPU6050.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_MPU6050_HH
22 #define COSA_MPU6050_HH
23 
24 #include "Cosa/TWI.hh"
25 #include "Cosa/IOStream.hh"
26 
53 class MPU6050 : private TWI::Driver {
54 public:
60  MPU6050(uint8_t subaddr = 0) : TWI::Driver(0x68 | (subaddr != 0)) {}
61 
67  bool begin(uint8_t clksel = CLKSEL_PLL_GYRO_X_REF);
68 
74  bool end();
75 
80  int16_t read_temperature();
81 
85  struct sample_t {
86  int16_t x;
87  int16_t y;
88  int16_t z;
89  };
90 
95  struct motion_t {
97  int16_t temp;
99  };
100 
106  void read_motion(motion_t& m);
107 
113  void read_accelerometer(sample_t& s);
114 
119  void read_gyroscope(sample_t& s);
120 
121 protected:
125  enum Register {
126  SELF_TEST_X = 0x0d,
127  SELF_TEST_Y = 0x0e,
128  SELF_TEST_Z = 0x0f,
129  SELF_TEST_A = 0x10,
130  SMPRT_DIV = 0x19,
131  CONFIG = 0x1a,
132  GYRO_CONFIG = 0x1b,
133  ACCEL_CONFIG = 0x1c,
134  FIFO_EN = 0x23,
135  I2C_MST_CTRL = 0x24,
136  I2C_SLV0_ADDR = 0x25,
137  I2C_SLV0_REG = 0x26,
138  I2C_SLV0_CTRL = 0x27,
139  I2C_SLV1_ADDR = 0x28,
140  I2C_SLV1_REG = 0x29,
141  I2C_SLV1_CTRL = 0x2a,
142  I2C_SLV2_ADDR = 0x2b,
143  I2C_SLV2_REG = 0x2c,
144  I2C_SLV2_CTRL = 0x2d,
145  I2C_SLV3_ADDR = 0x2e,
146  I2C_SLV3_REG = 0x2f,
147  I2C_SLV3_CTRL = 0x30,
148  I2C_SLV4_ADDR = 0x31,
149  I2C_SLV4_REG = 0x32,
150  I2C_SLV4_DO = 0x33,
151  I2C_SLV4_CTRL = 0x34,
152  I2C_SLV4_DI = 0x35,
153  I2C_MST_STATUS = 0x36,
154  INT_PIN_CFG = 0x37,
155  INT_ENABLE = 0x38,
156  INT_STATUS = 0x3a,
157  ACCEL_OUT = 0x3b,
158  ACCEL_XOUT = 0x3b,
159  ACCEL_XOUT_H = 0x3b,
160  ACCEL_XOUT_L = 0x3c,
161  ACCEL_YOUT = 0x3d,
162  ACCEL_YOUT_H = 0x3d,
163  ACCEL_YOUT_L = 0x3e,
164  ACCEL_ZOUT = 0x3f,
165  ACCEL_ZOUT_H = 0x3f,
166  ACCEL_ZOUT_L = 0x40,
167  TEMP_OUT = 0x41,
168  TEMP_OUT_H = 0x41,
169  TEMP_OUT_L = 0x42,
170  GYRO_OUT = 0x43,
171  GYRO_XOUT = 0x43,
172  GYRO_XOUT_H = 0x43,
173  GYRO_XOUT_L = 0x44,
174  GYRO_YOUT = 0x45,
175  GYRO_YOUT_H = 0x45,
176  GYRO_YOUT_L = 0x46,
177  GYRO_ZOUT = 0x47,
178  GYRO_ZOUT_H = 0x47,
179  GYRO_ZOUT_L = 0x48,
180  EXT_SENS_DATA = 0x49,
181  I2C_SLV0_DO = 0x63,
182  I2C_SLV1_DO = 0x64,
183  I2C_SLV2_DO = 0x65,
184  I2C_SLV3_DO = 0x66,
187  USER_CTRL = 0x6a,
188  PWR_MGMT_1 = 0x6b,
189  PWR_MGMT_2 = 0x6c,
190  FIFO_COUNT = 0x72,
191  FIFO_COUNT_H = 0x72,
192  FIFO_COUNT_L = 0x73,
193  FIFO_R_W = 0x74,
194  WHO_AM_I = 0x75,
195  } __attribute__((packed));
196 
200  union config_t {
201  uint8_t as_uint8;
202  struct {
203  uint8_t DLPF_CFG:3;
204  uint8_t EXT_SYNC_SET:3;
205  uint8_t reserved:2;
206  };
207  config_t(uint8_t value = 0)
208  {
209  as_uint8 = value;
210  }
211  operator uint8_t()
212  {
213  return (as_uint8);
214  }
215  };
216 
221  uint8_t as_uint8;
222  struct {
223  uint8_t reserved:3;
224  uint8_t FS_SEL:2;
225  uint8_t ZG_ST:1;
226  uint8_t YG_ST:1;
227  uint8_t XG_ST:1;
228  };
229  gyro_config_t(uint8_t value = 0)
230  {
231  as_uint8 = value;
232  }
233  operator uint8_t()
234  {
235  return (as_uint8);
236  }
237  };
238 
242  enum {
247  };
248 
253  uint8_t as_uint8;
254  struct {
255  uint8_t reserved:3;
256  uint8_t AFS_SEL:2;
257  uint8_t ZA_ST:1;
258  uint8_t YA_ST:1;
259  uint8_t XA_ST:1;
260  };
261  accel_config_t(uint8_t value = 0)
262  {
263  as_uint8 = value;
264  }
265  operator uint8_t()
266  {
267  return (as_uint8);
268  }
269  };
270 
274  enum {
279  };
280 
284  union fifo_en_t {
285  uint8_t as_uint8;
286  struct {
287  uint8_t SLV0_FIFO_EN:1;
288  uint8_t SLV1_FIFO_EN:1;
289  uint8_t SLV2_FIFO_EN:1;
290  uint8_t ACCEL_FIFO_EN:1;
291  uint8_t ZG_FIFO_EN:1;
292  uint8_t YG_FIFO_EN:1;
293  uint8_t XG_FIFO_EN:1;
294  uint8_t TEMP_FIFO_EN:1;
295  };
296  fifo_en_t(uint8_t value = 0)
297  {
298  as_uint8 = value;
299  }
300  operator uint8_t()
301  {
302  return (as_uint8);
303  }
304  };
305 
310  uint8_t as_uint8;
311  struct {
312  uint8_t reserved:1;
313  uint8_t I2C_BYPASS_EN:1;
314  uint8_t FSYNC_INT_EN:1;
315  uint8_t FSYNC_INT_LEVEL:1;
316  uint8_t INT_RD_CLEAR:1;
317  uint8_t LATCH_INT_EN:1;
318  uint8_t INT_OPEN:1;
319  uint8_t INT_LEVEL:1;
320  };
321  int_pin_cfg_t(uint8_t value = 0)
322  {
323  as_uint8 = value;
324  }
325  operator uint8_t()
326  {
327  return (as_uint8);
328  }
329  };
330 
334  union int_enable_t {
335  uint8_t as_uint8;
336  struct {
337  uint8_t DATA_RDY_EN:1;
338  uint8_t reserved1:2;
339  uint8_t I2C_MST_INT_EN:1;
340  uint8_t FIFO_OFLOW_EN:1;
341  uint8_t reserved2:3;
342  };
343  int_enable_t(uint8_t value = 0)
344  {
345  as_uint8 = value;
346  }
347  operator uint8_t()
348  {
349  return (as_uint8);
350  }
351  };
352 
356  union int_status_t {
357  uint8_t as_uint8;
358  struct {
359  uint8_t DATA_RDY_INT:1;
360  uint8_t reserved1:2;
361  uint8_t I2C_MST_INT:1;
362  uint8_t FIFO_OFLOW_INT:1;
363  uint8_t reserved2:3;
364  };
365  int_status_t(uint8_t value = 0)
366  {
367  as_uint8 = value;
368  }
369  operator uint8_t()
370  {
371  return (as_uint8);
372  }
373  };
374 
379  uint8_t as_uint8;
380  struct {
381  uint8_t TEMP_RESET:1;
382  uint8_t ACCEL_RESET:1;
383  uint8_t GYRO_RESET:1;
384  uint8_t reserved:5;
385  };
386  signal_path_reset_t(uint8_t value = 0)
387  {
388  as_uint8 = value;
389  }
390  operator uint8_t()
391  {
392  return (as_uint8);
393  }
394  };
395 
399  union user_ctrl_t {
400  uint8_t as_uint8;
401  struct {
402  uint8_t SIG_COND_RESET:1;
403  uint8_t I2C_MST_RESET:1;
404  uint8_t FIFO_RESET:1;
405  uint8_t reserved1:1;
406  uint8_t I2C_IF_DIS:1;
407  uint8_t I2C_MST_EN:1;
408  uint8_t FIFO_EN:1;
409  uint8_t reserved2:1;
410  };
411  user_ctrl_t(uint8_t value = 0)
412  {
413  as_uint8 = value;
414  }
415  operator uint8_t()
416  {
417  return (as_uint8);
418  }
419  };
420 
424  union pwr_mgmt_1_t {
425  uint8_t as_uint8;
426  struct {
427  uint8_t CLKSEL:3;
428  uint8_t TEMP_DIS:1;
429  uint8_t reserved:1;
430  uint8_t CYCLE:1;
431  uint8_t SLP:1;
432  uint8_t DEVICE_RESET:1;
433  };
434  pwr_mgmt_1_t(uint8_t value = 0)
435  {
436  as_uint8 = value;
437  }
438  operator uint8_t()
439  {
440  return (as_uint8);
441  }
442  };
443 
447  enum {
456  };
457 
461  union pwr_mgmt_2_t {
462  uint8_t as_uint8;
463  struct {
464  uint8_t STBY_ZG:1;
465  uint8_t STBY_YG:1;
466  uint8_t STBY_XG:1;
467  uint8_t STBY_ZA:1;
468  uint8_t STBY_YA:1;
469  uint8_t STBY_XA:1;
470  uint8_t LP_WAKE_CTRL:2;
471  };
472  pwr_mgmt_2_t(uint8_t value = 0)
473  {
474  as_uint8 = value;
475  }
476  operator uint8_t()
477  {
478  return (as_uint8);
479  }
480  };
481 
485  enum {
490  };
491 
497  void write(Register reg, uint8_t value);
498 
505  void write(Register reg, void* buffer, size_t count);
506 
512  uint8_t read(Register reg);
513 
520  void read(Register reg, void* buffer, size_t count);
521 };
522 
529 extern IOStream& operator<<(IOStream& outs, MPU6050& mpu);
530 
531 #endif
uint8_t as_uint8
As a byte.
Definition: MPU6050.hh:379
bool begin(uint8_t clksel=CLKSEL_PLL_GYRO_X_REF)
Definition: MPU6050.cpp:60
I2C Slave 1 Register.
Definition: MPU6050.hh:140
Definition: TWI.hh:51
I2C Slave 3 Control.
Definition: MPU6050.hh:147
uint8_t as_uint8
As a byte.
Definition: MPU6050.hh:221
I2C Master Delay Control.
Definition: MPU6050.hh:185
Temp MSB.
Definition: MPU6050.hh:168
config_t(uint8_t value=0)
Definition: MPU6050.hh:207
I2C Master Status.
Definition: MPU6050.hh:153
I2C Slave 2 Address.
Definition: MPU6050.hh:142
I2C Slave 2 Control.
Definition: MPU6050.hh:144
Temperature Measurement.
Definition: MPU6050.hh:167
Sample Rate Divider.
Definition: MPU6050.hh:130
Configuration.
Definition: MPU6050.hh:131
bool end()
Definition: MPU6050.cpp:84
uint8_t as_uint8
As a byte.
Definition: MPU6050.hh:335
FIFO Count Registers MSB.
Definition: MPU6050.hh:191
FIFO Count Registers LSB.
Definition: MPU6050.hh:192
fifo_en_t(uint8_t value=0)
Definition: MPU6050.hh:296
MPU6050(uint8_t subaddr=0)
Definition: MPU6050.hh:60
Interrupt Enable.
Definition: MPU6050.hh:155
IOStream & operator<<(IOStream &outs, MPU6050 &mpu)
Definition: MPU6050.cpp:120
int_enable_t(uint8_t value=0)
Definition: MPU6050.hh:343
Power Management 2.
Definition: MPU6050.hh:189
I2C Slave 3 Register.
Definition: MPU6050.hh:146
Who Am I.
Definition: MPU6050.hh:194
uint8_t read(Register reg)
Definition: MPU6050.cpp:40
int_status_t(uint8_t value=0)
Definition: MPU6050.hh:365
void read_gyroscope(sample_t &s)
Definition: MPU6050.cpp:113
I2C Master Control.
Definition: MPU6050.hh:135
Self Test Registers.
Definition: MPU6050.hh:127
I2C Slave 4 Address.
Definition: MPU6050.hh:148
uint8_t as_uint8
As a byte.
Definition: MPU6050.hh:462
FIFO Count Registers.
Definition: MPU6050.hh:190
I2C Slave 1 Address.
Definition: MPU6050.hh:139
user_ctrl_t(uint8_t value=0)
Definition: MPU6050.hh:411
I2C Slave 3 Data Out.
Definition: MPU6050.hh:184
gyro_config_t(uint8_t value=0)
Definition: MPU6050.hh:229
Signal Path Reset.
Definition: MPU6050.hh:186
I2C Slave 2 Register.
Definition: MPU6050.hh:143
void write(Register reg, uint8_t value)
Definition: MPU6050.cpp:24
sample_t accel
Definition: MPU6050.hh:96
Gyroscope Configuration.
Definition: MPU6050.hh:132
Self Test Registers.
Definition: MPU6050.hh:129
INT Pin/Bypass Enable Configuration.
Definition: MPU6050.hh:154
accel_config_t(uint8_t value=0)
Definition: MPU6050.hh:261
uint8_t as_uint8
As a byte.
Definition: MPU6050.hh:285
I2C Slave 4 Data In.
Definition: MPU6050.hh:152
Self Test Registers.
Definition: MPU6050.hh:128
uint8_t as_uint8
As a byte.
Definition: MPU6050.hh:400
int_pin_cfg_t(uint8_t value=0)
Definition: MPU6050.hh:321
I2C Slave 2 Data Out.
Definition: MPU6050.hh:183
External Sensor Data.
Definition: MPU6050.hh:180
uint8_t as_uint8
As a byte.
Definition: MPU6050.hh:310
Accelerometer Configuration.
Definition: MPU6050.hh:133
Accelerometer Measurements.
Definition: MPU6050.hh:157
Gyroscope Measurement.
Definition: MPU6050.hh:170
int16_t temp
Definition: MPU6050.hh:97
Driver(uint8_t addr)
Definition: TWI.hh:70
Self Test Registers.
Definition: MPU6050.hh:126
void read_motion(motion_t &m)
Definition: MPU6050.cpp:98
FIFO Read Write.
Definition: MPU6050.hh:193
User Control.
Definition: MPU6050.hh:187
Temp LSB.
Definition: MPU6050.hh:169
uint8_t as_uint8
As a byte.
Definition: MPU6050.hh:357
I2C Slave 1 Control.
Definition: MPU6050.hh:141
I2C Slave 0 Control.
Definition: MPU6050.hh:138
Power Management 1.
Definition: MPU6050.hh:188
I2C Slave 0 Register.
Definition: MPU6050.hh:137
Interrupt Status.
Definition: MPU6050.hh:156
I2C Slave 4 Data Out.
Definition: MPU6050.hh:150
uint8_t as_uint8
As a byte.
Definition: MPU6050.hh:253
pwr_mgmt_2_t(uint8_t value=0)
Definition: MPU6050.hh:472
I2C Slave 0 Address.
Definition: MPU6050.hh:136
pwr_mgmt_1_t(uint8_t value=0)
Definition: MPU6050.hh:434
int16_t read_temperature()
Definition: MPU6050.cpp:90
FIFO Enable.
Definition: MPU6050.hh:134
I2C Slave 4 Control.
Definition: MPU6050.hh:151
sample_t gyro
Definition: MPU6050.hh:98
uint8_t as_uint8
As a byte.
Definition: MPU6050.hh:201
I2C Slave 3 Address.
Definition: MPU6050.hh:145
uint8_t as_uint8
As a byte.
Definition: MPU6050.hh:425
signal_path_reset_t(uint8_t value=0)
Definition: MPU6050.hh:386
I2C Slave 4 Register.
Definition: MPU6050.hh:149
I2C Slave 1 Data Out.
Definition: MPU6050.hh:182
void read_accelerometer(sample_t &s)
Definition: MPU6050.cpp:106
I2C Slave 0 Data Out.
Definition: MPU6050.hh:181