summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/ads1x15/ads1x15.h
blob: 8ff4fa34e834d5dc0d5de4496d4683b2bbba4424 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Author: Marc Graham <marc@m2ag.net>
 * Copyright (c) 2015 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#pragma once

#include <iostream>
#include <string>
#include "mraa.hpp"
#include "mraa/i2c.hpp"

/*=========================================================================
    I2C ADDRESS/BITS
    -----------------------------------------------------------------------*/
#define ADS1X15_ADDRESS                 (0x48)    // 1001 000 (ADDR = GND)
/*=========================================================================*/


/*=========================================================================
    POINTER REGISTER
    -----------------------------------------------------------------------*/
#define ADS1X15_REG_POINTER_MASK        (0x03)
#define ADS1X15_REG_POINTER_CONVERT     (0x00)
#define ADS1X15_REG_POINTER_CONFIG      (0x01)
#define ADS1X15_REG_POINTER_LOWTHRESH   (0x02)
#define ADS1X15_REG_POINTER_HITHRESH    (0x03)
/*=========================================================================*/

/*=========================================================================
    CONFIG REGISTER

    -----------------------------------------------------------------------*/
#define ADS1X15_OS_MASK      (0x8000)
#define ADS1X15_OS_SINGLE    (0x8000)  // Write: Set to start a single-conversion
#define ADS1X15_OS_BUSY      (0x0000)  // Read: Bit = 0 when conversion is in progress
#define ADS1X15_OS_NOTBUSY   (0x8000)  // Read: Bit = 1 when device is not performing a conversion

#define ADS1X15_MUX_MASK     (0x7000)
#define ADS1X15_MUX_DIFF_0_1 (0x0000)  // Differential P = AIN0, N = AIN1 (default)
#define ADS1X15_MUX_DIFF_0_3 (0x1000)  // Differential P = AIN0, N = AIN3
#define ADS1X15_MUX_DIFF_1_3 (0x2000)  // Differential P = AIN1, N = AIN3
#define ADS1X15_MUX_DIFF_2_3 (0x3000)  // Differential P = AIN2, N = AIN3
#define ADS1X15_MUX_SINGLE_0 (0x4000)  // Single-ended AIN0
#define ADS1X15_MUX_SINGLE_1 (0x5000)  // Single-ended AIN1
#define ADS1X15_MUX_SINGLE_2 (0x6000)  // Single-ended AIN2
#define ADS1X15_MUX_SINGLE_3 (0x7000)  // Single-ended AIN3

#define ADS1X15_PGA_MASK     (0x0E00)
#define ADS1X15_PGA_6_144V   (0x0000)  // +/-6.144V range = Gain 2/3
#define ADS1X15_PGA_4_096V   (0x0200)  // +/-4.096V range = Gain 1
#define ADS1X15_PGA_2_048V   (0x0400)  // +/-2.048V range = Gain 2 (default)
#define ADS1X15_PGA_1_024V   (0x0600)  // +/-1.024V range = Gain 4
#define ADS1X15_PGA_0_512V   (0x0800)  // +/-0.512V range = Gain 8
#define ADS1X15_PGA_0_256V   (0x0A00)  // +/-0.256V range = Gain 16

#define ADS1X15_MODE_MASK    (0x0100)
#define ADS1X15_MODE_CONTIN  (0x0000)  // Continuous conversion mode
#define ADS1X15_MODE_SINGLE  (0x0100)  // Power-down single-shot mode (default)

#define ADS1X15_DR_MASK      (0x00E0)

#define ADS1X15_CMODE_MASK   (0x0010)
#define ADS1X15_CMODE_TRAD   (0x0000)  // Traditional comparator with hysteresis (default)
#define ADS1X15_CMODE_WINDOW (0x0010)  // Window comparator

#define ADS1X15_CPOL_MASK    (0x0008)
#define ADS1X15_CPOL_ACTVLOW (0x0000)  // ALERT/RDY pin is low when active (default)
#define ADS1X15_CPOL_ACTVHI  (0x0008)  // ALERT/RDY pin is high when active

