From d883c9c499b3e4c8f935cb889ff34ae6edc553f8 Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Sat, 3 Nov 2018 07:58:24 +0100 Subject: [PATCH] Add support for checking if alarms have matched and clearing the flag --- README.md | 2 ++ src/ds323x/status.rs | 36 ++++++++++++++++++++++++++++++++---- src/lib.rs | 4 ++++ tests/configuration.rs | 9 +++++++++ tests/status.rs | 6 ++++++ 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 24c4c47..29f51dc 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ 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`. +- Read whether alarms 1 or 2 have matched. See `has_alarm1_matched`. +- Clear flag indicating that alarms 1 or 2 have matched. See `clear_alarm1_matched_flag`. - Enable and disable the 32kHz output when battery-powered. See `enable_32khz_output_on_battery`. - Set the temperature conversion rate. See `set_temperature_conversion_rate`. - Enable and disable the temperature conversions when battery-powered. See `enable_temperature_conversions_on_battery`. diff --git a/src/ds323x/status.rs b/src/ds323x/status.rs index f6b0c61..632c284 100644 --- a/src/ds323x/status.rs +++ b/src/ds323x/status.rs @@ -40,11 +40,39 @@ where let status = self.status & !BitFlags::OSC_STOP; self.write_status_without_clearing_alarm(status) } + + /// Read whether the Alarm1 has matched at some point. + /// + /// Once this is true, it will stay as such until cleared with + /// [`clear_alarm1_matched_flag()`](#method.clear_alarm1_matched_flag) + pub fn has_alarm1_matched(&mut self) -> Result> { let status = self.iface.read_register(Register::STATUS)?; - if (status & BitFlags::OSC_STOP) != 0 { - self.iface.write_register(Register::STATUS, status & !BitFlags::OSC_STOP)?; - } - Ok(()) + Ok((status & BitFlags::ALARM1F) != 0) + } + + /// Clear flag signalling whether the Alarm1 has matched at some point. + /// + /// See also: [`has_alarm1_matched()`](#method.has_alarm1_matched) + pub fn clear_alarm1_matched_flag(&mut self) -> Result<(), Error> { + let status = self.status | BitFlags::ALARM2F; + self.iface.write_register(Register::STATUS, status) + } + + /// Read whether the Alarm2 has matched at some point. + /// + /// Once this is true, it will stay as such until cleared with + /// [`clear_alarm2_matched_flag()`](#method.clear_alarm2_matched_flag) + pub fn has_alarm2_matched(&mut self) -> Result> { + let status = self.iface.read_register(Register::STATUS)?; + Ok((status & BitFlags::ALARM2F) != 0) + } + + /// Clear flag signalling whether the Alarm2 has matched at some point. + /// + /// See also: [`has_alarm2_matched()`](#method.has_alarm2_matched) + pub fn clear_alarm2_matched_flag(&mut self) -> Result<(), Error> { + let status = self.status | BitFlags::ALARM1F; + self.iface.write_register(Register::STATUS, status) } /// Read the temperature. diff --git a/src/lib.rs b/src/lib.rs index 838b9d5..af30466 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,8 @@ //! - 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`]. +//! - Read whether alarms 1 or 2 have matched. See [`has_alarm1_matched`]. +//! - Clear flag indicating that alarms 1 or 2 have matched. See [`clear_alarm1_matched_flag`]. //! - Enable and disable the 32kHz output when battery powered. See [`enable_32khz_output_on_battery`]. //! - Set the temperature conversion rate. See [`set_temperature_conversion_rate`]. //! - Enable and disable the temperature conversions when battery-powered. See [`enable_temperature_conversions_on_battery`]. @@ -34,6 +36,8 @@ //! [`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 +//! [`has_alarm1_matched`]: struct.Ds323x.html#method.has_alarm1_matched +//! [`clear_alarm1_matched_flag`]: struct.Ds323x.html#method.clear_alarm1_matched_flag //! [`enable_32khz_output_on_battery`]: struct.Ds323x.html#method.enable_32khz_output_on_battery //! [`set_temperature_conversion_rate`]: struct.Ds323x.html#method.set_temperature_conversion_rate //! [`enable_temperature_conversions_on_battery`]: struct.Ds323x.html#method.enable_temperature_conversions_on_battery diff --git a/tests/configuration.rs b/tests/configuration.rs index df14dac..5658ff4 100644 --- a/tests/configuration.rs +++ b/tests/configuration.rs @@ -72,6 +72,15 @@ call_method_status_test!(en_32khz_out, enable_32khz_output, call_method_status_test!(dis_32khz_out, disable_32khz_output, DS3231_POR_STATUS & !BF::EN32KHZ | BF::ALARM2F | BF::ALARM1F, DS323X_POR_STATUS & !BF::EN32KHZ | BF::ALARM2F | BF::ALARM1F); + +call_method_status_test!(clear_alarm1_matched, clear_alarm1_matched_flag, + DS3231_POR_STATUS | BF::ALARM2F, + DS323X_POR_STATUS | BF::ALARM2F); + +call_method_status_test!(clear_alarm2_matched, clear_alarm2_matched_flag, + DS3231_POR_STATUS | BF::ALARM1F, + DS323X_POR_STATUS | BF::ALARM1F); + call_method_status_test!(clr_stop, clear_has_been_stopped_flag, DS3231_POR_STATUS & !BF::OSC_STOP | BF::ALARM2F | BF::ALARM1F, DS323X_POR_STATUS & !BF::OSC_STOP | BF::ALARM2F | BF::ALARM1F); diff --git a/tests/status.rs b/tests/status.rs index 22747bf..fb733e2 100644 --- a/tests/status.rs +++ b/tests/status.rs @@ -17,6 +17,12 @@ get_param_test!(is_not_busy, is_busy, STATUS, false, 0xFF & !BF::BUSY); get_param_test!(stopped, has_been_stopped, STATUS, true, 0xFF); get_param_test!(not_stopped, has_been_stopped, STATUS, false, 0xFF & !BF::OSC_STOP); +get_param_test!(alarm1_matched, has_alarm1_matched, STATUS, true, 0xFF); +get_param_test!(alarm1_not_matched, has_alarm1_matched, STATUS, false, 0xFF & !BF::ALARM1F); + +get_param_test!(alarm2_matched, has_alarm2_matched, STATUS, true, 0xFF); +get_param_test!(alarm2_not_matched, has_alarm2_matched, STATUS, false, 0xFF & !BF::ALARM2F); + get_param_read_array_test!(temp_0, get_temperature, 0.0, TEMP_MSB, [0, 0], [0, 0]); get_param_read_array_test!(temp_min, get_temperature, -128.0, TEMP_MSB, [0b1000_0000, 0], [0, 0]); get_param_read_array_test!(temp_max, get_temperature, 127.75, TEMP_MSB, [0b0111_1111, 0b1100_0000], [0, 0]);