DS3231

Dependents:   LoRa_Access_Point

Files at this revision

API Documentation at this revision

Comitter:
lukas_formanek
Date:
Sat May 08 16:13:55 2021 +0000
Parent:
15:bf11392ccaea
Commit message:
LoRa_Access_Point

Changed in this revision

DS3231.cpp Show annotated file Show diff for this revision Revisions of this file
DS3231.h Show annotated file Show diff for this revision Revisions of this file
diff -r bf11392ccaea -r 610f7091e0ac DS3231.cpp
--- a/DS3231.cpp	Thu Mar 28 09:55:44 2019 +0000
+++ b/DS3231.cpp	Sat May 08 16:13:55 2021 +0000
@@ -1,8 +1,10 @@
 #include "DS3231.h"
 
 DS3231::DS3231(PinName sda, PinName scl) : i2c(sda, scl)
-    {
-    } 
+{
+    w_adrs = ((DS3231_I2C_ADRS << 1) | I2C_WRITE);
+    r_adrs = ((DS3231_I2C_ADRS << 1) | I2C_READ);
+} 
     
     
 // BCD to decimal conversion    
@@ -135,4 +137,17 @@
 bool DS3231::OSF()
     {int reg=readRegister(DS3231_Control_Status);
     return(reg&DS3231_bit_OSF);
-    }
\ No newline at end of file
+    }
+
+uint16_t DS3231::set_cntl_stat_reg(ds3231_cntl_stat_t data)
+{
+    uint8_t local_data[] = {0,0,0};
+    uint8_t data_length = 0;
+    
+    local_data[data_length++] = CONTROL;
+    local_data[data_length++] = data.control;
+    local_data[data_length++] = data.status;
+
+    //users responsibility to make sure data is logical
+    return(i2c.write(w_adrs,(const char*) local_data, data_length));
+}
\ No newline at end of file
diff -r bf11392ccaea -r 610f7091e0ac DS3231.h
--- a/DS3231.h	Thu Mar 28 09:55:44 2019 +0000
+++ b/DS3231.h	Sat May 08 16:13:55 2021 +0000
@@ -72,8 +72,13 @@
 #ifndef MBED_DS3231_H
 #define MBED_DS3231_H
 
+#define DS3231_I2C_ADRS 0x68 
+#define I2C_WRITE 0
+#define I2C_READ  1
+
 //DS3231 8 bit adress
 #define DS3231_Address          0xD0
+//#define DS3231_Address          0x68
 
 //DS3231 registers
 #define DS3231_Seconds          0x00
@@ -119,9 +124,52 @@
 #define DS3231_MSB_Temp         0x11
 #define DS3231_LSB_Temp         0x12
 
+/**
+* ds3231_cntl_stat_t - Struct for containing control and status 
+* register data.
+* 
+* Members:
+*
+* - uint8_t control - Binary data for read/write of control register 
+*
+* - uint8_t status  - Binary data  for read/write of status register 
+*/
+typedef struct
+{
+    uint8_t control; 
+    uint8_t status; 
+}ds3231_cntl_stat_t;
+
 /* Interface to MAXIM DS3231 RTC */
 class DS3231
     {public :
+    
+    /**
+        * ds3231_regs_t - enumerated DS3231 registers 
+        */
+        typedef enum
+        {
+            SECONDS,
+            MINUTES,
+            HOURS,
+            DAY,
+            DATE,
+            MONTH,
+            YEAR,
+            ALRM1_SECONDS,
+            ALRM1_MINUTES,
+            ALRM1_HOURS,
+            ALRM1_DAY_DATE,
+            ALRM2_MINUTES,
+            ALRM2_HOURS,
+            ALRM2_DAY_DATE,
+            CONTROL,
+            STATUS,
+            AGING_OFFSET, //don't touch this register
+            MSB_TEMP,
+            LSB_TEMP
+        }ds3231_regs_t;
+        
      /** Create an instance of the DS3231 connected to specfied I2C pins
      *
      * @param sda The I2C data pin
@@ -210,7 +258,10 @@
      
      bool error;
      
+     uint16_t set_cntl_stat_reg(ds3231_cntl_stat_t data);
+     
      private :
+     uint8_t w_adrs, r_adrs;
      I2C i2c;
      int bcd2dec(int k); // bcd to decimal conversion
      int dec2bcd(int k); // decimal to bcd conversion