• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eldruin / ads1x1x-rs / 11969723285

22 Nov 2024 09:13AM UTC coverage: 81.304% (-1.5%) from 82.819%
11969723285

Pull #32

github

web-flow
Merge 57dc7d833 into d3af2b607
Pull Request #32: Improve documentation.

187 of 230 relevant lines covered (81.3%)

2.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

12.5
/src/lib.rs
1
//! This is a platform-agnostic Rust driver for the ADS1013, ADS1014, ADS1015,
2
//! ADS1113, ADS1114, and ADS1115 ultra-small, low-power
3
//! analog-to-digital converters (ADC), based on the [`embedded-hal`] traits.
4
//!
5
//! [`embedded-hal`]: https://github.com/rust-embedded/embedded-hal
6
//!
7
//! This driver allows you to:
8
//! - Set the operating mode to one-shot or continuous. See: [`into_continuous()`].
9
//! - Make a measurement in one-shot mode. See: [`read()`][read_os].
10
//! - Start continuous conversion mode. See: [`start()`].
11
//! - Read the last measurement made in continuous conversion mode. See: [`read()`][read_cont].
12
//! - Set the data rate. See: [`set_data_rate()`].
13
//! - Set the full-scale range (gain amplifier). See [`set_full_scale_range()`].
14
//! - Read whether a measurement is in progress. See: [`is_measurement_in_progress()`].
15
//! - Set the ALERT/RDY pin to be used as conversion-ready pin. See: [`use_alert_rdy_pin_as_ready()`].
16
//! - Comparator:
17
//!     - Set the low and high thresholds. See: [`set_high_threshold_raw()`].
18
//!     - Set the comparator mode. See: [`set_comparator_mode()`].
19
//!     - Set the comparator polarity. See: [`set_comparator_polarity()`].
20
//!     - Set the comparator latching. See: [`set_comparator_latching()`].
21
//!     - Set the comparator queue. See: [`set_comparator_queue()`].
22
//!     - Disable the comparator. See: [`disable_comparator()`].
23
//!
24
//! [`into_continuous()`]: struct.Ads1x1x.html#method.into_continuous
25
//! [read_os]: struct.Ads1x1x.html#method.read-1
26
//! [`start()`]: struct.Ads1x1x.html#method.start
27
//! [read_cont]: struct.Ads1x1x.html#method.read
28
//! [`set_data_rate()`]: struct.Ads1x1x.html#method.set_data_rate
29
//! [`set_full_scale_range()`]: struct.Ads1x1x.html#method.set_full_scale_range
30
//! [`is_measurement_in_progress()`]: struct.Ads1x1x.html#method.is_measurement_in_progress
31
//! [`set_high_threshold_raw()`]: struct.Ads1x1x.html#method.set_high_threshold_raw
32
//! [`set_comparator_mode()`]: struct.Ads1x1x.html#method.set_comparator_mode
33
//! [`set_comparator_polarity()`]: struct.Ads1x1x.html#method.set_comparator_polarity
34
//! [`set_comparator_latching()`]: struct.Ads1x1x.html#method.set_comparator_latching
35
//! [`set_comparator_queue()`]: struct.Ads1x1x.html#method.set_comparator_queue
36
//! [`disable_comparator()`]: struct.Ads1x1x.html#method.disable_comparator
37
//! [`use_alert_rdy_pin_as_ready()`]: struct.Ads1x1x.html#method.use_alert_rdy_pin_as_ready
38
//!
39
//! # The devices
40
//!
41
//! The devices are precision, low power, 12/16-bit analog-to-digital
42
//! converters (ADC) that provide all features necessary to measure the most
43
//! common sensor signals in an ultra-small package. Depending on the device,
44
//! these  integrate a programmable gain amplifier (PGA), voltage reference,
45
//! oscillator and high-accuracy temperature sensor.
46
//!
47
//! The devices can perform conversions at data rates up to 3300 samples per
48
//! second (SPS). The PGA offers input ranges from ±256 mV to ±6.144 V,
49
//! allowing both large and small signals to be measured with high resolution.
50
//! An input multiplexer (MUX) allows to measure two differential or four
51
//! single-ended inputs. The high-accuracy temperature sensor can be used for
52
//! system-level temperature monitoring or cold-junction compensation for
53
//! thermocouples.
54
//!
55
//! The devices operate either in continuous-conversion mode, or in a
56
//! single-shot mode that automatically powers down after a conversion.
57
//! Single-shot mode significantly reduces current consumption during idle
58
//! periods. Data is transferred through I²C.
59
//!
60
//! Here is a comparison of the caracteristics of the devices:
61
//!
62
//! | Device  | Resolution | Sample Rate  | Channels | Multi-channel | Features        |
63
//! |---------|------------|--------------|----------|---------------|-----------------|
64
//! | ADS1013 | 12-bit     | Max 3300 SPS | 1        | N/A           |                 |
65
//! | ADS1014 | 12-bit     | Max 3300 SPS | 1        | N/A           | Comparator, PGA |
66
//! | ADS1015 | 12-bit     | Max 3300 SPS | 4        | Multiplexed   | Comparator, PGA |
67
//! | ADS1113 | 16-bit     | Max 860 SPS  | 1        | N/A           |                 |
68
//! | ADS1114 | 16-bit     | Max 860 SPS  | 1        | N/A           | Comparator, PGA |
69
//! | ADS1115 | 16-bit     | Max 860 SPS  | 4        | Multiplexed   | Comparator, PGA |
70
//!
71
//! Datasheets:
72
//! - [ADS101x](http://www.ti.com/lit/ds/symlink/ads1015.pdf)
73
//! - [ADS111x](http://www.ti.com/lit/ds/symlink/ads1115.pdf)
74
//!
75
//! # Examples
76
//!
77
//! To use this driver, import this crate and an `embedded_hal` implementation,
78
//! then instantiate the appropriate device.
79
//! In the following examples an instance of the device ADS1013 will be created.
80
//!
81
//! Please find additional examples using hardware in this repository: [driver-examples]
82
//!
83
//! [driver-examples]: https://github.com/eldruin/driver-examples
84
//!
85
//! ## Creating a Driver Instance for an ADS1013
86
//!
87
//! ```no_run
88
//! use linux_embedded_hal::I2cdev;
89
//! use ads1x1x::{Ads1x1x, TargetAddr};
90
//!
91
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
92
//! let adc = Ads1x1x::new_ads1013(dev, TargetAddr::default());
93
//! // do something...
94
//!
95
//! // get the I2C device back
96
//! let dev = adc.destroy_ads1013();
97
//! ```
98
//!
99
//! ## Creating a driver instance for an ADS1013 with the ADDR pin connected to SDA.
100
//!
101
//! ```no_run
102
//! use linux_embedded_hal::I2cdev;
103
//! use ads1x1x::{Ads1x1x, TargetAddr};
104
//!
105
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
106
//! let adc = Ads1x1x::new_ads1013(dev, TargetAddr::Sda);
107
//! ```
108
//!
109
//! ## Taking a One-Shot Measurement
110
//!
111
//! ```no_run
112
//! use ads1x1x::{channel, Ads1x1x, TargetAddr};
113
//! use linux_embedded_hal::I2cdev;
114
//! use nb::block;
115
//!
116
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
117
//! let mut adc = Ads1x1x::new_ads1013(dev, TargetAddr::default());
118
//! let measurement = block!(adc.read(channel::DifferentialA0A1)).unwrap();
119
//! println!("Measurement: {}", measurement);
120
//! let _dev = adc.destroy_ads1013(); // get I2C device back
121
//! ```
122
//!
123
//! ## Changing to Continuous Conversion Mode and Reading the Last Measurement
124
//!
125
//! Changing the mode may fail in case there was a communication error.
126
//! In this case, you can retrieve the unchanged device from the error type.
127
//!
128
//! ```no_run
129
//! use linux_embedded_hal::I2cdev;
130
//! use ads1x1x::{Ads1x1x, ModeChangeError, TargetAddr};
131
//!
132
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
133
//! let adc = Ads1x1x::new_ads1013(dev, TargetAddr::default());
134
//! match adc.into_continuous() {
135
//!     Err(ModeChangeError::I2C(e, adc)) => {
136
//!         panic!("Mode change failed: {e}")
137
//!     },
138
//!     Ok(mut adc) => {
139
//!         let measurement = adc.read().unwrap();
140
//!         // ...
141
//!     }
142
//! }
143
//! ```
144
//!
145
//!
146
//! ## Setting the Data Rate
147
//! For 12-bit devices, the available data rates are given by `DataRate12Bit`.
148
//! For 16-bit devices, the available data rates are given by `DataRate16Bit`.
149
//!
150
//! ```no_run
151
//! use linux_embedded_hal::I2cdev;
152
//! use ads1x1x::{Ads1x1x, DataRate16Bit, TargetAddr};
153
//!
154
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
155
//! let mut adc = Ads1x1x::new_ads1115(dev, TargetAddr::default());
156
//! adc.set_data_rate(DataRate16Bit::Sps860).unwrap();
157
//! ```
158
//!
159
//! ## Configuring the Comparator
160
//!
161
//! Configure the comparator to assert when the voltage drops below -1.5V
162
//! or goes above 1.5V in at least two consecutive conversions. Then the
163
//! ALERT/RDY pin will be set high and it will be kept so until the
164
//! measurement is read or an appropriate SMBus alert response is sent by
165
//! the controller.
166
//!
167
//! ```no_run
168
//! use linux_embedded_hal::I2cdev;
169
//! use ads1x1x::{
170
//!     Ads1x1x, TargetAddr, ComparatorQueue, ComparatorPolarity,
171
//!     ComparatorMode, ComparatorLatching, FullScaleRange
172
//! };
173
//!
174
//! let dev = I2cdev::new("/dev/i2c-1").unwrap();
175
//! let address = TargetAddr::default();
176
//! let mut adc = Ads1x1x::new_ads1015(dev, address);
177
//! adc.set_comparator_queue(ComparatorQueue::Two).unwrap();
178
//! adc.set_comparator_polarity(ComparatorPolarity::ActiveHigh).unwrap();
179
//! adc.set_comparator_mode(ComparatorMode::Window).unwrap();
180
//! adc.set_full_scale_range(FullScaleRange::Within2_048V).unwrap();
181
//! adc.set_low_threshold_raw(-1500).unwrap();
182
//! adc.set_high_threshold_raw(1500).unwrap();
183
//! adc.set_comparator_latching(ComparatorLatching::Latching).unwrap();
184
//! ```
185
#![deny(unsafe_code)]
186
#![deny(missing_docs)]
187
#![no_std]
188

