Blame view

RIOT/drivers/include/adcxx1c.h 4.54 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) 2017 OTA keys S.A.
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @defgroup   drivers_adcxx1x ADCXX1C ADC device driver
   * @ingroup    drivers_sensors
   * @brief      I2C Analog-to-Digital Converter device driver
   *
   *             This driver works with adc081c, adc101c and adc121c versions.
   * @{
   *
   * @file
   * @brief      ADCXX1C ADC device driver
   *
   * @author     Vincent Dupont <vincent@otakeys.com>
   */
  
  #ifndef ADCXX1C_H
  #define ADCXX1C_H
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  #include "periph/i2c.h"
  #include "periph/gpio.h"
  
  /**
   * @brief  ADCxx1C default address (ADCxx1C021 address)
   */
  #ifndef ADCXX1C_I2C_ADDRESS
  #define ADCXX1C_I2C_ADDRESS  (0x54)
  #endif
  
  /**
   * @brief   ADC resolution
   */
  enum {
      ADCXX1C_RES_8BITS  = 8,  /**< 8 bits resolution (ADC081C family) */
      ADCXX1C_RES_10BITS = 10, /**< 10 bits resolution (ADC101C family) */
      ADCXX1C_RES_12BITS = 12, /**< 12 bits resolution (ADC121C family) */
  };
  
  /**
   * @brief   ADC default resolution for device variants
   */
  #if   defined(MODULE_ADC081C)
  #define ADCXX1C_RES_DEFAULT     ADCXX1C_RES_8BITS
  #elif defined(MODULE_ADC101C)
  #define ADCXX1C_RES_DEFAULT     ADCXX1C_RES_10BITS
  #elif defined(MODULE_ADC121C)
  #define ADCXX1C_RES_DEFAULT     ADCXX1C_RES_12BITS
  #else
  #define ADCXX1C_RES_DEFAULT     (-1)
  #error "ADCXX1C: Failed to select resolution: unknown ADCXX1C device variant!"
  #endif
  
  /**
   * @brief   Conversion interval configuration value
   */
  enum {
      ADCXX1C_CYCLE_DISABLED = 0,  /**< No cycle conversion */
      ADCXX1C_CYCLE_32,            /**< Conversion cycle = Tconvert x 32 */
      ADCXX1C_CYCLE_64,            /**< Conversion cycle = Tconvert x 64 */
      ADCXX1C_CYCLE_128,           /**< Conversion cycle = Tconvert x 128 */
      ADCXX1C_CYCLE_256,           /**< Conversion cycle = Tconvert x 256 */
      ADCXX1C_CYCLE_512,           /**< Conversion cycle = Tconvert x 512 */
      ADCXX1C_CYCLE_1024,          /**< Conversion cycle = Tconvert x 1024 */
      ADCXX1C_CYCLE_2048,          /**< Conversion cycle = Tconvert x 2048 */
  };
  
  /**
   * @brief   Named return values
   */
  enum {
      ADCXX1C_OK          =  0,       /**< everything was fine */
      ADCXX1C_NOI2C       = -1,       /**< I2C communication failed */
      ADCXX1C_NODEV       = -2,       /**< no ADCXX1C device found on the bus */
      ADCXX1C_NODATA      = -3        /**< no data available */
  };
  
  /**
   * @brief   ADCxx1C params
   */
  typedef struct adcxx1c_params {
      i2c_t i2c;            /**< i2c device */
      uint8_t addr;         /**< i2c address */
      uint8_t bits;         /**< resolution */
      uint8_t cycle;        /**< conversion interval */
      gpio_t alert_pin;     /**< alert pin (GPIO_UNDEF if not connected) */
      int16_t low_limit;    /**< alert low value */
      int16_t high_limit;   /**< alert high value */
      int16_t hysteresis;   /**< alert hysteresis */
  } adcxx1c_params_t;
  
  /**
   * @brief   ADCxx1C alert callback
   */
  typedef void (*adcxx1c_cb_t)(void *);
  
  /**
   * @brief   ADCxx1C device descriptor
   */
  typedef struct adcxx1c {
      adcxx1c_params_t params; /**< device driver configuration */
      adcxx1c_cb_t cb;         /**< alert callback */
      void *arg;               /**< alert callback param */
  } adcxx1c_t;
  
  /**
   * @brief   Initialize an ADCxx1C ADC device
   *
   * @param[in,out] dev  device descriptor
   * @param[in] params   device configuration
   *
   * @return zero on successful initialization, non zero on error
   */
  int adcxx1c_init(adcxx1c_t *dev, const adcxx1c_params_t *params);
  
  /**
   * @brief   Read a raw ADC value
   *
   * @param[in] dev   device descriptor
   * @param[out] raw  read value
   *
   * @return zero on successful read, non zero on error
   */
  int adcxx1c_read_raw(const adcxx1c_t *dev, int16_t *raw);
  
  /**
   * @brief   Enable alert interrupt
   *
   * @param[in] dev   device descriptor
   * @param[in] cb    callback called when the alert fires
   * @param[in] arg   callback argument
   *
   * @return zero on success, non zero on error
   */
  int adcxx1c_enable_alert(adcxx1c_t *dev, adcxx1c_cb_t cb, void *arg);
  
  /**
   * @brief   Set the alert parameters
   *
   * @param[in,out] dev      device descriptor
   * @param[in] low_limit    alert low limit
   * @param[in] high_limit   alert high limit
   * @param[in] hysteresis   alert hysteresis
   *
   * @return zero on success, non zero on error
   */
  int adcxx1c_set_alert_parameters(const adcxx1c_t *dev, int16_t low_limit,
                                   int16_t high_limit, int16_t hysteresis);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* ADCXX1C_H */
  /** @} */