Add function to set the square-wave output frequency

pull/4/head
Diego Barrios Romero 2018-10-31 06:55:16 +01:00
parent 5168dbe68f
commit 692e3ad834
4 changed files with 40 additions and 1 deletions

View File

@ -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<DI, IC, E> Ds323x<DI, IC>
@ -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<E>> {
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<E>> {
self.iface.write_register(Register::CONTROL, control)?;
self.control = control;

View File

@ -333,6 +333,19 @@ pub enum Error<E> {
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;

View File

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

View File

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