RTEMS 6.1-rc6
Loading...
Searching...
No Matches
ti-ads-16bit-adc.h
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*
4 * Copyright (c) 2016-2017 Chris Johns <chrisj@rtems.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * TI ADS1113 and ADS1115.
31 * http://www.ti.com/product/ads1113/description
32 */
33
34#ifndef TI_ADS1113_ADS1115_H
35#define TI_ADS1113_ADS1115_H
36
37#include <dev/i2c/i2c.h>
38
39/*
40 * Supported devices. Please others once tested.
41 */
42typedef enum {
43 TI_ADS1113,
44 TI_ADS1114,
45 TI_ADS1115
46} ti_ads_adc;
47
48/*
49 * Multiplexer interface. Avalable on ADS1115.
50 */
51typedef enum {
52 TI_ADS_MUX_ApA0_AnA1 = 0, /* default */
53 TI_ADS_MUX_ApA0_AnA3 = 1,
54 TI_ADS_MUX_ApA1_AnA3 = 2,
55 TI_ADS_MUX_ApA2_AnA3 = 3,
56 TI_ADS_MUX_ApA0_AnGND = 4,
57 TI_ADS_MUX_ApA1_AnGND = 5,
58 TI_ADS_MUX_ApA2_AnGND = 6,
59 TI_ADS_MUX_ApA3_AnGND = 7
60} ti_ads_adc_mux;
61
62/*
63 * Programmable Gain Amplifier. Avalable on ADS1114 and ADS1115.
64 */
65typedef enum {
66 TI_ADS_PGA_FS_6_144V = 0,
67 TI_ADS_PGA_FS_4_096V = 1,
68 TI_ADS_PGA_FS_2_048V = 2, /* default */
69 TI_ADS_PGA_FS_1_024V = 3,
70 TI_ADS_PGA_FS_0_512V = 4,
71 TI_ADS_PGA_FS_0_256V = 5,
72 TI_ADS_PGA_FS_0_256V_2 = 6,
73 TI_ADS_PGA_FS_0_256V_3 = 7,
74} ti_ads_adc_pga;
75
76/*
77 * Mode.
78 */
79typedef enum {
80 TI_ADS_MODE_CONTINUOUS = 0,
81 TI_ADS_MODE_SINGLE_SHOT = 1, /* default */
82} ti_ads_adc_mode;
83
84/*
85 * Data rate.
86 */
87typedef enum {
88 TI_ADS_DATARATE_8SPS = 0,
89 TI_ADS_DATARATE_16SPS = 1,
90 TI_ADS_DATARATE_32SPS = 2,
91 TI_ADS_DATARATE_64SPS = 3,
92 TI_ADS_DATARATE_128SPS = 4, /* default */
93 TI_ADS_DATARATE_250SPS = 5,
94 TI_ADS_DATARATE_475SPS = 6,
95 TI_ADS_DATARATE_860SPS = 7,
96} ti_ads_adc_data_rate;
97
98/*
99 * Comparitor interface. Avalable on ADS1114 and ADS1115.
100 *
101 * Create a value to write.
102 */
103#define TI_ADS_COMP_MODE_HYSTERESIS (0 << 4) /* default */
104#define TI_ADS_COMP_MODE_WINDOW (1 << 4)
105#define TI_ADS_COMP_POL_ACTIVE_LOW (0 << 3) /* default */
106#define TI_ADS_COMP_POL_ACTIVE_HIGH (1 << 3)
107#define TI_ADS_COMP_LAT_NON_LATCHING (0 << 2) /* default */
108#define TI_ADS_COMP_LAT_LATCHING (1 << 2)
109#define TI_ADS_COMP_QUE_DISABLE_COMP (3 << 0) /* default */
110#define TI_ADS_COMP_QUE_AFTER_4 (2 << 0)
111#define TI_ADS_COMP_QUE_AFTER_2 (1 << 0)
112#define TI_ADS_COMP_QUE_AFTER_1 (0 << 0)
113
114/*
115 * IO control interface.
116 *
117 * Note: if in Power-down single-shot mode (default) a conversion requires the
118 * device to power up and perform a conversion and this can take
119 * while. The TI_ADS_ADC_GET_CONV_WAIT call sets the micro-seconds to
120 * wait between polls. The actual rate will depend on the system tick.
121 */
122#define TI_ADS_ADC_GET_CONVERSION (I2C_DEV_IO_CONTROL + 0)
123#define TI_ADS_ADC_SET_MUX (I2C_DEV_IO_CONTROL + 1)
124#define TI_ADS_ADC_SET_PGA (I2C_DEV_IO_CONTROL + 2)
125#define TI_ADS_ADC_SET_MODE (I2C_DEV_IO_CONTROL + 3)
126#define TI_ADS_ADC_SET_DATA_RATE (I2C_DEV_IO_CONTROL + 4)
127#define TI_ADS_ADC_SET_COMP (I2C_DEV_IO_CONTROL + 5)
128#define TI_ADS_ADC_SET_LO_THRESH (I2C_DEV_IO_CONTROL + 6)
129#define TI_ADS_ADC_SET_HI_THRESH (I2C_DEV_IO_CONTROL + 7)
130#define TI_ADS_ADC_SET_CONV_WAIT (I2C_DEV_IO_CONTROL + 8)
131
132/*
133 * Register the device.
134 */
135int i2c_dev_register_ti_ads_adc(const char* bus_path,
136 const char* dev_path,
137 uint16_t address,
138 ti_ads_adc device);
139
140/*
141 * Perform a conversion. If the mode is single shot a single shot conversion is
142 * started and the call waits for the conversion to complete.
143 */
144static inline int
145ti_ads_adc_convert(int fd, uint16_t* sample)
146{
147 return ioctl(fd, TI_ADS_ADC_GET_CONVERSION, sample);
148}
149
150/*
151 * Set the multipler.
152 */
153static inline int
154ti_ads_adc_set_mux(int fd, ti_ads_adc_mux mux)
155{
156 return ioctl(fd, TI_ADS_ADC_SET_MUX, (void *)(uintptr_t) mux);
157}
158
159/*
160 * Set the PGA.
161 */
162static inline int
163ti_ads_adc_set_pga(int fd, ti_ads_adc_pga pga)
164{
165 return ioctl(fd, TI_ADS_ADC_SET_PGA, (void *)(uintptr_t) pga);
166}
167
168/*
169 * Set the mode.
170 */
171static inline int
172ti_ads_adc_set_mode(int fd, ti_ads_adc_mode mode)
173{
174 return ioctl(fd, TI_ADS_ADC_SET_MODE, (void *)(uintptr_t) mode);
175}
176
177/*
178 * Set the data rate.
179 */
180static inline int
181ti_ads_adc_set_data_rate(int fd, ti_ads_adc_data_rate rate)
182{
183 return ioctl(fd, TI_ADS_ADC_SET_DATA_RATE, (void *)(uintptr_t) rate);
184}
185
186/*
187 * Configure the comparator.
188 */
189static inline int
190ti_ads_adc_set_comparator(int fd, uint16_t comp)
191{
192 return ioctl(fd, TI_ADS_ADC_SET_COMP, (void *)(uintptr_t) comp);
193}
194
195/*
196 * Set the lower threshold.
197 */
198static inline int
199ti_ads_adc_set_low_threshold(int fd, uint16_t level)
200{
201 return ioctl(fd, TI_ADS_ADC_SET_LO_THRESH, (void *)(uintptr_t) level);
202}
203
204/*
205 * Set the upper threshold.
206 */
207static inline int
208ti_ads_adc_set_high_threshold(int fd, uint16_t level)
209{
210 return ioctl(fd, TI_ADS_ADC_SET_HI_THRESH, (void *)(uintptr_t) level);
211}
212
213/*
214 * Set the conversion poll wait period.
215 */
216static inline int
217ti_ads_adc_set_conversion_poll_wait(int fd, uint32_t micro_seconds)
218{
219 return ioctl(fd, TI_ADS_ADC_SET_CONV_WAIT, (void *)(uintptr_t) micro_seconds);
220}
221
222#endif
Inter-Integrated Circuit (I2C) Driver API.