#define ADS1X15_CLAT_MASK    (0x0400)  // Determines if ALERT/RDY pin latches once asserted
#define ADS1X15_CLAT_NONLAT  (0x0000)  // Non-latching comparator (default)
#define ADS1X15_CLAT_LATCH   (0x0400)  // Latching comparator

#define ADS1X15_CQUE_MASK    (0x0003)
/* This wouldn't compile for the python wrapper. with these in for some reason.
#define ADS1X15_CQUE_1CONV   (0x0000)  // Assert ALERT/RDY after one conversions
#define ADS1X15_CQUE_2CONV     (0x0001)  // Assert ALERT/RDY after two conversions
#define ADS1X15_CQUE_4CONV     (0x0002)  // Assert ALERT/RDY after four conversions
#define ADS1X15_CQUE_NONE      (0x0003)  // Disable the comparator and put ALERT/RDY in high state (default)
*/
/*=========================================================================*/

namespace upm {
  /**
   * @brief ADS1X15 family adc library
   *
   * Library for TI analog to digital converter ic. Base clase fro ADS1X15 provides all the functionality that
   * ADS1115 and ADS1015 ics have in common.
   *
   * @defgroup ads1x15 libupm-ads1x15
   * @ingroup ti adafruit i2c electric
   */
    class ADS1X15 {

        public:

           /**
            * @enum ADSGAIN
            * @brief uint16_t enum containing values for
            * setting gain for ADS1X15 devices.
            *
            * @var ADSGAIN::TWOTHIRDS = 0x0000
            * @var ADSGAIN::ONE       = 0x0200
            * @var ADSGAIN::TWO       = 0x0400
            * @var ADSGAIN::FOUR      = 0x0600
            * @var ADSGAIN::EIGHT     = 0x0800
            * @var ADSGAIN::SIXTEEN   = 0x0A00
            */
            typedef enum ADSGAIN {
              GAIN_TWOTHIRDS    = ADS1X15_PGA_6_144V,
              GAIN_ONE          = ADS1X15_PGA_4_096V,
              GAIN_TWO          = ADS1X15_PGA_2_048V,
              GAIN_FOUR         = ADS1X15_PGA_1_024V,
              GAIN_EIGHT        = ADS1X15_PGA_0_512V,
              GAIN_SIXTEEN      = ADS1X15_PGA_0_256V
            } ADSGAIN ;

            /**
             * @enum ADSMUXMODE
             * @brief uint16_t enum containing values used
             * for selecting ADS1X15 read operations.
             *
             * @var ADSMUXMODE::DIFF_0_1 = 0x0000
             * @var ADSMUXMODE::DIFF_0_2 = 0x1000
             * @var ADSMUXMODE::DIFF_1_3 = 0x2000
             * @var ADSMUXMODE::DIFF_2_3 = 0x3000
             * @var ADSMUXMODE::SINGLE_0 = 0x4000
             * @var ADSMUXMODE::SINGLE_1 = 0x5000
             * @var ADSMUXMODE::SINGLE_2 = 0x6000
             * @var ADSMUXMODE::SINGLE_3 = 0x7000
             */
            typedef enum ADSMUXMODE {
              DIFF_0_1        = ADS1X15_MUX_DIFF_0_1,  // Differential P = AIN0, N = AIN1 (default)
              DIFF_0_3        = ADS1X15_MUX_DIFF_0_3,  // Differential P = AIN0, N = AIN3
              DIFF_1_3        = ADS1X15_MUX_DIFF_1_3,  // Differential P = AIN1, N = AIN3
              DIFF_2_3        = ADS1X15_MUX_DIFF_2_3,  // Differential P = AIN2, N = AIN3
              SINGLE_0        = ADS1X15_MUX_SINGLE_0,  // Single-ended AIN0
              SINGLE_1        = ADS1X15_MUX_SINGLE_1,  // Single-ended AIN1
              SINGLE_2        = ADS1X15_MUX_SINGLE_2,  // Single-ended AIN2
              SINGLE_3        = ADS1X15_MUX_SINGLE_3   // Single-ended AIN3
            } ADSMUXMODE;

