From 62a9825c41fd0b859eeb2aa3a25b8f2a2eba58b5 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Sun, 11 Nov 2018 18:04:37 +0100 Subject: Add support for setting the data rate in 16bit devices --- src/devices/features/tier1.rs | 20 ++++++++------- src/lib.rs | 21 ++++++++++++++++ tests/tier1_i2c.rs | 58 ++++++++++++++++++++++++++++++------------- 3 files changed, 73 insertions(+), 26 deletions(-) diff --git a/src/devices/features/tier1.rs b/src/devices/features/tier1.rs index bd19419..16ed96c 100644 --- a/src/devices/features/tier1.rs +++ b/src/devices/features/tier1.rs @@ -1,6 +1,6 @@ //! Common functions -use { Ads1x1x, DataRate12Bit, Error, Register, BitFlags, interface, ic }; +use { Ads1x1x, DataRate12Bit, DataRate16Bit, Error, Register, BitFlags, interface, ic }; impl Ads1x1x where @@ -24,20 +24,22 @@ where } } +impl Ads1x1x where DI: interface::WriteData { /// Set data rate - pub fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error> { + pub fn set_data_rate(&mut self, rate: DataRate16Bit) -> Result<(), Error> { let config; match rate { - DataRate::Sps128 => config = self.config.with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0), - DataRate::Sps250 => config = self.config.with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0), - DataRate::Sps490 => config = self.config.with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0), - DataRate::Sps920 => config = self.config.with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0), - DataRate::Sps1600 => config = self.config.with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0), - DataRate::Sps2400 => config = self.config.with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0), - DataRate::Sps3300 => config = self.config.with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0), + DataRate16Bit::Sps8 => config = self.config.with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0), + DataRate16Bit::Sps16 => config = self.config.with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0), + DataRate16Bit::Sps32 => config = self.config.with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0), + DataRate16Bit::Sps64 => config = self.config.with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0), + DataRate16Bit::Sps128 => config = self.config.with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0), + DataRate16Bit::Sps250 => config = self.config.with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0), + DataRate16Bit::Sps475 => config = self.config.with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0), + DataRate16Bit::Sps860 => config = self.config.with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0), } self.iface.write_register(Register::CONFIG, config.bits)?; self.config = config; diff --git a/src/lib.rs b/src/lib.rs index d4b10e7..4abe8f8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -150,6 +150,27 @@ pub enum DataRate12Bit { Sps3300 } + +/// Data rate for ADS1113, ADS1114, ADS1115, ADS1118 +#[derive(Debug, Clone, PartialEq)] +pub enum DataRate16Bit { + /// 8 SPS + Sps8, + /// 16 SPS + Sps16, + /// 32 SPS + Sps32, + /// 64 SPS + Sps64, + /// 128 SPS (default) + Sps128, + /// 250 SPS + Sps250, + /// 475 SPS + Sps475, + /// 860 SPS + Sps860 +} /// Comparator mode (only for ADS1x14, ADS1x15) #[derive(Debug, Clone, PartialEq)] pub enum ComparatorMode { diff --git a/tests/tier1_i2c.rs b/tests/tier1_i2c.rs index 5484a73..845c919 100644 --- a/tests/tier1_i2c.rs +++ b/tests/tier1_i2c.rs @@ -4,7 +4,7 @@ extern crate embedded_hal; extern crate embedded_hal_mock as hal; use hal::i2c::Transaction as I2cTrans; extern crate ads1x1x; -use ads1x1x::{ channel, DataRate12Bit }; +use ads1x1x::{ channel, DataRate12Bit, DataRate16Bit }; #[macro_use] mod common; @@ -56,25 +56,49 @@ impl_tests!(ads1013, new_ads1013, destroy_ads1013, -2048); impl_tests!(ads1113, new_ads1113, destroy_ads1113, -32768); -macro_rules! test_set_data_rate { - ($name:ident, $variant:ident, $config:expr) => { - #[test] - fn $name() { - let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, $config.msb(), $config.lsb()]) ]; - let mut dev = new_ads1013(&transactions); - dev.set_data_rate(DataRate12Bit::$variant).unwrap(); - destroy_ads1013(dev); +mod data_rate_12bit { + macro_rules! test { + ($name:ident, $variant:ident, $config:expr) => { + #[test] + fn $name() { + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, $config.msb(), $config.lsb()]) ]; + let mut dev = new_ads1013(&transactions); + dev.set_data_rate(DataRate12Bit::$variant).unwrap(); + destroy_ads1013(dev); + } } } + + use super::*; + test!(sps128, Sps128, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps250, Sps250, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); + test!(sps490, Sps490, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps920, Sps920, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); + test!(sps1600, Sps1600, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps2400, Sps2400, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); + test!(sps3300, Sps3300, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); } -mod data_rate { +mod data_rate_16bit { + macro_rules! test { + ($name:ident, $variant:ident, $config:expr) => { + #[test] + fn $name() { + let transactions = [ I2cTrans::write(DEV_ADDR, vec![Register::CONFIG, $config.msb(), $config.lsb()]) ]; + let mut dev = new_ads1113(&transactions); + dev.set_data_rate(DataRate16Bit::$variant).unwrap(); + destroy_ads1113(dev); + } + } + } + use super::*; - test_set_data_rate!(sps128, Sps128, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); - test_set_data_rate!(sps250, Sps250, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); - test_set_data_rate!(sps490, Sps490, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); - test_set_data_rate!(sps920, Sps920, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); - test_set_data_rate!(sps1600, Sps1600, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); - test_set_data_rate!(sps2400, Sps2400, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); - test_set_data_rate!(sps3300, Sps3300, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps8, Sps8, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps16, Sps16, Config::default().with_low( BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); + test!(sps32, Sps32, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps64, Sps64, Config::default().with_low( BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); + test!(sps128, Sps128, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps250, Sps250, Config::default().with_high(BitFlags::DR2).with_low( BitFlags::DR1).with_high(BitFlags::DR0)); + test!(sps475, Sps475, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_low( BitFlags::DR0)); + test!(sps860, Sps860, Config::default().with_high(BitFlags::DR2).with_high(BitFlags::DR1).with_high(BitFlags::DR0)); } -- cgit v1.2.3-54-g00ecf