Library for Maxim DS3232M super-accurate, I2C based Real Time Clock chip with 234 bytes of user RAM. Library includes user RAM read/write operations along with CRC routines for accessing the user RAM area.
Dependents: ds3232m_HelloWorld
Diff: ds3232m.cpp
- Revision:
- 6:968b8efe3ca0
- Parent:
- 2:a9a8027a7cb2
--- a/ds3232m.cpp Thu Dec 25 21:38:22 2014 +0000 +++ b/ds3232m.cpp Fri Mar 11 18:09:54 2016 +0000 @@ -1,30 +1,35 @@ #include "mbed.h" #include "ds3232m.h" -#define RTCI2CADDRESS 0xd0 -#define DS3232SECONDS 0x00 -#define DS3232DAYOFWEEK 0x03 -#define DS3232ALM1BOTTOM 0x07 -#define DS3232ALM2BOTTOM 0x0b -#define DS3232CTLREG 0x0e -#define DS3232STATREG 0x0f -#define DS3232TEMPERATURE 0x11 -#define DS3232TESTREG 0x13 -#define DS3232RAMBOTTOM 0x14 -#define DS3232RAMCRC16 0xfe +#define RTCI2CADDRESS 0xd0 //i2c address of the DS3232M +#define DS3232SECONDS 0x00 //seconds register +#define DS3232DAYOFWEEK 0x03 //day of the week register +#define DS3232ALM1BOTTOM 0x07 //alarm 1 registers (not used) +#define DS3232ALM2BOTTOM 0x0b //alarm 2 registers (not used) +#define DS3232CTLREG 0x0e //control register +#define DS3232STATREG 0x0f //status register +#define DS3232TEMPERATURE 0x11 //temperature registers +#define DS3232TESTREG 0x13 //test register (do not use) +#define DS3232RAMBOTTOM 0x14 //where user RAM starts +#define DS3232RAMCRC16 0xfc //crc16 registers (top 2 bytes of user RAM) +#define DS3232RAMTOP 0xfe //where user RAM ends. Note: bug in F746 only allows for a I2C xfer of 255 max!!!! + //control register -#define DS3232_EOSCN_BIT 0x80 //turn on/off oscillator in batt backup mode, 0 = on -#define DS3232_BBSQW_BIT 0x40 //battery backed 1Hz enable, 1 = enable -#define DS3232_CONV_BIT 0x20 //start temperature conversion, 1 = start -#define DS3232_INTCN_BIT 0x04 //interrupt control, 1 = interrupt mode, 0 = 1Hz mode +#define DS3232_EOSCN_BIT 0x80 //turn on/off oscillator in batt backup mode, 0 = on +#define DS3232_BBSQW_BIT 0x40 //battery backed 1Hz enable, 1 = enable +#define DS3232_CONV_BIT 0x20 //start temperature conversion, 1 = start +#define DS3232_INTCN_BIT 0x04 //interrupt control, 1 = interrupt mode, 0 = 1Hz mode + //status register -#define DS3232_OSF_BIT 0x80 //oscillator stopped, 1 = stopped -#define DS3232_BB32KHZ_BIT 0x40 //battery backed 32KHz enable, 1 = enable(writable bit) -#define DS3232_EN32KHZ_BIT 0x08 //32KHz enable, 1 = enable(writable bit) -#define DS3232_BUSY_BIT 0x04 //busy doing a temperature A:D conversion, 1 = busy +#define DS3232_OSF_BIT 0x80 //oscillator stopped, 1 = stopped +#define DS3232_BB32KHZ_BIT 0x40 //battery backed 32KHz enable, 1 = enable(writable bit) +#define DS3232_EN32KHZ_BIT 0x08 //32KHz enable, 1 = enable(writable bit) +#define DS3232_BUSY_BIT 0x04 //busy doing a temperature A:D conversion, 1 = busy -char RtcCtlReg = 0; -char RtcStatReg = 0; +char RtcCtlReg = 0; //mirror of control register +char RtcStatReg = 0; //mirror of status register + +//char RTCbuffer[256]; //buffer may contain up to 256 bytes, depending on command used //--------------------------------------------------------------------------------------------------------------------------------------// // constructor with fixed frequency @@ -53,84 +58,69 @@ //--------------------------------------------------------------------------------------------------------------------------------------// // set time up into the DS3232M -void ds3232m::setTime(Time_rtc& time) { - char buffer[7]; - buffer[0] = DecToBCD(time.sec); - buffer[1] = DecToBCD(time.min); - buffer[2] = DecToBCD(time.hour); - buffer[3] = time.wday; - buffer[4] = DecToBCD(time.date); - buffer[5] = DecToBCD(time.mon); - buffer[6] = DecToBCD(time.year - 2000); +void ds3232m::setTime(Time_rtc& dsSTR) { + dsSTR.RTCbuffer[0] = DS3232SECONDS; + dsSTR.RTCbuffer[1] = DecToBCD(dsSTR.sec); + dsSTR.RTCbuffer[2] = DecToBCD(dsSTR.min); + dsSTR.RTCbuffer[3] = DecToBCD(dsSTR.hour); + dsSTR.RTCbuffer[4] = dsSTR.wday; + dsSTR.RTCbuffer[5] = DecToBCD(dsSTR.date); + dsSTR.RTCbuffer[6] = DecToBCD(dsSTR.mon); + dsSTR.RTCbuffer[7] = DecToBCD(dsSTR.year - 2000); - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(DS3232SECONDS); - for(int i = 0; i < 7; i++) _i2c_->write(buffer[i]); - _i2c_->stop(); + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 8, false); } //--------------------------------------------------------------------------------------------------------------------------------------// // get time from the DS3232M and stick it into the mbed's RTC -void ds3232m::getTime(Time_rtc& time) { - char buffer[7]; - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(DS3232SECONDS); - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS + 1); - for(int i = 0; i < 7; i++) buffer[i] = _i2c_->read(1); - int rtcgarbage = (_i2c_->read(0)); //final junk read - _i2c_->stop(); +void ds3232m::getTime(Time_rtc& dsSTR) { + dsSTR.RTCbuffer[0] = DS3232SECONDS; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 1, true); + _i2c_->read(RTCI2CADDRESS, dsSTR.RTCbuffer, 7, false); - time.sec = BCDToDec(buffer[0]); - time.min = BCDToDec(buffer[1]); - time.hour = ds3232m::BCDToDec(buffer[2]); - time.wday = buffer[3]; - time.date = ds3232m::BCDToDec(buffer[4]); - time.mon = ds3232m::BCDToDec(buffer[5]); - time.year = ds3232m::BCDToDec(buffer[6]) + 2000; // plus hundred is because RTC is giving the years since 2000, but std c struct tm needs years since 1900 + dsSTR.sec = BCDToDec(dsSTR.RTCbuffer[0]); + dsSTR.min = BCDToDec(dsSTR.RTCbuffer[1]); + dsSTR.hour = ds3232m::BCDToDec(dsSTR.RTCbuffer[2]); + dsSTR.wday = dsSTR.RTCbuffer[3]; + dsSTR.date = ds3232m::BCDToDec(dsSTR.RTCbuffer[4]); + dsSTR.mon = ds3232m::BCDToDec(dsSTR.RTCbuffer[5]); + dsSTR.year = ds3232m::BCDToDec(dsSTR.RTCbuffer[6]) + 2000; } //--------------------------------------------------------------------------------------------------------------------------------------// // retrieve the control and status registers -void ds3232m::getControlStatusRegs() { - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(DS3232CTLREG); - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS + 1); - RtcCtlReg = _i2c_->read(1); - RtcStatReg = _i2c_->read(0); - _i2c_->stop(); +void ds3232m::getControlStatusRegs(Time_rtc& dsSTR) { + dsSTR.RTCbuffer[0] = DS3232CTLREG; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 1, true); + _i2c_->read(RTCI2CADDRESS, dsSTR.RTCbuffer, 2, false); + RtcCtlReg = dsSTR.RTCbuffer[0]; + RtcStatReg = dsSTR.RTCbuffer[1]; } //--------------------------------------------------------------------------------------------------------------------------------------// // enables/disables the main oscillator during battery backup mode. true = enable during battery backup mode -void ds3232m::enableBattClock(bool batt) { - getControlStatusRegs(); +void ds3232m::enableBattClock(Time_rtc& dsSTR, bool batt) { + getControlStatusRegs(dsSTR); if(batt == true) { RtcCtlReg &= ~DS3232_EOSCN_BIT; } else { RtcCtlReg |= DS3232_EOSCN_BIT; } - - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(DS3232CTLREG); - _i2c_->write(RtcCtlReg); - _i2c_->stop(); + + dsSTR.RTCbuffer[0] = DS3232CTLREG; + dsSTR.RTCbuffer[1] = RtcCtlReg; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 2, false); } //--------------------------------------------------------------------------------------------------------------------------------------// // check to see if DS3232M's temperature conversion cycle is bust or not // only 1 per second max -bool ds3232m::checkTempBusy() { - getControlStatusRegs(); +bool ds3232m::checkTempBusy(Time_rtc& dsSTR) { + getControlStatusRegs(dsSTR); if(RtcCtlReg & DS3232_CONV_BIT) return true; //is already busy if(RtcStatReg & DS3232_BUSY_BIT) return true; //is already busy return false; @@ -140,16 +130,14 @@ // start a temperature conversion cycle // only 1 per second max -bool ds3232m::startTempCycle() { - getControlStatusRegs(); - if((checkTempBusy()) == true) return false; //is already busy +bool ds3232m::startTempCycle(Time_rtc& dsSTR) { + getControlStatusRegs(dsSTR); + if((checkTempBusy(dsSTR)) == true) return false; //is already busy RtcCtlReg |= DS3232_CONV_BIT; - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(DS3232CTLREG); - _i2c_->write(RtcCtlReg); - _i2c_->stop(); + dsSTR.RTCbuffer[0] = DS3232CTLREG; + dsSTR.RTCbuffer[1] = RtcCtlReg; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 2, false); return true; } @@ -157,20 +145,17 @@ // get temperature // if returned value = 255.0, then temperature conversion was still busy -float ds3232m::getTemperature() { - getControlStatusRegs(); - if((checkTempBusy()) == false) return(255.0); //is already busy - - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(DS3232TEMPERATURE); - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS + 1); - int8_t temp3232a = (_i2c_->read(1)); - uint8_t temp3232b = _i2c_->read(0); - _i2c_->stop(); +float ds3232m::getTemperature(Time_rtc& dsSTR) { + getControlStatusRegs(dsSTR); + if((checkTempBusy(dsSTR)) == false) return(255.0); //is already busy - float temp3232 = float(temp3232a); + dsSTR.RTCbuffer[0] = DS3232TEMPERATURE; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 1, true); + _i2c_->read(RTCI2CADDRESS, dsSTR.RTCbuffer, 2, false); + int8_t temp3232a = dsSTR.RTCbuffer[0]; + uint8_t temp3232b = dsSTR.RTCbuffer[1]; + + float temp3232 = (float)temp3232a; //for positive temp if((temp3232b == 0x40) && (temp3232a >= 0)) temp3232 += 0.25; if (temp3232b == 0x80) temp3232 += 0.5; @@ -184,83 +169,80 @@ //--------------------------------------------------------------------------------------------------------------------------------------// // get seconds register -int ds3232m::getSeconds() { - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(DS3232SECONDS); - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS + 1); - int seconds3232 = (_i2c_->read(0)); - _i2c_->stop(); - +uint8_t ds3232m::getSeconds(Time_rtc& dsSTR) { + dsSTR.RTCbuffer[0] = DS3232SECONDS; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 1, true); + _i2c_->read(RTCI2CADDRESS, dsSTR.RTCbuffer, 1, false); + int seconds3232 = dsSTR.RTCbuffer[0]; int xbcd = BCDToDec(seconds3232); return(xbcd); } //--------------------------------------------------------------------------------------------------------------------------------------// +// get day of the week register (1 = Monday... 7 = Sunday) + +uint8_t ds3232m::getDayOfWeek(Time_rtc& dsSTR) { + dsSTR.RTCbuffer[0] = DS3232DAYOFWEEK; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 1, true); + _i2c_->read(RTCI2CADDRESS, dsSTR.RTCbuffer, 1, false); + int dow3232 = dsSTR.RTCbuffer[0]; + return(BCDToDec(dow3232)); +} + +//--------------------------------------------------------------------------------------------------------------------------------------// +// set day of the week register (1 = Monday... 7 = Sunday) + +void ds3232m::putDayOfWeek(Time_rtc& dsSTR, uint8_t dow3232) { + dow3232 = dow3232 & 7; + dsSTR.RTCbuffer[0] = DS3232DAYOFWEEK; + dsSTR.RTCbuffer[1] = DecToBCD(dow3232); + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 2, false); +} + +//--------------------------------------------------------------------------------------------------------------------------------------// // clears all user memory inside the DS3232M. Top 2 locations - reserved for CRC, also set to 00 since CRC value is 0x00 0x00 -void ds3232m::clearRAM() { - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(DS3232RAMBOTTOM); - for(int i = DS3232RAMBOTTOM; i < 256; i++) _i2c_->write(NULL); - _i2c_->stop(); +void ds3232m::clearRAM(Time_rtc& dsSTR) { + for(int i = DS3232SECONDS; i <= DS3232RAMTOP; i++) dsSTR.RTCbuffer[i] = 0; + dsSTR.RTCbuffer[0] = DS3232RAMBOTTOM; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, (DS3232RAMTOP - DS3232RAMBOTTOM + 2), false); } //--------------------------------------------------------------------------------------------------------------------------------------// // retrieves user data from DS3232M RAM -// returns 00 = ok -// returns 01 = length + offset is over the highest user RAM location -// returns 02 = offset < 0x14 -// returns 04 = length = 0 -// returns 08 = crc error -int ds3232m::getUserRAM(char *buffer, int offset, int length) { - if((offset + length) > 0xfe) return(1); - if(offset < 0x14) return(2); - if(length == 0) return(4); - - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(offset); - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS + 1); - for(int i = 0; i < length; i++) buffer[i] = _i2c_->read(1); - _i2c_->read(0); - _i2c_->stop(); - - if(LoadRTCRam() == false) return(8); - return(0); +uint8_t ds3232m::getUserRAM(char *buffer, Time_rtc& dsSTR, int offset, int length) { + if((offset + length) > DS3232RAMCRC16) return(DS3232_OVERFLOWERROR); + if(offset < DS3232RAMBOTTOM) return(DS3232_OFFSETERROR); + if(length == 0) return(DS3232_LENZEROERROR); + dsSTR.RTCbuffer[0] = offset; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 1, true); + _i2c_->read(RTCI2CADDRESS, buffer, length, false); + if(LoadRTCRam(dsSTR) == false) return(DS3232_CRCERROR); + return(DS3232_NOERROR); } //--------------------------------------------------------------------------------------------------------------------------------------// // puts user data into DS3232M RAM. CRC16 value added to the top 2 RAM location -// returns 00 = ok -// returns 01 = length + offset is over the highest user RAM location -// returns 02 = offset < 0x14 -// returns 04 = length = 0 -int ds3232m::putUserRAM(char *buffer, int offset, int length) { - if((offset + length) > 0xfe) return(1); - if(offset < 0x14) return(2); - if(length == 0) return(4); - - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(offset); - for(int i = 0; i < length; i++) _i2c_->write(buffer[i]); - _i2c_->stop(); - - addCRC16(); - return(0); +uint8_t ds3232m::putUserRAM(char *buffer, Time_rtc& dsSTR, int offset, int length) { + if((offset + length) > DS3232RAMCRC16) return(DS3232_OVERFLOWERROR); + if(offset < DS3232RAMBOTTOM) return(DS3232_OFFSETERROR); + if(length == 0) return(DS3232_LENZEROERROR); + for(int i = 0; i < 32; i++) dsSTR.RTCbuffer[i] = NULL; + dsSTR.RTCbuffer[0] = offset; + dsSTR.RTCbuffer[1] = NULL; + strcat(dsSTR.RTCbuffer, buffer); + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, length + 1, false); + addCRC16(dsSTR); + return(DS3232_NOERROR); } //--------------------------------------------------------------------------------------------------------------------------------------// // enable/disable 32KHz output with run on battery mode option -void ds3232m::set32KhzOutput(bool ena, bool batt) { - getControlStatusRegs(); +void ds3232m::set32KhzOutput(Time_rtc& dsSTR, bool ena, bool batt) { + getControlStatusRegs(dsSTR); if(ena == true) { RtcStatReg |= DS3232_EN32KHZ_BIT; if(batt == true) { @@ -273,18 +255,16 @@ RtcStatReg &= ~DS3232_BB32KHZ_BIT; } - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(DS3232STATREG); - _i2c_->write(RtcStatReg); - _i2c_->stop(); + dsSTR.RTCbuffer[0] = DS3232STATREG; + dsSTR.RTCbuffer[1] = RtcStatReg; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 2, false); } //--------------------------------------------------------------------------------------------------------------------------------------// // enable/disable 1Hz output with run on battery mode option -void ds3232m::set1hzOutput(bool ena, bool batt) { - getControlStatusRegs(); +void ds3232m::set1hzOutput(Time_rtc& dsSTR, bool ena, bool batt) { + getControlStatusRegs(dsSTR); if(ena == true) { RtcCtlReg &= ~DS3232_INTCN_BIT; if(batt == true) { @@ -297,11 +277,9 @@ RtcCtlReg &= ~DS3232_BBSQW_BIT; } - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(DS3232CTLREG); - _i2c_->write(RtcCtlReg); - _i2c_->stop(); + dsSTR.RTCbuffer[0] = DS3232CTLREG; + dsSTR.RTCbuffer[1] = RtcCtlReg; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 2, false); } //--------------------------------------------------------------------------------------------------------------------------------------// @@ -347,45 +325,34 @@ //--------------------------------------------------------------------------------------------------------------------------------------// // calculate a crc value for the DS3232M user ram area. CRC value calculated from address 0x14 - 0xfd -uint16_t ds3232m::calculateCRC16(char input[], int offset, int length) { +uint16_t ds3232m::calculateCRC16(char input[], Time_rtc& dsSTR, int offset, int length) { uint16_t CRC16s = 0; input+= offset; for(int i = offset; i < (length + offset); i++) { uint16_t tableValue = crc16table[((CRC16s >> 8) ^ *(char *)input++) & 0x00ff]; CRC16s = (CRC16s << 8) ^ tableValue; } + dsSTR.c_crc = CRC16s; return CRC16s; } //--------------------------------------------------------------------------------------------------------------------------------------// // This function gets the entire contents of the RTC. Returns flase if a CRC error occured -char RTCbuffer[256]; +bool ds3232m::LoadRTCRam(Time_rtc& dsSTR) { + dsSTR.RTCbuffer[0] = DS3232SECONDS; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 1, true); + _i2c_->read(RTCI2CADDRESS, dsSTR.RTCbuffer, DS3232RAMTOP, false); //BUG in F746 only allows for xfer of 255 bytes max!!! -bool ds3232m::LoadRTCRam() { - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(0x00); - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS + 1); - int i = 0; - for(i = 0; i < 255; i++) { - RTCbuffer[i] = _i2c_->read(1); - } - RTCbuffer[i] = _i2c_->read(0); - _i2c_->stop(); + dsSTR.s_crc = (dsSTR.RTCbuffer[DS3232RAMCRC16] << 8) | dsSTR.RTCbuffer[DS3232RAMCRC16 + 1]; + calculateCRC16(dsSTR.RTCbuffer, dsSTR, DS3232RAMBOTTOM, (DS3232RAMCRC16 - DS3232RAMBOTTOM)); + + ////reload the time registers + //dsSTR.RTCbuffer[0] = DS3232SECONDS; + //_i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 1, true); + //_i2c_->read(RTCI2CADDRESS, dsSTR.RTCbuffer, 7, false); - uint16_t calcdCRC = calculateCRC16(RTCbuffer, DS3232RAMBOTTOM, (DS3232RAMCRC16 - DS3232RAMBOTTOM)); - - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(DS3232RAMCRC16); - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS + 1); - uint16_t storedCRC = (_i2c_->read(1) << 8) + (_i2c_->read(0) & 0xff); - _i2c_->stop(); - - if(storedCRC != calcdCRC) return false; + if(dsSTR.c_crc != dsSTR.s_crc) return false; return true; } @@ -393,15 +360,12 @@ // This function calculates and saves CRC data to the end of RTC's RAM. // CRC calculated address range is 0x14 - 0xfd. CRC data is placed in 0xfe and 0xff -void ds3232m::addCRC16() { - LoadRTCRam(); - uint16_t resultCRC = calculateCRC16(RTCbuffer, DS3232RAMBOTTOM, (DS3232RAMCRC16 - DS3232RAMBOTTOM)); //0x14 is offset from 0 in buffer, 230 is length // - - _i2c_->start(); - _i2c_->write(RTCI2CADDRESS); - _i2c_->write(DS3232RAMCRC16); - _i2c_->write(resultCRC >> 8); - _i2c_->write(resultCRC & 0xff); - _i2c_->stop(); +void ds3232m::addCRC16(Time_rtc& dsSTR) { + LoadRTCRam(dsSTR); + dsSTR.c_crc = calculateCRC16(dsSTR.RTCbuffer, dsSTR, DS3232RAMBOTTOM, (DS3232RAMCRC16 - DS3232RAMBOTTOM)); //0x14 is offset from 0 in buffer, 230 is length // + dsSTR.RTCbuffer[0] = DS3232RAMCRC16; + dsSTR.RTCbuffer[1] = dsSTR.c_crc >> 8; + dsSTR.RTCbuffer[2] = dsSTR.c_crc & 0xff; + _i2c_->write(RTCI2CADDRESS, dsSTR.RTCbuffer, 3, false); }