M41T62 is a serial real-time clock (RTC) made by STMicroelectronics.
Dependents: LPC1114_data_logger Check_external_RTC LPC1114_barometer_with_data_logging
m41t62_rtc.h@3:41c351da2fdf, 2015-02-22 (annotated)
- Committer:
- kenjiArai
- Date:
- Sun Feb 22 01:08:24 2015 +0000
- Revision:
- 3:41c351da2fdf
- Parent:
- 2:c58da9ec9ae3
- Child:
- 4:0c07690cb24f
added change function for I2C frequency
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:2919f8bd90f3 | 1 | /* |
kenjiArai | 3:41c351da2fdf | 2 | * mbed library program |
kenjiArai | 0:2919f8bd90f3 | 3 | * Control M41T62 RTC Module |
kenjiArai | 0:2919f8bd90f3 | 4 | * |
kenjiArai | 3:41c351da2fdf | 5 | * Copyright (c) 2014,'15 Kenji Arai / JH1PJL |
kenjiArai | 0:2919f8bd90f3 | 6 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 0:2919f8bd90f3 | 7 | * http://mbed.org/users/kenjiArai/ |
kenjiArai | 0:2919f8bd90f3 | 8 | * Created: June 21st, 2014 |
kenjiArai | 3:41c351da2fdf | 9 | * Revised: Feburary 22nd, 2015 |
kenjiArai | 0:2919f8bd90f3 | 10 | * |
kenjiArai | 0:2919f8bd90f3 | 11 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
kenjiArai | 0:2919f8bd90f3 | 12 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
kenjiArai | 0:2919f8bd90f3 | 13 | * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kenjiArai | 0:2919f8bd90f3 | 14 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenjiArai | 0:2919f8bd90f3 | 15 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kenjiArai | 0:2919f8bd90f3 | 16 | */ |
kenjiArai | 3:41c351da2fdf | 17 | /* |
kenjiArai | 3:41c351da2fdf | 18 | *---------------- REFERENCE ---------------------------------------------------------------------- |
kenjiArai | 3:41c351da2fdf | 19 | * http://www.st-japan.co.jp/web/jp/catalog/sense_power/FM151/CL1410/SC403/PF82507 |
kenjiArai | 3:41c351da2fdf | 20 | * http://strawberry-linux.com/catalog/items?code=12062 |
kenjiArai | 3:41c351da2fdf | 21 | */ |
kenjiArai | 0:2919f8bd90f3 | 22 | |
kenjiArai | 0:2919f8bd90f3 | 23 | #ifndef M41T62_H |
kenjiArai | 0:2919f8bd90f3 | 24 | #define M41T62_H |
kenjiArai | 0:2919f8bd90f3 | 25 | |
kenjiArai | 0:2919f8bd90f3 | 26 | #include "mbed.h" |
kenjiArai | 0:2919f8bd90f3 | 27 | |
kenjiArai | 0:2919f8bd90f3 | 28 | // RTC STmicro M41T62 |
kenjiArai | 0:2919f8bd90f3 | 29 | // Address b7=1,b6=1,b5=0,b4=1,b3=0,b2=0,b1=0, b0=R/W |
kenjiArai | 3:41c351da2fdf | 30 | #define M41T62ADDR (0x68 << 1) // No other choice |
kenjiArai | 0:2919f8bd90f3 | 31 | |
kenjiArai | 0:2919f8bd90f3 | 32 | #define RTC_Wk_Monday ((uint8_t)0x01) |
kenjiArai | 0:2919f8bd90f3 | 33 | #define RTC_Wk_Tuesday ((uint8_t)0x02) |
kenjiArai | 0:2919f8bd90f3 | 34 | #define RTC_Wk_Wednesday ((uint8_t)0x03) |
kenjiArai | 0:2919f8bd90f3 | 35 | #define RTC_Wk_Thursday ((uint8_t)0x04) |
kenjiArai | 0:2919f8bd90f3 | 36 | #define RTC_Wk_Friday ((uint8_t)0x05) |
kenjiArai | 0:2919f8bd90f3 | 37 | #define RTC_Wk_Saturday ((uint8_t)0x06) |
kenjiArai | 0:2919f8bd90f3 | 38 | #define RTC_Wk_Sunday ((uint8_t)0x07) |
kenjiArai | 0:2919f8bd90f3 | 39 | |
kenjiArai | 0:2919f8bd90f3 | 40 | #define RTC_SQW_NONE ((uint8_t)0x0) |
kenjiArai | 0:2919f8bd90f3 | 41 | #define RTC_SQW_32KHZ ((uint8_t)0x1) |
kenjiArai | 0:2919f8bd90f3 | 42 | #define RTC_SQW_8KHZ ((uint8_t)0x2) |
kenjiArai | 0:2919f8bd90f3 | 43 | #define RTC_SQW_4KHZ ((uint8_t)0x3) |
kenjiArai | 0:2919f8bd90f3 | 44 | #define RTC_SQW_2KHZ ((uint8_t)0x4) |
kenjiArai | 0:2919f8bd90f3 | 45 | #define RTC_SQW_1KHZ ((uint8_t)0x5) |
kenjiArai | 0:2919f8bd90f3 | 46 | #define RTC_SQW_512HZ ((uint8_t)0x6) |
kenjiArai | 0:2919f8bd90f3 | 47 | #define RTC_SQW_256HZ ((uint8_t)0x7) |
kenjiArai | 0:2919f8bd90f3 | 48 | #define RTC_SQW_128HZ ((uint8_t)0x8) |
kenjiArai | 0:2919f8bd90f3 | 49 | #define RTC_SQW_64HZ ((uint8_t)0x9) |
kenjiArai | 0:2919f8bd90f3 | 50 | #define RTC_SQW_32HZ ((uint8_t)0xa) |
kenjiArai | 0:2919f8bd90f3 | 51 | #define RTC_SQW_16HZ ((uint8_t)0xb) |
kenjiArai | 0:2919f8bd90f3 | 52 | #define RTC_SQW_8HZ ((uint8_t)0xc) |
kenjiArai | 0:2919f8bd90f3 | 53 | #define RTC_SQW_4HZ ((uint8_t)0xd) |
kenjiArai | 0:2919f8bd90f3 | 54 | #define RTC_SQW_2HZ ((uint8_t)0xe) |
kenjiArai | 0:2919f8bd90f3 | 55 | #define RTC_SQW_1HZ ((uint8_t)0xf) |
kenjiArai | 0:2919f8bd90f3 | 56 | |
kenjiArai | 0:2919f8bd90f3 | 57 | typedef struct { // BCD format |
kenjiArai | 3:41c351da2fdf | 58 | uint8_t rtc_seconds; |
kenjiArai | 3:41c351da2fdf | 59 | uint8_t rtc_minutes; |
kenjiArai | 3:41c351da2fdf | 60 | uint8_t rtc_hours; |
kenjiArai | 3:41c351da2fdf | 61 | uint8_t rtc_weekday; |
kenjiArai | 3:41c351da2fdf | 62 | uint8_t rtc_date; |
kenjiArai | 3:41c351da2fdf | 63 | uint8_t rtc_month; |
kenjiArai | 3:41c351da2fdf | 64 | uint8_t rtc_year_raw; |
kenjiArai | 3:41c351da2fdf | 65 | uint16_t rtc_year; |
kenjiArai | 3:41c351da2fdf | 66 | } rtc_time; |
kenjiArai | 3:41c351da2fdf | 67 | |
kenjiArai | 3:41c351da2fdf | 68 | // Register definition |
kenjiArai | 3:41c351da2fdf | 69 | #define M41T62_REG_SSEC 0x0 |
kenjiArai | 3:41c351da2fdf | 70 | #define M41T62_REG_SEC 0x1 |
kenjiArai | 3:41c351da2fdf | 71 | #define M41T62_REG_MIN 0x2 |
kenjiArai | 3:41c351da2fdf | 72 | #define M41T62_REG_HOUR 0x3 |
kenjiArai | 3:41c351da2fdf | 73 | #define M41T62_REG_WDAY 0x4 |
kenjiArai | 3:41c351da2fdf | 74 | #define M41T62_REG_DAY 0x5 |
kenjiArai | 3:41c351da2fdf | 75 | #define M41T62_REG_MON 0x6 |
kenjiArai | 3:41c351da2fdf | 76 | #define M41T62_REG_YEAR 0x7 |
kenjiArai | 3:41c351da2fdf | 77 | |
kenjiArai | 3:41c351da2fdf | 78 | #define M41T62_REG_ALARM_MON 0xa |
kenjiArai | 3:41c351da2fdf | 79 | #define M41T62_REG_ALARM_DAY 0xb |
kenjiArai | 3:41c351da2fdf | 80 | #define M41T62_REG_ALARM_HOUR 0xc |
kenjiArai | 3:41c351da2fdf | 81 | #define M41T62_REG_ALARM_MIN 0xd |
kenjiArai | 3:41c351da2fdf | 82 | #define M41T62_REG_ALARM_SEC 0xe |
kenjiArai | 3:41c351da2fdf | 83 | #define M41T62_REG_FLAGS 0xf |
kenjiArai | 0:2919f8bd90f3 | 84 | |
kenjiArai | 2:c58da9ec9ae3 | 85 | /** Interface for RTC (I2C Interface) STMicroelectronics M41T62 |
kenjiArai | 2:c58da9ec9ae3 | 86 | * |
kenjiArai | 2:c58da9ec9ae3 | 87 | * Standalone type RTC via I2C interface |
kenjiArai | 2:c58da9ec9ae3 | 88 | * |
kenjiArai | 2:c58da9ec9ae3 | 89 | * @code |
kenjiArai | 2:c58da9ec9ae3 | 90 | * #include "mbed.h" |
kenjiArai | 3:41c351da2fdf | 91 | * #include "m41t62_rtc.h" |
kenjiArai | 2:c58da9ec9ae3 | 92 | * |
kenjiArai | 2:c58da9ec9ae3 | 93 | * // I2C Communication |
kenjiArai | 2:c58da9ec9ae3 | 94 | * M41T62 m41t62(dp5,dp27); // STmicro RTC(M41T62) SDA, SCL |
kenjiArai | 2:c58da9ec9ae3 | 95 | * // If you connected I2C line not only this device but also other devices, |
kenjiArai | 2:c58da9ec9ae3 | 96 | * // you need to declare following method. |
kenjiArai | 2:c58da9ec9ae3 | 97 | * I2C i2c(dp5,dp27); // SDA, SCL |
kenjiArai | 2:c58da9ec9ae3 | 98 | * M41T62 m41t62(i2c); // STmicro RTC(M41T62) |
kenjiArai | 2:c58da9ec9ae3 | 99 | * |
kenjiArai | 2:c58da9ec9ae3 | 100 | * int main() { |
kenjiArai | 2:c58da9ec9ae3 | 101 | * tm t; |
kenjiArai | 2:c58da9ec9ae3 | 102 | * time_t seconds; |
kenjiArai | 2:c58da9ec9ae3 | 103 | * char buf[40]; |
kenjiArai | 2:c58da9ec9ae3 | 104 | * |
kenjiArai | 3:41c351da2fdf | 105 | * m41t62.set_sq_wave(RTC_SQW_NONE); // Stop output for more low current |
kenjiArai | 2:c58da9ec9ae3 | 106 | * |
kenjiArai | 2:c58da9ec9ae3 | 107 | * m41t62.read_rtc_std(&t); // read RTC data |
kenjiArai | 2:c58da9ec9ae3 | 108 | * seconds = mktime(&t); |
kenjiArai | 2:c58da9ec9ae3 | 109 | * strftime(buf, 40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); |
kenjiArai | 2:c58da9ec9ae3 | 110 | * printf("Date: %s\r\n", buf); |
kenjiArai | 2:c58da9ec9ae3 | 111 | * } |
kenjiArai | 2:c58da9ec9ae3 | 112 | * @endcode |
kenjiArai | 2:c58da9ec9ae3 | 113 | */ |
kenjiArai | 3:41c351da2fdf | 114 | |
kenjiArai | 3:41c351da2fdf | 115 | class M41T62 |
kenjiArai | 3:41c351da2fdf | 116 | { |
kenjiArai | 0:2919f8bd90f3 | 117 | public: |
kenjiArai | 2:c58da9ec9ae3 | 118 | /** Configure data pin |
kenjiArai | 2:c58da9ec9ae3 | 119 | * @param data SDA and SCL pins |
kenjiArai | 2:c58da9ec9ae3 | 120 | */ |
kenjiArai | 0:2919f8bd90f3 | 121 | M41T62(PinName p_sda, PinName p_scl); |
kenjiArai | 2:c58da9ec9ae3 | 122 | |
kenjiArai | 2:c58da9ec9ae3 | 123 | /** Configure data pin (with other devices on I2C line) |
kenjiArai | 2:c58da9ec9ae3 | 124 | * @param I2C previous definition |
kenjiArai | 2:c58da9ec9ae3 | 125 | */ |
kenjiArai | 0:2919f8bd90f3 | 126 | M41T62(I2C& p_i2c); |
kenjiArai | 0:2919f8bd90f3 | 127 | |
kenjiArai | 2:c58da9ec9ae3 | 128 | /** Read RTC data with Standard C "struct tm" format |
kenjiArai | 2:c58da9ec9ae3 | 129 | * @param tm (data save area) |
kenjiArai | 2:c58da9ec9ae3 | 130 | * @return none but all data in tm |
kenjiArai | 2:c58da9ec9ae3 | 131 | */ |
kenjiArai | 1:9d7702a887d3 | 132 | void read_rtc_std(tm *); |
kenjiArai | 2:c58da9ec9ae3 | 133 | |
kenjiArai | 2:c58da9ec9ae3 | 134 | /** Write data to RTC data with Standard C "struct tm" format |
kenjiArai | 2:c58da9ec9ae3 | 135 | * @param tm (save writing data) |
kenjiArai | 2:c58da9ec9ae3 | 136 | * @return none but all data in tm |
kenjiArai | 2:c58da9ec9ae3 | 137 | */ |
kenjiArai | 1:9d7702a887d3 | 138 | void write_rtc_std(tm *); |
kenjiArai | 2:c58da9ec9ae3 | 139 | |
kenjiArai | 2:c58da9ec9ae3 | 140 | /** Read RTC data with own format |
kenjiArai | 2:c58da9ec9ae3 | 141 | * @param tm (data save area) |
kenjiArai | 2:c58da9ec9ae3 | 142 | * @return none but all data in tm |
kenjiArai | 2:c58da9ec9ae3 | 143 | */ |
kenjiArai | 1:9d7702a887d3 | 144 | void read_rtc_direct(rtc_time *); |
kenjiArai | 2:c58da9ec9ae3 | 145 | |
kenjiArai | 2:c58da9ec9ae3 | 146 | /** Read RTC data with own format |
kenjiArai | 2:c58da9ec9ae3 | 147 | * @param tm (save writing data) |
kenjiArai | 2:c58da9ec9ae3 | 148 | * @return none but all data in tm |
kenjiArai | 2:c58da9ec9ae3 | 149 | */ |
kenjiArai | 1:9d7702a887d3 | 150 | void write_rtc_direct(rtc_time *); |
kenjiArai | 2:c58da9ec9ae3 | 151 | |
kenjiArai | 3:41c351da2fdf | 152 | /** Set I2C clock frequency |
kenjiArai | 3:41c351da2fdf | 153 | * @param freq. |
kenjiArai | 3:41c351da2fdf | 154 | * @return none |
kenjiArai | 3:41c351da2fdf | 155 | */ |
kenjiArai | 3:41c351da2fdf | 156 | void frequency(int hz); |
kenjiArai | 3:41c351da2fdf | 157 | |
kenjiArai | 2:c58da9ec9ae3 | 158 | /** Control Square wave output port |
kenjiArai | 2:c58da9ec9ae3 | 159 | * @param output_mode |
kenjiArai | 2:c58da9ec9ae3 | 160 | * @return none |
kenjiArai | 2:c58da9ec9ae3 | 161 | */ |
kenjiArai | 1:9d7702a887d3 | 162 | void set_sq_wave(uint8_t ); |
kenjiArai | 0:2919f8bd90f3 | 163 | |
kenjiArai | 0:2919f8bd90f3 | 164 | protected: |
kenjiArai | 0:2919f8bd90f3 | 165 | uint8_t bin2bcd(uint8_t); |
kenjiArai | 0:2919f8bd90f3 | 166 | uint8_t bcd2bin(uint8_t); |
kenjiArai | 0:2919f8bd90f3 | 167 | |
kenjiArai | 3:41c351da2fdf | 168 | I2C _i2c; |
kenjiArai | 0:2919f8bd90f3 | 169 | |
kenjiArai | 0:2919f8bd90f3 | 170 | private: |
kenjiArai | 0:2919f8bd90f3 | 171 | uint8_t M41T62_addr; |
kenjiArai | 0:2919f8bd90f3 | 172 | uint8_t rtc_buf[8 + 2]; // buffer for RTC |
kenjiArai | 0:2919f8bd90f3 | 173 | }; |
kenjiArai | 0:2919f8bd90f3 | 174 | |
kenjiArai | 2:c58da9ec9ae3 | 175 | #endif // M41T62_H |