From ad572b64eb0e99f269297d8de19d709ef66d3f24 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Tue, 20 Nov 2018 20:24:21 +0100 Subject: Add function to use the ALERT/RDY pin as conversion-ready pin --- README.md | 1 + src/devices/features/tier2.rs | 15 +++++++++++++++ src/lib.rs | 2 ++ tests/tier2_i2c.rs | 23 +++++++++++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/README.md b/README.md index e64e557..f471bb5 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ This driver allows you to: - Set the comparator latching. See: `set_comparator_latching()`. - Set the comparator queue. See: `set_comparator_queue()`. - Disable the comparator. See: `disable_comparator()`. +- Set the ALERT/RDY pin to be used as conversion-ready pin. See: `use_alert_rdy_pin_as_ready()`. ## The devices diff --git a/src/devices/features/tier2.rs b/src/devices/features/tier2.rs index 79843f6..6047572 100644 --- a/src/devices/features/tier2.rs +++ b/src/devices/features/tier2.rs @@ -115,4 +115,19 @@ where self.config = config; Ok(()) } + + /// Use the ALERT/RDY pin as conversion-ready pin. + /// + /// This the ALERT/RDY pin outputs the OS bit when in OneShot mode, and + /// provides a continuous-conversion ready pulse when in + /// continuous-conversion mode. + /// + /// When calling this the comparator will be disabled and the thresholds will be cleared. + pub fn use_alert_rdy_pin_as_ready(&mut self) -> Result<(), Error> { + if self.config != self.config.with_high(BitFlags::COMP_QUE1).with_high(BitFlags::COMP_QUE0) { + self.disable_comparator()?; + } + self.iface.write_register(Register::HIGH_TH, 0x8000)?; + self.iface.write_register(Register::LOW_TH, 0) + } } diff --git a/src/lib.rs b/src/lib.rs index fd4181e..5e94b43 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,7 @@ //! - Set the comparator latching. See: [`set_comparator_latching()`]. //! - Set the comparator queue. See: [`set_comparator_queue()`]. //! - Disable the comparator. See: [`disable_comparator()`]. +//! - Set the ALERT/RDY pin to be used as conversion-ready pin. See: [`use_alert_rdy_pin_as_ready()`]. //! //! [`into_continuous()`]: struct.Ads1x1x.html#method.into_continuous //! [read_os]: struct.Ads1x1x.html#method.read @@ -30,6 +31,7 @@ //! [`set_comparator_latching()`]: struct.Ads1x1x.html#method.set_comparator_latching //! [`set_comparator_queue()`]: struct.Ads1x1x.html#method.set_comparator_queue //! [`disable_comparator()`]: struct.Ads1x1x.html#method.disable_comparator +//! [`use_alert_rdy_pin_as_ready()`]: struct.Ads1x1x.html#method.use_alert_rdy_pin_as_ready //! //! ## The devices //! diff --git a/tests/tier2_i2c.rs b/tests/tier2_i2c.rs index 7d17270..4e43e90 100644 --- a/tests/tier2_i2c.rs +++ b/tests/tier2_i2c.rs @@ -67,6 +67,29 @@ mod can_set_comparator_queue { config_test!(four, set_comparator_queue, ComparatorQueue::Four, Config::default().with_high(BitFlags::COMP_QUE1).with_low( BitFlags::COMP_QUE0)); } +#[test] +fn can_use_alert_rdy_pin_as_rdy_does_not_disable_comparator_if_already_disabled() { + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0b1000_0000, 0]), + I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]), ]; + let mut dev = new_ads1014(&transactions); + dev.use_alert_rdy_pin_as_ready().unwrap(); + destroy_ads1014(dev); +} + +#[test] +fn can_use_alert_rdy_pin_as_rdy_disabled_comparator() { + let config = Config::default().with_low(BitFlags::COMP_QUE1).with_low(BitFlags::COMP_QUE0); + let config_disabled_comp = Config::default().with_high(BitFlags::COMP_QUE1).with_high(BitFlags::COMP_QUE0); + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), + I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config_disabled_comp.msb(), config_disabled_comp.lsb()]), + I2cTrans::write(DEV_ADDR, vec![Register::HIGH_TH, 0b1000_0000, 0]), + I2cTrans::write(DEV_ADDR, vec![Register::LOW_TH, 0, 0]), ]; + let mut dev = new_ads1014(&transactions); + dev.set_comparator_queue(ComparatorQueue::One).unwrap(); + dev.use_alert_rdy_pin_as_ready().unwrap(); + destroy_ads1014(dev); +} + mod can_set_full_scale_range { use super::*; config_test!(fsr6, set_full_scale_range, FullScaleRange::Within6_144V, Config::default().with_low( BitFlags::PGA2).with_low( BitFlags::PGA1).with_low( BitFlags::PGA0)); -- cgit v1.2.3-54-g00ecf