189
struct Register;
190
impl Register {
191
    const CONVERSION: u8 = 0x00;
192
    const CONFIG: u8 = 0x01;
193
    const LOW_TH: u8 = 0x02;
194
    const HIGH_TH: u8 = 0x03;
195
}
196

197
struct BitFlags;
198
impl BitFlags {
199
    const OS: u16 = 0b1000_0000_0000_0000;
200
    const MUX2: u16 = 0b0100_0000_0000_0000;
201
    const MUX1: u16 = 0b0010_0000_0000_0000;
202
    const MUX0: u16 = 0b0001_0000_0000_0000;
203
    const PGA2: u16 = 0b0000_1000_0000_0000;
204
    const PGA1: u16 = 0b0000_0100_0000_0000;
205
    const PGA0: u16 = 0b0000_0010_0000_0000;
206
    const OP_MODE: u16 = 0b0000_0001_0000_0000;
5✔
207
    const DR2: u16 = 0b0000_0000_1000_0000;
208
    const DR1: u16 = 0b0000_0000_0100_0000;
209
    const DR0: u16 = 0b0000_0000_0010_0000;
210
    const COMP_MODE: u16 = 0b0000_0000_0001_0000;
211
    const COMP_POL: u16 = 0b0000_0000_0000_1000;
212
    const COMP_LAT: u16 = 0b0000_0000_0000_0100;
213
    const COMP_QUE1: u16 = 0b0000_0000_0000_0010;
214
    const COMP_QUE0: u16 = 0b0000_0000_0000_0001;
×
215
}
216

