diff options
-rw-r--r-- | src/devices/mode/oneshot.rs | 12 | ||||
-rw-r--r-- | tests/mux_i2c.rs | 37 |
2 files changed, 37 insertions, 12 deletions
diff --git a/src/devices/mode/oneshot.rs b/src/devices/mode/oneshot.rs index 30958ab..57e72a0 100644 --- a/src/devices/mode/oneshot.rs +++ b/src/devices/mode/oneshot.rs @@ -45,13 +45,19 @@ where { type Error = Error<E>; - /// Request that the ADC begin a conversion on the specified channel + /// Request that the ADC begin a conversion on the specified channel. + /// + /// Returns `nb::Error::WouldBlock` while a measurement is in progress. + /// + /// In case a measurement was requested and after is it is finished a + /// measurement on a different channel is requested, a new measurement on + /// using the new channel selection is triggered. fn read(&mut self, _channel: &mut CH) -> nb::Result<i16, Self::Error> { - //TODO for devices with MUX select channel, if it is the wrong one, return AlreadyInProgress or WrongChannel error if self.is_measurement_in_progress().map_err(nb::Error::Other)? { return Err(nb::Error::WouldBlock); } - if self.a_conversion_was_started { + let same_channel = self.config == self.config.with_mux_bits(CH::channel()); + if self.a_conversion_was_started && same_channel { // result is ready let value = self.iface.read_register(Register::CONVERSION).map_err(nb::Error::Other)?; self.a_conversion_was_started = false; diff --git a/tests/mux_i2c.rs b/tests/mux_i2c.rs index 48fc325..ca10673 100644 --- a/tests/mux_i2c.rs +++ b/tests/mux_i2c.rs @@ -13,10 +13,11 @@ use common::{ new_ads1015 as new, destroy_ads1015 as destroy, macro_rules! mux_test { - ($name:ident, $CS:ident, $config_bits:expr) => { + ($name:ident, $CS:ident, $config_bits:expr, $other_CS:ident, $other_config_bits:expr) => { mod $name { use embedded_hal::adc::OneShot; use super::*; + #[test] fn can_read() { let default_config = Config::default(); @@ -30,15 +31,33 @@ macro_rules! mux_test { assert_eq!(-2048, measurement); destroy(dev); } + + #[test] + fn read_then_read_different_triggers_new_measurement() { + let default_config = Config::default(); + let config = Config::default().with_high(BF::OS).with_high($config_bits); + let other_config = Config::default().with_high($other_config_bits); + let transactions = [ I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![default_config.msb(), default_config.lsb()]), + I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, config.msb(), config.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![config.msb(), config.lsb()]), + I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, other_config.msb(), other_config.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONFIG], vec![other_config.msb(), other_config.lsb()]), + I2cTrans::write_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; + let mut dev = new(&transactions); + assert_would_block!(dev.read(&mut channel::$CS)); + let measurement = block!(dev.read(&mut channel::$other_CS)).unwrap(); + assert_eq!(-2048, measurement); + destroy(dev); + } } }; } -mux_test!(diffa0a1, DifferentialA0A1, 0); -mux_test!(diffa0a3, DifferentialA0A3, BF::MUX0); -mux_test!(diffa1a3, DifferentialA1A3, BF::MUX1); -mux_test!(diffa2a3, DifferentialA2A3, BF::MUX1 | BF::MUX0); -mux_test!(singlea0, SingleA0, BF::MUX2); -mux_test!(singlea1, SingleA1, BF::MUX2 | BF::MUX0); -mux_test!(singlea2, SingleA2, BF::MUX2 | BF::MUX1); -mux_test!(singlea3, SingleA3, BF::MUX2 | BF::MUX1 | BF::MUX0); +mux_test!(diffa0a1, DifferentialA0A1, 0, SingleA0, BF::MUX2); +mux_test!(diffa0a3, DifferentialA0A3, BF::MUX0, SingleA0, BF::MUX2); +mux_test!(diffa1a3, DifferentialA1A3, BF::MUX1, SingleA0, BF::MUX2); +mux_test!(diffa2a3, DifferentialA2A3, BF::MUX1 | BF::MUX0, SingleA0, BF::MUX2); +mux_test!(singlea0, SingleA0, BF::MUX2, DifferentialA0A1, 0); +mux_test!(singlea1, SingleA1, BF::MUX2 | BF::MUX0, SingleA0, BF::MUX2); +mux_test!(singlea2, SingleA2, BF::MUX2 | BF::MUX1, SingleA0, BF::MUX2); +mux_test!(singlea3, SingleA3, BF::MUX2 | BF::MUX1 | BF::MUX0, SingleA0, BF::MUX2); |