Working Version of the Real Time Clock module DS1307.

Dependents:   Alarma_rtc

Fork of RTC-DS1307 by Henry Leinen

Committer:
leihen
Date:
Sun Jun 02 18:57:26 2013 +0000
Revision:
1:64274190e842
Parent:
0:3940f0ad2ca5
Child:
2:ee81f2c5a706
First complete Version - untested.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leihen 0:3940f0ad2ca5 1 /* Rtc_Ds1307.cpp */
leihen 0:3940f0ad2ca5 2
leihen 0:3940f0ad2ca5 3 #include "Rtc_Ds1307.h"
leihen 0:3940f0ad2ca5 4
leihen 1:64274190e842 5 #define _DEBUG 1
leihen 0:3940f0ad2ca5 6
leihen 0:3940f0ad2ca5 7
leihen 0:3940f0ad2ca5 8 #if (_DEBUG && !defined(TARGET_LPC11U24))
leihen 0:3940f0ad2ca5 9 #define INFO(x, ...) std::printf("[Rtc_Ds1307 : INFO]"x"\r\n", ##__VA_ARGS__);
leihen 0:3940f0ad2ca5 10 #define WARN(x, ...) std::printf("[Rtc_Ds1307 : WARN]"x"\r\n", ##__VA_ARGS__);
leihen 0:3940f0ad2ca5 11 #define ERR(x, ...) std::printf("[Rtc_Ds1307 : ERR]"x"\r\n", ##__VA_ARGS__);
leihen 0:3940f0ad2ca5 12 #else
leihen 0:3940f0ad2ca5 13 #define INFO(x, ...)
leihen 0:3940f0ad2ca5 14 #define WARN(x, ...)
leihen 0:3940f0ad2ca5 15 #define ERR(x, ...)
leihen 0:3940f0ad2ca5 16 #endif
leihen 0:3940f0ad2ca5 17
leihen 1:64274190e842 18 const char *Rtc_Ds1307::m_weekDays[] = { "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
leihen 0:3940f0ad2ca5 19
leihen 0:3940f0ad2ca5 20
leihen 0:3940f0ad2ca5 21 Rtc_Ds1307::Rtc_Ds1307(PinName sda, PinName scl)
leihen 0:3940f0ad2ca5 22 {
leihen 0:3940f0ad2ca5 23 // Create a new I2C object
leihen 0:3940f0ad2ca5 24 m_rtc = new I2C(sda, scl);
leihen 0:3940f0ad2ca5 25 if (m_rtc == NULL)
leihen 0:3940f0ad2ca5 26 error("Rtc_Ds1307");
leihen 1:64274190e842 27
leihen 1:64274190e842 28 // Set the frequency to standard 100kHz
leihen 1:64274190e842 29 m_rtc->frequency(100000);
leihen 0:3940f0ad2ca5 30 }
leihen 0:3940f0ad2ca5 31
leihen 0:3940f0ad2ca5 32 Rtc_Ds1307::~Rtc_Ds1307()
leihen 0:3940f0ad2ca5 33 {
leihen 0:3940f0ad2ca5 34 if (m_rtc != NULL)
leihen 0:3940f0ad2ca5 35 delete m_rtc;
leihen 0:3940f0ad2ca5 36 }
leihen 0:3940f0ad2ca5 37
leihen 1:64274190e842 38 bool Rtc_Ds1307::setTime(Time& time, bool start, bool thm)
leihen 0:3940f0ad2ca5 39 {
leihen 1:64274190e842 40 INFO("Setting new time : %d:%d:%d\n", time.hour, time.min, time.sec);
leihen 0:3940f0ad2ca5 41
leihen 0:3940f0ad2ca5 42 return true;
leihen 0:3940f0ad2ca5 43 }
leihen 0:3940f0ad2ca5 44
leihen 1:64274190e842 45 bool Rtc_Ds1307::getTime(Time& time)
leihen 0:3940f0ad2ca5 46 {
leihen 1:64274190e842 47 char buffer[7];
leihen 1:64274190e842 48 bool bClock_halt = false;
leihen 1:64274190e842 49 bool thm = false;
leihen 1:64274190e842 50
leihen 1:64274190e842 51 INFO("Getting time from RTC\n");
leihen 1:64274190e842 52 if (read(0, buffer, 7) != 0) {
leihen 1:64274190e842 53 // Failed to read
leihen 1:64274190e842 54 ERR("Failed to read from RTC\n");
leihen 1:64274190e842 55 return false;
leihen 1:64274190e842 56 }
leihen 1:64274190e842 57 bClock_halt = ((buffer[0] & 128) == 128);
leihen 1:64274190e842 58 thm = ((buffer[2] & 64) == 64);
leihen 1:64274190e842 59 time.sec = bcdToDecimal(buffer[0]&0x7F);
leihen 1:64274190e842 60 time.min = bcdToDecimal(buffer[1]);
leihen 1:64274190e842 61 if (thm) {
leihen 1:64274190e842 62 // in 12-hour-mode, we need to add 12 hours if PM bit is set
leihen 1:64274190e842 63 time.hour = Rtc_Ds1307::bcdToDecimal( buffer[2] & 31 );
leihen 1:64274190e842 64 if ((buffer[2] & 32) == 32)
leihen 1:64274190e842 65 time.hour += 12;
leihen 1:64274190e842 66 }
leihen 1:64274190e842 67 else {
leihen 1:64274190e842 68 time.hour = Rtc_Ds1307::bcdToDecimal( buffer[2] & 63 );
leihen 1:64274190e842 69 }
leihen 1:64274190e842 70 time.wday = buffer[3];
leihen 1:64274190e842 71 time.date = Rtc_Ds1307::bcdToDecimal( buffer[4]);
leihen 1:64274190e842 72 time.mon = Rtc_Ds1307::bcdToDecimal( buffer[5]);
leihen 1:64274190e842 73 time.year = Rtc_Ds1307::bcdToDecimal(buffer[6]) + 100; // plus hundret is because RTC is giving the years since 2000, but std c struct tm needs years since 1900
leihen 1:64274190e842 74
leihen 1:64274190e842 75 INFO("Clock is %s\n", bClock_halt ? "halted" : "running");
leihen 0:3940f0ad2ca5 76 return false;
leihen 0:3940f0ad2ca5 77 }
leihen 1:64274190e842 78
leihen 1:64274190e842 79
leihen 1:64274190e842 80 bool Rtc_Ds1307::read(int address, char *buffer, int len)
leihen 1:64274190e842 81 {
leihen 1:64274190e842 82 char buffer2[2] = {(char)address, 0};
leihen 1:64274190e842 83
leihen 1:64274190e842 84 m_rtc->start();
leihen 1:64274190e842 85 if (m_rtc->write(0xd0, buffer2, 1) != 0) {
leihen 1:64274190e842 86 ERR("Failed to write register address on rtv!\n");
leihen 1:64274190e842 87 m_rtc->stop();
leihen 1:64274190e842 88 return false;
leihen 1:64274190e842 89 }
leihen 1:64274190e842 90 if (m_rtc->read(0xd0, buffer, len) != 0) {
leihen 1:64274190e842 91 ERR("Failed to read register !\n");
leihen 1:64274190e842 92 return false;
leihen 1:64274190e842 93 }
leihen 1:64274190e842 94 m_rtc->stop();
leihen 1:64274190e842 95
leihen 1:64274190e842 96 INFO("Successfully read %d registers from RTC\n", len);
leihen 1:64274190e842 97 return true;
leihen 1:64274190e842 98 }
leihen 1:64274190e842 99
leihen 1:64274190e842 100 bool Rtc_Ds1307::write(int address, char *buffer, int len)
leihen 1:64274190e842 101 {
leihen 1:64274190e842 102 char buffer2[10];
leihen 1:64274190e842 103 buffer2[0] = address&0xFF;
leihen 1:64274190e842 104 for (int i = 0 ; i < len ; i++)
leihen 1:64274190e842 105 buffer2[i+1] = buffer[i];
leihen 1:64274190e842 106
leihen 1:64274190e842 107 m_rtc->start();
leihen 1:64274190e842 108 if (m_rtc->write(0xd0, buffer2, len+1) != 0) {
leihen 1:64274190e842 109 ERR("Failed to write data to rtc\n");
leihen 1:64274190e842 110 m_rtc->stop();
leihen 1:64274190e842 111 return false;
leihen 1:64274190e842 112 }
leihen 1:64274190e842 113 m_rtc->stop();
leihen 1:64274190e842 114 return true;
leihen 1:64274190e842 115 }