217
pub mod channel;
218
pub use channel::ChannelId;
219
mod construction;
220
mod conversion;
221
pub use crate::conversion::{ConvertMeasurement, ConvertThreshold};
222
mod devices;
223
#[doc(hidden)]
224
pub mod ic;
225
mod types;
226
use crate::types::Config;
227
pub use crate::types::{
228
    mode, Ads1x1x, ComparatorLatching, ComparatorMode, ComparatorPolarity, ComparatorQueue,
229
    DataRate12Bit, DataRate16Bit, Error, FullScaleRange, ModeChangeError, TargetAddr,
230
};
231

232
mod private {
233
    use super::{ic, Ads1x1x};
234
    pub trait Sealed {}
×
235

236
    impl<I2C, IC, CONV, MODE> Sealed for Ads1x1x<I2C, IC, CONV, MODE> {}
×
237

238
    impl Sealed for ic::Resolution12Bit {}
239
    impl Sealed for ic::Resolution16Bit {}
×
240

241
    impl Sealed for ic::Ads1013 {}
242
    impl Sealed for ic::Ads1113 {}
×
243
    impl Sealed for ic::Ads1014 {}
×
244
    impl Sealed for ic::Ads1114 {}
245
    impl Sealed for ic::Ads1015 {}
×
246
    impl Sealed for ic::Ads1115 {}
247
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc