upm  0.3.1
Sensor/Actuator repository for libmraa (v0.7.2)
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 
57  class H3LIS331DL {
58  public:
59 
63  typedef enum {
64  // Reserved bytes must not be written into as they contain
65  // factory calibration data. Changing those values may lead to
66  // improper functioning of the device.
67 
68  // 0x00-0x0E reserved
69 
70  REG_WHOAMI = 0x0f,
71 
72  // 0x10-0x1f reserved
73 
74  REG_REG1 = 0x20,
75  REG_REG2 = 0x21,
76  REG_REG3 = 0x22,
77  REG_REG4 = 0x23,
78  REG_REG5 = 0x24,
79 
80  REG_HP_FILTER_RESET = 0x25,
81  REG_REFERENCE = 0x26,
82 
83  REG_STATUS = 0x27,
84 
85  REG_OUT_X_L = 0x28,
86  REG_OUT_X_H = 0x29,
87  REG_OUT_Y_L = 0x2a,
88  REG_OUT_Y_H = 0x2b,
89  REG_OUT_Z_L = 0x2c,
90  REG_OUT_Z_H = 0x2d,
91 
92  // 0x2e, 0x2f reserved
93 
94  REG_INT1_CFG = 0x30,
95  REG_INT1_SRC = 0x31,
96  REG_INT1_THS = 0x32,
97  REG_INT1_DUR = 0x33,
98 
99  REG_INT2_CFG = 0x34,
100  REG_INT2_SRC = 0x35,
101  REG_INT2_THS = 0x36,
102  REG_INT2_DUR = 0x37,
103 
104  // 0x38-0x3f reserved
106 
110  typedef enum {
111  REG1_XEN = 0x01, // X axis enable
112  REG1_YEN = 0x02,
113  REG1_ZEN = 0x04,
114 
115  REG1_DR0 = 0x08, // data rate
116  REG1_DR1 = 0x10,
117  REG1_DR_SHIFT = 3, // DR shift
118 
119  REG1_PM0 = 0x20, // power mode
120  REG1_PM1 = 0x40,
121  REG1_PM2 = 0x80,
122  REG1_PM_SHIFT = 5
123  } REG1_BITS_T;
124 
128  typedef enum {
129  DR_50_37 = 0x0, // 50Hz output with 37Hz LPF cutoff
130  DR_100_74 = 0x1,
131  DR_400_292 = 0x2,
132  DR_1000_780 = 0x3
133  } DR_BITS_T;
134 
138  typedef enum {
139  PM_POWERDWN = 0x0,
140  PM_NORMAL = 0x1,
141  PM_LP05 = 0x2, // .5 updates/sec
142  PM_LP1 = 0x3, // 1 update/sec
143  PM_LP2 = 0x4,
144  PM_LP5 = 0x5,
145  PM_LP10 = 0x6
146  } PM_BITS_T;
147 
151  typedef enum {
152  REG2_HPCF0 = 0x01,
153  REG2_HPCF1 = 0x02,
154  REG2_HPCF_SHIFT = 0,
155 
156  REG2_HPEN1 = 0x04,
157  REG2_HPEN2 = 0x08,
158  REG2_FDS = 0x10,
159 
160  REG2_HPM0 = 0x20,
161  REG2_HPM1 = 0x40,
162  REG2_HPM_SHIFT = 5,
163 
164  REG2_BOOT = 0x80
165  } REG2_BITS_T;
166 
170  typedef enum {
171  HPCF_8 = 0x0,
172  HPCF_16 = 0x1,
173  HPCF_32 = 0x2,
174  HPCF_64 = 0x3,
175  } HPCF_BITS_T;
176 
180  typedef enum {
181  HPM_NORMAL0 = 0x0,
182  HPM_REF = 0x1,
183  HPM_NORMAL1 = 0x2
184  } HPM_BITS_T;
185 
189  typedef enum {
190  REG3_I1_CFG0 = 0x01,
191  REG3_I1_CFG1 = 0x02,
192  REG3_I1_CFG_SHIFT = 0,
193 
194  REG3_LIR1 = 0x04,
195 
196  REG3_I2_CFG0 = 0x08,
197  REG3_I2_CFG1 = 0x10,
198  REG3_I2_CFG_SHIFT = 3,
199 
200  REG3_LIR2 = 0x20,
201  REG3_PP_OD = 0x40,
202  REG3_IHL = 0x80
203  } REG3_BITS_T;
204 
208  typedef enum {
209  I_SRC = 0x0, // INT source
210  I_OR = 0x1, // INT1 OR INT2 source
211  I_DR = 0x2, // Data Ready
212  I_BOOTING = 0x3 // Boot is running
213  } I_CFG_BITS_T;
214 
218  typedef enum {
219  REG4_SIM = 0x01, // SPI 4 or 3 wire
220 
221  // bits 01,02,04 reserved
222 
223  REG4_FS0 = 0x10,
224  REG4_FS1 = 0x20,
225  REG4_FS_SHIFT = 4,
226 
227  REG4_BLE = 0x40, // big/little endian
228  REG4_BDU = 0x80 // Block data update
229  } REG4_BITS_T;
230 
234  typedef enum {
235  FS_100 = 0x0, // 100g scale
236  FS_200 = 0x1, // 200g scale
237  FS_400 = 0x3 // 400g scale
238  } FS_BITS_T;
239 
243  typedef enum {
244  REG5_TURNON0 = 0x01, // turnon mode for sleep-to-wake
245  REG5_TURNON1 = 0x02
246 
247  // bits 04-80 reserved
248  } REG5_BITS_T;
249 
253  typedef enum {
254  STATUS_XDA = 0x01, // X data available
255  STATUS_YDA = 0x02,
256  STATUS_ZDA = 0x04,
257  STATUS_ZYXDA = 0x08, // X, Y, and Z data available
258  STATUS_XOR = 0x10, // X overrun
259  STATUS_YOR = 0x20,
260  STATUS_ZOR = 0x40,
261  STATUS_ZYXOR = 0x80 // X, Y, and Z data overrun
262  } STATUS_BITS_T;
263 
267  typedef enum {
268  INT_CFG_XLIE = 0x01, // enable intr on low X event
269  INT_CFG_XHIE = 0x02, // enable intr on high X event
270  INT_CFG_YLIE = 0x04,
271  INT_CFG_YHIE = 0x08,
272  INT_CFG_ZLIE = 0x10,
273  INT_CFG_ZHIE = 0x20,
274  // 0x40 reserved
275  INT_CFG_AOI = 0x80 // AND or OR combination or intrs
276  } INT_CFG_BITS_T;
277 
281  typedef enum {
282  INT_SRC_XL = 0x01, // X low intr event
283  INT_SRC_XH = 0x02, // X high intr event
284  INT_SRC_YL = 0x04,
285  INT_SRC_YH = 0x08,
286  INT_SRC_ZL = 0x10,
287  INT_SRC_ZH = 0x20,
288  INT_SRC_IA = 0x40 // Interrupt generated (active)
289  // 0x80 reserved
290  } INT_SRC_BITS_T;
291 
298  H3LIS331DL(int bus, uint8_t address = H3LIS331DL_DEFAULT_I2C_ADDR);
299 
303  ~H3LIS331DL();
304 
313  bool init(DR_BITS_T odr=DR_50_37, PM_BITS_T pm=PM_NORMAL,
314  FS_BITS_T fs=FS_100);
315 
321  uint8_t getChipID();
322 
329  bool setDataRate(DR_BITS_T odr);
330 
337  bool setPowerMode(PM_BITS_T pm);
338 
348  bool enableAxis(uint8_t axisEnable);
349 
356  bool setFullScale(FS_BITS_T fs);
357 
364  bool setHPCF(HPCF_BITS_T val);
365 
372  bool setHPM(HPM_BITS_T val);
373 
382  bool boot();
383 
390  bool enableHPF1(bool enable);
391 
398  bool enableHPF2(bool enable);
399 
406  bool enableFDS(bool enable);
407 
414  bool setInterruptActiveLow(bool enable);
415 
422  bool setInterruptOpenDrain(bool enable);
423 
430  bool setInterrupt1Latch(bool enable);
431 
438  bool setInterrupt2Latch(bool enable);
439 
447 
455 
464  bool enableBDU(bool enable);
465 
472  bool enableBLE(bool enable);
473 
480  bool enableSleepToWake(bool enable);
481 
487  uint8_t getStatus();
488 
495  bool setInterrupt1Config(uint8_t val);
496 
503  bool setInterrupt2Config(uint8_t val);
504 
511  bool setInterrupt1Source(uint8_t val);
512 
519  bool setInterrupt2Source(uint8_t val);
520 
527  bool setInterrupt1Threshold(uint8_t val);
528 
535  bool setInterrupt2Threshold(uint8_t val);
536 
543  bool setInterrupt1Duration(uint8_t val);
544 
551  bool setInterrupt2Duration(uint8_t val);
552 
556  void update();
557 
567  void setAdjustmentOffsets(int adjX, int adjY, int adjZ);
568 
576  void getAcceleration(float *aX, float *aY, float *aZ);
577 
585  void getRawXYZ(int *x, int *y, int *z);
586 
594  void getXYZ(int *x, int *y, int *z);
595 
602  mraa::I2c& i2cContext() { return m_i2c; };
603 
604 
605  protected:
606  int16_t m_rawX, m_rawY, m_rawZ;
607  int16_t m_adjX, m_adjY, m_adjZ;
608  mraa::I2c m_i2c;
609 
610  private:
611  uint8_t m_addr;
612  };
613 }
614 
615 
uint8_t getStatus()
Definition: h3lis331dl.cxx:408
REG3_BITS_T
Definition: h3lis331dl.h:189
REG1_BITS_T
Definition: h3lis331dl.h:110
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:281
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:218
bool setHPCF(HPCF_BITS_T val)
Definition: h3lis331dl.cxx:142
REG2_BITS_T
Definition: h3lis331dl.h:151
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:63
DR_BITS_T
Definition: h3lis331dl.h:128
PM_BITS_T
Definition: h3lis331dl.h:138
REG5_BITS_T
Definition: h3lis331dl.h:243
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:170
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:602
API for the H3LIS331DL based Grove 3-axis I2C (400G)
Definition: h3lis331dl.h:57
void getXYZ(int *x, int *y, int *z)
Definition: h3lis331dl.cxx:564
HPM_BITS_T
Definition: h3lis331dl.h:180
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:267
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:253
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:208
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:234