upm  0.8.0
Sensor/Actuator repository for libmraa (v1.1.1)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
bmm150.hpp
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <mraa/i2c.hpp>
28 #include <mraa/spi.hpp>
29 #include <mraa/gpio.hpp>
30 
31 #define BMM150_I2C_BUS 0
32 #define BMM150_SPI_BUS 0
33 #define BMM150_DEFAULT_ADDR 0x10
34 
35 
36 namespace upm {
37 
71  class BMM150 {
72  public:
73 
74  // NOTE: Reserved registers must not be written into. Reading
75  // from them may return indeterminate values. Registers
76  // containing reserved bitfields must be written as 0. Reading
77  // reserved bitfields may return indeterminate values.
78 
82  typedef enum : uint8_t {
83  REG_CHIP_ID = 0x40,
84 
85  // 0x41 reserved
86 
87  REG_MAG_X_LSB = 0x42,
88  REG_MAG_X_MSB = 0x43,
89  REG_MAG_Y_LSB = 0x44,
90  REG_MAG_Y_MSB = 0x45,
91  REG_MAG_Z_LSB = 0x46,
92  REG_MAG_Z_MSB = 0x47,
93 
94  REG_RHALL_LSB = 0x48,
95  REG_RHALL_MSB = 0x49,
96 
97  REG_INT_STATUS = 0x4a,
98 
99  REG_POWER_CTRL = 0x4b,
100 
101  REG_OPMODE = 0x4c,
102 
103  REG_INT_EN = 0x4d,
104  REG_INT_CONFIG = 0x4e,
105 
106  REG_LOW_THRES = 0x4f,
107  REG_HIGH_THRES = 0x50,
108 
109  REG_REP_XY = 0x51,
110  REG_REP_Z = 0x52,
111 
112  // 0x53-0x71 reserved (mostly)
113 
114  // TRIM registers from Bosch BMM050 driver
115  REG_TRIM_DIG_X1 = 0x5d,
116  REG_TRIM_DIG_Y1 = 0x5e,
117 
118  REG_TRIM_DIG_Z4_LSB = 0x62,
119  REG_TRIM_DIG_Z4_MSB = 0x63,
120  REG_TRIM_DIG_X2 = 0x64,
121  REG_TRIM_DIG_Y2 = 0x65,
122 
123  REG_TRIM_DIG_Z2_LSB = 0x68,
124  REG_TRIM_DIG_Z2_MSB = 0x69,
125  REG_TRIM_DIG_Z1_LSB = 0x6a,
126  REG_TRIM_DIG_Z1_MSB = 0x6b,
127  REG_TRIM_DIG_XYZ1_LSB = 0x6c,
128  REG_TRIM_DIG_XYZ1_MSB = 0x6d,
129  REG_TRIM_DIG_Z3_LSB = 0x6e,
130  REG_TRIM_DIG_Z3_MSB = 0x6f,
131  REG_TRIM_DIG_XY2 = 0x70,
132  REG_TRIM_DIG_XY1 = 0x71
133 
134  } BMM150_REGS_T;
135 
139  typedef enum {
140  _MAG_XY_LSB_RESERVED_BITS = 0x02 | 0x04,
141 
142  MAG_XY_LSB_SELFTEST_XY = 0x01,
143 
144  MAG_XY_LSB_LSB0 = 0x08,
145  MAG_XY_LSB_LSB1 = 0x10,
146  MAG_XY_LSB_LSB2 = 0x20,
147  MAG_XY_LSB_LSB3 = 0x40,
148  MAG_XY_LSB_LSB4 = 0x80,
149  _MAG_XY_LSB_LSB_MASK = 31,
150  _MAG_XY_LSB_LSB_SHIFT = 3
152 
156  typedef enum {
157  MAG_Z_LSB_SELFTEST_Z = 0x01,
158 
159  MAG_Z_LSB_LSB0 = 0x02,
160  MAG_Z_LSB_LSB1 = 0x04,
161  MAG_Z_LSB_LSB2 = 0x08,
162  MAG_Z_LSB_LSB3 = 0x10,
163  MAG_Z_LSB_LSB4 = 0x20,
164  MAG_Z_LSB_LSB5 = 0x40,
165  MAG_Z_LSB_LSB6 = 0x80,
166  _MAG_Z_LSB_LSB_MASK = 127,
167  _MAG_Z_LSB_LSB_SHIFT = 1
169 
173  typedef enum {
174  _MAG_RHALL_LSB_RESERVED_BITS = 0x02,
175 
176  MAG_RHALL_LSB_DATA_READY_STATUS = 0x01,
177 
178  MAG_RHALL_LSB_LSB0 = 0x04,
179  MAG_RHALL_LSB_LSB1 = 0x08,
180  MAG_RHALL_LSB_LSB2 = 0x10,
181  MAG_RHALL_LSB_LSB3 = 0x20,
182  MAG_RHALL_LSB_LSB4 = 0x40,
183  MAG_RHALL_LSB_LSB5 = 0x80,
184  _MAG_RHALL_LSB_LSB_MASK = 63,
185  _MAG_RHALL_LSB_LSB_SHIFT = 2
187 
191  typedef enum {
192  INT_STATUS_LOW_INT_X = 0x01,
193  INT_STATUS_LOW_INT_Y = 0x02,
194  INT_STATUS_LOW_INT_Z = 0x04,
195  INT_STATUS_HIGH_INT_X = 0x08,
196  INT_STATUS_HIGH_INT_Y = 0x10,
197  INT_STATUS_HIGH_INT_Z = 0x20,
198  INT_STATUS_OVERFLOW = 0x40,
199  INT_STATUS_DATA_OVERRUN = 0x80
201 
205  typedef enum {
206  _POWER_CTRL_RESERVED_BITS = 0x40 | 0x20 | 0x10 | 0x08,
207 
208  POWER_CTRL_POWER_CTRL_BIT = 0x01,
209  POWER_CTRL_SOFT_RESET0 = 0x02,
210  POWER_CTRL_SPI3EN = 0x04, // not supported
211 
212  POWER_CTRL_SOFT_RESET1 = 0x80
214 
218  typedef enum {
219  OPMODE_SELFTTEST = 0x01,
220 
221  OPMODE_OPERATION_MODE0 = 0x02,
222  OPMODE_OPERATION_MODE1 = 0x04,
223  _OPMODE_OPERATION_MODE_MASK = 3,
224  _OPMODE_OPERATION_MODE_SHIFT = 1,
225 
226  OPMODE_DATA_RATE0 = 0x08,
227  OPMODE_DATA_RATE1 = 0x10,
228  OPMODE_DATA_RATE2 = 0x20,
229  _OPMODE_DATA_RATE_MASK = 7,
230  _OPMODE_DATA_RATE_SHIFT = 3,
231 
232  OPMODE_ADV_SELFTEST0 = 0x40,
233  OPMODE_ADV_SELFTEST1 = 0x80,
234  _OPMODE_ADV_SELFTEST_MASK = 3,
235  _OPMODE_ADV_SELFTEST_SHIFT = 6
236  } OPMODE_BITS_T;
237 
241  typedef enum {
242  OPERATION_MODE_NORMAL = 0,
243  OPERATION_MODE_FORCED = 1,
244  OPERATION_MODE_SLEEP = 3
246 
250  typedef enum {
251  DATA_RATE_10HZ = 0,
252  DATA_RATE_2HZ = 1,
253  DATA_RATE_6HZ = 2,
254  DATA_RATE_8HZ = 3,
255  DATA_RATE_15HZ = 4,
256  DATA_RATE_20HZ = 5,
257  DATA_RATE_25HZ = 6,
258  DATA_RATE_30HZ = 7
259  } DATA_RATE_T;
260 
264  typedef enum {
265  INT_EN_LOW_INT_X_EN = 0x01,
266  INT_EN_LOW_INT_Y_EN = 0x02,
267  INT_EN_LOW_INT_Z_EN = 0x04,
268  INT_EN_HIGH_INT_X_EN = 0x08,
269  INT_EN_HIGH_INT_Y_EN = 0x10,
270  INT_EN_HIGH_INT_Z_EN = 0x20,
271  INT_EN_OVERFLOW_INT_EN = 0x40,
272  INT_EN_DATA_OVERRUN_INT_EN = 0x80
273  } INT_EN_T;
274 
278  typedef enum {
279  INT_CONFIG_INT_POLARITY = 0x01,
280  INT_CONFIG_INT_LATCH = 0x02,
281  INT_CONFIG_DR_POLARITY = 0x04,
282  INT_CONFIG_CHANNEL_X = 0x08,
283  INT_CONFIG_CHANNEL_Y = 0x10,
284  INT_CONFIG_CHANNEL_Z = 0x20,
285  INT_CONFIG_INT_PIN_EN = 0x40,
286  INT_CONFIG_DR_PIN_EN = 0x80
287  } INT_CONFIG_T;
288 
292  typedef enum {
293  INTERRUPT_INT,
294  INTERRUPT_DR
296 
300  typedef enum {
301  USAGE_LOW_POWER,
302  USAGE_REGULAR,
303  USAGE_ENHANCED_REGULAR,
304  USAGE_HIGH_ACCURACY
305  } USAGE_PRESETS_T;
306 
323  BMM150(int bus=BMM150_I2C_BUS, int addr=BMM150_DEFAULT_ADDR,
324  int cs=-1);
325 
329  ~BMM150();
330 
334  void update();
335 
341  uint8_t getChipID();
342 
354  void getMagnetometer(float *x, float *y, float *z);
355 
365  float *getMagnetometer();
366 
376  void init(USAGE_PRESETS_T usage=USAGE_HIGH_ACCURACY);
377 
385  void setPresetMode(USAGE_PRESETS_T usage);
386 
393  void reset();
394 
401  void setOutputDataRate(DATA_RATE_T odr);
402 
414  void setPowerBit(bool power);
415 
422  void setOpmode(OPERATION_MODE_T opmode);
423 
431 
439  uint8_t getInterruptEnable();
440 
447  void setInterruptEnable(uint8_t bits);
448 
457  uint8_t getInterruptConfig();
458 
467  void setInterruptConfig(uint8_t bits);
468 
476  uint8_t getInterruptStatus();
477 
486  void setRepetitionsXY(uint8_t reps);
487 
496  void setRepetitionsZ(uint8_t reps);
497 
498 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
499  void installISR(INTERRUPT_PINS_T intr, int gpio, mraa::Edge level,
500  jobject runnable);
501 #else
502 
514  void installISR(INTERRUPT_PINS_T intr, int gpio, mraa::Edge level,
515  void (*isr)(void *), void *arg);
516 #endif
517 
524  void uninstallISR(INTERRUPT_PINS_T intr);
525 
532  uint8_t readReg(uint8_t reg);
533 
541  int readRegs(uint8_t reg, uint8_t *buffer, int len);
542 
549  void writeReg(uint8_t reg, uint8_t val);
550 
551  protected:
552  mraa::I2c *m_i2c;
553  mraa::Spi *m_spi;
554 
555  // spi chip select
556  mraa::Gpio *m_gpioCS;
557 
558  mraa::Gpio *m_gpioIntr;
559  mraa::Gpio *m_gpioDR;
560 
561  uint8_t m_addr;
562 
563  OPERATION_MODE_T m_opmode;
564 
565  // SPI chip select
566  void csOn();
567  void csOff();
568 
569  // acc data
570  float m_magX;
571  float m_magY;
572  float m_magZ;
573 
574  // hall resistance
575  uint16_t m_hall;
576 
577  // trimming data
578  int8_t m_dig_x1;
579  int8_t m_dig_y1;
580 
581  int16_t m_dig_z4;
582  int8_t m_dig_x2;
583  int8_t m_dig_y2;
584 
585  int16_t m_dig_z2;
586  uint16_t m_dig_z1;
587  uint16_t m_dig_xyz1;
588  int16_t m_dig_z3;
589  int8_t m_dig_xy2;
590  uint8_t m_dig_xy1;
591 
592  // read trim data for compensation
593  void readTrimData();
594 
595  private:
596  bool m_isSPI;
597  // use the FIFO by default?
598  bool m_useFIFO;
599 
600  // return a reference to a gpio pin pointer depending on intr
601  mraa::Gpio*& getPin(INTERRUPT_PINS_T intr);
602 
603  // Adding a private function definition for java bindings
604 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
605  void installISR(INTERRUPT_PINS_T intr, int gpio, mraa::Edge level,
606  void (*isr)(void *), void *arg);
607 #endif
608 
609  // bosch compensation algorithms
610  float bmm050_compensate_X_float(int16_t mag_data_x, uint16_t data_r);
611  float bmm050_compensate_Y_float(int16_t mag_data_y, uint16_t data_r);
612  float bmm050_compensate_Z_float(int16_t mag_data_z, uint16_t data_r);
613  };
614 }
uint8_t getChipID()
Definition: bmm150.cxx:361
OPERATION_MODE_T getOpmode()
Definition: bmm150.cxx:432
uint8_t getInterruptEnable()
Definition: bmm150.cxx:442
void setPowerBit(bool power)
Definition: bmm150.cxx:408
BMM150_REGS_T
Definition: bmm150.hpp:82
void setOutputDataRate(DATA_RATE_T odr)
Definition: bmm150.cxx:398
DATA_RATE_T
Definition: bmm150.hpp:250
void writeReg(uint8_t reg, uint8_t val)
Definition: bmm150.cxx:321
int readRegs(uint8_t reg, uint8_t *buffer, int len)
Definition: bmm150.cxx:287
void reset()
Definition: bmm150.cxx:386
void setOpmode(OPERATION_MODE_T opmode)
Definition: bmm150.cxx:421
API for the BMM150 3-Axis Geomagnetic Sensor.
Definition: bmm150.hpp:71
OPERATION_MODE_T
Definition: bmm150.hpp:241
float * getMagnetometer()
Definition: bmm150.cxx:378
MAG_Z_LSB_BITS_T
Definition: bmm150.hpp:156
~BMM150()
Definition: bmm150.cxx:182
INT_STATUS_BITS_T
Definition: bmm150.hpp:191
BMM150(int bus=BMM150_I2C_BUS, int addr=BMM150_DEFAULT_ADDR, int cs=-1)
Definition: bmm150.cxx:95
void setRepetitionsXY(uint8_t reps)
Definition: bmm150.cxx:496
void setRepetitionsZ(uint8_t reps)
Definition: bmm150.cxx:501
void init(USAGE_PRESETS_T usage=USAGE_HIGH_ACCURACY)
Definition: bmm150.cxx:195
POWER_CTRL_BITS_T
Definition: bmm150.hpp:205
INT_EN_T
Definition: bmm150.hpp:264
void setInterruptConfig(uint8_t bits)
Definition: bmm150.cxx:457
void uninstallISR(INTERRUPT_PINS_T intr)
Definition: bmm150.cxx:574
uint8_t getInterruptConfig()
Definition: bmm150.cxx:452
MAG_XY_LSB_BITS_T
Definition: bmm150.hpp:139
void setPresetMode(USAGE_PRESETS_T usage)
Definition: bmm150.cxx:506
USAGE_PRESETS_T
Definition: bmm150.hpp:300
void setInterruptEnable(uint8_t bits)
Definition: bmm150.cxx:447
uint8_t getInterruptStatus()
Definition: bmm150.cxx:462
MAG_RHALL_LSB_BITS_T
Definition: bmm150.hpp:173
INTERRUPT_PINS_T
Definition: bmm150.hpp:292
OPMODE_BITS_T
Definition: bmm150.hpp:218
INT_CONFIG_T
Definition: bmm150.hpp:278
uint8_t readReg(uint8_t reg)
Definition: bmm150.cxx:265
void installISR(INTERRUPT_PINS_T intr, int gpio, mraa::Edge level, void(*isr)(void *), void *arg)
Definition: bmm150.cxx:560
void update()
Definition: bmm150.cxx:208