diff --git a/README.md b/README.md index 9a447b1..e0c3a9a 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ This driver allows you to: - Select the function of the INT/SQW output pin. See `use_int_sqw_output_as_interrupt`. - Enable and disable the square-wave generation. See `enable_square_wave`. - Select the square-wave frequency. See `set_square_wave_frequency`. +- Enable and disable the 32kHz output when battery powered. See `enable_32khz_output_on_battery`. ## The devices diff --git a/src/ds3232.rs b/src/ds3232.rs new file mode 100644 index 0000000..434a280 --- /dev/null +++ b/src/ds3232.rs @@ -0,0 +1,33 @@ +//! Functions exclusive of DS3232 + +extern crate embedded_hal as hal; +use hal::blocking; +use super::{ Ds323x, BitFlags, Error, ic }; +use interface::I2cInterface; + +impl Ds323x, ic::DS3232> +where + I2C: blocking::i2c::Write + blocking::i2c::WriteRead +{ + /// Enable the 32kHz output when battery-powered. + /// + /// Additionally, the 32kHz output needs to be enabled. See + /// [`enable_32khz_output`](#method.enable_32khz_output). + /// + /// Note: This is only available for DS3232 and DS3234 devices. + pub fn enable_32khz_output_on_battery(&mut self) -> Result<(), Error> { + let status = self.status | BitFlags::BB32KHZ; + self.write_status_without_clearing_alarm(status) + } + + /// Disable the 32kHz output when battery-powered. + /// + /// The 32kHz output will still generate a wave when not battery-powered if + /// it enabled. See [`enable_32khz_output`](#method.enable_32khz_output). + /// + /// Note: This is only available for DS3232 and DS3234 devices. + pub fn disable_32khz_output_on_battery(&mut self) -> Result<(), Error> { + let status = self.status & !BitFlags::BB32KHZ; + self.write_status_without_clearing_alarm(status) + } +} diff --git a/src/ds3234.rs b/src/ds3234.rs new file mode 100644 index 0000000..f406afd --- /dev/null +++ b/src/ds3234.rs @@ -0,0 +1,34 @@ +//! Functions exclusive of DS3234 + +extern crate embedded_hal as hal; +use hal::blocking; +use super::{ Ds323x, BitFlags, Error, ic }; +use interface::SpiInterface; + +impl Ds323x, ic::DS3234> +where + SPI: blocking::spi::Transfer + blocking::spi::Write, + CS: hal::digital::OutputPin +{ + /// Enable the 32kHz output when battery-powered. + /// + /// Additionally, the 32kHz output needs to be enabled. See + /// [`enable_32khz_output`](#method.enable_32khz_output). + /// + /// Note: This is only available for DS3232 and DS3234 devices. + pub fn enable_32khz_output_on_battery(&mut self) -> Result<(), Error> { + let status = self.status | BitFlags::BB32KHZ; + self.write_status_without_clearing_alarm(status) + } + + /// Disable the 32kHz output when battery-powered. + /// + /// The 32kHz output will still generate a wave when not battery-powered if + /// it enabled. See [`enable_32khz_output`](#method.enable_32khz_output). + /// + /// Note: This is only available for DS3232 and DS3234 devices. + pub fn disable_32khz_output_on_battery(&mut self) -> Result<(), Error> { + let status = self.status & !BitFlags::BB32KHZ; + self.write_status_without_clearing_alarm(status) + } +} diff --git a/src/lib.rs b/src/lib.rs index 8f8e292..bfa358f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,7 @@ //! - Select the function of the INT/SQW output pin. See [`use_int_sqw_output_as_interrupt`]. //! - Enable and disable the square-wave generation. See [`enable_square_wave`]. //! - Select the square-wave frequency. See [`set_square_wave_frequency`]. +//! - Enable and disable the 32kHz output when battery powered. See [`enable_32khz_output_on_battery`]. //! //! [`get_datetime`]: struct.Ds323x.html#method.get_datetime //! [`get_year`]: struct.Ds323x.html#method.get_year @@ -31,6 +32,7 @@ //! [`use_int_sqw_output_as_interrupt`]: Struct.Ds323x.html#method.use_int_sqw_output_as_interrupt //! [`enable_square_wave`]: Struct.Ds323x.html#method.enable_square_wave //! [`set_square_wave_frequency`]: Struct.Ds323x.html#method.set_square_wave_frequency +//! [`enable_32khz_output_on_battery`]: Struct.Ds323x.html#method.enable_32khz_output_on_battery //! //! ## The devices //! @@ -327,6 +329,24 @@ //! # } //! ``` //! +//! ### Enable the 32kHz output except when on battery power +//! +//! Additionally enabling the output depending on the power source is only +//! available for the devices DS3232 and DS3234. +//! +//! ```no_run +//! extern crate linux_embedded_hal as hal; +//! extern crate ds323x; +//! use ds323x::{ Ds323x, SqWFreq }; +//! +//! # fn main() { +//! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); +//! let mut rtc = Ds323x::new_ds3232(dev); +//! rtc.disable_32khz_output_on_battery().unwrap(); +//! rtc.enable_32khz_output().unwrap(); +//! # } +//! ``` +//! //! ### Set the aging offset //! //! ```no_run @@ -506,3 +526,5 @@ where mod ds323x; pub use ds323x::{ Hours, DateTime }; +mod ds3232; +mod ds3234; diff --git a/tests/ds3232.rs b/tests/ds3232.rs deleted file mode 100644 index ec80ae4..0000000 --- a/tests/ds3232.rs +++ /dev/null @@ -1,12 +0,0 @@ -#[deny(warnings)] - -extern crate embedded_hal_mock as hal; -extern crate ds323x; -use ds323x::Ds323x; - -#[test] -fn can_create_and_destroy() { - let dev = Ds323x::new_ds3232(hal::i2c::Mock::new(&[])); - let mut i2c = dev.destroy_ds3232(); - i2c.done(); -} diff --git a/tests/ds3232_4.rs b/tests/ds3232_4.rs new file mode 100644 index 0000000..ce50905 --- /dev/null +++ b/tests/ds3232_4.rs @@ -0,0 +1,42 @@ +#[deny(warnings)] + +extern crate embedded_hal_mock as hal; +use hal::i2c::Transaction as I2cTrans; +use hal::spi::Transaction as SpiTrans; + +#[allow(unused)] +mod common; +use common::{ DEVICE_ADDRESS as DEV_ADDR, Register, + new_ds3232, new_ds3234, destroy_ds3232, + destroy_ds3234, BitFlags as BF, DS323X_POR_STATUS }; + +macro_rules! call_method_status_test { + ($name:ident, $method:ident, $value:expr) => { + mod $name { + use super::*; + call_test!(can_call_ds3232, $method, new_ds3232, destroy_ds3232, + [ I2cTrans::write(DEV_ADDR, vec![Register::STATUS, $value]) ]); + call_test!(can_call_ds3234, $method, new_ds3234, destroy_ds3234, + [ SpiTrans::write(vec![Register::STATUS + 0x80, $value]) ]); + } + }; +} + +#[test] +fn can_create_and_destroy_ds3232() { + let dev = new_ds3232(&[]); + destroy_ds3232(dev); +} + +#[test] +fn can_create_and_destroy_ds3234() { + let dev = new_ds3234(&[]); + destroy_ds3234(dev); +} + +call_method_status_test!(can_en_32khz_bat, enable_32khz_output_on_battery, + DS323X_POR_STATUS | BF::BB32KHZ | BF::ALARM2F | BF::ALARM1F ); + +call_method_status_test!(can_dis_32khz_bat, disable_32khz_output_on_battery, + DS323X_POR_STATUS & !BF::BB32KHZ | BF::ALARM2F | BF::ALARM1F ); + diff --git a/tests/ds3234.rs b/tests/ds3234.rs deleted file mode 100644 index 1516fad..0000000 --- a/tests/ds3234.rs +++ /dev/null @@ -1,15 +0,0 @@ -#[deny(warnings)] - -extern crate embedded_hal_mock as hal; -extern crate ds323x; -use ds323x::Ds323x; -#[allow(dead_code)] -mod common; -use common::DummyOutputPin; - -#[test] -fn can_create_and_destroy() { - let dev = Ds323x::new_ds3234(hal::spi::Mock::new(&[]), DummyOutputPin); - let (mut spi, _cs) = dev.destroy_ds3234(); - spi.done(); -}