M41T62 is a serial real-time clock (RTC) made by STMicroelectronics.

Dependents:   LPC1114_data_logger Check_external_RTC LPC1114_barometer_with_data_logging

Revision:
3:41c351da2fdf
Parent:
2:c58da9ec9ae3
Child:
4:0c07690cb24f
--- a/m41t62_rtc.h	Sun Jun 22 06:08:15 2014 +0000
+++ b/m41t62_rtc.h	Sun Feb 22 01:08:24 2015 +0000
@@ -1,12 +1,12 @@
 /*
- * mbed library program 
+ * mbed library program
  *  Control M41T62 RTC Module
  *
- * Copyright (c) 2014 Kenji Arai / JH1PJL
+ * Copyright (c) 2014,'15 Kenji Arai / JH1PJL
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
  *      Created: June      21st, 2014
- *      Revised: June      22nd, 2014
+ *      Revised: Feburary  22nd, 2015
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -14,6 +14,11 @@
  * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
+/*
+ *---------------- REFERENCE ----------------------------------------------------------------------
+ *  http://www.st-japan.co.jp/web/jp/catalog/sense_power/FM151/CL1410/SC403/PF82507
+ *  http://strawberry-linux.com/catalog/items?code=12062
+ */
 
 #ifndef M41T62_H
 #define M41T62_H
@@ -22,7 +27,7 @@
 
 // RTC STmicro M41T62
 // Address b7=1,b6=1,b5=0,b4=1,b3=0,b2=0,b1=0, b0=R/W
-#define M41T62ADDR  0xd0        // No other choice
+#define M41T62ADDR  (0x68 << 1) // No other choice
 
 #define RTC_Wk_Monday       ((uint8_t)0x01)
 #define RTC_Wk_Tuesday      ((uint8_t)0x02)
@@ -50,15 +55,32 @@
 #define RTC_SQW_1HZ         ((uint8_t)0xf)
 
 typedef struct {    // BCD format
-  uint8_t rtc_seconds;    
-  uint8_t rtc_minutes;
-  uint8_t rtc_hours;    
-  uint8_t rtc_weekday;
-  uint8_t rtc_date;
-  uint8_t rtc_month;
-  uint8_t rtc_year_raw;
-  uint16_t rtc_year;
-}rtc_time;
+    uint8_t rtc_seconds;
+    uint8_t rtc_minutes;
+    uint8_t rtc_hours;
+    uint8_t rtc_weekday;
+    uint8_t rtc_date;
+    uint8_t rtc_month;
+    uint8_t rtc_year_raw;
+    uint16_t rtc_year;
+} rtc_time;
+
+// Register definition
+#define M41T62_REG_SSEC         0x0
+#define M41T62_REG_SEC          0x1
+#define M41T62_REG_MIN          0x2
+#define M41T62_REG_HOUR         0x3
+#define M41T62_REG_WDAY         0x4
+#define M41T62_REG_DAY          0x5
+#define M41T62_REG_MON          0x6
+#define M41T62_REG_YEAR         0x7
+
+#define M41T62_REG_ALARM_MON    0xa
+#define M41T62_REG_ALARM_DAY    0xb
+#define M41T62_REG_ALARM_HOUR   0xc
+#define M41T62_REG_ALARM_MIN    0xd
+#define M41T62_REG_ALARM_SEC    0xe
+#define M41T62_REG_FLAGS        0xf
 
 /** Interface for RTC (I2C Interface)  STMicroelectronics M41T62
  *
@@ -66,6 +88,7 @@
  *
  * @code
  * #include "mbed.h"
+ * #include "m41t62_rtc.h"
  *
  * // I2C Communication
  *  M41T62      m41t62(dp5,dp27);   // STmicro RTC(M41T62) SDA, SCL
@@ -79,7 +102,7 @@
  * time_t seconds;
  * char buf[40];
  *
- *   m41t62.set_sq_wave(RTC_SQW_NONE);      // Stop output for more low current 
+ *   m41t62.set_sq_wave(RTC_SQW_NONE);      // Stop output for more low current
  *
  *   m41t62.read_rtc_std(&t);   // read RTC data
  *   seconds = mktime(&t);
@@ -88,8 +111,9 @@
  * }
  * @endcode
  */
- 
-class M41T62 {
+
+class M41T62
+{
 public:
     /** Configure data pin
       * @param data SDA and SCL pins
@@ -125,6 +149,12 @@
       */
     void write_rtc_direct(rtc_time *);
 
+    /** Set I2C clock frequency
+      * @param freq.
+      * @return none
+      */
+    void frequency(int hz);
+
     /** Control Square wave output port
       * @param output_mode
       * @return none
@@ -132,12 +162,10 @@
     void set_sq_wave(uint8_t );
 
 protected:
-    void i2c_read_n_bytes(int, char*, int);
-    void i2c_write_n_bytes(int, char*, int);
     uint8_t bin2bcd(uint8_t);
     uint8_t bcd2bin(uint8_t);
 
-    I2C i2c;
+    I2C _i2c;
 
 private:
     uint8_t M41T62_addr;