From 692e3ad8346dc625e3027fe74d729fbd76fce198 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Wed, 31 Oct 2018 06:55:16 +0100 Subject: [PATCH] Add function to set the square-wave output frequency --- src/ds323x/configuration.rs | 15 ++++++++++++++- src/lib.rs | 15 +++++++++++++++ tests/common/mod.rs | 2 ++ tests/configuration.rs | 9 +++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/ds323x/configuration.rs b/src/ds323x/configuration.rs index 6d61dbc..d0b12c0 100644 --- a/src/ds323x/configuration.rs +++ b/src/ds323x/configuration.rs @@ -1,7 +1,7 @@ //! Device configuration extern crate embedded_hal as hal; -use super::super::{ Ds323x, Register, BitFlags, Error }; +use super::super::{ Ds323x, SqWFreq, Register, BitFlags, Error }; use interface::{ ReadData, WriteData }; impl Ds323x @@ -88,6 +88,19 @@ where let control = self.control; self.write_control(control & !BitFlags::BBSQW) } + + /// Set the square-wave output frequency. + pub fn set_square_wave_frequency(&mut self, freq: SqWFreq) -> Result<(), Error> { + let new_control; + match freq { + SqWFreq::_1Hz => new_control = self.control & !BitFlags::RS2 & !BitFlags::RS1, + SqWFreq::_1_024Hz => new_control = self.control & !BitFlags::RS2 | BitFlags::RS1, + SqWFreq::_4_096Hz => new_control = self.control | BitFlags::RS2 & !BitFlags::RS1, + SqWFreq::_8_192Hz => new_control = self.control | BitFlags::RS2 | BitFlags::RS1, + } + self.write_control(new_control) + } + fn write_control(&mut self, control: u8) -> Result<(), Error> { self.iface.write_register(Register::CONTROL, control)?; self.control = control; diff --git a/src/lib.rs b/src/lib.rs index 2991421..ddb7a1e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -333,6 +333,19 @@ pub enum Error { InvalidInputData } +/// Square-wave output frequency +#[derive(Debug, Clone, PartialEq)] +pub enum SqWFreq { + /// 1 Hz + _1Hz, + /// 1.024 Hz + _1_024Hz, + /// 4.096 Hz + _4_096Hz, + /// 8.192 Hz + _8_192Hz, +} + struct Register; impl Register { @@ -358,6 +371,8 @@ impl BitFlags { const EOSC : u8 = 0b1000_0000; const BBSQW : u8 = 0b0100_0000; const TEMP_CONV : u8 = 0b0010_0000; + const RS2 : u8 = 0b0001_0000; + const RS1 : u8 = 0b0000_1000; const INTCN : u8 = 0b0000_0100; const BUSY : u8 = 0b0000_0100; const EN32KHZ : u8 = 0b0000_1000; diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 5bd1da2..07509ad 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -31,6 +31,8 @@ impl BitFlags { pub const EOSC : u8 = 0b1000_0000; pub const BBSQW : u8 = 0b0100_0000; pub const TEMP_CONV : u8 = 0b0010_0000; + pub const RS2 : u8 = 0b0001_0000; + pub const RS1 : u8 = 0b0000_1000; pub const INTCN : u8 = 0b0000_0100; pub const BUSY : u8 = 0b0000_0100; pub const EN32KHZ : u8 = 0b0000_1000; diff --git a/tests/configuration.rs b/tests/configuration.rs index cb0f9f4..e2ac1be 100644 --- a/tests/configuration.rs +++ b/tests/configuration.rs @@ -3,6 +3,10 @@ extern crate embedded_hal_mock as hal; use hal::i2c::Transaction as I2cTrans; use hal::spi::Transaction as SpiTrans; + +extern crate ds323x; +use ds323x::SqWFreq; + mod common; use common::{ DEVICE_ADDRESS as DEV_ADDR, Register, new_ds3231, new_ds3232, new_ds3234, destroy_ds3231, destroy_ds3232, @@ -65,3 +69,8 @@ call_method_test!(int_sqw_out_sqw, use_int_sqw_output_as_square_wave, CONTROL, C call_method_test!(enable_sqw, enable_square_wave, CONTROL, CONTROL_POR_VALUE | BF::BBSQW); call_method_test!(disable_sqw, disable_square_wave, CONTROL, CONTROL_POR_VALUE & !BF::BBSQW); +set_param_test!(set_sqw_freq_1, set_square_wave_frequency, CONTROL, SqWFreq::_1Hz, CONTROL_POR_VALUE & !BF::RS2 & !BF::RS1); +set_param_test!(set_sqw_freq_1_024, set_square_wave_frequency, CONTROL, SqWFreq::_1_024Hz, CONTROL_POR_VALUE & !BF::RS2 | BF::RS1); +set_param_test!(set_sqw_freq_4_096, set_square_wave_frequency, CONTROL, SqWFreq::_4_096Hz, CONTROL_POR_VALUE | BF::RS2 & !BF::RS1); +set_param_test!(set_sqw_freq_8_192, set_square_wave_frequency, CONTROL, SqWFreq::_8_192Hz, CONTROL_POR_VALUE | BF::RS2 | BF::RS1); +