upm  0.4.1
Sensor/Actuator repository for libmraa (v0.8.0)
bma220.h
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 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/common.hpp>
28 #include <mraa/i2c.hpp>
29 #include <mraa/gpio.hpp>
30 
31 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
32 #include "../IsrCallback.h"
33 #endif
34 
35 #define BMA220_I2C_BUS 0
36 #define BMA220_DEFAULT_ADDR 0x0a
37 
38 namespace upm {
39 
73  class BMA220 {
74  public:
75 
76  // NOTE: reserved registers must not be written into or read from.
77  // Reserved bitfields must always be 0. The registers aren't
78  // named in the datasheet, so I made up some hopefully useful
79  // names for them. The whole I2C register map design is a little
80  // strange, IMO.
81 
85  typedef enum {
86  REG_CHIPID = 0x00,
87  REG_REVISIONID = 0x02,
88 
89  // 2 lsb bits of ACC regs are always 0, yeilding 6 bits resolution
90  REG_ACC_X = 0x04, // acceleration data
91  REG_ACC_Y = 0x06,
92  REG_ACC_Z = 0x08,
93 
94  REG_H_HYST_DUR = 0x0a, // high hysteresis/dur
95  REG_THRESHOLD = 0x0c, // high/low threshold
96  REG_L_HYST_DUR = 0x0e, // low hysteresis/dur
97 
98  REG_TAP_CONFIG = 0x10,
99 
100  REG_SLOPE_CONFIG = 0x12,
101  REG_TAP_CONFIG2 = 0x14,
102 
103  REG_INT_STATUS1 = 0x16,
104  REG_INT_STATUS2 = 0x18,
105 
106  REG_ENABLE_CONFIG = 0x1a,
107  REG_ENABLE_CONFIG2 = 0x1c,
108  REG_ENABLE_CONFIG3 = 0x1e,
109 
110  REG_FILTER_CONFIG = 0x20,
111 
112  REG_SBIST_FSL_CONFIG = 0x22, // self test and full scale range
113 
114  // 0x24 - 0x2c reserved
115 
116  REG_I2C_WATCHDOG = 0x2e, // + SPI 3-wire mode
117 
118  REG_SUSPEND = 0x30,
119  REG_SOFTRESET = 0x32
120  } REG_T;
121 
122 
126  typedef enum {
127  H_HYST_DUR_HIGH_DUR0 = 0x01,
128  H_HYST_DUR_HIGH_DUR1 = 0x02,
129  H_HYST_DUR_HIGH_DUR2 = 0x04,
130  H_HYST_DUR_HIGH_DUR3 = 0x08,
131  H_HYST_DUR_HIGH_DUR4 = 0x10,
132  H_HYST_DUR_HIGH_DUR5 = 0x20,
133  _H_HYST_DUR_HIGH_DUR_MASK = 63,
134  _H_HYST_DUR_HIGH_DUR_SHIFT = 0,
135 
136  H_HYST_DUR_HIGH_HY1 = 0x40,
137  H_HYST_DUR_HIGH_HY2 = 0x80,
138  _H_HYST_DUR_HIGH_HY_MASK = 3,
139  _H_HYST_DUR_HIGH_HY_SHIFT = 6
141 
145  typedef enum {
146  THRESHOLD_HIGH0 = 0x01,
147  THRESHOLD_HIGH1 = 0x02,
148  THRESHOLD_HIGH2 = 0x04,
149  THRESHOLD_HIGH3 = 0x08,
150  _THRESHOLD_HIGH_MASK = 15,
151  _THRESHOLD_HIGH_SHIFT = 0,
152 
153  THRESHOLD_LOW0 = 0x10,
154  THRESHOLD_LOW1 = 0x20,
155  THRESHOLD_LOW2 = 0x40,
156  THRESHOLD_LOW3 = 0x80,
157  _THRESHOLD_LOW_MASK = 15,
158  _THRESHOLD_LOW_SHIFT = 4
160 
164  typedef enum {
165  L_HYST_DUR_LOW_DUR0 = 0x01,
166  L_HYST_DUR_LOW_DUR1 = 0x02,
167  L_HYST_DUR_LOW_DUR2 = 0x04,
168  L_HYST_DUR_LOW_DUR3 = 0x08,
169  L_HYST_DUR_LOW_DUR4 = 0x10,
170  L_HYST_DUR_LOW_DUR5 = 0x20,
171  _L_HYST_DUR_LOW_DUR_MASK = 63,
172  _L_HYST_DUR_LOW_DUR_SHIFT = 0,
173 
174  L_HYST_DUR_LOW_HY1 = 0x40,
175  L_HYST_DUR_LOW_HY2 = 0x80,
176  _L_HYST_DUR_LOW_HY_MASK = 3,
177  _L_HYST_DUR_LOW_HY_SHIFT = 6
179 
183  typedef enum {
184  TAP_CONFIG_DUR0 = 0x01,
185  TAP_CONFIG_DUR1 = 0x02,
186  TAP_CONFIG_DUR2 = 0x04,
187  _TAP_CONFIG_DUR_MASK = 7,
188  _TAP_CONFIG_DUR_SHIFT = 0,
189 
190  TAP_CONFIG_THRESH0 = 0x08,
191  TAP_CONFIG_THRESH1 = 0x10,
192  TAP_CONFIG_THRESH2 = 0x20,
193  TAP_CONFIG_THRESH3 = 0x40,
194  _TAP_CONFIG_THRESH_MASK = 15,
195  _TAP_CONFIG_THRESH_SHIFT = 3,
196 
197  TAP_CONFIG_FILTER = 0x80
199 
203  typedef enum {
204  SLOPE_CONFIG_DUR0 = 0x01,
205  SLOPE_CONFIG_DUR1 = 0x02,
206  _SLOPE_CONFIG_DUR_MASK = 3,
207  _SLOPE_CONFIG_DUR_SHIFT = 0,
208 
209  SLOPE_CONFIG_THRESH0 = 0x04,
210  SLOPE_CONFIG_THRESH1 = 0x08,
211  SLOPE_CONFIG_THRESH2 = 0x10,
212  SLOPE_CONFIG_THRESH3 = 0x20,
213  _SLOPE_CONFIG_THRESH_MASK = 15,
214  _SLOPE_CONFIG_THRESH_SHIFT = 2,
215 
216  SLOPE_CONFIG_FILTER = 0x40,
217  SLOPE_CONFIG_ORIENT_EX = 0x80 // exchange x and z axis for orient
219 
223  typedef enum {
224  TAP_CONFIG2_SAMP0 = 0x01,
225  TAP_CONFIG2_SAMP1 = 0x02,
226  _TAP_CONFIG2_SAMP_MASK = 3,
227  _TAP_CONFIG2_SAMP_SHIFT = 0,
228 
229  TAP_CONFIG2_ORIENT_BLOCK0 = 0x04,
230  TAP_CONFIG2_ORIENT_BLOCK1 = 0x08,
231  _TAP_CONFIG2_ORIENT_BLOCK_MASK = 3,
232  _TAP_CONFIG2_ORIENT_BLOCK_SHIFT = 2,
233 
234  TAP_CONFIG2_TIP_EN = 0x10
235 
236  // 0x20-0x80 reserved
238 
242  typedef enum {
243  TAP_SAMP_2 = 0, // 2 data samples after wakeup
244  TAP_SAMP_4 = 1,
245  TAP_SAMP_8 = 2,
246  TAP_SAMP_16 = 3
247  } TAP_SAMP_T;
248 
253  typedef enum {
254  TAP_ORIENT_BLOCK_0 = 0, // orient blocking disabled
255  TAP_ORIENT_BLOCK_2 = 1, // |z|>0.9g OR |x|+|y| < 0.2g OR m<0.2g
256  TAP_ORIENT_BLOCK_3 = 2, // |z|>0.9g OR |x|+|y| < 0.3g OR m<0.3g
257  TAP_ORIENT_BLOCK_4 = 3, // |z|>0.9g OR |x|+|y| < 0.4g OR m<0.4g
259 
263  typedef enum {
264  INT_STATUS1_SIGN = 0x01,
265  INT_STATUS1_FIRST_Z = 0x02,
266  INT_STATUS1_FIRST_Y = 0x04,
267  INT_STATUS1_FIRST_X = 0x08,
268 
269  INT_STATUS1_ORIENT0 = 0x10,
270  INT_STATUS1_ORIENT1 = 0x20,
271  INT_STATUS1_ORIENT2 = 0x40,
272  _INT_STATUS1_ORIENT_MASK = 7,
273  _INT_STATUS1_ORIENT_SHIFT = 4,
274 
275  INT_STATUS1_ORIENT_INT = 0x80 // orient intr was generated
277 
284  typedef enum {
285  CONFIG_ORI_UP_PORT_UPRIGHT = 0, // up portrait
286  CONFIG_ORI_UP_PORT_UPSIDE_DOWN = 1,
287  CONFIG_ORI_UP_LAND_LEFT = 2, // landscape
288  CONFIG_ORI_UP_LAND_RIGHT = 3,
289  CONFIG_ORI_DN_PORT_UPRIGHT = 4, // down portrait
290  CONFIG_ORI_DN_PORT_UPSIDE_DOWN = 5,
291  CONFIG_ORI_DN_LAND_LEFT = 6, // landscape
292  CONFIG_ORI_DN_LAND_RIGHT = 7
293  } CONFIG_ORIENT_T;
294 
298  typedef enum {
299  INT_STATUS2_SLOPE = 0x01,
300  INT_STATUS2_DATA = 0x02,
301  INT_STATUS2_HIGH = 0x04,
302  INT_STATUS2_LOW = 0x08,
303  INT_STATUS2_TAP = 0x10
304 
305  // 0x20-0x80 reserved
307 
311  typedef enum {
312  ENABLE_CONFIG_TT_Z = 0x01,
313  ENABLE_CONFIG_TT_Y = 0x02,
314  ENABLE_CONFIG_TT_X = 0x04,
315  ENABLE_CONFIG_SLOPE_Z = 0x08,
316  ENABLE_CONFIG_SLOPE_Y = 0x10,
317  ENABLE_CONFIG_SLOPE_X = 0x20,
318  ENABLE_CONFIG_ORIENT = 0x40,
319  ENABLE_CONFIG_DATA = 0x80
321 
325  typedef enum {
326  ENABLE_CONFIG2_HIGH_Z = 0x01,
327  ENABLE_CONFIG2_HIGH_Y = 0x02,
328  ENABLE_CONFIG2_HIGH_X = 0x04,
329 
330  ENABLE_CONFIG2_LOW = 0x08,
331 
332  ENABLE_CONFIG2_LAT_INT0 = 0x10, // interrupt latching
333  ENABLE_CONFIG2_LAT_INT1 = 0x20,
334  ENABLE_CONFIG2_LAT_INT2 = 0x40,
335  _ENABLE_CONFIG2_LAT_INT_MASK = 7,
336  _ENABLE_CONFIG2_LAT_INT_SHIFT = 4,
337 
338  ENABLE_CONFIG2_RESET_INT = 0x80 // reset interrupts
340 
346  typedef enum {
347  CONFIG2_LAT_UNLATCH = 0, // unlatched intrs
348  CONFIG2_LAT_0_25 = 1, // latch intr for 0.25s
349  CONFIG2_LAT_0_5 = 2, // latch intr for 0.5s
350  CONFIG2_LAT_1 = 3, // latch intr for 1s
351  CONFIG2_LAT_2 = 4, // latch intr for 2s
352  CONFIG2_LAT_4 = 5, // latch intr for 4s
353  CONFIG2_LAT_8 = 6, // latch intr for 8s
354  CONFIG2_LAT_PERM = 7 // latch permanently
355  } CONFIG2_LAT_T;
356 
360  typedef enum {
361  ENABLE_CONFIG3_Z_CHAN = 0x01,
362  ENABLE_CONFIG3_Y_CHAN = 0x02,
363  ENABLE_CONFIG3_X_CHAN = 0x04,
364 
365  ENABLE_CONFIG3_SLEEP_DUR0 = 0x08,
366  ENABLE_CONFIG3_SLEEP_DUR1 = 0x10,
367  ENABLE_CONFIG3_SLEEP_DUR2 = 0x20,
368  _ENABLE_CONFIG3_SLEEP_DUR_MASK = 7,
369  _ENABLE_CONFIG3_SLEEP_DUR_SHIFT = 3,
370 
371  ENABLE_CONFIG3_SLEEP_EN = 0x40
372 
373  // 0x80 reserved
375 
382  typedef enum {
383  SLEEP_DUR_2MS = 0, // 2 ms
384  SLEEP_DUR_10MS = 1,
385  SLEEP_DUR_25MS = 2,
386  SLEEP_DUR_50MS = 3,
387  SLEEP_DUR_100MS = 4,
388  SLEEP_DUR_500MS = 5,
389  SLEEP_DUR_1S = 6, // 1 second
390  SLEEP_DUR_2S = 7
391  } SLEEP_DUR_T;
392 
396  typedef enum {
397  FILTER_CONFIG_FILTER0 = 0x01,
398  FILTER_CONFIG_FILTER1 = 0x02,
399  FILTER_CONFIG_FILTER2 = 0x04,
400  FILTER_CONFIG_FILTER3 = 0x08,
401  _FILTER_CONFIG_FILTER_MASK = 15,
402  _FILTER_CONFIG_FILTER_SHIFT = 0,
403 
404  // 0x10-0x40 reserved
405 
406  FILTER_CONFIG_SERIAL_HIGH_BW = 0x80
408 
414  typedef enum {
415  FILTER_CONFIG_1KHZ = 0, // 1Khz
416  FILTER_CONFIG_500HZ = 1,
417  FILTER_CONFIG_250HZ = 2,
418  FILTER_CONFIG_125HZ = 3,
419  FILTER_CONFIG_64HZ = 4,
420  FILTER_CONFIG_32HZ = 5
421  } FILTER_CONFIG_T;
422 
426  typedef enum {
427  REG_SBIST_FSL_RANGE0 = 0x01, // full scale range
428  REG_SBIST_FSL_RANGE1 = 0x02,
429  _REG_SBIST_FSL_RANGE_MASK = 3,
430  _REG_SBIST_FSL_RANGE_SHIFT = 0,
431 
432  REG_SBIST_FSL_SBIST0 = 0x04, // self test enables
433  REG_SBIST_FSL_SBIST1 = 0x08,
434  _REG_SBIST_FSL_SBIST_MASK = 3,
435  _REG_SBIST_FSL_SBIST_SHIFT = 2,
436 
437  REG_SBIST_FSL_SBIST_SIGN = 0x10 // signedness of self test
438 
439  // 0x20-0x80 reserved
441 
447  typedef enum {
448  FSL_RANGE_2G = 0, // 2G FSL
449  FSL_RANGE_4G = 1,
450  FSL_RANGE_8G = 2,
451  FSL_RANGE_16G = 3
452  } FSL_RANGE_T;
453 
459  typedef enum {
460  SBIST_OFF = 0, // self test off
461  SBIST_X = 1, // self test X
462  SBIST_Y = 2,
463  SBIST_Z = 3
464  } SBIST_T;
465 
469  typedef enum {
470  I2C_WATCHDOG_SPI3 = 0x01, // SPI 3wire mode (SPI not supported)
471 
472  I2C_WATCHDOG_TO_SEL = 0x02,
473  I2C_WATCHDOG_TO_EN = 0x04
474 
475  // 0x08-0x80 reserved
477 
478 
485  BMA220(int bus=BMA220_I2C_BUS, uint8_t addr=BMA220_DEFAULT_ADDR);
486 
490  ~BMA220();
491 
495  void update();
496 
504  uint8_t readReg(uint8_t reg);
505 
514  bool writeReg(uint8_t reg, uint8_t val);
515 
521  uint8_t getChipID();
522 
528  uint8_t getChipRevision();
529 
537 
545  void getAccelerometer(float *x, float *y, float *z);
546 
547 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
548 
553  float *getAccelerometer();
554 #endif
555 
562  bool setFilterConfig(FILTER_CONFIG_T filter);
563 
572  bool setSerialHighBW(bool high);
573 
582  bool enableAxes(bool xEn, bool yEn, bool zEn);
583 
590  uint8_t suspend();
591 
598  uint8_t softReset();
599 
608  bool sleep(bool enable);
609 
617  bool setSleepDuration(SLEEP_DUR_T dur);
618 
625  bool setLowGThreshold(uint8_t thresh);
626 
633  bool setHighGThreshold(uint8_t thresh);
634 
641  bool setLowGHysteresis(uint8_t hyst);
642 
649  bool setLowGDuration(uint8_t dur);
650 
657  bool setHighGHysteresis(uint8_t hyst);
658 
665  bool setHighGDuration(uint8_t dur);
666 
673  bool setTapDuration(uint8_t dur);
674 
681  bool setTapThreshold(uint8_t thresh);
682 
689  bool enableTapFilter(bool filt);
690 
697  bool setSlopeDuration(uint8_t dur);
698 
705  bool setSlopeThreshold(uint8_t thresh);
706 
713  bool enableSlopeFilter(bool filt);
714 
720  uint8_t getInterruptStatus1();
721 
728 
734  uint8_t getInterruptStatus2();
735 
742  bool setInterruptEnables1(uint8_t bits);
743 
749  uint8_t getInterruptEnables1();
750 
757  bool setInterruptEnables2(uint8_t bits);
758 
764  uint8_t getInterruptEnables2();
765 
773 
782  bool resetInterrupts();
783 
784 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
785  void installISR(int gpio, mraa::Edge level, IsrCallback *cb);
786 #else
787 
797  void installISR(int gpio, mraa::Edge level,
798  void (*isr)(void *), void *arg);
799 #endif
800 
805  void uninstallISR();
806 
807  protected:
808  mraa::I2c m_i2c;
809  mraa::Gpio *m_gpioIntr;
810  uint8_t m_addr;
811 
812  // uncompensated accelerometer values
813  float m_accelX;
814  float m_accelY;
815  float m_accelZ;
816 
817  // accelerometer full scale
818  float m_accelScale;
819 
820  private:
824  void updateAccelerometer();
825  // Adding a private function definition for java bindings
826 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
827  void installISR(int gpio, mraa::Edge level,
828  void (*isr)(void *), void *arg);
829 #endif
830 
831  };
832 }
833 
834 
uint8_t readReg(uint8_t reg)
Definition: bma220.cxx:97
uint8_t softReset()
Definition: bma220.cxx:239
bool enableTapFilter(bool filt)
Definition: bma220.cxx:371
uint8_t getInterruptEnables1()
Definition: bma220.cxx:447
REG_T
Definition: bma220.h:85
bool setTapDuration(uint8_t dur)
Definition: bma220.cxx:345
uint8_t getInterruptStatus2()
Definition: bma220.cxx:437
bool resetInterrupts()
Definition: bma220.cxx:481
SLOPE_CONFIG_BITS_T
Definition: bma220.h:203
void getAccelerometer(float *x, float *y, float *z)
Definition: bma220.cxx:158
uint8_t getInterruptEnables2()
Definition: bma220.cxx:465
bool setAccelerometerScale(FSL_RANGE_T scale)
Definition: bma220.cxx:115
FSL_RANGE_T
Definition: bma220.h:447
THRESHOLD_BITS_T
Definition: bma220.h:145
bool setSleepDuration(SLEEP_DUR_T dur)
Definition: bma220.cxx:256
bool setInterruptEnables1(uint8_t bits)
Definition: bma220.cxx:442
bool setInterruptEnables2(uint8_t bits)
Definition: bma220.cxx:452
bool setSlopeThreshold(uint8_t thresh)
Definition: bma220.cxx:396
bool setLowGThreshold(uint8_t thresh)
Definition: bma220.cxx:267
H_HYST_DUR_HIGH_BITS_T
Definition: bma220.h:126
TAP_SAMP_T
Definition: bma220.h:242
void uninstallISR()
Definition: bma220.cxx:513
SLEEP_DUR_T
Definition: bma220.h:382
uint8_t getInterruptStatus1()
Definition: bma220.cxx:421
I2C_WATCHDOG_BITS_T
Definition: bma220.h:469
Definition: a110x.h:33
uint8_t getChipRevision()
Definition: bma220.cxx:184
ENABLE_CONFIG2_BITS_T
Definition: bma220.h:325
L_HYST_DUR_LOW_BITS_T
Definition: bma220.h:164
bool setHighGThreshold(uint8_t thresh)
Definition: bma220.cxx:280
void update()
Definition: bma220.cxx:73
SBIST_T
Definition: bma220.h:459
TAP_CONFIG2_BITS_T
Definition: bma220.h:223
bool enableSlopeFilter(bool filt)
Definition: bma220.cxx:409
uint8_t suspend()
Definition: bma220.cxx:234
CONFIG_ORIENT_T getOrient()
Definition: bma220.cxx:426
CONFIG2_LAT_T
Definition: bma220.h:346
bool setFilterConfig(FILTER_CONFIG_T filter)
Definition: bma220.cxx:189
ENABLE_CONFIG3_BITS_T
Definition: bma220.h:360
void installISR(int gpio, mraa::Edge level, void(*isr)(void *), void *arg)
Definition: bma220.cxx:500
INT_STATUS1_BITS_T
Definition: bma220.h:263
INT_STATUS2_BITS_T
Definition: bma220.h:298
bool enableAxes(bool xEn, bool yEn, bool zEn)
Definition: bma220.cxx:212
bool setInterruptLatch(CONFIG2_LAT_T lat)
Definition: bma220.cxx:470
bool writeReg(uint8_t reg, uint8_t val)
Definition: bma220.cxx:102
FILTER_CONFIG_T
Definition: bma220.h:414
bool setHighGDuration(uint8_t dur)
Definition: bma220.cxx:332
bool sleep(bool enable)
Definition: bma220.cxx:244
FILTER_CONFIG_BITS_T
Definition: bma220.h:396
API for the BMA220 3-axis Accelerometer.
Definition: bma220.h:73
bool setSlopeDuration(uint8_t dur)
Definition: bma220.cxx:383
ENABLE_CONFIG_BITS_T
Definition: bma220.h:311
TAP_ORIENT_BLOCK_T
Definition: bma220.h:253
TAP_CONFIG_BITS_T
Definition: bma220.h:183
uint8_t getChipID()
Definition: bma220.cxx:179
SBIST_FSL_CONFIG_BITS_T
Definition: bma220.h:426
CONFIG_ORIENT_T
Definition: bma220.h:284
bool setHighGHysteresis(uint8_t hyst)
Definition: bma220.cxx:319
bool setLowGDuration(uint8_t dur)
Definition: bma220.cxx:306
bool setTapThreshold(uint8_t thresh)
Definition: bma220.cxx:358
BMA220(int bus=BMA220_I2C_BUS, uint8_t addr=BMA220_DEFAULT_ADDR)
Definition: bma220.cxx:37
bool setLowGHysteresis(uint8_t hyst)
Definition: bma220.cxx:293
bool setSerialHighBW(bool high)
Definition: bma220.cxx:200
~BMA220()
Definition: bma220.cxx:68