summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDiego Barrios Romero <eldruin@gmail.com>2018-11-10 10:01:27 +0100
committerDiego Barrios Romero <eldruin@gmail.com>2018-11-10 10:01:27 +0100
commitf6ac6188a652bd7ccff772648d57cff9a0963e84 (patch)
treeeccb385ef79bd57367f6a59312907304c042fc8e
parente7532aef9723a4078874d42ccb9a616628f1b3e6 (diff)
downloadads1x1x-async-f6ac6188a652bd7ccff772648d57cff9a0963e84.tar.gz
ads1x1x-async-f6ac6188a652bd7ccff772648d57cff9a0963e84.tar.xz
ads1x1x-async-f6ac6188a652bd7ccff772648d57cff9a0963e84.zip
Add support for setting the comparator latching
-rw-r--r--src/devices/ads1x1x/features/tier2.rs14
-rw-r--r--src/lib.rs21
-rw-r--r--tests/common/mod.rs1
-rw-r--r--tests/tier2_i2c.rs7
4 files changed, 41 insertions, 2 deletions
diff --git a/src/devices/ads1x1x/features/tier2.rs b/src/devices/ads1x1x/features/tier2.rs
index 7d22796..43dce5c 100644
--- a/src/devices/ads1x1x/features/tier2.rs
+++ b/src/devices/ads1x1x/features/tier2.rs
@@ -3,7 +3,7 @@
//! These are the features included only in ADS1x14, ADS1x15
use { Ads1x1x, Error, interface, ic, ComparatorMode, ComparatorPolarity,
- Register, BitFlags };
+ ComparatorLatching, Register, BitFlags };
impl<DI, IC, MODE, E> Ads1x1x<DI, IC, MODE>
where
@@ -33,4 +33,16 @@ where
self.config = config;
Ok(())
}
+
+ /// Set comparator latching
+ pub fn set_comparator_latching(&mut self, latching: ComparatorLatching) -> Result<(), Error<E>> {
+ let config;
+ match latching {
+ ComparatorLatching::Nonlatching => config = self.config.with_low( BitFlags::COMP_LAT),
+ ComparatorLatching::Latching => config = self.config.with_high(BitFlags::COMP_LAT)
+ }
+ self.iface.write_register(Register::CONFIG, config.bits)?;
+ self.config = config;
+ Ok(())
+ }
}
diff --git a/src/lib.rs b/src/lib.rs
index ee37a1d..f144b01 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -175,6 +175,26 @@ pub enum ComparatorPolarity {
ActiveHigh
}
+/// Comparator polarity (only for ADS1x14, ADS1x15)
+///
+/// Select whether the ALERT/RDY pin latches after being asserted or clears
+/// after conversions are within the margin of the upper and lower
+/// threshold values.
+#[derive(Debug, Clone, PartialEq)]
+pub enum ComparatorLatching {
+ /// Nonlatching (default)
+ ///
+ /// The ALERT/RDY pin does not latch when asserted.
+ Nonlatching,
+ /// Latching
+ ///
+ /// The asserted ALERT/RDY pin remains latched until conversion data are
+ /// read by the master or an appropriate SMBus alert response is sent by
+ /// the master. The device responds with its address, and it is the lowest
+ /// address currently asserting the ALERT/RDY bus line.
+ Latching
+}
+
/// Possible slave addresses
#[derive(Debug, Clone)]
pub enum SlaveAddr {
@@ -220,6 +240,7 @@ impl BitFlags {
const DR0 : u16 = 0b0000_0000_0010_0000;
const COMP_MODE : u16 = 0b0000_0000_0001_0000;
const COMP_POL : u16 = 0b0000_0000_0000_1000;
+ const COMP_LAT : u16 = 0b0000_0000_0000_0100;
}
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 8ffd50e..695280e 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -23,6 +23,7 @@ impl BitFlags {
pub const DR0 : u16 = 0b0000_0000_0010_0000;
pub const COMP_MODE : u16 = 0b0000_0000_0001_0000;
pub const COMP_POL : u16 = 0b0000_0000_0000_1000;
+ pub const COMP_LAT : u16 = 0b0000_0000_0000_0100;
}
pub struct Config {
diff --git a/tests/tier2_i2c.rs b/tests/tier2_i2c.rs
index e8162c4..e032a6c 100644
--- a/tests/tier2_i2c.rs
+++ b/tests/tier2_i2c.rs
@@ -2,7 +2,7 @@ extern crate embedded_hal;
extern crate embedded_hal_mock as hal;
use hal::i2c::Transaction as I2cTrans;
extern crate ads1x1x;
-use ads1x1x::{ ComparatorMode, ComparatorPolarity };
+use ads1x1x::{ ComparatorMode, ComparatorPolarity, ComparatorLatching};
#[macro_use]
mod common;
@@ -33,4 +33,9 @@ mod can_set_comparator_polarity {
set_value_test!(high, set_comparator_polarity, ComparatorPolarity::ActiveHigh, Config::default().with_high(BitFlags::COMP_POL));
}
+mod can_set_comparator_latching {
+ use super::*;
+ set_value_test!(non, set_comparator_latching, ComparatorLatching::Nonlatching, Config::default().with_low( BitFlags::COMP_LAT));
+ set_value_test!(lat, set_comparator_latching, ComparatorLatching::Latching, Config::default().with_high(BitFlags::COMP_LAT));
+}