Dallas DS1307 real-time clock minimalistic driver

Dependents:   testing_RTC_OneWire EMIRv2

Files at this revision

API Documentation at this revision

Comitter:
alpov
Date:
Sun Apr 27 19:07:57 2014 +0000
Parent:
0:f29e45a25cba
Child:
2:6d8d35a5b3f2
Commit message:
added docu, even more simplification, constants

Changed in this revision

DS1307.cpp Show annotated file Show diff for this revision Revisions of this file
DS1307.h Show annotated file Show diff for this revision Revisions of this file
--- a/DS1307.cpp	Fri Apr 25 18:15:47 2014 +0000
+++ b/DS1307.cpp	Sun Apr 27 19:07:57 2014 +0000
@@ -1,59 +1,21 @@
 #include "mbed.h"
 #include "DS1307.h"
 
-
-const char *DS1307::m_weekDays[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
-
-#define DS1307_ADDR 0xd0
-
-
-DS1307::DS1307(PinName sda, PinName scl)
+DS1307::DS1307(PinName sda, PinName scl) : ds1307_i2c(sda, scl)
 {
-    // create a new I2C object
-    m_rtc = new I2C(sda, scl);
-    if (m_rtc == NULL) error("DS1307");
-        
-    // set the frequency to standard 100kHz
-    m_rtc->frequency(100000);
-}
-
-
-DS1307::~DS1307()
-{
-    if (m_rtc != NULL) delete m_rtc;
+    ds1307_i2c.frequency(DS1307_FREQ);
 }
 
-
-bool DS1307::read(int address, char *buffer, int len)
-{
-    char data[1] = { (char)address };
-
-    if (m_rtc->write(DS1307_ADDR, data, 1) != 0) return false;
-    if (m_rtc->read(DS1307_ADDR, buffer, len) != 0) return false;
-
-    return true;
-}
-
-
-bool DS1307::write(int address, char *buffer, int len)
-{
-    char data[9] = { (char)address };
-
-    if (len > 8) return false;
-    memcpy(&data[1], buffer, len);
-
-    if (m_rtc->write(DS1307_ADDR, data, len + 1) != 0) return false;
-    
-    return true;
-}
-
-
-time_t DS1307::getNow()
+time_t DS1307::now()
 {
     struct tm now;
+    char addr = 0x00; // memory address
     char buffer[7];
+
+    if (ds1307_i2c.write(DS1307_ADDR, &addr, 1) != 0) return 0;
+    if (ds1307_i2c.read(DS1307_ADDR, buffer, 7) != 0) return 0;
     
-    if (!read(0, buffer, 7)) return 0;
+    if (buffer[0] & 0x80) return 0; // clock stopped
     if (!(buffer[2] & 0x40)) return 0; // 12-hour format not supported
     now.tm_sec = bcdToDecimal(buffer[0] & 0x7F);
     now.tm_min = bcdToDecimal(buffer[1]);
@@ -66,23 +28,23 @@
     return mktime(&now);
 }
 
-
-bool DS1307::setNow(time_t t)
+bool DS1307::set_time(time_t t)
 {
     struct tm *now;
-    char buffer[8];
+    char buffer[9];
 
     now = localtime(&t);
 
-    buffer[0] = decimalToBcd(now->tm_sec) & 0x7f; // CH = 0
-    buffer[1] = decimalToBcd(now->tm_min);
-    buffer[2] = 0x40 | (decimalToBcd(now->tm_hour) & 0x3F); // 24-hour format
-    buffer[3] = now->tm_wday + 1;
-    buffer[4] = decimalToBcd(now->tm_mday);
-    buffer[5] = decimalToBcd(now->tm_mon+1);
-    buffer[6] = decimalToBcd(now->tm_year + 1900 - 2000);
-    buffer[7] = 0x00; // OUT = 0
-    if (!write(0, buffer, 8)) return false;
+    buffer[0] = 0x00; // memory address
+    buffer[1] = decimalToBcd(now->tm_sec) & 0x7f; // CH = 0
+    buffer[2] = decimalToBcd(now->tm_min);
+    buffer[3] = 0x40 | (decimalToBcd(now->tm_hour) & 0x3F); // 24-hour format
+    buffer[4] = now->tm_wday + 1;
+    buffer[5] = decimalToBcd(now->tm_mday);
+    buffer[6] = decimalToBcd(now->tm_mon+1);
+    buffer[7] = decimalToBcd(now->tm_year + 1900 - 2000);
+    buffer[8] = 0x00; // OUT = 0
+    if (ds1307_i2c.write(DS1307_ADDR, buffer, 9) != 0) return false;
 
     return true;
 }
--- a/DS1307.h	Fri Apr 25 18:15:47 2014 +0000
+++ b/DS1307.h	Sun Apr 27 19:07:57 2014 +0000
@@ -1,58 +1,65 @@
-/* DS1307.h */
-/*
-Copyright (c) 2014 Ales Povalac
-Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
+#ifndef _DS1307_H
+#define _DS1307_H
 
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
+/* Dallas DS1307 real-time clock minimalistic driver
+ *
+ * Copyright (c) 2014 Ales Povalac, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
+ * and associated documentation files (the "Software"), to deal in the Software without restriction, 
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or 
+ * substantial portions of the Software.
+ *
+ * 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 AND 
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
+ * 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.
+ */
 
-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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 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.
-*/
-#ifndef __DS1307_H__
-#define __DS1307_H__
+#define DS1307_ADDR     0xD0    // I2C address
+#define DS1307_FREQ     100000  // bus speed
 
-
+/** Dallas DS1307 real-time clock minimalistic driver
+ */
 class DS1307
 {
-protected:
-    I2C* m_rtc;
-    static const char *m_weekDays[];
-
 public:
+    /** Create DS1307 instance on the specified pins of I2C bus
+     */
     DS1307(PinName sda, PinName scl);
-    ~DS1307();
 
-    const char* weekdayToString(int wday) { 
-        return m_weekDays[wday % 7]; 
-    }
-
-    time_t getNow();
-    bool setNow(time_t time);
+    /** Read current real time from DS1307
+     *
+     * @returns
+     *   current time on success,
+     *   0 on error (I2C fail, clock not set)
+     */
+    time_t now();
+    
+    /** Write current real time to DS1307
+     *
+     * @param time Real time to set up
+     * @returns
+     *   true on success,
+     *   false on error (I2C fail)
+     */
+    bool set_time(time_t time);
 
 private:
-    bool read(int address, char* buffer, int len);
-    bool write(int address, char* buffer, int len);
+    I2C ds1307_i2c;
 
     static int bcdToDecimal(int bcd) {
-        return ((bcd&0xF0)>>4)*10 + (bcd&0x0F);
+        return ((bcd & 0xF0) >> 4) * 10 + (bcd & 0x0F);
     }
 
     static int decimalToBcd(int dec) {
-        return (dec%10) + ((dec/10)<<4);
+        return (dec % 10) + ((dec / 10) << 4);
     }
 };
 
-#endif // __DS1307_H__
+#endif // __DS1307_H