upm  0.2.0
Sensor/Actuator repository for libmraa (v0.6.1)
h3lis331dl.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/i2c.hpp>
28 
29 #define H3LIS331DL_I2C_BUS 0
30 #define H3LIS331DL_DEFAULT_I2C_ADDR 0x18
31 
32 namespace upm {
33 
55  class H3LIS331DL {
56  public:
57 
61  typedef enum {
62  // Reserved bytes must not be written into as they contain
63  // factory calibration data. Changing those values may lead to
64  // improper functioning of the device.
65 
66  // 0x00-0x0E reserved
67 
68  REG_WHOAMI = 0x0f,
69 
70  // 0x10-0x1f reserved
71 
72  REG_REG1 = 0x20,
73  REG_REG2 = 0x21,
74  REG_REG3 = 0x22,
75  REG_REG4 = 0x23,
76  REG_REG5 = 0x24,
77 
78  REG_HP_FILTER_RESET = 0x25,
79  REG_REFERENCE = 0x26,
80 
81  REG_STATUS = 0x27,
82 
83  REG_OUT_X_L = 0x28,
84  REG_OUT_X_H = 0x29,
85  REG_OUT_Y_L = 0x2a,
86  REG_OUT_Y_H = 0x2b,
87  REG_OUT_Z_L = 0x2c,
88  REG_OUT_Z_H = 0x2d,
89 
90  // 0x2e, 0x2f reserved
91 
92  REG_INT1_CFG = 0x30,
93  REG_INT1_SRC = 0x31,
94  REG_INT1_THS = 0x32,
95  REG_INT1_DUR = 0x33,
96 
97  REG_INT2_CFG = 0x34,
98  REG_INT2_SRC = 0x35,
99  REG_INT2_THS = 0x36,
100  REG_INT2_DUR = 0x37,
101 
102  // 0x38-0x3f reserved
104 
108  typedef enum {
109  REG1_XEN = 0x01, // X axis enable
110  REG1_YEN = 0x02,
111  REG1_ZEN = 0x04,
112 
113  REG1_DR0 = 0x08, // data rate
114  REG1_DR1 = 0x10,
115  REG1_DR_SHIFT = 3, // DR shift
116 
117  REG1_PM0 = 0x20, // power mode
118  REG1_PM1 = 0x40,
119  REG1_PM2 = 0x80,
120  REG1_PM_SHIFT = 5
121  } REG1_BITS_T;
122 
126  typedef enum {
127  DR_50_37 = 0x0, // 50Hz output with 37Hz LPF cutoff
128  DR_100_74 = 0x1,
129  DR_400_292 = 0x2,
130  DR_1000_780 = 0x3
131  } DR_BITS_T;
132 
136  typedef enum {
137  PM_POWERDWN = 0x0,
138  PM_NORMAL = 0x1,
139  PM_LP05 = 0x2, // .5 updates/sec
140  PM_LP1 = 0x3, // 1 update/sec
141  PM_LP2 = 0x4,
142  PM_LP5 = 0x5,
143  PM_LP10 = 0x6
144  } PM_BITS_T;
145 
149  typedef enum {
150  REG2_HPCF0 = 0x01,
151  REG2_HPCF1 = 0x02,
152  REG2_HPCF_SHIFT = 0,
153 
154  REG2_HPEN1 = 0x04,
155  REG2_HPEN2 = 0x08,
156  REG2_FDS = 0x10,
157 
158  REG2_HPM0 = 0x20,
159  REG2_HPM1 = 0x40,
160  REG2_HPM_SHIFT = 5,
161 
162  REG2_BOOT = 0x80
163  } REG2_BITS_T;
164 
168  typedef enum {
169  HPCF_8 = 0x0,
170  HPCF_16 = 0x1,
171  HPCF_32 = 0x2,
172  HPCF_64 = 0x3,
173  } HPCF_BITS_T;
174 
178  typedef enum {
179  HPM_NORMAL0 = 0x0,
180  HPM_REF = 0x1,
181  HPM_NORMAL1 = 0x2
182  } HPM_BITS_T;
183 
187  typedef enum {
188  REG3_I1_CFG0 = 0x01,
189  REG3_I1_CFG1 = 0x02,
190  REG3_I1_CFG_SHIFT = 0,
191 
192  REG3_LIR1 = 0x04,
193 
194  REG3_I2_CFG0 = 0x08,
195  REG3_I2_CFG1 = 0x10,
196  REG3_I2_CFG_SHIFT = 3,
197 
198  REG3_LIR2 = 0x20,
199  REG3_PP_OD = 0x40,
200  REG3_IHL = 0x80
201  } REG3_BITS_T;
202 
206  typedef enum {
207  I_SRC = 0x0, // INT source
208  I_OR = 0x1, // INT1 OR INT2 source
209  I_DR = 0x2, // Data Ready
210  I_BOOTING = 0x3 // Boot is running
211  } I_CFG_BITS_T;
212 
216  typedef enum {
217  REG4_SIM = 0x01, // SPI 4 or 3 wire
218 
219  // bits 01,02,04 reserved
220 
221  REG4_FS0 = 0x10,
222  REG4_FS1 = 0x20,
223  REG4_FS_SHIFT = 4,
224 
225  REG4_BLE = 0x40, // big/little endian
226  REG4_BDU = 0x80 // Block data update
227  } REG4_BITS_T;
228 
232  typedef enum {
233  FS_100 = 0x0, // 100g scale
234  FS_200 = 0x1, // 200g scale
235  FS_400 = 0x3 // 400g scale
236  } FS_BITS_T;
237 
241  typedef enum {
242  REG5_TURNON0 = 0x01, // turnon mode for sleep-to-wake
243  REG5_TURNON1 = 0x02
244 
245  // bits 04-80 reserved
246  } REG5_BITS_T;
247 
251  typedef enum {
252  STATUS_XDA = 0x01, // X data available
253  STATUS_YDA = 0x02,
254  STATUS_ZDA = 0x04,
255  STATUS_ZYXDA = 0x08, // X, Y, and Z data available
256  STATUS_XOR = 0x10, // X overrun
257  STATUS_YOR = 0x20,
258  STATUS_ZOR = 0x40,
259  STATUS_ZYXOR = 0x80 // X, Y, and Z data overrun
260  } STATUS_BITS_T;
261 
265  typedef enum {
266  INT_CFG_XLIE = 0x01, // enable intr on low X event
267  INT_CFG_XHIE = 0x02, // enable intr on high X event
268  INT_CFG_YLIE = 0x04,
269  INT_CFG_YHIE = 0x08,
270  INT_CFG_ZLIE = 0x10,
271  INT_CFG_ZHIE = 0x20,
272  // 0x40 reserved
273  INT_CFG_AOI = 0x80 // AND or OR combination or intrs
274  } INT_CFG_BITS_T;
275 
279  typedef enum {
280  INT_SRC_XL = 0x01, // X low intr event
281  INT_SRC_XH = 0x02, // X high intr event
282  INT_SRC_YL = 0x04,
283  INT_SRC_YH = 0x08,
284  INT_SRC_ZL = 0x10,
285  INT_SRC_ZH = 0x20,
286  INT_SRC_IA = 0x40 // Interrupt generated (active)
287  // 0x80 reserved
288  } INT_SRC_BITS_T;
289 
296  H3LIS331DL(int bus, uint8_t address = H3LIS331DL_DEFAULT_I2C_ADDR);
297 
301  ~H3LIS331DL();
302 
311  bool init(DR_BITS_T odr=DR_50_37, PM_BITS_T pm=PM_NORMAL,
312  FS_BITS_T fs=FS_100);
313 
319  uint8_t getChipID();
320 
327  bool setDataRate(DR_BITS_T odr);
328 
335  bool setPowerMode(PM_BITS_T pm);
336 
346  bool enableAxis(uint8_t axisEnable);
347 
354  bool setFullScale(FS_BITS_T fs);
355 
362  bool setHPCF(HPCF_BITS_T val);
363 
370  bool setHPM(HPM_BITS_T val);
371 
380  bool boot();
381 
388  bool enableHPF1(bool enable);
389 
396  bool enableHPF2(bool enable);
397 
404  bool enableFDS(bool enable);
405 
412  bool setInterruptActiveLow(bool enable);
413 
420  bool setInterruptOpenDrain(bool enable);
421 
428  bool setInterrupt1Latch(bool enable);
429 
436  bool setInterrupt2Latch(bool enable);
437 
445 
453 
462  bool enableBDU(bool enable);
463 
470  bool enableBLE(bool enable);
471 
478  bool enableSleepToWake(bool enable);
479 
485  uint8_t getStatus();
486 
493  bool setInterrupt1Config(uint8_t val);
494 
501  bool setInterrupt2Config(uint8_t val);
502 
509  bool setInterrupt1Source(uint8_t val);
510 
517  bool setInterrupt2Source(uint8_t val);
518 
525  bool setInterrupt1Threshold(uint8_t val);
526 
533  bool setInterrupt2Threshold(uint8_t val);
534 
541  bool setInterrupt1Duration(uint8_t val);
542 
549  bool setInterrupt2Duration(uint8_t val);
550 
554  void update();
555 
565  void setAdjustmentOffsets(int adjX, int adjY, int adjZ);
566 
574  void getAcceleration(float *aX, float *aY, float *aZ);
575 
583  void getRawXYZ(int *x, int *y, int *z);
584 
592  void getXYZ(int *x, int *y, int *z);
593 
600  mraa::I2c& i2cContext() { return m_i2c; };
601 
602 
603  protected:
604  int16_t m_rawX, m_rawY, m_rawZ;
605  int16_t m_adjX, m_adjY, m_adjZ;
606  mraa::I2c m_i2c;
607 
608  private:
609  uint8_t m_addr;
610  };
611 }
612 
613 
uint8_t getStatus()
Definition: h3lis331dl.cxx:408
REG3_BITS_T
Definition: h3lis331dl.h:187
REG1_BITS_T
Definition: h3lis331dl.h:108
uint8_t getChipID()
Definition: h3lis331dl.cxx:73
H3LIS331DL(int bus, uint8_t address=H3LIS331DL_DEFAULT_I2C_ADDR)
Definition: h3lis331dl.cxx:36
bool setInterrupt1Source(uint8_t val)
Definition: h3lis331dl.cxx:429
bool setInterrupt1Duration(uint8_t val)
Definition: h3lis331dl.cxx:456
INT_SRC_BITS_T
Definition: h3lis331dl.h:279
bool setFullScale(FS_BITS_T fs)
Definition: h3lis331dl.cxx:126
bool setInterrupt2PadConfig(I_CFG_BITS_T val)
Definition: h3lis331dl.cxx:337
REG4_BITS_T
Definition: h3lis331dl.h:216
bool setHPCF(HPCF_BITS_T val)
Definition: h3lis331dl.cxx:142
REG2_BITS_T
Definition: h3lis331dl.h:149
bool setInterrupt2Source(uint8_t val)
Definition: h3lis331dl.cxx:483
bool setDataRate(DR_BITS_T odr)
Definition: h3lis331dl.cxx:78
bool enableBDU(bool enable)
Definition: h3lis331dl.cxx:354
bool enableHPF1(bool enable)
Definition: h3lis331dl.cxx:195
H3LIS331DL_REG_T
Definition: h3lis331dl.h:61
DR_BITS_T
Definition: h3lis331dl.h:126
PM_BITS_T
Definition: h3lis331dl.h:136
REG5_BITS_T
Definition: h3lis331dl.h:241
bool enableHPF2(bool enable)
Definition: h3lis331dl.cxx:213
void update()
Definition: h3lis331dl.cxx:521
void getAcceleration(float *aX, float *aY, float *aZ)
Definition: h3lis331dl.cxx:548
bool enableBLE(bool enable)
Definition: h3lis331dl.cxx:372
HPCF_BITS_T
Definition: h3lis331dl.h:168
void setAdjustmentOffsets(int adjX, int adjY, int adjZ)
Definition: h3lis331dl.cxx:541
bool setInterrupt2Threshold(uint8_t val)
Definition: h3lis331dl.cxx:499
bool setInterrupt1Latch(bool enable)
Definition: h3lis331dl.cxx:285
Definition: a110x.h:29
mraa::I2c & i2cContext()
Definition: h3lis331dl.h:600
C++ API for the H3LIS331DL based Grove 3-axis I2C Accelerometer (400G)
Definition: h3lis331dl.h:55
void getXYZ(int *x, int *y, int *z)
Definition: h3lis331dl.cxx:564
HPM_BITS_T
Definition: h3lis331dl.h:178
bool boot()
Definition: h3lis331dl.cxx:174
bool enableSleepToWake(bool enable)
Definition: h3lis331dl.cxx:390
bool setInterruptActiveLow(bool enable)
Definition: h3lis331dl.cxx:249
INT_CFG_BITS_T
Definition: h3lis331dl.h:265
bool setPowerMode(PM_BITS_T pm)
Definition: h3lis331dl.cxx:94
bool setInterrupt1Threshold(uint8_t val)
Definition: h3lis331dl.cxx:445
STATUS_BITS_T
Definition: h3lis331dl.h:251
bool setInterrupt2Duration(uint8_t val)
Definition: h3lis331dl.cxx:510
bool setInterrupt1Config(uint8_t val)
Definition: h3lis331dl.cxx:413
bool setInterrupt2Latch(bool enable)
Definition: h3lis331dl.cxx:303
bool setHPM(HPM_BITS_T val)
Definition: h3lis331dl.cxx:158
bool setInterrupt2Config(uint8_t val)
Definition: h3lis331dl.cxx:467
bool setInterruptOpenDrain(bool enable)
Definition: h3lis331dl.cxx:267
I_CFG_BITS_T
Definition: h3lis331dl.h:206
bool init(DR_BITS_T odr=DR_50_37, PM_BITS_T pm=PM_NORMAL, FS_BITS_T fs=FS_100)
Definition: h3lis331dl.cxx:57
bool enableAxis(uint8_t axisEnable)
Definition: h3lis331dl.cxx:110
~H3LIS331DL()
Definition: h3lis331dl.cxx:53
bool setInterrupt1PadConfig(I_CFG_BITS_T val)
Definition: h3lis331dl.cxx:321
bool enableFDS(bool enable)
Definition: h3lis331dl.cxx:231
void getRawXYZ(int *x, int *y, int *z)
Definition: h3lis331dl.cxx:557
FS_BITS_T
Definition: h3lis331dl.h:232