summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/adc121c021/adc121c021.hpp
blob: 8a2519db84d501744ff15c584baa0bb196e36ccd (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
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2014 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 <string>
#include <mraa/i2c.h>

#define ADC121C021_I2C_BUS 0
#define ADC121C021_DEFAULT_I2C_ADDR 0x55

#define ADC121C021_RESOLUTION  4096 // 12 bits

/**
 * ADC121C021 registers
 */

#define ADC121C021_REG_RESULT          0x00
#define ADC121C021_REG_ALERT_STATUS    0x01
#define ADC121C021_REG_CONFIG          0x02
#define ADC121C021_REG_ALERT_LIM_UNDER 0x03
#define ADC121C021_REG_ALERT_LIM_OVER  0x04
#define ADC121C021_REG_ALERT_HYS       0x05
#define ADC121C021_REG_LOWEST_CONV     0x06
#define ADC121C021_REG_HIGHEST_CONV    0x07

// For the Grove I2C ADC
#define ADC121C021_DEFAULT_VREF        3.0

namespace upm {
  /**
   * @brief ADC121C021 I2C Analog-to-Digital Converter library
   * @defgroup adc121c021 libupm-adc121c021
   * @ingroup seeed i2c electric
   */


  /**
   * Valid cycle times for the automatic conversion mode
   */

  typedef enum { ADC121C021_CYCLE_NONE = 0,  // disabled
                 ADC121C021_CYCLE_32   = 1,  // 27 ksps
                 ADC121C021_CYCLE_64   = 2,  // 13.5
                 ADC121C021_CYCLE_128  = 3,  // 6.7
                 ADC121C021_CYCLE_256  = 4,  // 3.4
                 ADC121C021_CYCLE_512  = 5,  // 1.7
                 ADC121C021_CYCLE_1024 = 6,  // 0.9
                 ADC121C021_CYCLE_2048 = 7   // 0.4
  } ADC121C021_CYCLE_TIME_T;

  /**
   * @library adc121c021
   * @sensor adc121c021
   * @comname ADC121C021 Analog-to-Digital Converter
   * @altname Grove I2C ADC
   * @type electric
   * @man seeed
   * @web http://www.seeedstudio.com/depot/Grove-I2C-ADC-p-1580.html
   * @con i2c
   *
   * @brief API for the ADC121C021 I2C Analog-to-Digital Converter
   *
   * UPM module for the ADC121C021 12-bit analog-to-digital converter (ADC).
   * By constantly providing a reference voltage, this sensor helps
   * increase the accuracy of a value collected from an analog sensor.
   *
   * @image html adc121c021.jpg
   * @snippet adc121c021.cxx Interesting
   */
  class ADC121C021 {
  public:
    /**
     * ADC121C021 ADC constructor
     *
     * @param bus I2C bus to use
     * @param address Address for this sensor; default is 0x55
     * @param vref Reference voltage for this sensor; default is 3.0v
     */
    ADC121C021(int bus, uint8_t address = ADC121C021_DEFAULT_I2C_ADDR,
               float vref = ADC121C021_DEFAULT_VREF);

    /**
     * ADC121C021 destructor
     */
    ~ADC121C021();

    /**
     * Writes a byte value into the register
     *
     * @param reg Register location to write into
     * @param byte Byte to write
     * @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
     */
    mraa_result_t writeByte(uint8_t reg, uint8_t byte);

    /**
     * Writes a word value into the register
     *
     * @param reg Register location to write into
     * @param word Word to write
     * @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
     */
    mraa_result_t writeWord(uint8_t reg, uint16_t word);

    /**
     * Reads the byte value from the register
     *
     * @param reg Register location to read from
     * @return Value in the specified register
     */
    uint8_t readByte(uint8_t reg);

    /**
     * Reads the word value from the register
     *
     * @param reg Register location to read from
     * @return Value in the specified register
     */
    uint16_t readWord(uint8_t reg);

    /**
     * Reads the current value of conversion 
     *
     * @return Current value of conversion
     */
    uint16_t value();

    /**
     * Converts a supplied value to voltage based on the set vref
     *
     * @param val Value of conversion (from value())
     * @return Value of conversion in volts
     */
    float valueToVolts(uint16_t val);

    /**
     * Reads the current status of the alert flag.  If the flag is set, the
     * low or high alert indicators are set as appropriate, and
     * you can access these values with alertLowTriggered() or
     * alertHighTriggered().
     *
     * @return True if the alert flag is set
     */
    bool getAlertStatus();

    /**
     * Returns the current value of m_alertLow.  You must call
     * getAlertStatus() to update this value. 
     *
     * @return Current status of the alert low flag 
     */
    bool alertLowTriggered() { return m_alertLow; };

    /**
     * Returns the current value of m_alertHigh.  You must call
     * getAlertStatus() to update this value. 
     *
     * @return Current status of the alert high flag
     */
    bool alertHighTriggered() { return m_alertHigh; };

    /**
     * Clears the alert low and alert high flags. This also clears the
     * last stored alert values.
     */
    void clearAlertStatus();

    /**
     * Enables or disables the alert flag functionality.  If enabled,
     * when the measured value exceeds the low or high limits
     * configured, the alert flag is set. Use getAlertStatus()
     * to access these values.
     *
     * @param enable If true, enables the alert flag; otherwise, disables the
     * alert flag
     */
    void enableAlertFlag(bool enable);

    /**
     * Enables or disables the alert pin functionality.
     *
     * @param enable If true, enables the alert pin; otherwise, disables the
     * alert pin
     */
    void enableAlertPin(bool enable);

    /**
     * Enables or disables the alert hold functionality.  When the alert
     * hold is enabled, the alert status remains until manually
     * cleared via clearAlertStatus().  Otherwise, the alert self-clears
     * when the value moves into the defined limits if alerts
     * are enabled via enableAlertFlag().
     *
     * @param enable If true, enables the alert hold; otherwise, disables the
     * alert hold
     */
    void enableAlertHold(bool enable);

    /**
     * If the alert pin is enabled, defines the active
     * polarity of the pin in an alert condition. Enabling this sets
     * the pin to active high in an alert condition; otherwise,
     * active low is used.
     *
     * @param enable If true, the alert pin is active high; otherwise, active
     * low
     */
    void enableAlertPinPolarityHigh(bool enable);

    /**
     * Enables or disables the automatic conversion mode. When enabled, the
     * ADC samples and updates the conversion value independently.
     * It is disabled by default, so conversion is only done by
     * calling value(). 
     *
     * @param cycleTime Sets the cycle time for automatic conversion
     */
    void setAutomaticConversion(ADC121C021_CYCLE_TIME_T cycleTime);

    /**
     * Sets the alert low limit.  If alerts are enabled and the
     * measured conversion value is lower than the low limit, an alert is
     * triggered.
     *
     * @param limit Alert low limit
     * @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
     */
    mraa_result_t setAlertLowLimit(uint16_t limit);

    /**
     * Sets the alert high limit.  If alerts are enabled and the
     * measured conversion value is higher than the high limit, an alert is
     * triggered.
     *
     * @param limit Alert high limit
     * @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
     */
    mraa_result_t setAlertHighLimit(uint16_t limit);

    /**
     * Sets the hysteresis value.  If a high or low alert condition is
     * triggered, the conversion result must move within the high or
     * low limit by more than this value to clear the alert condition.
     * If the alert hold is set, the alert doesn't self-clear
     * regardless of this value.
     *
     * @param limit Hysteresis limit
     * @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
     */
    mraa_result_t setHysteresis(uint16_t limit);

    /**
     * Returns the highest conversion value recorded so far. This value
     * is only updated by the converter when the automatic conversion mode
     * is enabled.
     *
     * @return Highest conversion value
     */
    uint16_t getHighestConversion();

    /**
     * Returns the lowest conversion value recorded so far. This value
     * is only updated by the converter when the automatic conversion mode
     * is enabled.
     *
     * @return Lowest conversion value
     */
    uint16_t getLowestConversion();

    /**
     * Clears the highest conversion value recorded so far.
     *
     * @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
     */
    mraa_result_t clearHighestConversion();

    /**
     * Clears the lowest conversion value recorded so far.
     *
     * @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
     */
    mraa_result_t clearLowestConversion();

  private:
    mraa_i2c_context m_i2c;
    uint8_t m_addr;
    float m_vref;
    bool m_alertLow;
    bool m_alertHigh;
  };
}