This is a class for using the BQ32000 Real Time Clock chip from TI.

BQ32000

This is a class for using the BQ32000 Real Time Clock chip from TI.This is supossed to be pin and code compatible with the most usual DS1307. It is pin compatible as you can see:

/media/uploads/xeta05/bq32000.jpg

On the code side it is almost compatible, with some advanced functions added (trickle charge, calibration) and minor differences in the registers, For example is only capable of generating 1 Hz and 512 Hz square wave.

This library is based on the DS1307 library by Henry Leinen http://developer.mbed.org/users/leihen/code/RTC-DS1307/file/ee81f2c5a706

The trickle charge and calibration are included in the code but not tested against the chip.

Committer:
xeta05
Date:
Thu Nov 27 17:05:53 2014 +0000
Revision:
1:17f606b5b59b
Parent:
0:c12a004a9113
Initial Revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xeta05 0:c12a004a9113 1 /* Rtc_bq32k.h */
xeta05 1:17f606b5b59b 2 /*2014/11/27 Oskar Lopez de Gamboa
xeta05 0:c12a004a9113 3 */
xeta05 1:17f606b5b59b 4 /* Copyright (c) <2014> <Oskar Lopez de Gamboa>, MIT License
xeta05 1:17f606b5b59b 5 *
xeta05 1:17f606b5b59b 6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
xeta05 1:17f606b5b59b 7 * and associated documentation files (the "Software"), to deal in the Software without restriction,
xeta05 1:17f606b5b59b 8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
xeta05 1:17f606b5b59b 9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
xeta05 1:17f606b5b59b 10 * furnished to do so, subject to the following conditions:
xeta05 1:17f606b5b59b 11 *
xeta05 1:17f606b5b59b 12 * The above copyright notice and this permission notice shall be included in all copies or
xeta05 1:17f606b5b59b 13 * substantial portions of the Software.
xeta05 1:17f606b5b59b 14 *
xeta05 1:17f606b5b59b 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
xeta05 1:17f606b5b59b 16 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
xeta05 1:17f606b5b59b 17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
xeta05 1:17f606b5b59b 18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
xeta05 1:17f606b5b59b 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
xeta05 1:17f606b5b59b 20 */
xeta05 1:17f606b5b59b 21
xeta05 0:c12a004a9113 22 #ifndef __Rtc_bq32k_H__
xeta05 0:c12a004a9113 23 #define __Rtc_bq32k_H__
xeta05 0:c12a004a9113 24
xeta05 0:c12a004a9113 25 #define BQ32000_ADDRESS 0xD0
xeta05 0:c12a004a9113 26 // BQ32000 register addresses:
xeta05 0:c12a004a9113 27 #define BQ32000_CAL_CFG1 0x07
xeta05 0:c12a004a9113 28 #define BQ32000_TCH2 0x08
xeta05 0:c12a004a9113 29 #define BQ32000_CFG2 0x09
xeta05 0:c12a004a9113 30 #define BQ32000_SFKEY1 0x20
xeta05 0:c12a004a9113 31 #define BQ32000_SFKEY2 0x21
xeta05 0:c12a004a9113 32 #define BQ32000_SFR 0x22
xeta05 0:c12a004a9113 33 // BQ32000 config bits:
xeta05 0:c12a004a9113 34 #define BQ32000__OUT 0x07 // CAL_CFG1 - IRQ active state
xeta05 0:c12a004a9113 35 #define BQ32000__FT 0x06 // CAL_CFG1 - IRQ square wave enable
xeta05 0:c12a004a9113 36 #define BQ32000__CAL_S 0x05 // CAL_CFG1 - Calibration sign
xeta05 0:c12a004a9113 37 #define BQ32000__TCH2_BIT 0x05 // TCH2 - Trickle charger switch 2
xeta05 1:17f606b5b59b 38 #define BQ32000__TCFE_BIT 0x06 // CFG2 - Trickle FET control
xeta05 0:c12a004a9113 39 // BQ32000 config values:
xeta05 0:c12a004a9113 40 #define BQ32000_CHARGE_ENABLE 0x05 // CFG2 - Trickle charger switch 1 enable
xeta05 0:c12a004a9113 41 #define BQ32000_SFKEY1_VAL 0x5E
xeta05 0:c12a004a9113 42 #define BQ32000_SFKEY2_VAL 0xC7
xeta05 0:c12a004a9113 43
xeta05 0:c12a004a9113 44
xeta05 0:c12a004a9113 45 /** Class Rtc_bq32k implements the real time clock module bq32000 from TI
xeta05 0:c12a004a9113 46 *
xeta05 0:c12a004a9113 47 * You can read the clock and set a new time and date.
xeta05 0:c12a004a9113 48 * It is also possible to start and stop the clock, config the IRQ, set calibration, enable trickle Charger.
xeta05 0:c12a004a9113 49 *
xeta05 0:c12a004a9113 50 */
xeta05 0:c12a004a9113 51 class Rtc_bq32k
xeta05 0:c12a004a9113 52 {
xeta05 0:c12a004a9113 53 public:
xeta05 0:c12a004a9113 54 /** Structure which is used to exchange the time and date
xeta05 0:c12a004a9113 55 */
xeta05 0:c12a004a9113 56 typedef struct {
xeta05 0:c12a004a9113 57 int sec; /*!< seconds [0..59] */
xeta05 0:c12a004a9113 58 int min; /*!< minutes {0..59] */
xeta05 0:c12a004a9113 59 int hour; /*!< hours [0..23] */
xeta05 0:c12a004a9113 60 int wday; /*!< weekday [1..7, where 1 = sunday, 2 = monday, ... */
xeta05 0:c12a004a9113 61 int date; /*!< day of month [0..31] */
xeta05 0:c12a004a9113 62 int mon; /*!< month of year [1..12] */
xeta05 0:c12a004a9113 63 int year; /*!< year [2000..2255] */
xeta05 0:c12a004a9113 64 } Time_rtc;
xeta05 0:c12a004a9113 65
xeta05 0:c12a004a9113 66
xeta05 0:c12a004a9113 67 /** RateSelect specifies the valid frequency values for the square wave output
xeta05 0:c12a004a9113 68 */
xeta05 0:c12a004a9113 69 typedef enum {
xeta05 0:c12a004a9113 70 RS512Hz = 0,
xeta05 0:c12a004a9113 71 RS1Hz = 1
xeta05 0:c12a004a9113 72 } SqwRateSelect_t;
xeta05 0:c12a004a9113 73
xeta05 0:c12a004a9113 74 protected:
xeta05 0:c12a004a9113 75 I2C* m_rtc;
xeta05 0:c12a004a9113 76
xeta05 0:c12a004a9113 77 static const char *m_weekDays[];
xeta05 0:c12a004a9113 78
xeta05 0:c12a004a9113 79 public:
xeta05 0:c12a004a9113 80 /** public constructor which creates the real time clock object
xeta05 0:c12a004a9113 81 *
xeta05 0:c12a004a9113 82 * @param sda : specifies the pin for the SDA communication line.
xeta05 0:c12a004a9113 83 *
xeta05 0:c12a004a9113 84 * @param scl : the pin for the serial clock
xeta05 0:c12a004a9113 85 *
xeta05 0:c12a004a9113 86 */
xeta05 0:c12a004a9113 87 Rtc_bq32k(PinName sda, PinName scl);
xeta05 0:c12a004a9113 88
xeta05 0:c12a004a9113 89 ~Rtc_bq32k();
xeta05 0:c12a004a9113 90
xeta05 0:c12a004a9113 91 /** Read the current time from RTC chip
xeta05 0:c12a004a9113 92 *
xeta05 0:c12a004a9113 93 * @param time : reference to a struct tm which will be filled with the time from rtc
xeta05 0:c12a004a9113 94 *
xeta05 0:c12a004a9113 95 * @returns true if successful, otherwise an acknowledge error occured
xeta05 0:c12a004a9113 96 */
xeta05 0:c12a004a9113 97 virtual bool getTime(Time_rtc& time);
xeta05 0:c12a004a9113 98
xeta05 0:c12a004a9113 99 /** Write the given time onto the RTC chip
xeta05 0:c12a004a9113 100 *
xeta05 0:c12a004a9113 101 * @param time : reference to a struct which contains valid date and time information
xeta05 0:c12a004a9113 102 *
xeta05 0:c12a004a9113 103 * @param start : contains true if the clock shall start (or keep on running).
xeta05 0:c12a004a9113 104 *
xeta05 0:c12a004a9113 105 * @returns true if successful, otherwise an acknowledge error occured
xeta05 0:c12a004a9113 106 */
xeta05 0:c12a004a9113 107 virtual bool setTime(Time_rtc& time, bool start);
xeta05 0:c12a004a9113 108
xeta05 0:c12a004a9113 109 /** Look for the status of the Oscillator fail flag OF.
xeta05 0:c12a004a9113 110 *
xeta05 0:c12a004a9113 111 * @returns true if No failure was detected , false if a failure was detected (at least
xeta05 0:c12a004a9113 112 * four consecutive dropped pulses)or a communication error occured.
xeta05 0:c12a004a9113 113 */
xeta05 0:c12a004a9113 114 bool oscStatus();
xeta05 0:c12a004a9113 115
xeta05 0:c12a004a9113 116 /** Start the clock. Please note that the seconds register need to be read and
xeta05 0:c12a004a9113 117 * written in order to start or stop the clock. This can lead to an error
xeta05 0:c12a004a9113 118 * in the time value. The recommended way of starting and stoping the clock is
xeta05 0:c12a004a9113 119 * to write the actual date and time and set the start bit accordingly.
xeta05 0:c12a004a9113 120 *
xeta05 0:c12a004a9113 121 * @returns true if the clock was started, false if a communication error occured
xeta05 0:c12a004a9113 122 */
xeta05 0:c12a004a9113 123 bool startClock();
xeta05 0:c12a004a9113 124
xeta05 0:c12a004a9113 125 /** Stop the clock. Please note that the seconds register need to be read and
xeta05 0:c12a004a9113 126 * written in order to start or stop the clock. This can lead to an error
xeta05 0:c12a004a9113 127 * in the time value. The recommended way of starting and stoping the clock is
xeta05 0:c12a004a9113 128 * to write the actual date and time and set the start bit accordingly.
xeta05 0:c12a004a9113 129 *
xeta05 0:c12a004a9113 130 * @returns true if the clock was stopped, false if a communication error occured
xeta05 0:c12a004a9113 131 */
xeta05 0:c12a004a9113 132 bool stopClock();
xeta05 0:c12a004a9113 133
xeta05 0:c12a004a9113 134 /** Service function to convert a weekday into a string representation
xeta05 0:c12a004a9113 135 *
xeta05 0:c12a004a9113 136 * @param wday : day of week to convert (starting with sunday = 1, monday = 2, ..., saturday = 7
xeta05 0:c12a004a9113 137 *
xeta05 0:c12a004a9113 138 * @returns the corresponding string representation
xeta05 0:c12a004a9113 139 */
xeta05 0:c12a004a9113 140 const char* weekdayToString( int wday ) {
xeta05 0:c12a004a9113 141 return m_weekDays[wday%7];
xeta05 0:c12a004a9113 142 }
xeta05 0:c12a004a9113 143
xeta05 0:c12a004a9113 144 /** Enable Square Wave output. The function enables or disables the square wave output
xeta05 0:c12a004a9113 145 * of the module and sets the desired frequency.
xeta05 0:c12a004a9113 146 *
xeta05 0:c12a004a9113 147 * @param ena : if set to true, the square wave output is enabled.
xeta05 0:c12a004a9113 148 *
xeta05 0:c12a004a9113 149 * @param rs : rate select, can be either one of the two values defined by type /c RateSelect_t
xeta05 0:c12a004a9113 150 *
xeta05 0:c12a004a9113 151 * @return true if the operation was successful or false otherwise
xeta05 0:c12a004a9113 152 */
xeta05 0:c12a004a9113 153 bool setSquareWaveOutput(bool ena, SqwRateSelect_t rs);
xeta05 0:c12a004a9113 154
xeta05 0:c12a004a9113 155 /** Set IRQ output level to LOW or HIGH. when IRQ square wave output is disabled
xeta05 0:c12a004a9113 156 *
xeta05 0:c12a004a9113 157 * @param level : if set to true, the IRQ will be HIGH else it will be LOW.
xeta05 0:c12a004a9113 158 *
xeta05 0:c12a004a9113 159 * @return true if the operation was successful or false otherwise
xeta05 0:c12a004a9113 160 */
xeta05 0:c12a004a9113 161 bool setIRQLevel(bool level);
xeta05 0:c12a004a9113 162 /* Sets the calibration value to given value in the range -31 - 31, which
xeta05 0:c12a004a9113 163 * corresponds to -126ppm - +63ppm; see table 13 in th BQ32000 datasheet.
xeta05 0:c12a004a9113 164 *
xeta05 0:c12a004a9113 165 *@param value:the signed value of the calibration desired
xeta05 0:c12a004a9113 166 *
xeta05 0:c12a004a9113 167 *@return true if the operation was successful or false otherwise
xeta05 0:c12a004a9113 168 */
xeta05 0:c12a004a9113 169 bool setCalibration(int8_t value);
xeta05 0:c12a004a9113 170
xeta05 0:c12a004a9113 171 /** If using a super capacitor instead of a battery for backup power, use this
xeta05 0:c12a004a9113 172 * method to set the state of the trickle charger: In low-voltage charge mode, the super cap is
xeta05 0:c12a004a9113 173 * charged through a diode with a voltage drop of about 0.5V, so it will charge
xeta05 0:c12a004a9113 174 * up to VCC-0.5V. In high-voltage charge mode the diode is bypassed and the super
xeta05 0:c12a004a9113 175 * cap will be charged up to VCC (make sure the charge voltage does not exceed your
xeta05 0:c12a004a9113 176 * super cap's voltage rating!!).
xeta05 0:c12a004a9113 177 *
xeta05 0:c12a004a9113 178 *
xeta05 0:c12a004a9113 179 * *@param state:0=disabled, 1=low-voltage charge, 2=high-voltage charge.
xeta05 0:c12a004a9113 180 */
xeta05 0:c12a004a9113 181 void setCharger(int state);
xeta05 0:c12a004a9113 182
xeta05 0:c12a004a9113 183
xeta05 0:c12a004a9113 184 private:
xeta05 0:c12a004a9113 185 bool read(int address, char* buffer, int len);
xeta05 0:c12a004a9113 186 bool write(int address, char* buffer, int len);
xeta05 0:c12a004a9113 187
xeta05 0:c12a004a9113 188 static int bcdToDecimal(int bcd) {
xeta05 0:c12a004a9113 189 return ((bcd&0xF0)>>4)*10 + (bcd&0x0F);
xeta05 0:c12a004a9113 190 }
xeta05 0:c12a004a9113 191
xeta05 0:c12a004a9113 192 static int decimalToBcd(int dec) {
xeta05 0:c12a004a9113 193 return (dec%10) + ((dec/10)<<4);
xeta05 0:c12a004a9113 194 }
xeta05 0:c12a004a9113 195 };
xeta05 0:c12a004a9113 196
xeta05 0:c12a004a9113 197
xeta05 0:c12a004a9113 198 #endif // __Rtc_bq32k_H__