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.h
- Committer:
- loopsva
- Date:
- 2015-03-11
- Revision:
- 3:e9c5025ba2ca
- Parent:
- 2:a9a8027a7cb2
- Child:
- 4:b5acccb6c3d1
File content as of revision 3:e9c5025ba2ca:
#ifndef DS3232M_H #define DS3232M_H /* Maxim Integrated DS3232M, fully integrated Real Time Clock chip. The chip contains an I2C interface, compatable time-base registers to the DS1307 and M11T41, a super-accurate internal oscillator, 2 alarms and 236 bytes of user defined battery-backed RAM. */ /** Class ds3232m implements the real time clock * * */ class ds3232m { public: /** Structure which is used to exchange the time and date */ typedef struct { int sec; /*!< seconds [0..59] */ int min; /*!< minutes {0..59] */ int hour; /*!< hours [0..23] */ int wday; /*!< weekday [1..7, where 1 = sunday, 2 = monday, ... */ int date; /*!< day of month [0..31] */ int mon; /*!< month of year [1..12] */ int year; /*!< year [2000..2255] */ } Time_rtc; protected: I2C* _i2c_; // static const char *m_weekDays[]; public: /** * Constructor with default i2c clock speed * - Fixed at I2C address 0x80 * - I2C speed set to 400KHz * * @param sda - mbed I2C interface pin * @param scl - mbed I2C interface pin */ ds3232m(PinName sda, PinName scl); /** * Constructor with i2c clock setting option * - Fixed at I2C address 0x80 * - I2C speed set by user * * @param sda - mbed I2C interface pin * @param scl - mbed I2C interface pin * @param set_I2C_frequency */ ds3232m(PinName sda, PinName scl, int i2cFrequency); /** * Destructor * * @param --none-- */ ~ds3232m(); /** * See if temperature conversion is busy or not * * @param --none-- * * @return busy_flag - Temperature Conversion is busy * - true = busy * - false = ready */ bool checkTempBusy(); /** * Start a temperature conversion cycle * - Note: only 1 conversion per second allowed * * @param --none-- * * @return busy_flag Temperature Conversion is Busy * - true = started new conversion * - false = already busy from previous converstion start */ bool startTempCycle(); /** * Get the chip temperature from the DS3232M * * @param --none-- * * @return Temperature 0.25degC accuracy * @return 255.0 if temperature conversion not complete (still busy) */ float getTemperature(); /** * Get seconds register from DS3232M * * @param --none-- * * @return BCD-to-decimal corrected seconds register */ int getSeconds(); /** * Get day-of-the-week register from DS3232M * * @param --none-- * * @return BCD-to-decimal corrected day of week register (1-7) */ int getDayOfWeek(); /** * Set the day-of-the-week register in the DS3232M * * @param dow3232 day_of_the_week (1=Mon....7=Sun) * * @return --none-- */ void putDayOfWeek(char dow3232); /** * Clear out all 236 bytes of user RAM, from address 0x14 to 0xff * - 00h is put in all user RAM area * * @param --none-- * * @return --none-- */ void clearRAM(); /** * Retrieve data from DS3232M's user RAM area * - addresses range 0x14 - 0xfd * - CRC is checked of entire RAM contents * * @param buffer pointer * @param offset into DS3232M's RAM (range 0x14 = 0xfd) * @param length number of bytes to get * * @return 00 = no error * @return 01 = length + offset is over the highest user RAM location * @return 02 = offset < 0x14 * @return 04 = length = 0 * @return 08 = crc error occured from LoadRTCRam() */ int getUserRAM(char *buffer, int offset, int length); /** * Store data DS3232M's user RAM area * - addresses range 0x14 - 0xfd * - CRC is added to the last 2 locations using addCRC16() * * @param buffer pointer * @param offset into DS3232M's RAM (range 0x14 = 0xfd) * @param length number of bytes to store * * @return 00 = no error * @return 01 = length + offset is over the highest user RAM location * @return 02 = offset < 0x14 * @return 04 = length = 0 */ int putUserRAM(char *buffer, int offset, int length); /** * Calculate CRC16 * * @param input buffer pointer * @param address offset into buffer * @param length number of bytes to calculate * * @return CRC16 value */ uint16_t calculateCRC16(char input[], int offset, int length); /** * Turn on/off the 32KHz output pin * - with option to keep 32KHz output ON during battery backup * - This pin is push-pull, no pullup is required * * @param ena * - true = enable during normal operation * - false = disable 32KHz output pin during normal operation * @param batt * - true = enable 32KHz output pin during battery backup mode * - false = disable 32KHz output pin during battery backup mode * - Note: "ena" must be true or else batt is ignored * * @return --none-- */ void set32KhzOutput(bool ena, bool batt); /** * Turn on/off the 1Hz output pin * - with option to keep 1Hz output ON during battery backup * - This pin is open-drain * - Requires a pullup resistor to Vcc or the backup battery * * @param ena * - true = enable 1Hz output pin during normal operation * - false = disable 1Hz output pin during normal operation * @param batt * - true = enable 1Hz output pin during battery backup mode * - false = disable 1Hz output pin during battery backup mode * - Note: "ena" must be true or else batt is ignored * * @return --none-- */ void set1hzOutput(bool ena, bool batt); /** * Turn on/off the main oscillator during battery backup mode * - The oscillator always runs when Vcc is valid * * @param batt * - true = enable the oscillator during battery backup mode * - false = disable the oscillator during battery backup mode * * @return --none-- */ void enableBattClock(bool batt); /** * Read all of the current time registers from the DS3232M * * @param time_structure - will be filled with the time from DS3232M * * @returns --none-- */ void getTime(Time_rtc& time); /** * Write all of the current time registers into the DS3232M * * @param time_structure - will be filled with the time into the DS3232M * * @returns --none-- */ void setTime(Time_rtc& time); /** * Load the entire contents of the DS3232M into dedicated buffer RTCbuffer[] * - includes time registers, alarm registers and user RAM * * @param --none-- * * @return CRC_Status - pass/fail of CRC readback of user RAM data * - true = CRC is ok * - false = CRC failed */ bool LoadRTCRam(); /** * Dedicated buffer that mirrors the RTC's entire contents * - loaded by LoadRTCRam() * * @param --none-- * * @return --none-- */ char RTCbuffer[256]; private: void getControlStatusRegs(); char RtcCtlReg; char RtcStatReg; void addCRC16(); // BCD-Decimal Conversions, hacked from Henry Leinen's RTC-DS1307 library static int BCDToDec(int dat) { return ((dat & 0xF0) >> 4) * 10 + (dat & 0x0F); } static int DecToBCD(int dat) { return (dat % 10) + ((dat / 10) << 4); } }; #endif // DS3232M_H