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