Library for Real Time Clock module MCP97410 based on Library for DS1307
Fork of RTC-DS1307 by
Rtc_Mcp97410.h@13:10e564536e23, 2015-03-08 (annotated)
- Committer:
- charly
- Date:
- Sun Mar 08 21:12:29 2015 +0000
- Revision:
- 13:10e564536e23
- Parent:
- 12:88f82e47b6a1
added Methods for reading and manipulating (inc/dec) the Calibration-Register of RTC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charly | 10:780027029afe | 1 | /* Rtc_Mcp97410.h */ |
leihen | 0:3940f0ad2ca5 | 2 | /* |
charly | 10:780027029afe | 3 | Support for Michrochip RTC MCP97410 |
charly | 10:780027029afe | 4 | Should work for MCP97410, MCP97411, MCP97412 - see datasheet |
charly | 10:780027029afe | 5 | Based on Library for DS1307 |
charly | 10:780027029afe | 6 | Chips are similar but not equal! |
charly | 10:780027029afe | 7 | Ported for MCP97410 by Karl Zweimueller |
charly | 10:780027029afe | 8 | |
charly | 10:780027029afe | 9 | Original Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de) |
leihen | 7:dca20be3ef38 | 10 | |
leihen | 0:3940f0ad2ca5 | 11 | Permission is hereby granted, free of charge, to any person obtaining a copy |
leihen | 0:3940f0ad2ca5 | 12 | of this software and associated documentation files (the "Software"), to deal |
leihen | 0:3940f0ad2ca5 | 13 | in the Software without restriction, including without limitation the rights |
leihen | 0:3940f0ad2ca5 | 14 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
leihen | 0:3940f0ad2ca5 | 15 | copies of the Software, and to permit persons to whom the Software is |
leihen | 0:3940f0ad2ca5 | 16 | furnished to do so, subject to the following conditions: |
leihen | 7:dca20be3ef38 | 17 | |
leihen | 0:3940f0ad2ca5 | 18 | The above copyright notice and this permission notice shall be included in |
leihen | 0:3940f0ad2ca5 | 19 | all copies or substantial portions of the Software. |
leihen | 7:dca20be3ef38 | 20 | |
leihen | 0:3940f0ad2ca5 | 21 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
leihen | 0:3940f0ad2ca5 | 22 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
leihen | 0:3940f0ad2ca5 | 23 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
leihen | 0:3940f0ad2ca5 | 24 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
leihen | 0:3940f0ad2ca5 | 25 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
leihen | 0:3940f0ad2ca5 | 26 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
leihen | 0:3940f0ad2ca5 | 27 | THE SOFTWARE. |
leihen | 0:3940f0ad2ca5 | 28 | */ |
charly | 10:780027029afe | 29 | #ifndef __RTC_MCP97410_H__ |
charly | 10:780027029afe | 30 | #define __RTC_MCP97410_H__ |
leihen | 0:3940f0ad2ca5 | 31 | |
leihen | 0:3940f0ad2ca5 | 32 | |
charly | 11:ef48dcb888c9 | 33 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
charly | 11:ef48dcb888c9 | 34 | // GLOBAL CONSTANTS RTCC - ADDRESSES |
charly | 11:ef48dcb888c9 | 35 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
charly | 11:ef48dcb888c9 | 36 | #define ADDR_EEPROM 0xaf // DEVICE ADDR for EEPROM |
charly | 11:ef48dcb888c9 | 37 | #define ADDR_RTCC 0xdf // DEVICE ADDR for RTCC MCHP |
charly | 11:ef48dcb888c9 | 38 | //................................................................................. |
charly | 11:ef48dcb888c9 | 39 | #define SRAM_PTR 0x20 // pointer of the SRAM area (RTCC) |
charly | 11:ef48dcb888c9 | 40 | #define ADDR_EEPROM_SR 0xff // STATUS REGISTER in the EEPROM |
charly | 11:ef48dcb888c9 | 41 | //................................................................................. |
charly | 11:ef48dcb888c9 | 42 | #define ADDR_SEC 0x00 // address of SECONDS register |
charly | 11:ef48dcb888c9 | 43 | #define ADDR_MIN 0x01 // address of MINUTES register |
charly | 11:ef48dcb888c9 | 44 | #define ADDR_HOUR 0x02 // address of HOURS register |
charly | 11:ef48dcb888c9 | 45 | #define ADDR_DAY 0x03 // address of DAY OF WK register |
charly | 11:ef48dcb888c9 | 46 | #define ADDR_STAT 0x03 // address of STATUS register |
charly | 11:ef48dcb888c9 | 47 | #define ADDR_DATE 0x04 // address of DATE register |
charly | 11:ef48dcb888c9 | 48 | #define ADDR_MNTH 0x05 // address of MONTH register |
charly | 11:ef48dcb888c9 | 49 | #define ADDR_YEAR 0x06 // address of YEAR register |
charly | 11:ef48dcb888c9 | 50 | #define ADDR_CTRL 0x07 // address of CONTROL register |
charly | 11:ef48dcb888c9 | 51 | #define ADDR_CAL 0x08 // address of CALIB register |
charly | 11:ef48dcb888c9 | 52 | #define ADDR_ULID 0x09 // address of UNLOCK ID register |
charly | 11:ef48dcb888c9 | 53 | //................................................................................. |
charly | 11:ef48dcb888c9 | 54 | #define ADDR_ALM0SEC 0x0a // address of ALARMO SEC register |
charly | 11:ef48dcb888c9 | 55 | #define ADDR_ALM0MIN 0x0b // address of ALARMO MIN register |
charly | 11:ef48dcb888c9 | 56 | #define ADDR_ALM0HR 0x0c // address of ALARMO HOUR register |
charly | 11:ef48dcb888c9 | 57 | #define ADDR_ALM0CTL 0x0d // address of ALARM0 CONTR register |
charly | 11:ef48dcb888c9 | 58 | #define ADDR_ALM0DAT 0x0e // address of ALARMO DATE register |
charly | 11:ef48dcb888c9 | 59 | #define ADDR_ALM0MTH 0x0f // address of ALARMO MONTH register |
charly | 11:ef48dcb888c9 | 60 | //................................................................................. |
charly | 11:ef48dcb888c9 | 61 | #define ADDR_ALM1SEC 0x11 // address of ALARM1 SEC register |
charly | 11:ef48dcb888c9 | 62 | #define ADDR_ALM1MIN 0x12 // address of ALARM1 MIN register |
charly | 11:ef48dcb888c9 | 63 | #define ADDR_ALM1HR 0x13 // address of ALARM1 HOUR register |
charly | 11:ef48dcb888c9 | 64 | #define ADDR_ALM1CTL 0x14 // address of ALARM1 CONTR register |
charly | 11:ef48dcb888c9 | 65 | #define ADDR_ALM1DAT 0x15 // address of ALARM1 DATE register |
charly | 11:ef48dcb888c9 | 66 | #define ADDR_ALM1MTH 0x16 // address of ALARM1 MONTH register |
charly | 11:ef48dcb888c9 | 67 | //................................................................................. |
charly | 11:ef48dcb888c9 | 68 | #define ADDR_SAVtoBAT_MIN 0x18 // address of T_SAVER MIN(VDD->BAT) |
charly | 11:ef48dcb888c9 | 69 | #define ADDR_SAVtoBAT_HR 0x19 // address of T_SAVER HR (VDD->BAT) |
charly | 11:ef48dcb888c9 | 70 | #define ADDR_SAVtoBAT_DAT 0x1a // address of T_SAVER DAT(VDD->BAT) |
charly | 11:ef48dcb888c9 | 71 | #define ADDR_SAVtoBAT_MTH 0x1b // address of T_SAVER MTH(VDD->BAT) |
charly | 11:ef48dcb888c9 | 72 | //.................................................................................. |
charly | 11:ef48dcb888c9 | 73 | #define ADDR_SAVtoVDD_MIN 0x1c // address of T_SAVER MIN(BAT->VDD) |
charly | 11:ef48dcb888c9 | 74 | #define ADDR_SAVtoVDD_HR 0x1d // address of T_SAVER HR (BAT->VDD) |
charly | 11:ef48dcb888c9 | 75 | #define ADDR_SAVtoVDD_DAT 0x1e // address of T_SAVER DAT(BAT->VDD) |
charly | 11:ef48dcb888c9 | 76 | #define ADDR_SAVtoVDD_MTH 0x1f // address of T_SAVER MTH(BAT->VDD) |
charly | 11:ef48dcb888c9 | 77 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
charly | 11:ef48dcb888c9 | 78 | // GLOBAL CONSTANTS RTCC - INITIALIZATION |
charly | 11:ef48dcb888c9 | 79 | //.................................................................................. |
charly | 11:ef48dcb888c9 | 80 | #define START_32KHZ 0x80 // start crystal: ST = b7 (ADDR_SEC) |
charly | 11:ef48dcb888c9 | 81 | #define LP 0x20 // mask for the leap year bit(MONTH REG) |
charly | 11:ef48dcb888c9 | 82 | #define HOUR_12 0x40 // 12 hours format (ADDR_HOUR) |
charly | 11:ef48dcb888c9 | 83 | #define PM 0x20 // post-meridian bit (ADDR_HOUR) |
charly | 11:ef48dcb888c9 | 84 | #define OUT_PIN 0x80 // = b7 (ADDR_CTRL) |
charly | 11:ef48dcb888c9 | 85 | #define SQWE 0x40 // SQWE = b6 (ADDR_CTRL) |
charly | 11:ef48dcb888c9 | 86 | #define ALM_NO 0x00 // no alarm activated (ADDR_CTRL) |
charly | 11:ef48dcb888c9 | 87 | #define ALM_0 0x10 // ALARM0 is activated (ADDR_CTRL) |
charly | 11:ef48dcb888c9 | 88 | #define ALM_1 0x20 // ALARM1 is activated (ADDR_CTRL) |
charly | 11:ef48dcb888c9 | 89 | #define ALM_01 0x30 // both alarms are activated (ADDR_CTRL) |
charly | 11:ef48dcb888c9 | 90 | #define MFP_01H 0x00 // MFP = SQVAW(01 HERZ) (ADDR_CTRL) |
charly | 11:ef48dcb888c9 | 91 | #define MFP_04K 0x01 // MFP = SQVAW(04 KHZ) (ADDR_CTRL) |
charly | 11:ef48dcb888c9 | 92 | #define MFP_08K 0x02 // MFP = SQVAW(08 KHZ) (ADDR_CTRL) |
charly | 11:ef48dcb888c9 | 93 | #define MFP_32K 0x03 // MFP = SQVAW(32 KHZ) (ADDR_CTRL) |
charly | 11:ef48dcb888c9 | 94 | #define MFP_64H 0x04 // MFP = SQVAW(64 HERZ) (ADDR_CTRL) |
charly | 11:ef48dcb888c9 | 95 | #define ALMx_POL 0x80 // polarity of MFP on alarm (ADDR_ALMxCTL) |
charly | 11:ef48dcb888c9 | 96 | #define ALMxC_SEC 0x00 // ALARM compare on SEC (ADDR_ALMxCTL) |
charly | 11:ef48dcb888c9 | 97 | #define ALMxC_MIN 0x10 // ALARM compare on MIN (ADDR_ALMxCTL) |
charly | 11:ef48dcb888c9 | 98 | #define ALMxC_HR 0x20 // ALARM compare on HOUR (ADDR_ALMxCTL) |
charly | 11:ef48dcb888c9 | 99 | #define ALMxC_DAY 0x30 // ALARM compare on DAY (ADDR_ALMxCTL) |
charly | 11:ef48dcb888c9 | 100 | #define ALMxC_DAT 0x40 // ALARM compare on DATE (ADDR_ALMxCTL) |
charly | 11:ef48dcb888c9 | 101 | #define ALMxC_ALL 0x70 // ALARM compare on all param(ADDR_ALMxCTL) |
charly | 11:ef48dcb888c9 | 102 | #define ALMx_IF 0x08 // MASK of the ALARM_IF (ADDR_ALMxCTL) |
charly | 11:ef48dcb888c9 | 103 | #define OSCON 0x20 // state of the oscillator(running or not) |
charly | 11:ef48dcb888c9 | 104 | #define VBATEN 0x08 // enable battery for back-up |
leihen | 7:dca20be3ef38 | 105 | |
charly | 10:780027029afe | 106 | /** Class Rtc_Mcp97410 implements the real time clock module MCP97410 |
leihen | 6:bba89618ee63 | 107 | * |
leihen | 6:bba89618ee63 | 108 | * You can read the clock and set a new time and date. |
leihen | 6:bba89618ee63 | 109 | * It is also possible to start and stop the clock. |
charly | 10:780027029afe | 110 | * Rtc_Mcp97410 allows you to display the time in a 12h or 24h format. |
charly | 10:780027029afe | 111 | * Based on Library for DS1307 by Henry Leinen. |
charly | 10:780027029afe | 112 | * Ported for MC97410 by Karl Zweimueller. |
leihen | 6:bba89618ee63 | 113 | */ |
charly | 10:780027029afe | 114 | class Rtc_Mcp97410 |
leihen | 6:bba89618ee63 | 115 | { |
leihen | 7:dca20be3ef38 | 116 | public: |
leihen | 7:dca20be3ef38 | 117 | /** Structure which is used to exchange the time and date |
leihen | 7:dca20be3ef38 | 118 | */ |
leihen | 7:dca20be3ef38 | 119 | typedef struct { |
leihen | 7:dca20be3ef38 | 120 | int sec; /*!< seconds [0..59] */ |
leihen | 7:dca20be3ef38 | 121 | int min; /*!< minutes {0..59] */ |
leihen | 7:dca20be3ef38 | 122 | int hour; /*!< hours [0..23] */ |
leihen | 7:dca20be3ef38 | 123 | int wday; /*!< weekday [1..7, where 1 = sunday, 2 = monday, ... */ |
leihen | 7:dca20be3ef38 | 124 | int date; /*!< day of month [0..31] */ |
leihen | 7:dca20be3ef38 | 125 | int mon; /*!< month of year [1..12] */ |
leihen | 7:dca20be3ef38 | 126 | int year; /*!< year [2000..2255] */ |
leihen | 7:dca20be3ef38 | 127 | } Time_rtc; |
leihen | 1:64274190e842 | 128 | |
leihen | 1:64274190e842 | 129 | |
leihen | 7:dca20be3ef38 | 130 | /** RateSelect specifies the valid frequency values for the square wave output |
leihen | 7:dca20be3ef38 | 131 | */ |
leihen | 7:dca20be3ef38 | 132 | typedef enum { |
leihen | 7:dca20be3ef38 | 133 | RS1Hz = 0, |
leihen | 7:dca20be3ef38 | 134 | RS4kHz = 1, |
leihen | 7:dca20be3ef38 | 135 | RS8kHz = 2, |
leihen | 7:dca20be3ef38 | 136 | RS32kHz = 3 |
leihen | 7:dca20be3ef38 | 137 | } SqwRateSelect_t; |
charly | 11:ef48dcb888c9 | 138 | |
leihen | 7:dca20be3ef38 | 139 | |
leihen | 7:dca20be3ef38 | 140 | protected: |
charly | 11:ef48dcb888c9 | 141 | I2C* _i2c; |
leihen | 7:dca20be3ef38 | 142 | |
leihen | 7:dca20be3ef38 | 143 | static const char *m_weekDays[]; |
leihen | 7:dca20be3ef38 | 144 | |
leihen | 7:dca20be3ef38 | 145 | public: |
leihen | 7:dca20be3ef38 | 146 | /** public constructor which creates the real time clock object |
leihen | 7:dca20be3ef38 | 147 | * |
charly | 10:780027029afe | 148 | * @param i2c : Pointer to I2C-Object for I2C-Interface. |
leihen | 7:dca20be3ef38 | 149 | * |
leihen | 7:dca20be3ef38 | 150 | */ |
charly | 10:780027029afe | 151 | Rtc_Mcp97410(I2C* i2c); |
leihen | 7:dca20be3ef38 | 152 | |
charly | 10:780027029afe | 153 | ~Rtc_Mcp97410(); |
leihen | 7:dca20be3ef38 | 154 | |
leihen | 7:dca20be3ef38 | 155 | /** Read the current time from RTC chip |
leihen | 7:dca20be3ef38 | 156 | * |
leihen | 7:dca20be3ef38 | 157 | * @param time : reference to a struct tm which will be filled with the time from rtc |
leihen | 7:dca20be3ef38 | 158 | * |
leihen | 7:dca20be3ef38 | 159 | * @returns true if successful, otherwise an acknowledge error occured |
leihen | 7:dca20be3ef38 | 160 | */ |
leihen | 7:dca20be3ef38 | 161 | virtual bool getTime(Time_rtc& time); |
leihen | 7:dca20be3ef38 | 162 | |
charly | 10:780027029afe | 163 | /** Write the given time onto the RTC chip (and enable Battery-Backup) |
leihen | 7:dca20be3ef38 | 164 | * |
leihen | 7:dca20be3ef38 | 165 | * @param time : refereence to a struct which contains valid date and time information |
leihen | 7:dca20be3ef38 | 166 | * |
leihen | 7:dca20be3ef38 | 167 | * @param start : contains true if the clock shall start (or keep on running). |
leihen | 7:dca20be3ef38 | 168 | * |
leihen | 7:dca20be3ef38 | 169 | * @param thm : 12-hour-mode if set to true, otherwise 24-hour-mode will be set. |
leihen | 7:dca20be3ef38 | 170 | * |
leihen | 7:dca20be3ef38 | 171 | * @returns true if successful, otherwise an acknowledge error occured |
leihen | 7:dca20be3ef38 | 172 | */ |
leihen | 7:dca20be3ef38 | 173 | virtual bool setTime(Time_rtc& time, bool start, bool thm); |
leihen | 7:dca20be3ef38 | 174 | |
leihen | 7:dca20be3ef38 | 175 | /** Start the clock. Please note that the seconds register need to be read and |
leihen | 7:dca20be3ef38 | 176 | * written in order to start or stop the clock. This can lead to an error |
leihen | 7:dca20be3ef38 | 177 | * in the time value. The recommended way of starting and stoping the clock is |
leihen | 7:dca20be3ef38 | 178 | * to write the actual date and time and set the start bit accordingly. |
leihen | 7:dca20be3ef38 | 179 | * |
leihen | 7:dca20be3ef38 | 180 | * @returns true if the clock was started, false if a communication error occured |
leihen | 7:dca20be3ef38 | 181 | */ |
leihen | 7:dca20be3ef38 | 182 | bool startClock(); |
leihen | 7:dca20be3ef38 | 183 | |
leihen | 7:dca20be3ef38 | 184 | /** Stop the clock. Please note that the seconds register need to be read and |
leihen | 7:dca20be3ef38 | 185 | * written in order to start or stop the clock. This can lead to an error |
leihen | 7:dca20be3ef38 | 186 | * in the time value. The recommended way of starting and stoping the clock is |
leihen | 7:dca20be3ef38 | 187 | * to write the actual date and time and set the start bit accordingly. |
leihen | 7:dca20be3ef38 | 188 | * |
leihen | 7:dca20be3ef38 | 189 | * @returns true if the clock was stopped, false if a communication error occured |
leihen | 7:dca20be3ef38 | 190 | */ |
leihen | 7:dca20be3ef38 | 191 | bool stopClock(); |
leihen | 7:dca20be3ef38 | 192 | |
leihen | 7:dca20be3ef38 | 193 | /** Service function to convert a weekday into a string representation |
leihen | 7:dca20be3ef38 | 194 | * |
leihen | 7:dca20be3ef38 | 195 | * @param wday : day of week to convert (starting with sunday = 1, monday = 2, ..., saturday = 7 |
leihen | 7:dca20be3ef38 | 196 | * |
leihen | 7:dca20be3ef38 | 197 | * @returns the corresponding string representation |
leihen | 7:dca20be3ef38 | 198 | */ |
leihen | 7:dca20be3ef38 | 199 | const char* weekdayToString( int wday ) { |
leihen | 7:dca20be3ef38 | 200 | return m_weekDays[wday%7]; |
leihen | 7:dca20be3ef38 | 201 | } |
leihen | 7:dca20be3ef38 | 202 | |
leihen | 7:dca20be3ef38 | 203 | /** Enable Square Wave output. The function enables or disables the square wave output |
leihen | 7:dca20be3ef38 | 204 | * of the module and sets the desired frequency. |
leihen | 7:dca20be3ef38 | 205 | * |
leihen | 7:dca20be3ef38 | 206 | * @param ena : if set to true, the square wave output is enabled. |
leihen | 7:dca20be3ef38 | 207 | * |
leihen | 7:dca20be3ef38 | 208 | * @param rs : rate select, can be either one of the four values defined by type /c RateSelect_t |
leihen | 7:dca20be3ef38 | 209 | * |
leihen | 7:dca20be3ef38 | 210 | * @return true if the operation was successful or false otherwise |
leihen | 7:dca20be3ef38 | 211 | */ |
leihen | 7:dca20be3ef38 | 212 | bool setSquareWaveOutput(bool ena, SqwRateSelect_t rs); |
charly | 11:ef48dcb888c9 | 213 | |
charly | 11:ef48dcb888c9 | 214 | /** enable write-Operations to EEPROM |
charly | 11:ef48dcb888c9 | 215 | * |
charly | 11:ef48dcb888c9 | 216 | * |
charly | 11:ef48dcb888c9 | 217 | */ |
charly | 11:ef48dcb888c9 | 218 | bool enableEEPROMWrite(); |
charly | 11:ef48dcb888c9 | 219 | |
charly | 11:ef48dcb888c9 | 220 | /** disable write-Operations to EEPROM |
charly | 11:ef48dcb888c9 | 221 | * |
charly | 11:ef48dcb888c9 | 222 | * |
charly | 11:ef48dcb888c9 | 223 | */ |
charly | 11:ef48dcb888c9 | 224 | bool disableEEPROMWrite(); |
charly | 11:ef48dcb888c9 | 225 | |
charly | 13:10e564536e23 | 226 | /** read the OSCTRIM-Register (0x08) |
charly | 13:10e564536e23 | 227 | * |
charly | 13:10e564536e23 | 228 | * -127 .. +127 |
charly | 13:10e564536e23 | 229 | * bit 7 is Sign |
charly | 13:10e564536e23 | 230 | * bit 0:6 Trim-Value |
charly | 13:10e564536e23 | 231 | */ |
charly | 13:10e564536e23 | 232 | uint8_t readTrim(); |
charly | 13:10e564536e23 | 233 | |
charly | 13:10e564536e23 | 234 | /** Increment TRIM-Value |
charly | 13:10e564536e23 | 235 | * |
charly | 13:10e564536e23 | 236 | * Increment TRIM-Value by one |
charly | 13:10e564536e23 | 237 | */ |
charly | 13:10e564536e23 | 238 | bool incTrim(); |
charly | 13:10e564536e23 | 239 | |
charly | 13:10e564536e23 | 240 | /** Derement TRIM-Value |
charly | 13:10e564536e23 | 241 | * |
charly | 13:10e564536e23 | 242 | * Decrement TRIM-Value by one |
charly | 13:10e564536e23 | 243 | */ |
charly | 13:10e564536e23 | 244 | bool decTrim(); |
charly | 13:10e564536e23 | 245 | |
charly | 13:10e564536e23 | 246 | |
charly | 11:ef48dcb888c9 | 247 | /** read from EEPROM |
charly | 13:10e564536e23 | 248 | * |
charly | 11:ef48dcb888c9 | 249 | * address from 0x00 to 0x7F |
charly | 11:ef48dcb888c9 | 250 | * all 128 Bytes can be read in one chunk |
charly | 11:ef48dcb888c9 | 251 | * |
charly | 11:ef48dcb888c9 | 252 | * @param address : start address to read from |
charly | 11:ef48dcb888c9 | 253 | * |
charly | 11:ef48dcb888c9 | 254 | * @param *buffer : buffer of char to write the result to. Be sure to have enough storage |
charly | 11:ef48dcb888c9 | 255 | * |
charly | 11:ef48dcb888c9 | 256 | * @param len : number of bytes to read |
charly | 11:ef48dcb888c9 | 257 | **/ |
charly | 11:ef48dcb888c9 | 258 | bool readEEPROM(int address, char* buffer, int len); |
charly | 11:ef48dcb888c9 | 259 | |
charly | 11:ef48dcb888c9 | 260 | /** write to EEPROM |
charly | 13:10e564536e23 | 261 | * |
charly | 11:ef48dcb888c9 | 262 | * address from 0x00 to 0x7F |
charly | 11:ef48dcb888c9 | 263 | * only maximum 8 Bytes can be written in one chunk |
charly | 11:ef48dcb888c9 | 264 | * |
charly | 11:ef48dcb888c9 | 265 | * @param address : start address to write |
charly | 11:ef48dcb888c9 | 266 | * |
charly | 11:ef48dcb888c9 | 267 | * @param *buffer : buffer of chars to write |
charly | 11:ef48dcb888c9 | 268 | * |
charly | 11:ef48dcb888c9 | 269 | * @param len : number of bytes to write |
charly | 11:ef48dcb888c9 | 270 | **/ |
charly | 11:ef48dcb888c9 | 271 | bool writeEEPROM(int address, char* buffer, int len); |
charly | 11:ef48dcb888c9 | 272 | |
charly | 11:ef48dcb888c9 | 273 | |
charly | 11:ef48dcb888c9 | 274 | |
charly | 11:ef48dcb888c9 | 275 | /** read 6Byte EUI-48 address from EEPROM |
charly | 11:ef48dcb888c9 | 276 | * |
charly | 11:ef48dcb888c9 | 277 | * @param eui48 : array to hold 6 Bytes of EUI48 |
charly | 11:ef48dcb888c9 | 278 | * |
charly | 11:ef48dcb888c9 | 279 | */ |
charly | 11:ef48dcb888c9 | 280 | bool readEUI48(uint8_t* eui48); |
charly | 11:ef48dcb888c9 | 281 | |
charly | 11:ef48dcb888c9 | 282 | /** write 6Byte EUI-48 address to EEPROM |
charly | 11:ef48dcb888c9 | 283 | * |
charly | 11:ef48dcb888c9 | 284 | * be sure to enable EUI-write-Operation (UNLOCK) before |
charly | 11:ef48dcb888c9 | 285 | * @param eui48 : array with 6 Bytes of EUI48 |
charly | 11:ef48dcb888c9 | 286 | * |
charly | 11:ef48dcb888c9 | 287 | */ |
charly | 11:ef48dcb888c9 | 288 | bool writeEUI48(uint8_t* eui48); |
charly | 11:ef48dcb888c9 | 289 | |
charly | 12:88f82e47b6a1 | 290 | /** read 8Byte EUI-64 address from EEPROM |
charly | 12:88f82e47b6a1 | 291 | * |
charly | 12:88f82e47b6a1 | 292 | * @param eui64 : array to hold 8 Bytes of EUI64 |
charly | 12:88f82e47b6a1 | 293 | * |
charly | 12:88f82e47b6a1 | 294 | */ |
charly | 12:88f82e47b6a1 | 295 | bool readEUI64(uint8_t* eui64); |
charly | 12:88f82e47b6a1 | 296 | |
charly | 12:88f82e47b6a1 | 297 | /** write 8Byte EUI-64 address to EEPROM |
charly | 12:88f82e47b6a1 | 298 | * |
charly | 12:88f82e47b6a1 | 299 | * be sure to enable EUI-write-Operation (UNLOCK) before |
charly | 12:88f82e47b6a1 | 300 | * @param eui64 : array with 8 Bytes of EUI64 |
charly | 12:88f82e47b6a1 | 301 | * |
charly | 12:88f82e47b6a1 | 302 | */ |
charly | 12:88f82e47b6a1 | 303 | bool writeEUI64(uint8_t* eui64); |
charly | 12:88f82e47b6a1 | 304 | |
charly | 11:ef48dcb888c9 | 305 | /** unlock the EUI area in the EEPROM |
charly | 11:ef48dcb888c9 | 306 | * |
charly | 11:ef48dcb888c9 | 307 | * write operation must immediatly follow the unlock procedure |
charly | 11:ef48dcb888c9 | 308 | * after a write the EEPROM is automatically locked again |
charly | 11:ef48dcb888c9 | 309 | * |
charly | 11:ef48dcb888c9 | 310 | */ |
charly | 11:ef48dcb888c9 | 311 | bool unlockEUI(); |
leihen | 7:dca20be3ef38 | 312 | |
leihen | 7:dca20be3ef38 | 313 | private: |
charly | 11:ef48dcb888c9 | 314 | // read from a memory area RTC/SRAM or EEPROM. specify correct control_byte |
charly | 11:ef48dcb888c9 | 315 | bool read(int control_byte, int address, char* buffer, int len); |
charly | 11:ef48dcb888c9 | 316 | // write to a memory area RTC/SRAM or EEPROM. specify correct control_byte |
charly | 11:ef48dcb888c9 | 317 | bool write(int control_byte, int address, char* buffer, int len); |
charly | 11:ef48dcb888c9 | 318 | // read from RTC/SRAM |
charly | 11:ef48dcb888c9 | 319 | bool readRTC(int address, char* buffer, int len); |
charly | 11:ef48dcb888c9 | 320 | //wrtie to RTC/SRAM |
charly | 11:ef48dcb888c9 | 321 | bool writeRTC(int address, char* buffer, int len); |
charly | 11:ef48dcb888c9 | 322 | |
leihen | 7:dca20be3ef38 | 323 | |
leihen | 7:dca20be3ef38 | 324 | static int bcdToDecimal(int bcd) { |
leihen | 7:dca20be3ef38 | 325 | return ((bcd&0xF0)>>4)*10 + (bcd&0x0F); |
leihen | 7:dca20be3ef38 | 326 | } |
leihen | 7:dca20be3ef38 | 327 | |
leihen | 7:dca20be3ef38 | 328 | static int decimalToBcd(int dec) { |
leihen | 7:dca20be3ef38 | 329 | return (dec%10) + ((dec/10)<<4); |
leihen | 7:dca20be3ef38 | 330 | } |
leihen | 0:3940f0ad2ca5 | 331 | }; |
leihen | 7:dca20be3ef38 | 332 | |
leihen | 7:dca20be3ef38 | 333 | |
leihen | 7:dca20be3ef38 | 334 | |
leihen | 7:dca20be3ef38 | 335 | typedef void (*RtcCallback_t) (void); |
leihen | 7:dca20be3ef38 | 336 | |
leihen | 7:dca20be3ef38 | 337 | |
charly | 10:780027029afe | 338 | class RtcCls : public Rtc_Mcp97410 |
leihen | 7:dca20be3ef38 | 339 | { |
leihen | 7:dca20be3ef38 | 340 | protected: |
leihen | 7:dca20be3ef38 | 341 | InterruptIn m_sqw; |
leihen | 7:dca20be3ef38 | 342 | bool m_bUseSqw; |
leihen | 7:dca20be3ef38 | 343 | time_t m_time; // Only used in case SQW is used |
leihen | 7:dca20be3ef38 | 344 | |
leihen | 7:dca20be3ef38 | 345 | bool m_bAlarmEnabled; |
leihen | 7:dca20be3ef38 | 346 | RtcCallback_t m_alarmfunc; |
leihen | 7:dca20be3ef38 | 347 | time_t m_alarmTime; |
leihen | 7:dca20be3ef38 | 348 | |
leihen | 7:dca20be3ef38 | 349 | public: |
charly | 10:780027029afe | 350 | RtcCls(I2C* i2c, PinName sqw, bool bUseSqw); |
leihen | 7:dca20be3ef38 | 351 | |
leihen | 7:dca20be3ef38 | 352 | protected: |
leihen | 7:dca20be3ef38 | 353 | void _callback(void); |
leihen | 7:dca20be3ef38 | 354 | |
leihen | 7:dca20be3ef38 | 355 | public: |
leihen | 7:dca20be3ef38 | 356 | time_t getTime(); |
charly | 10:780027029afe | 357 | virtual bool getTime(Time_rtc& time) { return Rtc_Mcp97410::getTime(time); } |
leihen | 7:dca20be3ef38 | 358 | void setTime(time_t time); |
charly | 10:780027029afe | 359 | virtual bool setTime(Time_rtc& time, bool start, bool thm) { return Rtc_Mcp97410::setTime(time, start, thm); } |
leihen | 7:dca20be3ef38 | 360 | public: |
leihen | 7:dca20be3ef38 | 361 | void setAlarm(int nSeconds, RtcCallback_t alarmfunc) { |
leihen | 7:dca20be3ef38 | 362 | m_alarmfunc = alarmfunc; |
leihen | 7:dca20be3ef38 | 363 | m_alarmTime = m_time + nSeconds; |
leihen | 7:dca20be3ef38 | 364 | m_bAlarmEnabled = (alarmfunc == NULL) ? false : true; |
leihen | 7:dca20be3ef38 | 365 | } |
leihen | 7:dca20be3ef38 | 366 | }; |
leihen | 7:dca20be3ef38 | 367 | |
charly | 10:780027029afe | 368 | #endif // __RTC_MCP97410_H__ |