upm  0.5.0
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
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 #define BMA220_I2C_BUS 0
32 #define BMA220_DEFAULT_ADDR 0x0a
33 
34 namespace upm {
35 
69  class BMA220 {
70  public:
71 
72  // NOTE: reserved registers must not be written into or read from.
73  // Reserved bitfields must always be 0. The registers aren't
74  // named in the datasheet, so I made up some hopefully useful
75  // names for them. The whole I2C register map design is a little
76  // strange, IMO.
77 
81  typedef enum {
82  REG_CHIPID = 0x00,
83  REG_REVISIONID = 0x02,
84 
85  // 2 lsb bits of ACC regs are always 0, yeilding 6 bits resolution
86  REG_ACC_X = 0x04, // acceleration data
87  REG_ACC_Y = 0x06,
88  REG_ACC_Z = 0x08,
89 
90  REG_H_HYST_DUR = 0x0a, // high hysteresis/dur
91  REG_THRESHOLD = 0x0c, // high/low threshold
92  REG_L_HYST_DUR = 0x0e, // low hysteresis/dur
93 
94  REG_TAP_CONFIG = 0x10,
95 
96  REG_SLOPE_CONFIG = 0x12,
97  REG_TAP_CONFIG2 = 0x14,
98 
99  REG_INT_STATUS1 = 0x16,
100  REG_INT_STATUS2 = 0x18,
101 
102  REG_ENABLE_CONFIG = 0x1a,
103  REG_ENABLE_CONFIG2 = 0x1c,
104  REG_ENABLE_CONFIG3 = 0x1e,
105 
106  REG_FILTER_CONFIG = 0x20,
107 
108  REG_SBIST_FSL_CONFIG = 0x22, // self test and full scale range
109 
110  // 0x24 - 0x2c reserved
111 
112  REG_I2C_WATCHDOG = 0x2e, // + SPI 3-wire mode
113 
114  REG_SUSPEND = 0x30,
115  REG_SOFTRESET = 0x32
116  } REG_T;
117 
118 
122  typedef enum {
123  H_HYST_DUR_HIGH_DUR0 = 0x01,
124  H_HYST_DUR_HIGH_DUR1 = 0x02,
125  H_HYST_DUR_HIGH_DUR2 = 0x04,
126  H_HYST_DUR_HIGH_DUR3 = 0x08,
127  H_HYST_DUR_HIGH_DUR4 = 0x10,
128  H_HYST_DUR_HIGH_DUR5 = 0x20,
129  _H_HYST_DUR_HIGH_DUR_MASK = 63,
130  _H_HYST_DUR_HIGH_DUR_SHIFT = 0,
131 
132  H_HYST_DUR_HIGH_HY1 = 0x40,
133  H_HYST_DUR_HIGH_HY2 = 0x80,
134  _H_HYST_DUR_HIGH_HY_MASK = 3,
135  _H_HYST_DUR_HIGH_HY_SHIFT = 6
137 
141  typedef enum {
142  THRESHOLD_HIGH0 = 0x01,
143  THRESHOLD_HIGH1 = 0x02,
144  THRESHOLD_HIGH2 = 0x04,
145  THRESHOLD_HIGH3 = 0x08,
146  _THRESHOLD_HIGH_MASK = 15,
147  _THRESHOLD_HIGH_SHIFT = 0,
148 
149  THRESHOLD_LOW0 = 0x10,
150  THRESHOLD_LOW1 = 0x20,
151  THRESHOLD_LOW2 = 0x40,
152  THRESHOLD_LOW3 = 0x80,
153  _THRESHOLD_LOW_MASK = 15,
154  _THRESHOLD_LOW_SHIFT = 4
156 
160  typedef enum {
161  L_HYST_DUR_LOW_DUR0 = 0x01,
162  L_HYST_DUR_LOW_DUR1 = 0x02,
163  L_HYST_DUR_LOW_DUR2 = 0x04,
164  L_HYST_DUR_LOW_DUR3 = 0x08,
165  L_HYST_DUR_LOW_DUR4 = 0x10,
166  L_HYST_DUR_LOW_DUR5 = 0x20,
167  _L_HYST_DUR_LOW_DUR_MASK = 63,
168  _L_HYST_DUR_LOW_DUR_SHIFT = 0,
169 
170  L_HYST_DUR_LOW_HY1 = 0x40,
171  L_HYST_DUR_LOW_HY2 = 0x80,
172  _L_HYST_DUR_LOW_HY_MASK = 3,
173  _L_HYST_DUR_LOW_HY_SHIFT = 6
175 
179  typedef enum {
180  TAP_CONFIG_DUR0 = 0x01,
181  TAP_CONFIG_DUR1 = 0x02,
182  TAP_CONFIG_DUR2 = 0x04,
183  _TAP_CONFIG_DUR_MASK = 7,
184  _TAP_CONFIG_DUR_SHIFT = 0,
185 
186  TAP_CONFIG_THRESH0 = 0x08,
187  TAP_CONFIG_THRESH1 = 0x10,
188  TAP_CONFIG_THRESH2 = 0x20,
189  TAP_CONFIG_THRESH3 = 0x40,
190  _TAP_CONFIG_THRESH_MASK = 15,
191  _TAP_CONFIG_THRESH_SHIFT = 3,
192 
193  TAP_CONFIG_FILTER = 0x80
195 
199  typedef enum {
200  SLOPE_CONFIG_DUR0 = 0x01,
201  SLOPE_CONFIG_DUR1 = 0x02,
202  _SLOPE_CONFIG_DUR_MASK = 3,
203  _SLOPE_CONFIG_DUR_SHIFT = 0,
204 
205  SLOPE_CONFIG_THRESH0 = 0x04,
206  SLOPE_CONFIG_THRESH1 = 0x08,
207  SLOPE_CONFIG_THRESH2 = 0x10,
208  SLOPE_CONFIG_THRESH3 = 0x20,
209  _SLOPE_CONFIG_THRESH_MASK = 15,
210  _SLOPE_CONFIG_THRESH_SHIFT = 2,
211 
212  SLOPE_CONFIG_FILTER = 0x40,
213  SLOPE_CONFIG_ORIENT_EX = 0x80 // exchange x and z axis for orient
215 
219  typedef enum {
220  TAP_CONFIG2_SAMP0 = 0x01,
221  TAP_CONFIG2_SAMP1 = 0x02,
222  _TAP_CONFIG2_SAMP_MASK = 3,
223  _TAP_CONFIG2_SAMP_SHIFT = 0,
224 
225  TAP_CONFIG2_ORIENT_BLOCK0 = 0x04,
226  TAP_CONFIG2_ORIENT_BLOCK1 = 0x08,
227  _TAP_CONFIG2_ORIENT_BLOCK_MASK = 3,
228  _TAP_CONFIG2_ORIENT_BLOCK_SHIFT = 2,
229 
230  TAP_CONFIG2_TIP_EN = 0x10
231 
232  // 0x20-0x80 reserved
234 
238  typedef enum {
239  TAP_SAMP_2 = 0, // 2 data samples after wakeup
240  TAP_SAMP_4 = 1,
241  TAP_SAMP_8 = 2,
242  TAP_SAMP_16 = 3
243  } TAP_SAMP_T;
244 
249  typedef enum {
250  TAP_ORIENT_BLOCK_0 = 0, // orient blocking disabled
251  TAP_ORIENT_BLOCK_2 = 1, // |z|>0.9g OR |x|+|y| < 0.2g OR m<0.2g
252  TAP_ORIENT_BLOCK_3 = 2, // |z|>0.9g OR |x|+|y| < 0.3g OR m<0.3g
253  TAP_ORIENT_BLOCK_4 = 3, // |z|>0.9g OR |x|+|y| < 0.4g OR m<0.4g
255 
259  typedef enum {
260  INT_STATUS1_SIGN = 0x01,
261  INT_STATUS1_FIRST_Z = 0x02,
262  INT_STATUS1_FIRST_Y = 0x04,
263  INT_STATUS1_FIRST_X = 0x08,
264 
265  INT_STATUS1_ORIENT0 = 0x10,
266  INT_STATUS1_ORIENT1 = 0x20,
267  INT_STATUS1_ORIENT2 = 0x40,
268  _INT_STATUS1_ORIENT_MASK = 7,
269  _INT_STATUS1_ORIENT_SHIFT = 4,
270 
271  INT_STATUS1_ORIENT_INT = 0x80 // orient intr was generated
273 
280  typedef enum {
281  CONFIG_ORI_UP_PORT_UPRIGHT = 0, // up portrait
282  CONFIG_ORI_UP_PORT_UPSIDE_DOWN = 1,
283  CONFIG_ORI_UP_LAND_LEFT = 2, // landscape
284  CONFIG_ORI_UP_LAND_RIGHT = 3,
285  CONFIG_ORI_DN_PORT_UPRIGHT = 4, // down portrait
286  CONFIG_ORI_DN_PORT_UPSIDE_DOWN = 5,
287  CONFIG_ORI_DN_LAND_LEFT = 6, // landscape
288  CONFIG_ORI_DN_LAND_RIGHT = 7
289  } CONFIG_ORIENT_T;
290 
294  typedef enum {
295  INT_STATUS2_SLOPE = 0x01,
296  INT_STATUS2_DATA = 0x02,
297  INT_STATUS2_HIGH = 0x04,
298  INT_STATUS2_LOW = 0x08,
299  INT_STATUS2_TAP = 0x10
300 
301  // 0x20-0x80 reserved
303 
307  typedef enum {
308  ENABLE_CONFIG_TT_Z = 0x01,
309  ENABLE_CONFIG_TT_Y = 0x02,
310  ENABLE_CONFIG_TT_X = 0x04,
311  ENABLE_CONFIG_SLOPE_Z = 0x08,
312  ENABLE_CONFIG_SLOPE_Y = 0x10,
313  ENABLE_CONFIG_SLOPE_X = 0x20,
314  ENABLE_CONFIG_ORIENT = 0x40,
315  ENABLE_CONFIG_DATA = 0x80
317 
321  typedef enum {
322  ENABLE_CONFIG2_HIGH_Z = 0x01,
323  ENABLE_CONFIG2_HIGH_Y = 0x02,
324  ENABLE_CONFIG2_HIGH_X = 0x04,
325 
326  ENABLE_CONFIG2_LOW = 0x08,
327 
328  ENABLE_CONFIG2_LAT_INT0 = 0x10, // interrupt latching
329  ENABLE_CONFIG2_LAT_INT1 = 0x20,
330  ENABLE_CONFIG2_LAT_INT2 = 0x40,
331  _ENABLE_CONFIG2_LAT_INT_MASK = 7,
332  _ENABLE_CONFIG2_LAT_INT_SHIFT = 4,
333 
334  ENABLE_CONFIG2_RESET_INT = 0x80 // reset interrupts
336 
342  typedef enum {
343  CONFIG2_LAT_UNLATCH = 0, // unlatched intrs
344  CONFIG2_LAT_0_25 = 1, // latch intr for 0.25s
345  CONFIG2_LAT_0_5 = 2, // latch intr for 0.5s
346  CONFIG2_LAT_1 = 3, // latch intr for 1s
347  CONFIG2_LAT_2 = 4, // latch intr for 2s
348  CONFIG2_LAT_4 = 5, // latch intr for 4s
349  CONFIG2_LAT_8 = 6, // latch intr for 8s
350  CONFIG2_LAT_PERM = 7 // latch permanently
351  } CONFIG2_LAT_T;
352 
356  typedef enum {
357  ENABLE_CONFIG3_Z_CHAN = 0x01,
358  ENABLE_CONFIG3_Y_CHAN = 0x02,
359  ENABLE_CONFIG3_X_CHAN = 0x04,
360 
361  ENABLE_CONFIG3_SLEEP_DUR0 = 0x08,
362  ENABLE_CONFIG3_SLEEP_DUR1 = 0x10,
363  ENABLE_CONFIG3_SLEEP_DUR2 = 0x20,
364  _ENABLE_CONFIG3_SLEEP_DUR_MASK = 7,
365  _ENABLE_CONFIG3_SLEEP_DUR_SHIFT = 3,
366 
367  ENABLE_CONFIG3_SLEEP_EN = 0x40
368 
369  // 0x80 reserved
371 
378  typedef enum {
379  SLEEP_DUR_2MS = 0, // 2 ms
380  SLEEP_DUR_10MS = 1,
381  SLEEP_DUR_25MS = 2,
382  SLEEP_DUR_50MS = 3,
383  SLEEP_DUR_100MS = 4,
384  SLEEP_DUR_500MS = 5,
385  SLEEP_DUR_1S = 6, // 1 second
386  SLEEP_DUR_2S = 7
387  } SLEEP_DUR_T;
388 
392  typedef enum {
393  FILTER_CONFIG_FILTER0 = 0x01,
394  FILTER_CONFIG_FILTER1 = 0x02,
395  FILTER_CONFIG_FILTER2 = 0x04,
396  FILTER_CONFIG_FILTER3 = 0x08,
397  _FILTER_CONFIG_FILTER_MASK = 15,
398  _FILTER_CONFIG_FILTER_SHIFT = 0,
399 
400  // 0x10-0x40 reserved
401 
402  FILTER_CONFIG_SERIAL_HIGH_BW = 0x80
404 
410  typedef enum {
411  FILTER_CONFIG_1KHZ = 0, // 1Khz
412  FILTER_CONFIG_500HZ = 1,
413  FILTER_CONFIG_250HZ = 2,
414  FILTER_CONFIG_125HZ = 3,
415  FILTER_CONFIG_64HZ = 4,
416  FILTER_CONFIG_32HZ = 5
417  } FILTER_CONFIG_T;
418 
422  typedef enum {
423  REG_SBIST_FSL_RANGE0 = 0x01, // full scale range
424  REG_SBIST_FSL_RANGE1 = 0x02,
425  _REG_SBIST_FSL_RANGE_MASK = 3,
426  _REG_SBIST_FSL_RANGE_SHIFT = 0,
427 
428  REG_SBIST_FSL_SBIST0 = 0x04, // self test enables
429  REG_SBIST_FSL_SBIST1 = 0x08,
430  _REG_SBIST_FSL_SBIST_MASK = 3,
431  _REG_SBIST_FSL_SBIST_SHIFT = 2,
432 
433  REG_SBIST_FSL_SBIST_SIGN = 0x10 // signedness of self test
434 
435  // 0x20-0x80 reserved
437 
443  typedef enum {
444  FSL_RANGE_2G = 0, // 2G FSL
445  FSL_RANGE_4G = 1,
446  FSL_RANGE_8G = 2,
447  FSL_RANGE_16G = 3
448  } FSL_RANGE_T;
449 
455  typedef enum {
456  SBIST_OFF = 0, // self test off
457  SBIST_X = 1, // self test X
458  SBIST_Y = 2,
459  SBIST_Z = 3
460  } SBIST_T;
461 
465  typedef enum {
466  I2C_WATCHDOG_SPI3 = 0x01, // SPI 3wire mode (SPI not supported)
467 
468  I2C_WATCHDOG_TO_SEL = 0x02,
469  I2C_WATCHDOG_TO_EN = 0x04
470 
471  // 0x08-0x80 reserved
473 
474 
481  BMA220(int bus=BMA220_I2C_BUS, uint8_t addr=BMA220_DEFAULT_ADDR);
482 
486  ~BMA220();
487 
491  void update();
492 
500  uint8_t readReg(uint8_t reg);
501 
510  bool writeReg(uint8_t reg, uint8_t val);
511 
517  uint8_t getChipID();
518 
524  uint8_t getChipRevision();
525 
533 
541  void getAccelerometer(float *x, float *y, float *z);
542 
543 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
544 
549  float *getAccelerometer();
550 #endif
551 
558  bool setFilterConfig(FILTER_CONFIG_T filter);
559 
568  bool setSerialHighBW(bool high);
569 
578  bool enableAxes(bool xEn, bool yEn, bool zEn);
579 
586  uint8_t suspend();
587 
594  uint8_t softReset();
595 
604  bool sleep(bool enable);
605 
613  bool setSleepDuration(SLEEP_DUR_T dur);
614 
621  bool setLowGThreshold(uint8_t thresh);
622 
629  bool setHighGThreshold(uint8_t thresh);
630 
637  bool setLowGHysteresis(uint8_t hyst);
638 
645  bool setLowGDuration(uint8_t dur);
646 
653  bool setHighGHysteresis(uint8_t hyst);
654 
661  bool setHighGDuration(uint8_t dur);
662 
669  bool setTapDuration(uint8_t dur);
670 
677  bool setTapThreshold(uint8_t thresh);
678 
685  bool enableTapFilter(bool filt);
686 
693  bool setSlopeDuration(uint8_t dur);
694 
701  bool setSlopeThreshold(uint8_t thresh);
702 
709  bool enableSlopeFilter(bool filt);
710 
716  uint8_t getInterruptStatus1();
717 
724 
730  uint8_t getInterruptStatus2();
731 
738  bool setInterruptEnables1(uint8_t bits);
739 
745  uint8_t getInterruptEnables1();
746 
753  bool setInterruptEnables2(uint8_t bits);
754 
760  uint8_t getInterruptEnables2();
761 
769 
778  bool resetInterrupts();
779 
780 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
781  void installISR(int gpio, mraa::Edge level, jobject runnable);
782 #else
783 
793  void installISR(int gpio, mraa::Edge level,
794  void (*isr)(void *), void *arg);
795 #endif
796 
801  void uninstallISR();
802 
803  protected:
804  mraa::I2c m_i2c;
805  mraa::Gpio *m_gpioIntr;
806  uint8_t m_addr;
807 
808  // uncompensated accelerometer values
809  float m_accelX;
810  float m_accelY;
811  float m_accelZ;
812 
813  // accelerometer full scale
814  float m_accelScale;
815 
816  private:
820  void updateAccelerometer();
821  // Adding a private function definition for java bindings
822 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
823  void installISR(int gpio, mraa::Edge level,
824  void (*isr)(void *), void *arg);
825 #endif
826 
827  };
828 }
829 
830 
uint8_t readReg(uint8_t reg)
Definition: bma220.cxx:97
uint8_t softReset()
Definition: bma220.cxx:239
SBIST_FSL_CONFIG_BITS_T
Definition: bma220.h:422
bool enableTapFilter(bool filt)
Definition: bma220.cxx:371
FILTER_CONFIG_BITS_T
Definition: bma220.h:392
uint8_t getInterruptEnables1()
Definition: bma220.cxx:447
bool setTapDuration(uint8_t dur)
Definition: bma220.cxx:345
uint8_t getInterruptStatus2()
Definition: bma220.cxx:437
bool resetInterrupts()
Definition: bma220.cxx:481
TAP_SAMP_T
Definition: bma220.h:238
THRESHOLD_BITS_T
Definition: bma220.h:141
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
L_HYST_DUR_LOW_BITS_T
Definition: bma220.h:160
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
ENABLE_CONFIG3_BITS_T
Definition: bma220.h:356
SLOPE_CONFIG_BITS_T
Definition: bma220.h:199
void uninstallISR()
Definition: bma220.cxx:521
I2C_WATCHDOG_BITS_T
Definition: bma220.h:465
TAP_CONFIG2_BITS_T
Definition: bma220.h:219
uint8_t getInterruptStatus1()
Definition: bma220.cxx:421
uint8_t getChipRevision()
Definition: bma220.cxx:184
FSL_RANGE_T
Definition: bma220.h:443
bool setHighGThreshold(uint8_t thresh)
Definition: bma220.cxx:280
void update()
Definition: bma220.cxx:73
REG_T
Definition: bma220.h:81
bool enableSlopeFilter(bool filt)
Definition: bma220.cxx:409
uint8_t suspend()
Definition: bma220.cxx:234
CONFIG_ORIENT_T getOrient()
Definition: bma220.cxx:426
TAP_CONFIG_BITS_T
Definition: bma220.h:179
bool setFilterConfig(FILTER_CONFIG_T filter)
Definition: bma220.cxx:189
void installISR(int gpio, mraa::Edge level, void(*isr)(void *), void *arg)
Definition: bma220.cxx:507
SBIST_T
Definition: bma220.h:455
bool enableAxes(bool xEn, bool yEn, bool zEn)
Definition: bma220.cxx:212
ENABLE_CONFIG_BITS_T
Definition: bma220.h:307
INT_STATUS1_BITS_T
Definition: bma220.h:259
CONFIG2_LAT_T
Definition: bma220.h:342
FILTER_CONFIG_T
Definition: bma220.h:410
bool setInterruptLatch(CONFIG2_LAT_T lat)
Definition: bma220.cxx:470
bool writeReg(uint8_t reg, uint8_t val)
Definition: bma220.cxx:102
ENABLE_CONFIG2_BITS_T
Definition: bma220.h:321
bool setHighGDuration(uint8_t dur)
Definition: bma220.cxx:332
bool sleep(bool enable)
Definition: bma220.cxx:244
API for the BMA220 3-axis Accelerometer.
Definition: bma220.h:69
CONFIG_ORIENT_T
Definition: bma220.h:280
H_HYST_DUR_HIGH_BITS_T
Definition: bma220.h:122
bool setSlopeDuration(uint8_t dur)
Definition: bma220.cxx:383
TAP_ORIENT_BLOCK_T
Definition: bma220.h:249
uint8_t getChipID()
Definition: bma220.cxx:179
INT_STATUS2_BITS_T
Definition: bma220.h:294
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
SLEEP_DUR_T
Definition: bma220.h:378
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