Library for Real Time Clock module MCP97410 based on Library for DS1307

Fork of RTC-DS1307 by Henry Leinen

Committer:
charly
Date:
Wed Jan 07 21:46:02 2015 +0000
Revision:
10:780027029afe
Parent:
Rtc_Ds1307.h@7:dca20be3ef38
Child:
11:ef48dcb888c9
RTC-part working for MCP97410

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charly 10:780027029afe 1 /* Rtc_Mcp97410.h */
leihen 0:3940f0ad2ca5 2 /*
charly 10:780027029afe 3 Support for Michrochip RTC MCP97410
charly 10:780027029afe 4 Should work for MCP97410, MCP97411, MCP97412 - see datasheet
charly 10:780027029afe 5 Based on Library for DS1307
charly 10:780027029afe 6 Chips are similar but not equal!
charly 10:780027029afe 7 Ported for MCP97410 by Karl Zweimueller
charly 10:780027029afe 8
charly 10:780027029afe 9 Original Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
leihen 7:dca20be3ef38 10
leihen 0:3940f0ad2ca5 11 Permission is hereby granted, free of charge, to any person obtaining a copy
leihen 0:3940f0ad2ca5 12 of this software and associated documentation files (the "Software"), to deal
leihen 0:3940f0ad2ca5 13 in the Software without restriction, including without limitation the rights
leihen 0:3940f0ad2ca5 14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
leihen 0:3940f0ad2ca5 15 copies of the Software, and to permit persons to whom the Software is
leihen 0:3940f0ad2ca5 16 furnished to do so, subject to the following conditions:
leihen 7:dca20be3ef38 17
leihen 0:3940f0ad2ca5 18 The above copyright notice and this permission notice shall be included in
leihen 0:3940f0ad2ca5 19 all copies or substantial portions of the Software.
leihen 7:dca20be3ef38 20
leihen 0:3940f0ad2ca5 21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
leihen 0:3940f0ad2ca5 22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
leihen 0:3940f0ad2ca5 23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
leihen 0:3940f0ad2ca5 24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
leihen 0:3940f0ad2ca5 25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
leihen 0:3940f0ad2ca5 26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
leihen 0:3940f0ad2ca5 27 THE SOFTWARE.
leihen 0:3940f0ad2ca5 28 */
charly 10:780027029afe 29 #ifndef __RTC_MCP97410_H__
charly 10:780027029afe 30 #define __RTC_MCP97410_H__
leihen 0:3940f0ad2ca5 31
leihen 0:3940f0ad2ca5 32
leihen 7:dca20be3ef38 33
charly 10:780027029afe 34 /** Class Rtc_Mcp97410 implements the real time clock module MCP97410
leihen 6:bba89618ee63 35 *
leihen 6:bba89618ee63 36 * You can read the clock and set a new time and date.
leihen 6:bba89618ee63 37 * It is also possible to start and stop the clock.
charly 10:780027029afe 38 * Rtc_Mcp97410 allows you to display the time in a 12h or 24h format.
charly 10:780027029afe 39 * Based on Library for DS1307 by Henry Leinen.
charly 10:780027029afe 40 * Ported for MC97410 by Karl Zweimueller.
leihen 6:bba89618ee63 41 */
charly 10:780027029afe 42 class Rtc_Mcp97410
leihen 6:bba89618ee63 43 {
leihen 7:dca20be3ef38 44 public:
leihen 7:dca20be3ef38 45 /** Structure which is used to exchange the time and date
leihen 7:dca20be3ef38 46 */
leihen 7:dca20be3ef38 47 typedef struct {
leihen 7:dca20be3ef38 48 int sec; /*!< seconds [0..59] */
leihen 7:dca20be3ef38 49 int min; /*!< minutes {0..59] */
leihen 7:dca20be3ef38 50 int hour; /*!< hours [0..23] */
leihen 7:dca20be3ef38 51 int wday; /*!< weekday [1..7, where 1 = sunday, 2 = monday, ... */
leihen 7:dca20be3ef38 52 int date; /*!< day of month [0..31] */
leihen 7:dca20be3ef38 53 int mon; /*!< month of year [1..12] */
leihen 7:dca20be3ef38 54 int year; /*!< year [2000..2255] */
leihen 7:dca20be3ef38 55 } Time_rtc;
leihen 1:64274190e842 56
leihen 1:64274190e842 57
leihen 7:dca20be3ef38 58 /** RateSelect specifies the valid frequency values for the square wave output
leihen 7:dca20be3ef38 59 */
leihen 7:dca20be3ef38 60 typedef enum {
leihen 7:dca20be3ef38 61 RS1Hz = 0,
leihen 7:dca20be3ef38 62 RS4kHz = 1,
leihen 7:dca20be3ef38 63 RS8kHz = 2,
leihen 7:dca20be3ef38 64 RS32kHz = 3
leihen 7:dca20be3ef38 65 } SqwRateSelect_t;
leihen 7:dca20be3ef38 66
leihen 7:dca20be3ef38 67 protected:
leihen 7:dca20be3ef38 68 I2C* m_rtc;
leihen 7:dca20be3ef38 69
leihen 7:dca20be3ef38 70 static const char *m_weekDays[];
leihen 7:dca20be3ef38 71
leihen 7:dca20be3ef38 72 public:
leihen 7:dca20be3ef38 73 /** public constructor which creates the real time clock object
leihen 7:dca20be3ef38 74 *
charly 10:780027029afe 75 * @param i2c : Pointer to I2C-Object for I2C-Interface.
leihen 7:dca20be3ef38 76 *
leihen 7:dca20be3ef38 77 */
charly 10:780027029afe 78 Rtc_Mcp97410(I2C* i2c);
leihen 7:dca20be3ef38 79
charly 10:780027029afe 80 ~Rtc_Mcp97410();
leihen 7:dca20be3ef38 81
leihen 7:dca20be3ef38 82 /** Read the current time from RTC chip
leihen 7:dca20be3ef38 83 *
leihen 7:dca20be3ef38 84 * @param time : reference to a struct tm which will be filled with the time from rtc
leihen 7:dca20be3ef38 85 *
leihen 7:dca20be3ef38 86 * @returns true if successful, otherwise an acknowledge error occured
leihen 7:dca20be3ef38 87 */
leihen 7:dca20be3ef38 88 virtual bool getTime(Time_rtc& time);
leihen 7:dca20be3ef38 89
charly 10:780027029afe 90 /** Write the given time onto the RTC chip (and enable Battery-Backup)
leihen 7:dca20be3ef38 91 *
leihen 7:dca20be3ef38 92 * @param time : refereence to a struct which contains valid date and time information
leihen 7:dca20be3ef38 93 *
leihen 7:dca20be3ef38 94 * @param start : contains true if the clock shall start (or keep on running).
leihen 7:dca20be3ef38 95 *
leihen 7:dca20be3ef38 96 * @param thm : 12-hour-mode if set to true, otherwise 24-hour-mode will be set.
leihen 7:dca20be3ef38 97 *
leihen 7:dca20be3ef38 98 * @returns true if successful, otherwise an acknowledge error occured
leihen 7:dca20be3ef38 99 */
leihen 7:dca20be3ef38 100 virtual bool setTime(Time_rtc& time, bool start, bool thm);
leihen 7:dca20be3ef38 101
leihen 7:dca20be3ef38 102 /** Start the clock. Please note that the seconds register need to be read and
leihen 7:dca20be3ef38 103 * written in order to start or stop the clock. This can lead to an error
leihen 7:dca20be3ef38 104 * in the time value. The recommended way of starting and stoping the clock is
leihen 7:dca20be3ef38 105 * to write the actual date and time and set the start bit accordingly.
leihen 7:dca20be3ef38 106 *
leihen 7:dca20be3ef38 107 * @returns true if the clock was started, false if a communication error occured
leihen 7:dca20be3ef38 108 */
leihen 7:dca20be3ef38 109 bool startClock();
leihen 7:dca20be3ef38 110
leihen 7:dca20be3ef38 111 /** Stop the clock. Please note that the seconds register need to be read and
leihen 7:dca20be3ef38 112 * written in order to start or stop the clock. This can lead to an error
leihen 7:dca20be3ef38 113 * in the time value. The recommended way of starting and stoping the clock is
leihen 7:dca20be3ef38 114 * to write the actual date and time and set the start bit accordingly.
leihen 7:dca20be3ef38 115 *
leihen 7:dca20be3ef38 116 * @returns true if the clock was stopped, false if a communication error occured
leihen 7:dca20be3ef38 117 */
leihen 7:dca20be3ef38 118 bool stopClock();
leihen 7:dca20be3ef38 119
leihen 7:dca20be3ef38 120 /** Service function to convert a weekday into a string representation
leihen 7:dca20be3ef38 121 *
leihen 7:dca20be3ef38 122 * @param wday : day of week to convert (starting with sunday = 1, monday = 2, ..., saturday = 7
leihen 7:dca20be3ef38 123 *
leihen 7:dca20be3ef38 124 * @returns the corresponding string representation
leihen 7:dca20be3ef38 125 */
leihen 7:dca20be3ef38 126 const char* weekdayToString( int wday ) {
leihen 7:dca20be3ef38 127 return m_weekDays[wday%7];
leihen 7:dca20be3ef38 128 }
leihen 7:dca20be3ef38 129
leihen 7:dca20be3ef38 130 /** Enable Square Wave output. The function enables or disables the square wave output
leihen 7:dca20be3ef38 131 * of the module and sets the desired frequency.
leihen 7:dca20be3ef38 132 *
leihen 7:dca20be3ef38 133 * @param ena : if set to true, the square wave output is enabled.
leihen 7:dca20be3ef38 134 *
leihen 7:dca20be3ef38 135 * @param rs : rate select, can be either one of the four values defined by type /c RateSelect_t
leihen 7:dca20be3ef38 136 *
leihen 7:dca20be3ef38 137 * @return true if the operation was successful or false otherwise
leihen 7:dca20be3ef38 138 */
leihen 7:dca20be3ef38 139 bool setSquareWaveOutput(bool ena, SqwRateSelect_t rs);
leihen 7:dca20be3ef38 140
leihen 7:dca20be3ef38 141 private:
leihen 7:dca20be3ef38 142 bool read(int address, char* buffer, int len);
leihen 7:dca20be3ef38 143 bool write(int address, char* buffer, int len);
leihen 7:dca20be3ef38 144
leihen 7:dca20be3ef38 145 static int bcdToDecimal(int bcd) {
leihen 7:dca20be3ef38 146 return ((bcd&0xF0)>>4)*10 + (bcd&0x0F);
leihen 7:dca20be3ef38 147 }
leihen 7:dca20be3ef38 148
leihen 7:dca20be3ef38 149 static int decimalToBcd(int dec) {
leihen 7:dca20be3ef38 150 return (dec%10) + ((dec/10)<<4);
leihen 7:dca20be3ef38 151 }
leihen 0:3940f0ad2ca5 152 };
leihen 7:dca20be3ef38 153
leihen 7:dca20be3ef38 154
leihen 7:dca20be3ef38 155
leihen 7:dca20be3ef38 156 typedef void (*RtcCallback_t) (void);
leihen 7:dca20be3ef38 157
leihen 7:dca20be3ef38 158
charly 10:780027029afe 159 class RtcCls : public Rtc_Mcp97410
leihen 7:dca20be3ef38 160 {
leihen 7:dca20be3ef38 161 protected:
leihen 7:dca20be3ef38 162 InterruptIn m_sqw;
leihen 7:dca20be3ef38 163 bool m_bUseSqw;
leihen 7:dca20be3ef38 164 time_t m_time; // Only used in case SQW is used
leihen 7:dca20be3ef38 165
leihen 7:dca20be3ef38 166 bool m_bAlarmEnabled;
leihen 7:dca20be3ef38 167 RtcCallback_t m_alarmfunc;
leihen 7:dca20be3ef38 168 time_t m_alarmTime;
leihen 7:dca20be3ef38 169
leihen 7:dca20be3ef38 170 public:
charly 10:780027029afe 171 RtcCls(I2C* i2c, PinName sqw, bool bUseSqw);
leihen 7:dca20be3ef38 172
leihen 7:dca20be3ef38 173 protected:
leihen 7:dca20be3ef38 174 void _callback(void);
leihen 7:dca20be3ef38 175
leihen 7:dca20be3ef38 176 public:
leihen 7:dca20be3ef38 177 time_t getTime();
charly 10:780027029afe 178 virtual bool getTime(Time_rtc& time) { return Rtc_Mcp97410::getTime(time); }
leihen 7:dca20be3ef38 179 void setTime(time_t time);
charly 10:780027029afe 180 virtual bool setTime(Time_rtc& time, bool start, bool thm) { return Rtc_Mcp97410::setTime(time, start, thm); }
leihen 7:dca20be3ef38 181 public:
leihen 7:dca20be3ef38 182 void setAlarm(int nSeconds, RtcCallback_t alarmfunc) {
leihen 7:dca20be3ef38 183 m_alarmfunc = alarmfunc;
leihen 7:dca20be3ef38 184 m_alarmTime = m_time + nSeconds;
leihen 7:dca20be3ef38 185 m_bAlarmEnabled = (alarmfunc == NULL) ? false : true;
leihen 7:dca20be3ef38 186 }
leihen 7:dca20be3ef38 187 };
leihen 7:dca20be3ef38 188
charly 10:780027029afe 189 #endif // __RTC_MCP97410_H__