Library for Real Time Clock module MCP97410 based on Library for DS1307
Fork of RTC-DS1307 by
Rtc_Mcp97410.cpp@11:ef48dcb888c9, 2015-01-16 (annotated)
- Committer:
- charly
- Date:
- Fri Jan 16 20:44:51 2015 +0000
- Revision:
- 11:ef48dcb888c9
- Parent:
- 10:780027029afe
- Child:
- 12:88f82e47b6a1
added functions for EEPROM read/write and EUI-address(unlock, read write)
Who changed what in which revision?
User | Revision | Line number | New 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 | 11:ef48dcb888c9 | 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 | 11:ef48dcb888c9 | 14 | |
charly | 11:ef48dcb888c9 | 15 | _i2c = i2c; |
charly | 11:ef48dcb888c9 | 16 | if (_i2c == NULL) |
charly | 10:780027029afe | 17 | error("Rtc_Mcp97410: no I2C specified"); |
charly | 11:ef48dcb888c9 | 18 | |
leihen | 1:64274190e842 | 19 | // Set the frequency to standard 100kHz |
charly | 10:780027029afe | 20 | // MCP97410 can handle full 400kHz-speed! |
charly | 11:ef48dcb888c9 | 21 | //_i2c->frequency(100000); |
leihen | 0:3940f0ad2ca5 | 22 | } |
leihen | 0:3940f0ad2ca5 | 23 | |
charly | 10:780027029afe | 24 | Rtc_Mcp97410::~Rtc_Mcp97410() |
leihen | 0:3940f0ad2ca5 | 25 | { |
charly | 11:ef48dcb888c9 | 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); |
charly | 11:ef48dcb888c9 | 33 | if (!readRTC(ADDR_SEC,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 | 11:ef48dcb888c9 | 38 | if (start) { |
charly | 11:ef48dcb888c9 | 39 | buffer[ADDR_SEC] |= START_32KHZ; |
charly | 11:ef48dcb888c9 | 40 | } else { |
charly | 11:ef48dcb888c9 | 41 | buffer[ADDR_SEC] &= ~START_32KHZ; |
charly | 11:ef48dcb888c9 | 42 | } |
charly | 11:ef48dcb888c9 | 43 | buffer[ADDR_SEC] = (buffer[ADDR_SEC]& START_32KHZ) | (decimalToBcd(time.sec)& ~START_32KHZ); |
charly | 11:ef48dcb888c9 | 44 | buffer[ADDR_MIN] = decimalToBcd(time.min); |
leihen | 2:ee81f2c5a706 | 45 | if (thm) { |
leihen | 2:ee81f2c5a706 | 46 | // AM PM format |
charly | 11:ef48dcb888c9 | 47 | buffer[ADDR_HOUR] = HOUR_12 | (time.hour>12 ? (0x20 | ((decimalToBcd(time.hour-12)))) : decimalToBcd(time.hour)); |
charly | 11:ef48dcb888c9 | 48 | } else { |
charly | 11:ef48dcb888c9 | 49 | // 24 hours format: HOUR_12 is 0 |
charly | 11:ef48dcb888c9 | 50 | buffer[ADDR_HOUR] = decimalToBcd(time.hour) & 0x3F; |
leihen | 2:ee81f2c5a706 | 51 | } |
charly | 10:780027029afe | 52 | // bit 3 of register 03 on MCP97410 is VBATEN (different to DS1307)! |
charly | 10:780027029afe | 53 | // should be set to 1 to enable Battery-Backup |
charly | 11:ef48dcb888c9 | 54 | buffer[ADDR_DAY] = VBATEN | (time.wday & 0x07); |
charly | 11:ef48dcb888c9 | 55 | buffer[ADDR_DATE] = decimalToBcd(time.date); |
charly | 11:ef48dcb888c9 | 56 | buffer[ADDR_MNTH] = decimalToBcd(time.mon); |
charly | 11:ef48dcb888c9 | 57 | buffer[ADDR_YEAR] = decimalToBcd(time.year-2000); |
leihen | 2:ee81f2c5a706 | 58 | INFO("Writing new time and date data to RTC\n"); |
charly | 11:ef48dcb888c9 | 59 | if (!writeRTC(ADDR_SEC, buffer, 7) ) { |
leihen | 2:ee81f2c5a706 | 60 | ERR("Failed to write the data to RTC!\n"); |
leihen | 2:ee81f2c5a706 | 61 | return false; |
leihen | 2:ee81f2c5a706 | 62 | } |
leihen | 0:3940f0ad2ca5 | 63 | return true; |
leihen | 0:3940f0ad2ca5 | 64 | } |
leihen | 0:3940f0ad2ca5 | 65 | |
charly | 10:780027029afe | 66 | bool Rtc_Mcp97410::getTime(Time_rtc& time) |
leihen | 0:3940f0ad2ca5 | 67 | { |
leihen | 1:64274190e842 | 68 | char buffer[7]; |
leihen | 1:64274190e842 | 69 | bool thm = false; |
charly | 11:ef48dcb888c9 | 70 | |
leihen | 1:64274190e842 | 71 | INFO("Getting time from RTC\n"); |
charly | 11:ef48dcb888c9 | 72 | if (!readRTC(ADDR_SEC, buffer, 7) ) { |
leihen | 1:64274190e842 | 73 | // Failed to read |
leihen | 1:64274190e842 | 74 | ERR("Failed to read from RTC\n"); |
leihen | 1:64274190e842 | 75 | return false; |
leihen | 1:64274190e842 | 76 | } |
charly | 11:ef48dcb888c9 | 77 | thm = ((buffer[ADDR_HOUR] & HOUR_12) == HOUR_12); |
charly | 11:ef48dcb888c9 | 78 | time.sec = bcdToDecimal(buffer[ADDR_SEC]&0x7F); |
charly | 11:ef48dcb888c9 | 79 | time.min = bcdToDecimal(buffer[ADDR_MIN]); |
leihen | 1:64274190e842 | 80 | if (thm) { |
leihen | 1:64274190e842 | 81 | // in 12-hour-mode, we need to add 12 hours if PM bit is set |
charly | 11:ef48dcb888c9 | 82 | time.hour = Rtc_Mcp97410::bcdToDecimal( buffer[ADDR_HOUR] & 0x1F ); |
charly | 11:ef48dcb888c9 | 83 | if ((buffer[ADDR_HOUR] & PM) == PM) |
leihen | 1:64274190e842 | 84 | time.hour += 12; |
charly | 11:ef48dcb888c9 | 85 | } else { |
charly | 11:ef48dcb888c9 | 86 | time.hour = Rtc_Mcp97410::bcdToDecimal( buffer[ADDR_HOUR] & 0x3F ); |
leihen | 1:64274190e842 | 87 | } |
charly | 11:ef48dcb888c9 | 88 | time.wday = buffer[ADDR_DAY] & 0x07; |
charly | 11:ef48dcb888c9 | 89 | INFO("OSCRUN:%0d PWRFAIL:%0d VBATEN:%0d\n", (buffer[ADDR_STAT]>>5) & 0x01, buffer[ADDR_STAT]>>4 & 0x01, buffer[ADDR_STAT]>>3 & 0x01); |
charly | 11:ef48dcb888c9 | 90 | time.date = Rtc_Mcp97410::bcdToDecimal( buffer[ADDR_DATE]); |
charly | 11:ef48dcb888c9 | 91 | time.mon = Rtc_Mcp97410::bcdToDecimal( buffer[ADDR_MNTH]); |
charly | 11:ef48dcb888c9 | 92 | time.year = Rtc_Mcp97410::bcdToDecimal(buffer[ADDR_YEAR]) + 2000; // plus hundret is because RTC is giving the years since 2000, but std c struct tm needs years since 1900 |
charly | 11:ef48dcb888c9 | 93 | |
leihen | 2:ee81f2c5a706 | 94 | return true; |
leihen | 2:ee81f2c5a706 | 95 | } |
leihen | 2:ee81f2c5a706 | 96 | |
leihen | 2:ee81f2c5a706 | 97 | |
charly | 10:780027029afe | 98 | bool Rtc_Mcp97410::startClock() |
charly | 10:780027029afe | 99 | { |
charly | 10:780027029afe | 100 | char strtStop; |
charly | 11:ef48dcb888c9 | 101 | |
charly | 10:780027029afe | 102 | INFO ("Reading clock start/stop register value\n"); |
charly | 11:ef48dcb888c9 | 103 | if (!readRTC(ADDR_SEC, &strtStop, 1)) { |
charly | 10:780027029afe | 104 | ERR("Failed to read clock start stop register !\n"); |
charly | 10:780027029afe | 105 | return false; |
charly | 10:780027029afe | 106 | } |
charly | 11:ef48dcb888c9 | 107 | //set bit |
charly | 11:ef48dcb888c9 | 108 | strtStop |= START_32KHZ; |
charly | 11:ef48dcb888c9 | 109 | |
charly | 11:ef48dcb888c9 | 110 | |
charly | 10:780027029afe | 111 | INFO("Writing back start/stop register value\n"); |
charly | 11:ef48dcb888c9 | 112 | if (!writeRTC(ADDR_SEC, &strtStop, 1)) { |
charly | 10:780027029afe | 113 | ERR("Failed to write the start stop register !\n"); |
charly | 10:780027029afe | 114 | return false; |
charly | 10:780027029afe | 115 | } |
charly | 11:ef48dcb888c9 | 116 | |
charly | 10:780027029afe | 117 | INFO("Start/stop register value successfully written\n"); |
charly | 10:780027029afe | 118 | return true; |
charly | 10:780027029afe | 119 | } |
charly | 10:780027029afe | 120 | |
charly | 10:780027029afe | 121 | bool Rtc_Mcp97410::stopClock() |
leihen | 2:ee81f2c5a706 | 122 | { |
leihen | 2:ee81f2c5a706 | 123 | char strtStop; |
charly | 11:ef48dcb888c9 | 124 | |
leihen | 2:ee81f2c5a706 | 125 | INFO ("Reading clock start/stop register value\n"); |
charly | 11:ef48dcb888c9 | 126 | if (!readRTC(ADDR_SEC, &strtStop, 1)) { |
leihen | 2:ee81f2c5a706 | 127 | ERR("Failed to read clock start stop register !\n"); |
leihen | 2:ee81f2c5a706 | 128 | return false; |
leihen | 2:ee81f2c5a706 | 129 | } |
charly | 11:ef48dcb888c9 | 130 | //clear bit |
charly | 11:ef48dcb888c9 | 131 | strtStop &= ~START_32KHZ; |
charly | 11:ef48dcb888c9 | 132 | |
leihen | 2:ee81f2c5a706 | 133 | INFO("Writing back start/stop register value\n"); |
charly | 11:ef48dcb888c9 | 134 | if (!writeRTC(ADDR_SEC, &strtStop, 1)) { |
leihen | 2:ee81f2c5a706 | 135 | ERR("Failed to write the start stop register !\n"); |
leihen | 2:ee81f2c5a706 | 136 | return false; |
leihen | 2:ee81f2c5a706 | 137 | } |
charly | 11:ef48dcb888c9 | 138 | |
leihen | 2:ee81f2c5a706 | 139 | INFO("Start/stop register value successfully written\n"); |
leihen | 2:ee81f2c5a706 | 140 | return true; |
leihen | 2:ee81f2c5a706 | 141 | } |
leihen | 2:ee81f2c5a706 | 142 | |
charly | 10:780027029afe | 143 | bool Rtc_Mcp97410::setSquareWaveOutput(bool ena, SqwRateSelect_t rs) |
leihen | 2:ee81f2c5a706 | 144 | { |
leihen | 2:ee81f2c5a706 | 145 | char reg; |
leihen | 2:ee81f2c5a706 | 146 | INFO("Reading register value first\n"); |
charly | 11:ef48dcb888c9 | 147 | |
charly | 11:ef48dcb888c9 | 148 | if (!readRTC(ADDR_CTRL,®, 1)) { |
charly | 11:ef48dcb888c9 | 149 | ERR("Failed to read RTC register value !\n"); |
leihen | 2:ee81f2c5a706 | 150 | return false; |
leihen | 2:ee81f2c5a706 | 151 | } |
charly | 11:ef48dcb888c9 | 152 | INFO("[Reg:0x07] = %02x\n", reg); |
charly | 11:ef48dcb888c9 | 153 | |
leihen | 2:ee81f2c5a706 | 154 | // preserve the OUT control bit while writing the frequency and enable bits |
charly | 11:ef48dcb888c9 | 155 | reg = (reg & OUT_PIN) | (ena ? 0x10 : 0) | ((char)rs & 0x03); |
leihen | 2:ee81f2c5a706 | 156 | |
leihen | 2:ee81f2c5a706 | 157 | INFO("Writing back register value\n"); |
charly | 11:ef48dcb888c9 | 158 | INFO("[Reg:0x07] = %02x\n", reg); |
charly | 11:ef48dcb888c9 | 159 | |
charly | 11:ef48dcb888c9 | 160 | if (!writeRTC(ADDR_CTRL, ®, 1)) { |
leihen | 2:ee81f2c5a706 | 161 | ERR("Failed to write register value !\n"); |
leihen | 2:ee81f2c5a706 | 162 | return false; |
leihen | 2:ee81f2c5a706 | 163 | } |
charly | 11:ef48dcb888c9 | 164 | |
leihen | 2:ee81f2c5a706 | 165 | INFO("Successfully changed the square wave output.\n"); |
leihen | 2:ee81f2c5a706 | 166 | return true; |
leihen | 2:ee81f2c5a706 | 167 | } |
leihen | 2:ee81f2c5a706 | 168 | |
leihen | 2:ee81f2c5a706 | 169 | |
leihen | 1:64274190e842 | 170 | |
charly | 11:ef48dcb888c9 | 171 | bool Rtc_Mcp97410::read(int control_byte, int address, char *buffer, int len) |
leihen | 1:64274190e842 | 172 | { |
leihen | 1:64274190e842 | 173 | char buffer2[2] = {(char)address, 0}; |
charly | 11:ef48dcb888c9 | 174 | |
charly | 11:ef48dcb888c9 | 175 | // _i2c->start(); |
charly | 11:ef48dcb888c9 | 176 | if (_i2c->write(control_byte, buffer2, 1) != 0) { |
charly | 11:ef48dcb888c9 | 177 | ERR("Failed to write register address on RTC or EEPROM!\n"); |
charly | 11:ef48dcb888c9 | 178 | _i2c->stop(); |
leihen | 1:64274190e842 | 179 | return false; |
leihen | 1:64274190e842 | 180 | } |
charly | 11:ef48dcb888c9 | 181 | if (_i2c->read(control_byte, buffer, len) != 0) { |
leihen | 1:64274190e842 | 182 | ERR("Failed to read register !\n"); |
leihen | 1:64274190e842 | 183 | return false; |
leihen | 1:64274190e842 | 184 | } |
charly | 11:ef48dcb888c9 | 185 | _i2c->stop(); |
charly | 11:ef48dcb888c9 | 186 | |
charly | 11:ef48dcb888c9 | 187 | INFO("Successfully read %d bytes from RTC or EEPROM\n", len); |
leihen | 1:64274190e842 | 188 | return true; |
leihen | 1:64274190e842 | 189 | } |
leihen | 1:64274190e842 | 190 | |
charly | 11:ef48dcb888c9 | 191 | bool Rtc_Mcp97410::readRTC(int address, char *buffer, int len) |
charly | 11:ef48dcb888c9 | 192 | { |
charly | 11:ef48dcb888c9 | 193 | return read(ADDR_RTCC, address,buffer, len); |
charly | 11:ef48dcb888c9 | 194 | } |
charly | 11:ef48dcb888c9 | 195 | |
charly | 11:ef48dcb888c9 | 196 | bool Rtc_Mcp97410::readEEPROM(int address, char *buffer, int len) |
charly | 11:ef48dcb888c9 | 197 | { |
charly | 11:ef48dcb888c9 | 198 | return read(ADDR_EEPROM, address,buffer, len); |
charly | 11:ef48dcb888c9 | 199 | } |
charly | 11:ef48dcb888c9 | 200 | |
charly | 11:ef48dcb888c9 | 201 | bool Rtc_Mcp97410::write(int control_byte, int address, char *buffer, int len) |
leihen | 1:64274190e842 | 202 | { |
leihen | 1:64274190e842 | 203 | char buffer2[10]; |
leihen | 1:64274190e842 | 204 | buffer2[0] = address&0xFF; |
leihen | 1:64274190e842 | 205 | for (int i = 0 ; i < len ; i++) |
leihen | 1:64274190e842 | 206 | buffer2[i+1] = buffer[i]; |
leihen | 1:64274190e842 | 207 | |
charly | 11:ef48dcb888c9 | 208 | // _i2c->start(); |
charly | 11:ef48dcb888c9 | 209 | if (_i2c->write(control_byte, buffer2, len+1) != 0) { |
charly | 11:ef48dcb888c9 | 210 | ERR("Failed to write data to rtc or EEPROM\n"); |
charly | 11:ef48dcb888c9 | 211 | _i2c->stop(); |
leihen | 1:64274190e842 | 212 | return false; |
leihen | 1:64274190e842 | 213 | } |
charly | 11:ef48dcb888c9 | 214 | _i2c->stop(); |
charly | 11:ef48dcb888c9 | 215 | INFO("Successfully written %d bytes to RTC or EEPROM\n", len); |
leihen | 1:64274190e842 | 216 | return true; |
leihen | 7:dca20be3ef38 | 217 | } |
leihen | 7:dca20be3ef38 | 218 | |
leihen | 7:dca20be3ef38 | 219 | |
charly | 11:ef48dcb888c9 | 220 | bool Rtc_Mcp97410::writeRTC(int address, char *buffer, int len) |
charly | 11:ef48dcb888c9 | 221 | { |
charly | 11:ef48dcb888c9 | 222 | return write(ADDR_RTCC, address,buffer, len); |
charly | 11:ef48dcb888c9 | 223 | } |
leihen | 7:dca20be3ef38 | 224 | |
charly | 11:ef48dcb888c9 | 225 | bool Rtc_Mcp97410::writeEEPROM(int address, char* buffer, int len) |
charly | 11:ef48dcb888c9 | 226 | { |
charly | 11:ef48dcb888c9 | 227 | if (len <=8) { |
charly | 11:ef48dcb888c9 | 228 | return write(ADDR_EEPROM, address,buffer, len); |
charly | 11:ef48dcb888c9 | 229 | } else { |
charly | 11:ef48dcb888c9 | 230 | ERR("Can write maximum 8 Bytes to EEPROM in one call\n"); |
charly | 11:ef48dcb888c9 | 231 | return false; |
charly | 11:ef48dcb888c9 | 232 | } |
charly | 11:ef48dcb888c9 | 233 | } |
charly | 11:ef48dcb888c9 | 234 | |
charly | 11:ef48dcb888c9 | 235 | bool Rtc_Mcp97410::disableEEPROMWrite() |
charly | 11:ef48dcb888c9 | 236 | { |
charly | 11:ef48dcb888c9 | 237 | // protect all EEPROM from write operations |
charly | 11:ef48dcb888c9 | 238 | // set STATUS_REGISTER 0xFF bits 2 and 3 |
charly | 11:ef48dcb888c9 | 239 | char reg = 0x0C; |
charly | 11:ef48dcb888c9 | 240 | return write(ADDR_EEPROM, ADDR_EEPROM_SR,®, 1); |
charly | 11:ef48dcb888c9 | 241 | } |
charly | 11:ef48dcb888c9 | 242 | |
charly | 11:ef48dcb888c9 | 243 | |
charly | 11:ef48dcb888c9 | 244 | bool Rtc_Mcp97410::enableEEPROMWrite() |
charly | 11:ef48dcb888c9 | 245 | { |
charly | 11:ef48dcb888c9 | 246 | // enable all EEPROM write operations |
charly | 11:ef48dcb888c9 | 247 | // unset STATUS_REGISTER 0xFF bits 2 and 3 |
charly | 11:ef48dcb888c9 | 248 | char reg = 0x00; |
charly | 11:ef48dcb888c9 | 249 | return write(ADDR_EEPROM, ADDR_EEPROM_SR,®, 1); |
charly | 11:ef48dcb888c9 | 250 | } |
charly | 11:ef48dcb888c9 | 251 | |
charly | 11:ef48dcb888c9 | 252 | bool Rtc_Mcp97410::readEUI48(uint8_t* eui48) |
charly | 11:ef48dcb888c9 | 253 | { |
charly | 11:ef48dcb888c9 | 254 | //EUI48 start at EEPROM-address 0xF2 |
charly | 11:ef48dcb888c9 | 255 | return(readEEPROM(0xF2,(char *) eui48,6)); |
charly | 11:ef48dcb888c9 | 256 | } |
charly | 11:ef48dcb888c9 | 257 | |
charly | 11:ef48dcb888c9 | 258 | |
charly | 11:ef48dcb888c9 | 259 | bool Rtc_Mcp97410::writeEUI48(uint8_t* eui48) |
charly | 11:ef48dcb888c9 | 260 | { |
charly | 11:ef48dcb888c9 | 261 | //EUI48 start at EEPROM-address 0xF2 |
charly | 11:ef48dcb888c9 | 262 | return(writeEEPROM(0xF2,(char *) eui48,6)); |
charly | 11:ef48dcb888c9 | 263 | } |
charly | 11:ef48dcb888c9 | 264 | |
charly | 11:ef48dcb888c9 | 265 | bool Rtc_Mcp97410::unlockEUI() |
charly | 11:ef48dcb888c9 | 266 | { |
charly | 11:ef48dcb888c9 | 267 | // unlock the special EEPROM area |
charly | 11:ef48dcb888c9 | 268 | // write 0x55 and then 0xAA to the EEUNLOCK-register |
charly | 11:ef48dcb888c9 | 269 | char reg =0x00; |
charly | 11:ef48dcb888c9 | 270 | bool result = true; |
charly | 11:ef48dcb888c9 | 271 | |
charly | 11:ef48dcb888c9 | 272 | reg = 0x55; |
charly | 11:ef48dcb888c9 | 273 | result &= writeRTC(ADDR_ULID,®,1); |
charly | 11:ef48dcb888c9 | 274 | reg = 0xAA; |
charly | 11:ef48dcb888c9 | 275 | result &= writeRTC(ADDR_ULID,®,1); |
charly | 11:ef48dcb888c9 | 276 | if (result) { |
charly | 11:ef48dcb888c9 | 277 | INFO("Successfully UNLOCKED the EUI-Area in EEPROM\n"); |
charly | 11:ef48dcb888c9 | 278 | return true; |
charly | 11:ef48dcb888c9 | 279 | } else { |
charly | 11:ef48dcb888c9 | 280 | ERR("Error in UNLOCKING the EUI-Area in EEPROM\n"); |
charly | 11:ef48dcb888c9 | 281 | return false; |
charly | 11:ef48dcb888c9 | 282 | } |
charly | 11:ef48dcb888c9 | 283 | } |
leihen | 7:dca20be3ef38 | 284 | |
charly | 10:780027029afe | 285 | RtcCls::RtcCls(I2C* i2c, PinName sqw, bool bUseSqw) |
charly | 10:780027029afe | 286 | : Rtc_Mcp97410(i2c), m_sqw(sqw), m_bUseSqw(bUseSqw), m_bAlarmEnabled(false), m_alarmfunc(NULL) |
leihen | 7:dca20be3ef38 | 287 | { |
leihen | 9:5627b407e097 | 288 | Time_rtc t; |
leihen | 9:5627b407e097 | 289 | // query time from device |
leihen | 9:5627b407e097 | 290 | getTime(t); |
leihen | 9:5627b407e097 | 291 | // sync the time with MBED RTC |
leihen | 9:5627b407e097 | 292 | struct tm now = {t.sec, t.min, t.hour, t.date, t.mon-1, t.year-1900}; |
leihen | 9:5627b407e097 | 293 | m_time = mktime(&now); |
leihen | 9:5627b407e097 | 294 | set_time(m_time); |
charly | 11:ef48dcb888c9 | 295 | |
leihen | 7:dca20be3ef38 | 296 | // Only register the callback and start the SQW if requested to do so. Otherwise the system |
leihen | 7:dca20be3ef38 | 297 | // will use the MBED built-in RTC. |
leihen | 7:dca20be3ef38 | 298 | if (m_bUseSqw) { |
leihen | 7:dca20be3ef38 | 299 | // start the wave |
leihen | 7:dca20be3ef38 | 300 | setSquareWaveOutput(true, RS1Hz); |
leihen | 7:dca20be3ef38 | 301 | // register callback from now on the time will be maintained by the square wave input |
leihen | 7:dca20be3ef38 | 302 | m_sqw.rise(this, &RtcCls::_callback); |
leihen | 7:dca20be3ef38 | 303 | } |
leihen | 7:dca20be3ef38 | 304 | } |
leihen | 7:dca20be3ef38 | 305 | |
leihen | 7:dca20be3ef38 | 306 | void RtcCls::_callback(void) |
leihen | 7:dca20be3ef38 | 307 | { |
leihen | 8:d0e66fa78e79 | 308 | // INFO("Tick!"); |
leihen | 7:dca20be3ef38 | 309 | // Simply increase the number of seconds |
leihen | 7:dca20be3ef38 | 310 | m_time++; |
leihen | 8:d0e66fa78e79 | 311 | // if (m_bAlarmEnabled && (m_time == m_alarmTime)) { |
leihen | 8:d0e66fa78e79 | 312 | // if (m_alarmfunc != NULL) |
leihen | 8:d0e66fa78e79 | 313 | // m_alarmfunc(); |
leihen | 8:d0e66fa78e79 | 314 | // m_bAlarmEnabled = false; |
leihen | 8:d0e66fa78e79 | 315 | // } |
leihen | 7:dca20be3ef38 | 316 | } |
leihen | 7:dca20be3ef38 | 317 | |
leihen | 7:dca20be3ef38 | 318 | time_t RtcCls::getTime() |
leihen | 7:dca20be3ef38 | 319 | { |
leihen | 7:dca20be3ef38 | 320 | // 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 | 321 | if (!m_bUseSqw) { |
leihen | 7:dca20be3ef38 | 322 | Time_rtc t; |
leihen | 7:dca20be3ef38 | 323 | getTime(t); |
leihen | 8:d0e66fa78e79 | 324 | struct tm now = {t.sec, t.min, t.hour, t.date, t.mon-1, t.year-1900}; |
leihen | 7:dca20be3ef38 | 325 | m_time = mktime(&now); |
leihen | 8:d0e66fa78e79 | 326 | 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); |
charly | 11:ef48dcb888c9 | 327 | } else { |
leihen | 8:d0e66fa78e79 | 328 | INFO("getting time Ticks=%08lx", m_time); |
leihen | 7:dca20be3ef38 | 329 | } |
leihen | 7:dca20be3ef38 | 330 | return m_time; |
leihen | 7:dca20be3ef38 | 331 | } |
leihen | 7:dca20be3ef38 | 332 | |
leihen | 7:dca20be3ef38 | 333 | void RtcCls::setTime(time_t t) |
leihen | 7:dca20be3ef38 | 334 | { |
leihen | 7:dca20be3ef38 | 335 | Time_rtc tim; |
leihen | 7:dca20be3ef38 | 336 | struct tm *now; |
leihen | 7:dca20be3ef38 | 337 | now = localtime(&t); |
leihen | 7:dca20be3ef38 | 338 | |
leihen | 7:dca20be3ef38 | 339 | tim.sec = now->tm_sec; |
leihen | 7:dca20be3ef38 | 340 | tim.min = now->tm_min; |
leihen | 7:dca20be3ef38 | 341 | tim.hour = now->tm_hour; |
leihen | 7:dca20be3ef38 | 342 | tim.date = now->tm_mday; |
leihen | 8:d0e66fa78e79 | 343 | tim.mon = now->tm_mon+1; |
leihen | 7:dca20be3ef38 | 344 | tim.year = now->tm_year + 1900; |
leihen | 7:dca20be3ef38 | 345 | tim.wday = now->tm_wday +1; |
leihen | 7:dca20be3ef38 | 346 | |
leihen | 7:dca20be3ef38 | 347 | setTime( tim, true, true); |
leihen | 8:d0e66fa78e79 | 348 | set_time(t); |
leihen | 7:dca20be3ef38 | 349 | } |