Home | History | Annotate | Download | only in ds1307
      1 /*
      2  * Author: Jon Trulson <jtrulson (at) ics.com>
      3  * Copyright (c) 2014 Intel Corporation.
      4  *
      5  * Adapted from Seeed Studio library:
      6  * https://github.com/Seeed-Studio/RTC_DS1307
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining
      9  * a copy of this software and associated documentation files (the
     10  * "Software"), to deal in the Software without restriction, including
     11  * without limitation the rights to use, copy, modify, merge, publish,
     12  * distribute, sublicense, and/or sell copies of the Software, and to
     13  * permit persons to whom the Software is furnished to do so, subject to
     14  * the following conditions:
     15  *
     16  * The above copyright notice and this permission notice shall be
     17  * included in all copies or substantial portions of the Software.
     18  *
     19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     26  */
     27 #pragma once
     28 
     29 #include <string>
     30 #include <mraa/i2c.hpp>
     31 
     32 #define DS1307_I2C_BUS     0
     33 #define DS1307_I2C_ADDR    0x68
     34 
     35 // Days of the week
     36 #define DS1307_DAY_MON 1
     37 #define DS1307_DAY_TUE 2
     38 #define DS1307_DAY_WED 3
     39 #define DS1307_DAY_THU 4
     40 #define DS1307_DAY_FRI 5
     41 #define DS1307_DAY_SAT 6
     42 #define DS1307_DAY_SUN 7
     43 
     44 namespace upm {
     45   /**
     46    * @brief DS1307 Real-Time Clock library
     47    * @defgroup ds1307 libupm-ds1307
     48    * @ingroup seeed i2c time
     49    */
     50 
     51   /**
     52    * @library ds1307
     53    * @sensor ds1307
     54    * @comname Grove RTC (Real-Time Clock)
     55    * @altname DS1307 RTC
     56    * @type time
     57    * @man seeed
     58    * @con i2c
     59    *
     60    * @brief API for the DS1307 Real-Time CLock
     61    *
     62    * UPM module for the DS1307-based real-time clock. The clock can provide information
     63    * about seconds, minutes, hours, day of the week, day of the month,
     64    * month, and year. It can operate in either a 24-hour or a 12-hour format.
     65    * This device can also output a square wave at 1Khz, 4Khz, 8Khz, and 32Khz.
     66    * However, this capability is not implemented in this module.
     67    *
     68    * @image html ds1307.jpg
     69    * @snippet ds1307.cxx Interesting
     70    */
     71   class DS1307 {
     72   public:
     73     /**
     74      * DS1307 constructor
     75      *
     76      * @param bus I2C bus to use
     77      */
     78     DS1307(int bus);
     79 
     80     /**
     81      * Loads all the time values
     82      *
     83      * @return True if time data loaded successfully
     84      */
     85     bool loadTime();
     86 
     87     /**
     88      * Sets the time. You should call loadTime() beforehand to
     89      * maintain consistency
     90      *
     91      * @return True if time is set successfully
     92      */
     93     bool setTime();
     94 
     95     /**
     96      * Enables an oscillator on the clock.
     97      *
     98      * @return 0 (mraa::SUCCESS) if successful; non-zero otherwise
     99      */
    100     mraa::Result enableClock();
    101 
    102     /**
    103      * Disables the oscillator on the clock. This prevents the clock
    104      * from updating any time/date values
    105      *
    106      * @return 0 (mraa::SUCCESS) if successful; non-zero otherwise
    107      */
    108     mraa::Result disableClock();
    109 
    110     /**
    111      * Writes value(s) into registers
    112      *
    113      * @param reg Register location to start writing into
    114      * @param buffer Buffer for data storage
    115      * @param len Number of bytes to write
    116      * @return 0 (mraa::SUCCESS) if successful; non-zero otherwise
    117      */
    118     mraa::Result writeBytes(uint8_t reg, uint8_t *buffer, int len);
    119 
    120     /**
    121      * Reads value(s) from registers
    122      *
    123      * @param reg Register location to start reading from
    124      * @param buffer Buffer for data storage
    125      * @param len Number of bytes to read
    126      * @return Number of bytes read
    127      */
    128     int readBytes(uint8_t reg, uint8_t *buffer, int len);
    129 
    130     /**
    131      * Converts a BCD value into decimal
    132      *
    133      * @param val BCD value to convert
    134      * @return Converted decimal value
    135      */
    136     unsigned int bcdToDec(uint8_t val);
    137 
    138     /**
    139      * Converts a decimal value into BCD
    140      *
    141      * @param val Decimal value to convert
    142      * @return Converted BCD value
    143      */
    144     uint8_t decToBcd(unsigned int val);
    145 
    146     // These variables store the time data loaded with loadTime(), and
    147     // are the source of data when setTime() is called.  It is a
    148     // good idea to call loadTime() to set up the current values before
    149     // calling setTime() to ensure RTC data is consistent
    150 
    151     /**
    152      * Holds seconds
    153      */
    154     unsigned int seconds;
    155     /**
    156      * Holds minutes
    157      */
    158     unsigned int minutes;
    159     /**
    160      * Holds hours; 1-12 in the am/pm format, 0-23 otherwise
    161      */
    162     unsigned int hours;
    163     /**
    164      * Holds a day of the week; 1-7, where 1 is Sunday
    165      */
    166     unsigned int dayOfWeek;
    167     /**
    168      * Holds a day of the month, 1-31
    169      */
    170     unsigned int dayOfMonth;
    171     /**
    172      * Holds a month, 1-12
    173      */
    174     unsigned int month;
    175     /**
    176      * Holds a year, 0-99
    177      */
    178     unsigned int year;
    179     /**
    180      * True if the am/pm format is used, false otherwise
    181      */
    182     bool amPmMode;
    183     /**
    184      * For the am/pm format, it is true if it's pm, false otherwise
    185      */
    186     bool pm;
    187 
    188   private:
    189     mraa::I2c m_i2c;
    190   };
    191 }
    192 
    193 
    194