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.cpp@9:5627b407e097
Child:
11:ef48dcb888c9
RTC-part working for MCP97410

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leihen 5:30531f2121a2 1 #include "mbed.h"
charly 10:780027029afe 2 #include "Rtc_Mcp97410.h"
leihen 7:dca20be3ef38 3
leihen 5:30531f2121a2 4 #ifndef DEBUG
charly 10:780027029afe 5 #define DEBUG
leihen 5:30531f2121a2 6 #endif
leihen 3:e89d63f3342e 7 #include "debug.h"
leihen 0:3940f0ad2ca5 8
charly 10:780027029afe 9 const char *Rtc_Mcp97410::m_weekDays[] = { "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
leihen 0:3940f0ad2ca5 10
leihen 0:3940f0ad2ca5 11
charly 10:780027029afe 12 Rtc_Mcp97410::Rtc_Mcp97410(I2C* i2c)
leihen 0:3940f0ad2ca5 13 {
charly 10:780027029afe 14
charly 10:780027029afe 15 m_rtc = i2c;
leihen 0:3940f0ad2ca5 16 if (m_rtc == NULL)
charly 10:780027029afe 17 error("Rtc_Mcp97410: no I2C specified");
leihen 1:64274190e842 18
leihen 1:64274190e842 19 // Set the frequency to standard 100kHz
charly 10:780027029afe 20 // MCP97410 can handle full 400kHz-speed!
charly 10:780027029afe 21 //m_rtc->frequency(100000);
leihen 0:3940f0ad2ca5 22 }
leihen 0:3940f0ad2ca5 23
charly 10:780027029afe 24 Rtc_Mcp97410::~Rtc_Mcp97410()
leihen 0:3940f0ad2ca5 25 {
charly 10:780027029afe 26
leihen 0:3940f0ad2ca5 27 }
leihen 0:3940f0ad2ca5 28
charly 10:780027029afe 29 bool Rtc_Mcp97410::setTime(Time_rtc& time, bool start, bool thm)
leihen 0:3940f0ad2ca5 30 {
leihen 2:ee81f2c5a706 31 char buffer[7];
leihen 2:ee81f2c5a706 32 INFO("reading clock registers to write the new time : %d:%d:%d\n", time.hour, time.min, time.sec);
leihen 2:ee81f2c5a706 33 if (!read(0,buffer,7)) {
leihen 2:ee81f2c5a706 34 ERR("Failed to read from RTC\n");
leihen 2:ee81f2c5a706 35 return false;
leihen 2:ee81f2c5a706 36 }
leihen 2:ee81f2c5a706 37 // Now update only the time part (saving the existing flags)
charly 10:780027029afe 38 if (start) { buffer[0] |= 0x80; } else { buffer[0] &= 0x7F; }
leihen 2:ee81f2c5a706 39 buffer[0] = (buffer[0]&0x80) | (decimalToBcd(time.sec)& 0x7f);
leihen 2:ee81f2c5a706 40 buffer[1] = decimalToBcd(time.min);
leihen 2:ee81f2c5a706 41 if (thm) {
leihen 2:ee81f2c5a706 42 // AM PM format
leihen 2:ee81f2c5a706 43 buffer[2] = (buffer[2]& 196) | (time.hour>12 ? (0x20 | ((decimalToBcd(time.hour-12)))) : decimalToBcd(time.hour));
leihen 2:ee81f2c5a706 44 }
leihen 2:ee81f2c5a706 45 else {
leihen 2:ee81f2c5a706 46 // 24 hours format
leihen 2:ee81f2c5a706 47 buffer[2] = (buffer[2]& 196) | (decimalToBcd(time.hour) & 0x3F);
leihen 2:ee81f2c5a706 48 }
charly 10:780027029afe 49 // bit 3 of register 03 on MCP97410 is VBATEN (different to DS1307)!
charly 10:780027029afe 50 // should be set to 1 to enable Battery-Backup
charly 10:780027029afe 51 buffer[3] = 0x08 | (time.wday & 0x07);
leihen 2:ee81f2c5a706 52 buffer[4] = decimalToBcd(time.date);
leihen 2:ee81f2c5a706 53 buffer[5] = decimalToBcd(time.mon);
leihen 2:ee81f2c5a706 54 buffer[6] = decimalToBcd(time.year-2000);
leihen 2:ee81f2c5a706 55 INFO("Writing new time and date data to RTC\n");
leihen 2:ee81f2c5a706 56 if (!write(0, buffer, 7) ) {
leihen 2:ee81f2c5a706 57 ERR("Failed to write the data to RTC!\n");
leihen 2:ee81f2c5a706 58 return false;
leihen 2:ee81f2c5a706 59 }
leihen 0:3940f0ad2ca5 60 return true;
leihen 0:3940f0ad2ca5 61 }
leihen 0:3940f0ad2ca5 62
charly 10:780027029afe 63 bool Rtc_Mcp97410::getTime(Time_rtc& time)
leihen 0:3940f0ad2ca5 64 {
leihen 1:64274190e842 65 char buffer[7];
leihen 1:64274190e842 66 bool thm = false;
leihen 1:64274190e842 67
leihen 1:64274190e842 68 INFO("Getting time from RTC\n");
leihen 2:ee81f2c5a706 69 if (!read(0, buffer, 7) ) {
leihen 1:64274190e842 70 // Failed to read
leihen 1:64274190e842 71 ERR("Failed to read from RTC\n");
leihen 1:64274190e842 72 return false;
leihen 1:64274190e842 73 }
leihen 1:64274190e842 74 thm = ((buffer[2] & 64) == 64);
leihen 1:64274190e842 75 time.sec = bcdToDecimal(buffer[0]&0x7F);
leihen 1:64274190e842 76 time.min = bcdToDecimal(buffer[1]);
leihen 1:64274190e842 77 if (thm) {
leihen 1:64274190e842 78 // in 12-hour-mode, we need to add 12 hours if PM bit is set
charly 10:780027029afe 79 time.hour = Rtc_Mcp97410::bcdToDecimal( buffer[2] & 31 );
leihen 1:64274190e842 80 if ((buffer[2] & 32) == 32)
leihen 1:64274190e842 81 time.hour += 12;
leihen 1:64274190e842 82 }
leihen 1:64274190e842 83 else {
charly 10:780027029afe 84 time.hour = Rtc_Mcp97410::bcdToDecimal( buffer[2] & 63 );
leihen 1:64274190e842 85 }
charly 10:780027029afe 86 time.wday = buffer[3] & 0x07;
charly 10:780027029afe 87 INFO("OSCRUN:%0d PWRFAIL:%0d VBATEN:%0d\n", (buffer[3]>>5) & 0x01, buffer[3]>>4 & 0x01, buffer[3]>>3 & 0x01);
charly 10:780027029afe 88 time.date = Rtc_Mcp97410::bcdToDecimal( buffer[4]);
charly 10:780027029afe 89 time.mon = Rtc_Mcp97410::bcdToDecimal( buffer[5]);
charly 10:780027029afe 90 time.year = Rtc_Mcp97410::bcdToDecimal(buffer[6]) + 2000; // plus hundret is because RTC is giving the years since 2000, but std c struct tm needs years since 1900
leihen 2:ee81f2c5a706 91
leihen 2:ee81f2c5a706 92 return true;
leihen 2:ee81f2c5a706 93 }
leihen 2:ee81f2c5a706 94
leihen 2:ee81f2c5a706 95
charly 10:780027029afe 96 bool Rtc_Mcp97410::startClock()
charly 10:780027029afe 97 {
charly 10:780027029afe 98 char strtStop;
charly 10:780027029afe 99
charly 10:780027029afe 100 INFO ("Reading clock start/stop register value\n");
charly 10:780027029afe 101 if (!read(0, &strtStop, 1)) {
charly 10:780027029afe 102 ERR("Failed to read clock start stop register !\n");
charly 10:780027029afe 103 return false;
charly 10:780027029afe 104 }
charly 10:780027029afe 105
charly 10:780027029afe 106 strtStop |= 0x80;
charly 10:780027029afe 107
charly 10:780027029afe 108
charly 10:780027029afe 109 INFO("Writing back start/stop register value\n");
charly 10:780027029afe 110 if (!write(0, &strtStop, 1)) {
charly 10:780027029afe 111 ERR("Failed to write the start stop register !\n");
charly 10:780027029afe 112 return false;
charly 10:780027029afe 113 }
charly 10:780027029afe 114
charly 10:780027029afe 115 INFO("Start/stop register value successfully written\n");
charly 10:780027029afe 116 return true;
charly 10:780027029afe 117 }
charly 10:780027029afe 118
charly 10:780027029afe 119 bool Rtc_Mcp97410::stopClock()
leihen 2:ee81f2c5a706 120 {
leihen 2:ee81f2c5a706 121 char strtStop;
leihen 2:ee81f2c5a706 122
leihen 2:ee81f2c5a706 123 INFO ("Reading clock start/stop register value\n");
leihen 2:ee81f2c5a706 124 if (!read(0, &strtStop, 1)) {
leihen 2:ee81f2c5a706 125 ERR("Failed to read clock start stop register !\n");
leihen 2:ee81f2c5a706 126 return false;
leihen 2:ee81f2c5a706 127 }
leihen 2:ee81f2c5a706 128
leihen 2:ee81f2c5a706 129 strtStop &= 0x7F;
leihen 2:ee81f2c5a706 130
leihen 2:ee81f2c5a706 131 INFO("Writing back start/stop register value\n");
leihen 2:ee81f2c5a706 132 if (!write(0, &strtStop, 1)) {
leihen 2:ee81f2c5a706 133 ERR("Failed to write the start stop register !\n");
leihen 2:ee81f2c5a706 134 return false;
leihen 2:ee81f2c5a706 135 }
leihen 2:ee81f2c5a706 136
leihen 2:ee81f2c5a706 137 INFO("Start/stop register value successfully written\n");
leihen 2:ee81f2c5a706 138 return true;
leihen 2:ee81f2c5a706 139 }
leihen 2:ee81f2c5a706 140
charly 10:780027029afe 141 bool Rtc_Mcp97410::setSquareWaveOutput(bool ena, SqwRateSelect_t rs)
leihen 2:ee81f2c5a706 142 {
leihen 2:ee81f2c5a706 143 char reg;
leihen 2:ee81f2c5a706 144 INFO("Reading register value first\n");
leihen 2:ee81f2c5a706 145
leihen 2:ee81f2c5a706 146 if (!read(7,&reg, 1)) {
leihen 2:ee81f2c5a706 147 ERR("Failed to read register value !\n");
leihen 2:ee81f2c5a706 148 return false;
leihen 2:ee81f2c5a706 149 }
leihen 2:ee81f2c5a706 150 INFO("[Reg:0x07] = %02x\n", reg);
leihen 2:ee81f2c5a706 151
leihen 2:ee81f2c5a706 152 // preserve the OUT control bit while writing the frequency and enable bits
leihen 2:ee81f2c5a706 153 reg = (reg & 0x80) | (ena ? 0x10 : 0) | ((char)rs & 0x03);
leihen 2:ee81f2c5a706 154
leihen 2:ee81f2c5a706 155 INFO("Writing back register value\n");
leihen 2:ee81f2c5a706 156 INFO("[Reg:0x07] = %02x\n", reg);
leihen 2:ee81f2c5a706 157
leihen 2:ee81f2c5a706 158 if (!write(7, &reg, 1)) {
leihen 2:ee81f2c5a706 159 ERR("Failed to write register value !\n");
leihen 2:ee81f2c5a706 160 return false;
leihen 2:ee81f2c5a706 161 }
leihen 2:ee81f2c5a706 162
leihen 2:ee81f2c5a706 163 INFO("Successfully changed the square wave output.\n");
leihen 2:ee81f2c5a706 164 return true;
leihen 2:ee81f2c5a706 165 }
leihen 2:ee81f2c5a706 166
leihen 2:ee81f2c5a706 167
leihen 1:64274190e842 168
charly 10:780027029afe 169 bool Rtc_Mcp97410::read(int address, char *buffer, int len)
leihen 1:64274190e842 170 {
leihen 1:64274190e842 171 char buffer2[2] = {(char)address, 0};
leihen 1:64274190e842 172
leihen 2:ee81f2c5a706 173 // m_rtc->start();
charly 10:780027029afe 174 if (m_rtc->write(0xDF, buffer2, 1) != 0) {
leihen 1:64274190e842 175 ERR("Failed to write register address on rtv!\n");
leihen 1:64274190e842 176 m_rtc->stop();
leihen 1:64274190e842 177 return false;
leihen 1:64274190e842 178 }
charly 10:780027029afe 179 if (m_rtc->read(0xDF, buffer, len) != 0) {
leihen 1:64274190e842 180 ERR("Failed to read register !\n");
leihen 1:64274190e842 181 return false;
leihen 1:64274190e842 182 }
leihen 1:64274190e842 183 m_rtc->stop();
leihen 1:64274190e842 184
leihen 1:64274190e842 185 INFO("Successfully read %d registers from RTC\n", len);
leihen 1:64274190e842 186 return true;
leihen 1:64274190e842 187 }
leihen 1:64274190e842 188
charly 10:780027029afe 189 bool Rtc_Mcp97410::write(int address, char *buffer, int len)
leihen 1:64274190e842 190 {
leihen 1:64274190e842 191 char buffer2[10];
leihen 1:64274190e842 192 buffer2[0] = address&0xFF;
leihen 1:64274190e842 193 for (int i = 0 ; i < len ; i++)
leihen 1:64274190e842 194 buffer2[i+1] = buffer[i];
leihen 1:64274190e842 195
leihen 2:ee81f2c5a706 196 // m_rtc->start();
charly 10:780027029afe 197 if (m_rtc->write(0xDF, buffer2, len+1) != 0) {
leihen 1:64274190e842 198 ERR("Failed to write data to rtc\n");
leihen 1:64274190e842 199 m_rtc->stop();
leihen 1:64274190e842 200 return false;
leihen 1:64274190e842 201 }
leihen 1:64274190e842 202 m_rtc->stop();
leihen 1:64274190e842 203 return true;
leihen 7:dca20be3ef38 204 }
leihen 7:dca20be3ef38 205
leihen 7:dca20be3ef38 206
leihen 7:dca20be3ef38 207
leihen 7:dca20be3ef38 208
charly 10:780027029afe 209 RtcCls::RtcCls(I2C* i2c, PinName sqw, bool bUseSqw)
charly 10:780027029afe 210 : Rtc_Mcp97410(i2c), m_sqw(sqw), m_bUseSqw(bUseSqw), m_bAlarmEnabled(false), m_alarmfunc(NULL)
leihen 7:dca20be3ef38 211 {
leihen 9:5627b407e097 212 Time_rtc t;
leihen 9:5627b407e097 213 // query time from device
leihen 9:5627b407e097 214 getTime(t);
leihen 9:5627b407e097 215 // sync the time with MBED RTC
leihen 9:5627b407e097 216 struct tm now = {t.sec, t.min, t.hour, t.date, t.mon-1, t.year-1900};
leihen 9:5627b407e097 217 m_time = mktime(&now);
leihen 9:5627b407e097 218 set_time(m_time);
leihen 9:5627b407e097 219
leihen 7:dca20be3ef38 220 // Only register the callback and start the SQW if requested to do so. Otherwise the system
leihen 7:dca20be3ef38 221 // will use the MBED built-in RTC.
leihen 7:dca20be3ef38 222 if (m_bUseSqw) {
leihen 7:dca20be3ef38 223 // start the wave
leihen 7:dca20be3ef38 224 setSquareWaveOutput(true, RS1Hz);
leihen 7:dca20be3ef38 225 // register callback from now on the time will be maintained by the square wave input
leihen 7:dca20be3ef38 226 m_sqw.rise(this, &RtcCls::_callback);
leihen 7:dca20be3ef38 227 }
leihen 7:dca20be3ef38 228 }
leihen 7:dca20be3ef38 229
leihen 7:dca20be3ef38 230 void RtcCls::_callback(void)
leihen 7:dca20be3ef38 231 {
leihen 8:d0e66fa78e79 232 // INFO("Tick!");
leihen 7:dca20be3ef38 233 // Simply increase the number of seconds
leihen 7:dca20be3ef38 234 m_time++;
leihen 8:d0e66fa78e79 235 // if (m_bAlarmEnabled && (m_time == m_alarmTime)) {
leihen 8:d0e66fa78e79 236 // if (m_alarmfunc != NULL)
leihen 8:d0e66fa78e79 237 // m_alarmfunc();
leihen 8:d0e66fa78e79 238 // m_bAlarmEnabled = false;
leihen 8:d0e66fa78e79 239 // }
leihen 7:dca20be3ef38 240 }
leihen 7:dca20be3ef38 241
leihen 7:dca20be3ef38 242 time_t RtcCls::getTime()
leihen 7:dca20be3ef38 243 {
leihen 7:dca20be3ef38 244 // when not using the HW support, we have to query the RTC chip. Other wise we can just return out stored value
leihen 7:dca20be3ef38 245 if (!m_bUseSqw) {
leihen 7:dca20be3ef38 246 Time_rtc t;
leihen 7:dca20be3ef38 247 getTime(t);
leihen 8:d0e66fa78e79 248 struct tm now = {t.sec, t.min, t.hour, t.date, t.mon-1, t.year-1900};
leihen 7:dca20be3ef38 249 m_time = mktime(&now);
leihen 8:d0e66fa78e79 250 INFO("getting time %02d.%02d.%04d %02d:%02d:%02d Ticks=%08lx", t.date, t.mon, t.year, t.hour, t.min, t.sec, m_time);
leihen 8:d0e66fa78e79 251 }
leihen 8:d0e66fa78e79 252 else {
leihen 8:d0e66fa78e79 253 INFO("getting time Ticks=%08lx", m_time);
leihen 7:dca20be3ef38 254 }
leihen 7:dca20be3ef38 255 return m_time;
leihen 7:dca20be3ef38 256 }
leihen 7:dca20be3ef38 257
leihen 7:dca20be3ef38 258 void RtcCls::setTime(time_t t)
leihen 7:dca20be3ef38 259 {
leihen 7:dca20be3ef38 260 Time_rtc tim;
leihen 7:dca20be3ef38 261 struct tm *now;
leihen 7:dca20be3ef38 262 now = localtime(&t);
leihen 7:dca20be3ef38 263
leihen 7:dca20be3ef38 264 tim.sec = now->tm_sec;
leihen 7:dca20be3ef38 265 tim.min = now->tm_min;
leihen 7:dca20be3ef38 266 tim.hour = now->tm_hour;
leihen 7:dca20be3ef38 267 tim.date = now->tm_mday;
leihen 8:d0e66fa78e79 268 tim.mon = now->tm_mon+1;
leihen 7:dca20be3ef38 269 tim.year = now->tm_year + 1900;
leihen 7:dca20be3ef38 270 tim.wday = now->tm_wday +1;
leihen 7:dca20be3ef38 271
leihen 7:dca20be3ef38 272 setTime( tim, true, true);
leihen 8:d0e66fa78e79 273 set_time(t);
leihen 7:dca20be3ef38 274 }