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

ds3232m.cpp

Committer:
loopsva
Date:
2015-03-11
Revision:
3:e9c5025ba2ca
Parent:
2:a9a8027a7cb2
Child:
4:b5acccb6c3d1

File content as of revision 3:e9c5025ba2ca:

#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
//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
//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

char RtcCtlReg = 0;
char RtcStatReg = 0;

//--------------------------------------------------------------------------------------------------------------------------------------//
// constructor with fixed frequency 

ds3232m::ds3232m(PinName sda, PinName scl) {
    // Create a new I2C object
    _i2c_ = new I2C(sda, scl);
    // Set the frequency to standard 400kHz
    _i2c_->frequency(400000);
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// constructor with I2C frequency selection

ds3232m::ds3232m(PinName sda, PinName scl, int i2cFrequency) {
    _i2c_ = new I2C(sda, scl);
    _i2c_->frequency(i2cFrequency);
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// deconstructor

ds3232m::~ds3232m() {
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// 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);
    
    _i2c_->start();
    _i2c_->write(RTCI2CADDRESS);
    _i2c_->write(DS3232SECONDS);
    for(int i = 0; i < 7; i++) _i2c_->write(buffer[i]);
    _i2c_->stop();
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// 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();
    
    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
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// 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();
}
    
//--------------------------------------------------------------------------------------------------------------------------------------//
// enables/disables the main oscillator during battery backup mode. true = enable during battery backup mode

void ds3232m::enableBattClock(bool batt) {
    getControlStatusRegs();
    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();
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// check to see if DS3232M's temperature conversion cycle is bust or not
// only 1 per second max

bool ds3232m::checkTempBusy() {
    getControlStatusRegs();
    if(RtcCtlReg & DS3232_CONV_BIT) return true;     //is already busy
    if(RtcStatReg & DS3232_BUSY_BIT) return true;    //is already busy
    return false;
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// start a temperature conversion cycle
// only 1 per second max

bool ds3232m::startTempCycle() {
    getControlStatusRegs();
    if((checkTempBusy()) == true) return false;     //is already busy
    RtcCtlReg |= DS3232_CONV_BIT;

    _i2c_->start();
    _i2c_->write(RTCI2CADDRESS);
    _i2c_->write(DS3232CTLREG);
    _i2c_->write(RtcCtlReg);
    _i2c_->stop();
    return true;
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// 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 temp3232 = float(temp3232a);
    //for positive temp
    if((temp3232b == 0x40) && (temp3232a >= 0)) temp3232 += 0.25;
    if (temp3232b == 0x80) temp3232 += 0.5;
    if((temp3232b == 0xc0) && (temp3232a >= 0)) temp3232 += 0.75;
    //for negative temp
    if((temp3232b == 0x40) && (temp3232a < 0)) temp3232 += 0.75;
    if((temp3232b == 0xc0) && (temp3232a < 0)) temp3232 += 0.25;
    return (temp3232);
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// 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();

    int xbcd = BCDToDec(seconds3232);
    return(xbcd);
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// get day of the week register (1 = Monday... 7 = Sunday)

int ds3232m::getDayOfWeek() {
    _i2c_->start();
    _i2c_->write(RTCI2CADDRESS);
    _i2c_->write(DS3232DAYOFWEEK);
    _i2c_->start();
    _i2c_->write(RTCI2CADDRESS + 1);
    int dow3232 = (_i2c_->read(0));
    _i2c_->stop();
    return(BCDToDec(dow3232));
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// set day of the week register (1 = Monday... 7 = Sunday)

void ds3232m::putDayOfWeek(char dow3232) {
    dow3232 = dow3232 & 7;
    _i2c_->start();
    _i2c_->write(RTCI2CADDRESS);
    _i2c_->write(DS3232DAYOFWEEK);
    _i2c_->write(DecToBCD(dow3232));
    _i2c_->stop();
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// 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();
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// 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);
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// 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);
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// enable/disable 32KHz output with run on battery mode option

void ds3232m::set32KhzOutput(bool ena, bool batt) {
    getControlStatusRegs();
    if(ena == true) {
        RtcStatReg |= DS3232_EN32KHZ_BIT;
        if(batt == true) {
            RtcStatReg |= DS3232_BB32KHZ_BIT;
        } else {
            RtcStatReg &= ~DS3232_BB32KHZ_BIT;
        }
    } else {
        RtcStatReg &= ~DS3232_EN32KHZ_BIT;
        RtcStatReg &= ~DS3232_BB32KHZ_BIT;
    }

    _i2c_->start();
    _i2c_->write(RTCI2CADDRESS);
    _i2c_->write(DS3232STATREG);
    _i2c_->write(RtcStatReg);
    _i2c_->stop();
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// enable/disable 1Hz output with run on battery mode option

void ds3232m::set1hzOutput(bool ena, bool batt) {
    getControlStatusRegs();
    if(ena == true) {
        RtcCtlReg &= ~DS3232_INTCN_BIT;
        if(batt == true) {
            RtcCtlReg |= DS3232_BBSQW_BIT;
        } else {
            RtcCtlReg &= ~DS3232_BBSQW_BIT;
        }
    } else {
        RtcCtlReg |= DS3232_INTCN_BIT;
        RtcCtlReg &= ~DS3232_BBSQW_BIT;
    }

    _i2c_->start();
    _i2c_->write(RTCI2CADDRESS);
    _i2c_->write(DS3232CTLREG);
    _i2c_->write(RtcCtlReg);
    _i2c_->stop();
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// CRC table and routine taken from Emilie Laverge's CRC16 library
//--------------------------------------------------------------------------------------------------------------------------------------//
// lookup table for calculating crc16

uint16_t crc16table[256] = { 
        0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011,
        0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022,
        0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072,
        0x0050, 0x8055, 0x805F, 0x005A, 0x804B, 0x004E, 0x0044, 0x8041,
        0x80C3, 0x00C6, 0x00CC, 0x80C9, 0x00D8, 0x80DD, 0x80D7, 0x00D2,
        0x00F0, 0x80F5, 0x80FF, 0x00FA, 0x80EB, 0x00EE, 0x00E4, 0x80E1,
        0x00A0, 0x80A5, 0x80AF, 0x00AA, 0x80BB, 0x00BE, 0x00B4, 0x80B1,
        0x8093, 0x0096, 0x009C, 0x8099, 0x0088, 0x808D, 0x8087, 0x0082,
        0x8183, 0x0186, 0x018C, 0x8189, 0x0198, 0x819D, 0x8197, 0x0192,
        0x01B0, 0x81B5, 0x81BF, 0x01BA, 0x81AB, 0x01AE, 0x01A4, 0x81A1,
        0x01E0, 0x81E5, 0x81EF, 0x01EA, 0x81FB, 0x01FE, 0x01F4, 0x81F1,
        0x81D3, 0x01D6, 0x01DC, 0x81D9, 0x01C8, 0x81CD, 0x81C7, 0x01C2,
        0x0140, 0x8145, 0x814F, 0x014A, 0x815B, 0x015E, 0x0154, 0x8151,
        0x8173, 0x0176, 0x017C, 0x8179, 0x0168, 0x816D, 0x8167, 0x0162,
        0x8123, 0x0126, 0x012C, 0x8129, 0x0138, 0x813D, 0x8137, 0x0132,
        0x0110, 0x8115, 0x811F, 0x011A, 0x810B, 0x010E, 0x0104, 0x8101, 
        0x8303, 0x0306, 0x030C, 0x8309, 0x0318, 0x831D, 0x8317, 0x0312,
        0x0330, 0x8335, 0x833F, 0x033A, 0x832B, 0x032E, 0x0324, 0x8321,
        0x0360, 0x8365, 0x836F, 0x036A, 0x837B, 0x037E, 0x0374, 0x8371,
        0x8353, 0x0356, 0x035C, 0x8359, 0x0348, 0x834D, 0x8347, 0x0342,
        0x03C0, 0x83C5, 0x83CF, 0x03CA, 0x83DB, 0x03DE, 0x03D4, 0x83D1,
        0x83F3, 0x03F6, 0x03FC, 0x83F9, 0x03E8, 0x83ED, 0x83E7, 0x03E2,
        0x83A3, 0x03A6, 0x03AC, 0x83A9, 0x03B8, 0x83BD, 0x83B7, 0x03B2,
        0x0390, 0x8395, 0x839F, 0x039A, 0x838B, 0x038E, 0x0384, 0x8381,
        0x0280, 0x8285, 0x828F, 0x028A, 0x829B, 0x029E, 0x0294, 0x8291,
        0x82B3, 0x02B6, 0x02BC, 0x82B9, 0x02A8, 0x82AD, 0x82A7, 0x02A2,
        0x82E3, 0x02E6, 0x02EC, 0x82E9, 0x02F8, 0x82FD, 0x82F7, 0x02F2,
        0x02D0, 0x82D5, 0x82DF, 0x02DA, 0x82CB, 0x02CE, 0x02C4, 0x82C1,
        0x8243, 0x0246, 0x024C, 0x8249, 0x0258, 0x825D, 0x8257, 0x0252,
        0x0270, 0x8275, 0x827F, 0x027A, 0x826B, 0x026E, 0x0264, 0x8261,
        0x0220, 0x8225, 0x822F, 0x022A, 0x823B, 0x023E, 0x0234, 0x8231,
        0x8213, 0x0216, 0x021C, 0x8219, 0x0208, 0x820D, 0x8207, 0x0202
};

//--------------------------------------------------------------------------------------------------------------------------------------//
// 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 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;
    }
    return CRC16s;
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// This function gets the entire contents of the RTC. Returns flase if a CRC error occured

char RTCbuffer[256];

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();

    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;
    return true;
}

//--------------------------------------------------------------------------------------------------------------------------------------//
// 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();
}