From 7e9bfcff13d1296ea4d3a46b1ada775f2df0959f Mon Sep 17 00:00:00 2001
From: Diego Barrios Romero <eldruin@gmail.com>
Date: Sat, 3 Nov 2018 07:59:41 +0100
Subject: [PATCH] Add support for enabling/disabling alarm interrupts

---
 README.md                   |  1 +
 src/ds323x/configuration.rs | 24 ++++++++++++++++++++++++
 src/lib.rs                  |  4 ++++
 tests/common/mod.rs         |  2 ++
 tests/configuration.rs      |  6 ++++++
 5 files changed, 37 insertions(+)

diff --git a/README.md b/README.md
index 29f51dc..a037a97 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,7 @@ This driver allows you to:
 - 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 alarms 1 and 2 interrupt generation. See `enable_alarm1_interrupts`.
 - 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/configuration.rs b/src/ds323x/configuration.rs
index 277da6e..9f58ca0 100644
--- a/src/ds323x/configuration.rs
+++ b/src/ds323x/configuration.rs
@@ -91,6 +91,30 @@ where
         self.write_control(new_control)
     }
 
+    /// Enable Alarm1 interrupts.
+    pub fn enable_alarm1_interrupts(&mut self) -> Result<(), Error<E>> {
+        let control = self.control;
+        self.write_control(control | BitFlags::ALARM1_INT_EN)
+    }
+
+    /// Disable Alarm1 interrupts.
+    pub fn disable_alarm1_interrupts(&mut self) -> Result<(), Error<E>> {
+        let control = self.control;
+        self.write_control(control & !BitFlags::ALARM1_INT_EN)
+    }
+
+    /// Enable Alarm2 interrupts.
+    pub fn enable_alarm2_interrupts(&mut self) -> Result<(), Error<E>> {
+        let control = self.control;
+        self.write_control(control | BitFlags::ALARM2_INT_EN)
+    }
+
+    /// Disable Alarm2 interrupts.
+    pub fn disable_alarm2_interrupts(&mut self) -> Result<(), Error<E>> {
+        let control = self.control;
+        self.write_control(control & !BitFlags::ALARM2_INT_EN)
+    }
+
     fn write_control(&mut self, control: u8) -> Result<(), Error<E>> {
         self.iface.write_register(Register::CONTROL, control)?;
         self.control = control;
diff --git a/src/lib.rs b/src/lib.rs
index af30466..34dd199 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -19,6 +19,7 @@
 //! - 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 alarms 1 and 2 interrupt generation. See [`enable_alarm1_interrupts`].
 //! - 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`].
@@ -38,6 +39,7 @@
 //! [`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_alarm1_interrupts`]: struct.Ds323x.html#method.enable_alarm1_interrupts
 //! [`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
@@ -459,6 +461,8 @@ impl BitFlags {
     const RS2           : u8 = 0b0001_0000;
     const RS1           : u8 = 0b0000_1000;
     const INTCN         : u8 = 0b0000_0100;
+    const ALARM2_INT_EN : u8 = 0b0000_0010;
+    const ALARM1_INT_EN : u8 = 0b0000_0001;
     const OSC_STOP      : u8 = 0b1000_0000;
     const BB32KHZ       : u8 = 0b0100_0000;
     const CRATE1        : u8 = 0b0010_0000;
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
index 116e9c0..3311c4a 100644
--- a/tests/common/mod.rs
+++ b/tests/common/mod.rs
@@ -39,6 +39,8 @@ impl BitFlags {
     pub const RS2           : u8 = 0b0001_0000;
     pub const RS1           : u8 = 0b0000_1000;
     pub const INTCN         : u8 = 0b0000_0100;
+    pub const ALARM2_INT_EN : u8 = 0b0000_0010;
+    pub const ALARM1_INT_EN : u8 = 0b0000_0001;
     pub const OSC_STOP      : u8 = 0b1000_0000;
     pub const BB32KHZ       : u8 = 0b0100_0000;
     pub const CRATE1        : u8 = 0b0010_0000;
diff --git a/tests/configuration.rs b/tests/configuration.rs
index 5658ff4..162df0d 100644
--- a/tests/configuration.rs
+++ b/tests/configuration.rs
@@ -87,6 +87,12 @@ call_method_status_test!(clr_stop, clear_has_been_stopped_flag,
 
 change_if_necessary_test!(conv_temp, convert_temperature, CONTROL, CONTROL_POR_VALUE | BF::TEMP_CONV, CONTROL_POR_VALUE & !BF::TEMP_CONV);
 
+call_method_test!(en_al1_int,  enable_alarm1_interrupts,  CONTROL, CONTROL_POR_VALUE |  BF::ALARM1_INT_EN);
+call_method_test!(dis_al1_int, disable_alarm1_interrupts, CONTROL, CONTROL_POR_VALUE & !BF::ALARM1_INT_EN);
+
+call_method_test!(en_al2_int,  enable_alarm2_interrupts,  CONTROL, CONTROL_POR_VALUE |  BF::ALARM2_INT_EN);
+call_method_test!(dis_al2_int, disable_alarm2_interrupts, CONTROL, CONTROL_POR_VALUE & !BF::ALARM2_INT_EN);
+
 set_param_test!(set_aging_offset_min, set_aging_offset, AGING_OFFSET, -128, 0b1000_0000);
 set_param_test!(set_aging_offset_max, set_aging_offset, AGING_OFFSET,  127, 127);