            /**
             * @enum ADSCOMP
             * @brief uint16_t enum containing values
             * for setting ADS1X15 comparator queue modes.
             *
             * @var ADSCOMP::CQUE_1CONV = 0x0000
             * @var ADSCOMP::CQUE_2CONV = 0x0001
             * @var ADSCOMP::CQUE_3CONV = 0x0002
             * @var ADSCOMP::CQUE_NONE  = 0x0003
             */
            typedef enum ADSCOMP {
               CQUE_1CONV      = 0x0000,    // Assert ALERT/RDY after one conversions
               CQUE_2CONV      = 0x0001,    // Assert ALERT/RDY after two conversions
               CQUE_4CONV      = 0x0002,    // Assert ALERT/RDY after four conversions
               CQUE_NONE       = 0x0003      // Disable the comparator and put ALERT/RDY in high state (default)
            } ADSCOMP;

            /**
             * @enum ADSTHRESH
             * @brief uint8_t enum containing register addresses
             * used for setting HI and LOW threshold values as
             * well as setting conversion ready and set to default.
             *
             * @var ADSTHRESH::THRESH_LOW     = 0x02
             * @var ADSTHRESH::THRESH_HIGH    = 0x03
             * @var ADSTHRESH::CONVERSION_RDY = 0x04
             * @var ADSTHRESH::THRESH_DEFAULT = 0x05
             */
            typedef enum ADSTHRESH {
                THRESH_LOW      = ADS1X15_REG_POINTER_LOWTHRESH,
                THRESH_HIGH     = ADS1X15_REG_POINTER_HITHRESH,
                CONVERSION_RDY  = 0x04,
                THRESH_DEFAULT  = 0x05

            } ADSTHRESH;

            /**
             * @enum ADSSAMPLERATE
             * @brief uint16_t enum containing values
             * representing the sample rate of the device.
             * Will be overridden in subclass
             *
             * @var ADSSAMPLERATE::SPS_DEFAULT = 0x0080
             */
            typedef enum ADSSAMPLERATE {
               SPS_DEFAULT     = 0x0080
            } ADSSAMPLERATE;

            /**
             * ADS1X15 constructor
             *
             * @param bus i2c bus the sensor is attached to.
             * @param address. Device address. Default is 0x48.
             */
            ADS1X15(int bus, uint8_t address);

            /**
             * ADS1X15 destructor
             */
            virtual ~ADS1X15 ();

            /**
             * Returns the name of the sensor
             */
            std::string name()
            {
                return m_name;
            }

            /**
             *  Returns the contents of conversion register without performing
             *  a conversion operation. Will use a multiplier based on the
             *  current gain setting to give the voltage as a float. Used
             *  internally to return the HI and LOW threshold values.
             *
             *  @param reg uint8_t value specifying register to read.
             *  Should generally be called with no parameter.
             */
            float getLastSample(int reg = ADS1X15_REG_POINTER_CONVERT);

            /**
             * Performs a read as specified by ADS1X15::ADSMUXMOE and
             * returns the value as a float. Uses getLastSample() internally
             * to return voltage value.
             *
             * @pram mode ADSMUXMODE specifying inputs to be sampled.
             */
            float getSample(ADSMUXMODE mode = ADS1X15::DIFF_0_1);

            /**
             * Returns the current gain setting being used by the device
             * as an ADSGAIN value.
             */
            ADSGAIN getGain(){
                return  (ADSGAIN)(m_config_reg & ADS1X15_PGA_MASK);
            }

            /**
             * Sets the PGA gain bits to the desired gain. Default
             * is +/- 2.094 volts.
             *
             * @param gain ADSGAIN value reprenting the desired gain.
             * See warnings in spec sheet.
             */
            void setGain(ADSGAIN gain = ADS1X15::GAIN_TWO);

            /**
             * Returns the current device sample rate a an ADSSAMPLERATE
             * value.
             */
            ADSSAMPLERATE getSPS(void){
                return (ADSSAMPLERATE)(m_config_reg & ADS1X15_DR_MASK);
            }

            /**
             * Sets the sample rate of the device. This function
             * needs to be overrode in subclasses as the ADS1115 and
             * ADS1015 have different data rates.
             *
             * @param ADSSAMPLERATE enum
             * SPS_DEFAULT = 0x0080
             */
            virtual void setSPS(ADSSAMPLERATE rate);

