Add functions to enable/disable the 32kHz output when battery-powered

pull/4/head
Diego Barrios Romero 2018-10-31 10:10:41 +01:00
parent 8bea870813
commit 4361e87645
7 changed files with 132 additions and 27 deletions

View File

@ -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

33
src/ds3232.rs 100644
View File

@ -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<I2C, E> Ds323x<I2cInterface<I2C>, ic::DS3232>
where
I2C: blocking::i2c::Write<Error = E> + blocking::i2c::WriteRead<Error = E>
{
/// 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<E>> {
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<E>> {
let status = self.status & !BitFlags::BB32KHZ;
self.write_status_without_clearing_alarm(status)
}
}

34
src/ds3234.rs 100644
View File

@ -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<SPI, CS, E> Ds323x<SpiInterface<SPI, CS>, ic::DS3234>
where
SPI: blocking::spi::Transfer<u8, Error = E> + blocking::spi::Write<u8, Error = E>,
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<E>> {
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<E>> {
let status = self.status & !BitFlags::BB32KHZ;
self.write_status_without_clearing_alarm(status)
}
}

View File

@ -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;

View File

@ -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();
}

42
tests/ds3232_4.rs 100644
View File

@ -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 );

View File

@ -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();
}