APS_SO

Dependencies:   mbed

Committer:
ianwillianb
Date:
Fri Dec 15 20:07:49 2017 +0000
Revision:
2:27a7a42b2bae
Parent:
0:f603fa86c646
finalizado;

Who changed what in which revision?

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