            /**
             * Returns the comparator mode.
             * False = Traditional comparator with Hysteresis (default)
             * True  = Window Comparator
             */
            bool getCompMode(void){
                return (bool)(m_config_reg & ADS1X15_CMODE_MASK);
            }

            /**
             * Sets the comparator mode of the device.
             *
             * @param mode bool value denoting mode.
             * False = Traditional comparator with Hysteresis (default)
             * True  = Window Comparator
             */
            void setCompMode(bool mode = false);

            /**
             * Get comparator polarity. Reports the polarity
             * of the ALERT/RDY pin. Returns:
             * False = Active Low (default)
             * True  = Active High
             */
            bool getCompPol(void){
                return (bool)(m_config_reg & ADS1X15_CPOL_MASK);
            }

            /**
             * Sets the comparator polarity. Controls the
             * polarity of the ALERT/RDY pin.
             *
             * @param mode bool.
             * False = Active Low (default)
             * True  = Active High
             */
            void setCompPol(bool mode = false);

            /**
             * Returns bool representing the state of the
             * comparator latching functionality.
             * False = Non Latching comparator (default)
             * True  = Latching Comparator
             */
            bool getCompLatch(void){
                return (bool)(m_config_reg & ADS1X15_CLAT_MASK);
            }

            /**
             * Sets bit controlling comparator operation.
             *
             * @param mode bool
             * False = Non Latching comparator (default)
             * True  = Latching Comparator
             */
            void setCompLatch(bool mode = false);

            /**
             * Returns ADSCOMP value representing the state of
             * comparator queue.
             *
             * CQUE_1CONV = Assert after one conversion
             * CQUE_2CONV = Assert after two conversions
             * CQUE_2CONV = Assert after four conversions
             * CQUE_NONE = Disable comparator (default)
             */
            ADSCOMP getCompQue(void){
                return (ADSCOMP)(m_config_reg & ADS1X15_CQUE_MASK);
            }

            /**
             * Sets bits controlling Comparator queue operation.
             *
             * @param mode ADSCOMP enum.
             * CQUE_1CONV = Assert after one conversion
             * CQUE_2CONV = Assert after two conversions
             * CQUE_2CONV = Assert after four conversions
             * CQUE_NONE  = Disable comparator (default)
             */
            void setCompQue(ADSCOMP mode = ADS1X15::CQUE_NONE);

            /**
             * Returns bool reflecting state of device mode bit.
             *
             * False = Power Down Single shot mode (default)
             * True  = Continuous conversion mode
             */
            bool getContinuous(void){
                return !(bool)(m_config_reg & ADS1X15_MODE_MASK);
            }

            /**
             * Sets the state of device mode but.
             *
             * @param mode bool
             * False = Power Down Single shot mode (default)
             * True  = Continuous conversion mode
             */
            void setContinuous(bool mode = false);

            /**
             * Returns current high or low threshold setting.
             *
             * @param reg ADSTHRES enum value.
             * Returns 0.0 unless THRESH_HIGH or THRESH_LOW requested.
             */
            float getThresh(ADSTHRESH reg = THRESH_LOW);

            /**
             * Sets threshold levels or configures for conversion ready
             * operation of ALERT/RDY output.
             *
             * @param reg ADSTHRESH enum
             * @param value float value to set threshold register to.
             *
             * THRESH_LOW      = Sets low thresh register.
             * THRESH_HIGH     = Sets high thresh register.
             * CONVERSION_RDY  = Configures conversion ready operation
             * THRESH_DEFAULT  = resets high/low registers to startup values.
             */
            void setThresh(ADSTHRESH reg = THRESH_DEFAULT , float value = 0.0);

        protected:
            std::string m_name;
            float m_conversionDelay;
            uint8_t m_bitShift;
            uint16_t m_config_reg;
            //Must be overridden in subclass for proper values.
            virtual float getMultiplier(void) = 0;
            //Must be overridden in subclass for proper values.
            virtual void setDelay(void) = 0;
            void getCurrentConfig();
            void updateConfigRegister(uint16_t update, bool read = false);
            uint16_t swapWord(uint16_t value);

               mraa::I2c* i2c;

    };}