diff options
| -rw-r--r-- | src/devices/ads1x1x/mode/oneshot.rs | 13 | ||||
| -rw-r--r-- | src/devices/channels.rs | 18 | ||||
| -rw-r--r-- | src/lib.rs | 3 | ||||
| -rw-r--r-- | tests/common/mod.rs | 4 | ||||
| -rw-r--r-- | tests/mux_i2c.rs | 41 | 
5 files changed, 73 insertions, 6 deletions
| diff --git a/src/devices/ads1x1x/mode/oneshot.rs b/src/devices/ads1x1x/mode/oneshot.rs index 0da4a7b..7ff9ac9 100644 --- a/src/devices/ads1x1x/mode/oneshot.rs +++ b/src/devices/ads1x1x/mode/oneshot.rs @@ -3,7 +3,8 @@  use core::marker::PhantomData;  use { Ads1x1x, mode, Error, Register, BitFlags, Config, ic };  use { interface, hal, nb }; -use super::super::OperatingMode; +use devices::ads1x1x::OperatingMode; +use devices::channels::ChannelSelection;  use super::convert_measurement;  impl<DI, IC, E> Ads1x1x<DI, IC, mode::OneShot> @@ -30,8 +31,8 @@ where          Ok(!config.is_high(BitFlags::OS))      } -    fn trigger_measurement(&mut self) -> Result<(), Error<E>> { -        let config = self.config.with_high(BitFlags::OS); +    fn trigger_measurement(&mut self, config: &Config) -> Result<(), Error<E>> { +        let config = config.with_high(BitFlags::OS);          self.iface.write_register(Register::CONFIG, config.bits)      }  } @@ -40,7 +41,7 @@ impl<DI, IC, E, CH> hal::adc::OneShot<Ads1x1x<DI, IC, mode::OneShot>, i16, CH> f  where      DI: interface::ReadData<Error = E> + interface::WriteData<Error = E>,      IC: ic::Resolution, -    CH: hal::adc::Channel<Ads1x1x<DI, IC, mode::OneShot>> +    CH: hal::adc::Channel<Ads1x1x<DI, IC, mode::OneShot>, ID = ChannelSelection>  {      type Error = Error<E>; @@ -56,7 +57,9 @@ where              self.a_conversion_was_started = false;              return Ok(convert_measurement::<IC>(value));          } -        self.trigger_measurement().map_err(nb::Error::Other)?; +        let config = self.config.with_mux_bits(CH::channel()); +        self.trigger_measurement(&config).map_err(nb::Error::Other)?; +        self.config = config;          self.a_conversion_was_started = true;          Err(nb::Error::WouldBlock)      } diff --git a/src/devices/channels.rs b/src/devices/channels.rs index 2482df5..c202ad3 100644 --- a/src/devices/channels.rs +++ b/src/devices/channels.rs @@ -1,5 +1,5 @@  //! ADC input channels -use { Ads1x1x, ic, hal }; +use { Ads1x1x, ic, hal, Config, BitFlags as BF };  /// ADC input channel selection  #[allow(dead_code)] @@ -68,3 +68,19 @@ impl_channel!(Ads1115, SingleA0);  impl_channel!(Ads1115, SingleA1);  impl_channel!(Ads1115, SingleA2);  impl_channel!(Ads1115, SingleA3); + +impl Config { +    pub(crate) fn with_mux_bits(&self, ch: ChannelSelection) -> Self { +        use self::ChannelSelection as CS; +        match ch { +            CS::DifferentialA0A1 => self.with_low( BF::MUX2).with_low( BF::MUX1).with_low( BF::MUX0), +            CS::DifferentialA0A3 => self.with_low( BF::MUX2).with_low( BF::MUX1).with_high(BF::MUX0), +            CS::DifferentialA1A3 => self.with_low( BF::MUX2).with_high(BF::MUX1).with_low( BF::MUX0), +            CS::DifferentialA2A3 => self.with_low( BF::MUX2).with_high(BF::MUX1).with_high(BF::MUX0), +            CS::SingleA0         => self.with_high(BF::MUX2).with_low( BF::MUX1).with_low( BF::MUX0), +            CS::SingleA1         => self.with_high(BF::MUX2).with_low( BF::MUX1).with_high(BF::MUX0), +            CS::SingleA2         => self.with_high(BF::MUX2).with_high(BF::MUX1).with_low( BF::MUX0), +            CS::SingleA3         => self.with_high(BF::MUX2).with_high(BF::MUX1).with_high(BF::MUX0), +        } +    } +} @@ -237,6 +237,9 @@ struct BitFlags;  impl BitFlags {      const OP_MODE      : u16 = 0b0000_0001_0000_0000;      const OS           : u16 = 0b1000_0000_0000_0000; +    const MUX2         : u16 = 0b0100_0000_0000_0000; +    const MUX1         : u16 = 0b0010_0000_0000_0000; +    const MUX0         : u16 = 0b0001_0000_0000_0000;      const DR2          : u16 = 0b0000_0000_1000_0000;      const DR1          : u16 = 0b0000_0000_0100_0000;      const DR0          : u16 = 0b0000_0000_0010_0000; diff --git a/tests/common/mod.rs b/tests/common/mod.rs index ec63ec0..886967c 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -18,6 +18,9 @@ pub struct BitFlags;  impl BitFlags {      //pub const OP_MODE      : u16 = 0b0000_0001_0000_0000;      pub const OS           : u16 = 0b1000_0000_0000_0000; +    pub const MUX2         : u16 = 0b0100_0000_0000_0000; +    pub const MUX1         : u16 = 0b0010_0000_0000_0000; +    pub const MUX0         : u16 = 0b0001_0000_0000_0000;      pub const DR2          : u16 = 0b0000_0000_1000_0000;      pub const DR1          : u16 = 0b0000_0000_0100_0000;      pub const DR0          : u16 = 0b0000_0000_0010_0000; @@ -71,6 +74,7 @@ macro_rules! impl_new_destroy {  impl_new_destroy!(Ads1013, new_ads1013, destroy_ads1013, I2cTrans, interface::I2cInterface<I2cMock>);  impl_new_destroy!(Ads1113, new_ads1113, destroy_ads1113, I2cTrans, interface::I2cInterface<I2cMock>);  impl_new_destroy!(Ads1014, new_ads1014, destroy_ads1014, I2cTrans, interface::I2cInterface<I2cMock>); +impl_new_destroy!(Ads1015, new_ads1015, destroy_ads1015, I2cTrans, interface::I2cInterface<I2cMock>);  #[macro_export]  macro_rules! assert_would_block { diff --git a/tests/mux_i2c.rs b/tests/mux_i2c.rs new file mode 100644 index 0000000..95f97a5 --- /dev/null +++ b/tests/mux_i2c.rs @@ -0,0 +1,41 @@ +#[macro_use(block)] +extern crate nb; +extern crate embedded_hal; +extern crate embedded_hal_mock as hal; +use hal::i2c::Transaction as I2cTrans; +extern crate ads1x1x; +use ads1x1x::channel; + +#[macro_use] +mod common; +use common::{ new_ads1015 as new, destroy_ads1015 as destroy, +              DEVICE_ADDRESS as DEV_ADDR, Register, BitFlags as BF, Config }; + +use embedded_hal::adc::OneShot; + +macro_rules! mux_test { +    ($name:ident, $CS:ident, $config_bits:expr) => { +        #[test] +        fn $name() { +            let default_config = Config::default(); +            let config = Config::default().with_high(BF::OS).with_high($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_read(DEV_ADDR, vec![Register::CONVERSION], vec![0x80, 0x00] ) ]; +            let mut dev = new(&transactions); +            let measurement = block!(dev.read(&mut channel::$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); | 
