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@6:0bf0f681fb69, 2020-08-07 (annotated)
- Committer:
- kenjiArai
- Date:
- Fri Aug 07 06:21:06 2020 +0000
- Revision:
- 6:0bf0f681fb69
- Parent:
- 5:959683e91979
bug fix in set_alarm_reg
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 | 5:959683e91979 | 4 | * STMicroelectronics |
kenjiArai | 0:2919f8bd90f3 | 5 | * |
kenjiArai | 5:959683e91979 | 6 | * Copyright (c) 2014,'15,'17,'20 Kenji Arai / JH1PJL |
kenjiArai | 5:959683e91979 | 7 | * http://www7b.biglobe.ne.jp/~kenjia/ |
kenjiArai | 5:959683e91979 | 8 | * https://os.mbed.com/users/kenjiArai/ |
kenjiArai | 4:0c07690cb24f | 9 | * Created: June 21st, 2014 |
kenjiArai | 5:959683e91979 | 10 | * Revised: August 7th, 2020 |
kenjiArai | 0:2919f8bd90f3 | 11 | */ |
kenjiArai | 5:959683e91979 | 12 | |
kenjiArai | 3:41c351da2fdf | 13 | /* |
kenjiArai | 5:959683e91979 | 14 | *---------------- REFERENCE --------------------------------------------------- |
kenjiArai | 5:959683e91979 | 15 | * http://www.st-japan.co.jp/web/jp/ |
kenjiArai | 5:959683e91979 | 16 | * catalog/sense_power/FM151/CL1410/SC403/PF82507 |
kenjiArai | 3:41c351da2fdf | 17 | * http://strawberry-linux.com/catalog/items?code=12062 |
kenjiArai | 3:41c351da2fdf | 18 | */ |
kenjiArai | 0:2919f8bd90f3 | 19 | |
kenjiArai | 0:2919f8bd90f3 | 20 | #ifndef M41T62_H |
kenjiArai | 0:2919f8bd90f3 | 21 | #define M41T62_H |
kenjiArai | 0:2919f8bd90f3 | 22 | |
kenjiArai | 0:2919f8bd90f3 | 23 | #include "mbed.h" |
kenjiArai | 0:2919f8bd90f3 | 24 | |
kenjiArai | 0:2919f8bd90f3 | 25 | // RTC STmicro M41T62 |
kenjiArai | 0:2919f8bd90f3 | 26 | // Address b7=1,b6=1,b5=0,b4=1,b3=0,b2=0,b1=0, b0=R/W |
kenjiArai | 3:41c351da2fdf | 27 | #define M41T62ADDR (0x68 << 1) // No other choice |
kenjiArai | 0:2919f8bd90f3 | 28 | |
kenjiArai | 3:41c351da2fdf | 29 | // Register definition |
kenjiArai | 3:41c351da2fdf | 30 | #define M41T62_REG_SSEC 0x0 |
kenjiArai | 3:41c351da2fdf | 31 | #define M41T62_REG_SEC 0x1 |
kenjiArai | 3:41c351da2fdf | 32 | #define M41T62_REG_MIN 0x2 |
kenjiArai | 3:41c351da2fdf | 33 | #define M41T62_REG_HOUR 0x3 |
kenjiArai | 3:41c351da2fdf | 34 | #define M41T62_REG_WDAY 0x4 |
kenjiArai | 3:41c351da2fdf | 35 | #define M41T62_REG_DAY 0x5 |
kenjiArai | 3:41c351da2fdf | 36 | #define M41T62_REG_MON 0x6 |
kenjiArai | 3:41c351da2fdf | 37 | #define M41T62_REG_YEAR 0x7 |
kenjiArai | 5:959683e91979 | 38 | #define M41T62_REG_CALIB 0x8 |
kenjiArai | 5:959683e91979 | 39 | #define M41T62_REG_WDT 0x9 |
kenjiArai | 3:41c351da2fdf | 40 | #define M41T62_REG_ALARM_MON 0xa |
kenjiArai | 3:41c351da2fdf | 41 | #define M41T62_REG_ALARM_DAY 0xb |
kenjiArai | 3:41c351da2fdf | 42 | #define M41T62_REG_ALARM_HOUR 0xc |
kenjiArai | 3:41c351da2fdf | 43 | #define M41T62_REG_ALARM_MIN 0xd |
kenjiArai | 3:41c351da2fdf | 44 | #define M41T62_REG_ALARM_SEC 0xe |
kenjiArai | 3:41c351da2fdf | 45 | #define M41T62_REG_FLAGS 0xf |
kenjiArai | 0:2919f8bd90f3 | 46 | |
kenjiArai | 5:959683e91979 | 47 | // Buffer size |
kenjiArai | 5:959683e91979 | 48 | #define RTC_BUF_SIZ (M41T62_REG_FLAGS + 5) |
kenjiArai | 5:959683e91979 | 49 | |
kenjiArai | 5:959683e91979 | 50 | typedef enum { |
kenjiArai | 5:959683e91979 | 51 | RTC_SQW_NONE = 0, |
kenjiArai | 5:959683e91979 | 52 | RTC_SQW_32KHZ, |
kenjiArai | 5:959683e91979 | 53 | RTC_SQW_8KHZ, |
kenjiArai | 5:959683e91979 | 54 | RTC_SQW_4KHZ, |
kenjiArai | 5:959683e91979 | 55 | RTC_SQW_2KHZ, |
kenjiArai | 5:959683e91979 | 56 | RTC_SQW_1KHZ, |
kenjiArai | 5:959683e91979 | 57 | RTC_SQW_512HZ, |
kenjiArai | 5:959683e91979 | 58 | RTC_SQW_256HZ, |
kenjiArai | 5:959683e91979 | 59 | RTC_SQW_128HZ, |
kenjiArai | 5:959683e91979 | 60 | RTC_SQW_64HZ, |
kenjiArai | 5:959683e91979 | 61 | RTC_SQW_32HZ, |
kenjiArai | 5:959683e91979 | 62 | RTC_SQW_16HZ, |
kenjiArai | 5:959683e91979 | 63 | RTC_SQW_8HZ, |
kenjiArai | 5:959683e91979 | 64 | RTC_SQW_4HZ, |
kenjiArai | 5:959683e91979 | 65 | RTC_SQW_2HZ, |
kenjiArai | 5:959683e91979 | 66 | RTC_SQW_1HZ |
kenjiArai | 5:959683e91979 | 67 | } sq_wave_t; |
kenjiArai | 5:959683e91979 | 68 | |
kenjiArai | 2:c58da9ec9ae3 | 69 | /** Interface for RTC (I2C Interface) STMicroelectronics M41T62 |
kenjiArai | 2:c58da9ec9ae3 | 70 | * |
kenjiArai | 2:c58da9ec9ae3 | 71 | * Standalone type RTC via I2C interface |
kenjiArai | 2:c58da9ec9ae3 | 72 | * |
kenjiArai | 2:c58da9ec9ae3 | 73 | * @code |
kenjiArai | 2:c58da9ec9ae3 | 74 | * #include "mbed.h" |
kenjiArai | 3:41c351da2fdf | 75 | * #include "m41t62_rtc.h" |
kenjiArai | 2:c58da9ec9ae3 | 76 | * |
kenjiArai | 2:c58da9ec9ae3 | 77 | * // I2C Communication |
kenjiArai | 2:c58da9ec9ae3 | 78 | * M41T62 m41t62(dp5,dp27); // STmicro RTC(M41T62) SDA, SCL |
kenjiArai | 2:c58da9ec9ae3 | 79 | * // If you connected I2C line not only this device but also other devices, |
kenjiArai | 2:c58da9ec9ae3 | 80 | * // you need to declare following method. |
kenjiArai | 2:c58da9ec9ae3 | 81 | * I2C i2c(dp5,dp27); // SDA, SCL |
kenjiArai | 2:c58da9ec9ae3 | 82 | * M41T62 m41t62(i2c); // STmicro RTC(M41T62) |
kenjiArai | 2:c58da9ec9ae3 | 83 | * |
kenjiArai | 2:c58da9ec9ae3 | 84 | * int main() { |
kenjiArai | 2:c58da9ec9ae3 | 85 | * tm t; |
kenjiArai | 2:c58da9ec9ae3 | 86 | * time_t seconds; |
kenjiArai | 2:c58da9ec9ae3 | 87 | * char buf[40]; |
kenjiArai | 2:c58da9ec9ae3 | 88 | * |
kenjiArai | 5:959683e91979 | 89 | * m41t62.set_sq_wave(RTC_SQW_NONE); // Stop output for more low current |
kenjiArai | 5:959683e91979 | 90 | * while(1){ |
kenjiArai | 5:959683e91979 | 91 | * m41t62.read_rtc_std(&t); // read RTC data |
kenjiArai | 5:959683e91979 | 92 | * seconds = mktime(&t); |
kenjiArai | 5:959683e91979 | 93 | * strftime(buf, 40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); |
kenjiArai | 5:959683e91979 | 94 | * printf("Date: %s\r\n", buf); |
kenjiArai | 5:959683e91979 | 95 | * } |
kenjiArai | 2:c58da9ec9ae3 | 96 | * } |
kenjiArai | 2:c58da9ec9ae3 | 97 | * @endcode |
kenjiArai | 2:c58da9ec9ae3 | 98 | */ |
kenjiArai | 3:41c351da2fdf | 99 | |
kenjiArai | 3:41c351da2fdf | 100 | class M41T62 |
kenjiArai | 3:41c351da2fdf | 101 | { |
kenjiArai | 0:2919f8bd90f3 | 102 | public: |
kenjiArai | 5:959683e91979 | 103 | |
kenjiArai | 5:959683e91979 | 104 | typedef struct { // BCD format |
kenjiArai | 5:959683e91979 | 105 | uint8_t rtc_seconds; |
kenjiArai | 5:959683e91979 | 106 | uint8_t rtc_minutes; |
kenjiArai | 5:959683e91979 | 107 | uint8_t rtc_hours; |
kenjiArai | 5:959683e91979 | 108 | uint8_t rtc_weekday; |
kenjiArai | 5:959683e91979 | 109 | uint8_t rtc_date; |
kenjiArai | 5:959683e91979 | 110 | uint8_t rtc_month; |
kenjiArai | 5:959683e91979 | 111 | uint8_t rtc_year_raw; |
kenjiArai | 5:959683e91979 | 112 | uint16_t rtc_year; |
kenjiArai | 5:959683e91979 | 113 | } rtc_time; |
kenjiArai | 5:959683e91979 | 114 | |
kenjiArai | 5:959683e91979 | 115 | /** Configure data pin (with other devices on I2C line) |
kenjiArai | 2:c58da9ec9ae3 | 116 | * @param data SDA and SCL pins |
kenjiArai | 2:c58da9ec9ae3 | 117 | */ |
kenjiArai | 0:2919f8bd90f3 | 118 | M41T62(PinName p_sda, PinName p_scl); |
kenjiArai | 2:c58da9ec9ae3 | 119 | |
kenjiArai | 2:c58da9ec9ae3 | 120 | /** Configure data pin (with other devices on I2C line) |
kenjiArai | 2:c58da9ec9ae3 | 121 | * @param I2C previous definition |
kenjiArai | 2:c58da9ec9ae3 | 122 | */ |
kenjiArai | 0:2919f8bd90f3 | 123 | M41T62(I2C& p_i2c); |
kenjiArai | 0:2919f8bd90f3 | 124 | |
kenjiArai | 2:c58da9ec9ae3 | 125 | /** Read RTC data with Standard C "struct tm" format |
kenjiArai | 2:c58da9ec9ae3 | 126 | * @param tm (data save area) |
kenjiArai | 2:c58da9ec9ae3 | 127 | * @return none but all data in tm |
kenjiArai | 2:c58da9ec9ae3 | 128 | */ |
kenjiArai | 1:9d7702a887d3 | 129 | void read_rtc_std(tm *); |
kenjiArai | 5:959683e91979 | 130 | void get_time_rtc(tm *); |
kenjiArai | 2:c58da9ec9ae3 | 131 | |
kenjiArai | 2:c58da9ec9ae3 | 132 | /** Write data to RTC data with Standard C "struct tm" format |
kenjiArai | 2:c58da9ec9ae3 | 133 | * @param tm (save writing data) |
kenjiArai | 2:c58da9ec9ae3 | 134 | * @return none but all data in tm |
kenjiArai | 2:c58da9ec9ae3 | 135 | */ |
kenjiArai | 1:9d7702a887d3 | 136 | void write_rtc_std(tm *); |
kenjiArai | 5:959683e91979 | 137 | void set_time_rtc(tm *); |
kenjiArai | 5:959683e91979 | 138 | |
kenjiArai | 5:959683e91979 | 139 | /** Set Alarm / IRQ time |
kenjiArai | 5:959683e91979 | 140 | * @param next time (unit: minutes) from now on minimum = 2 minutes!! |
kenjiArai | 5:959683e91979 | 141 | * @return none |
kenjiArai | 5:959683e91979 | 142 | */ |
kenjiArai | 5:959683e91979 | 143 | void set_next_IRQ(uint16_t time); |
kenjiArai | 5:959683e91979 | 144 | |
kenjiArai | 5:959683e91979 | 145 | /** Clear Alarm / IRQ pin interrupt |
kenjiArai | 5:959683e91979 | 146 | * @param none |
kenjiArai | 5:959683e91979 | 147 | * @return none |
kenjiArai | 5:959683e91979 | 148 | */ |
kenjiArai | 5:959683e91979 | 149 | void clear_IRQ(void); |
kenjiArai | 2:c58da9ec9ae3 | 150 | |
kenjiArai | 2:c58da9ec9ae3 | 151 | /** Read RTC data with own format |
kenjiArai | 2:c58da9ec9ae3 | 152 | * @param tm (data save area) |
kenjiArai | 2:c58da9ec9ae3 | 153 | * @return none but all data in tm |
kenjiArai | 2:c58da9ec9ae3 | 154 | */ |
kenjiArai | 1:9d7702a887d3 | 155 | void read_rtc_direct(rtc_time *); |
kenjiArai | 2:c58da9ec9ae3 | 156 | |
kenjiArai | 2:c58da9ec9ae3 | 157 | /** Read RTC data with own format |
kenjiArai | 2:c58da9ec9ae3 | 158 | * @param tm (save writing data) |
kenjiArai | 2:c58da9ec9ae3 | 159 | * @return none but all data in tm |
kenjiArai | 2:c58da9ec9ae3 | 160 | */ |
kenjiArai | 1:9d7702a887d3 | 161 | void write_rtc_direct(rtc_time *); |
kenjiArai | 2:c58da9ec9ae3 | 162 | |
kenjiArai | 3:41c351da2fdf | 163 | /** Set I2C clock frequency |
kenjiArai | 3:41c351da2fdf | 164 | * @param freq. |
kenjiArai | 3:41c351da2fdf | 165 | * @return none |
kenjiArai | 3:41c351da2fdf | 166 | */ |
kenjiArai | 5:959683e91979 | 167 | void frequency(int); |
kenjiArai | 3:41c351da2fdf | 168 | |
kenjiArai | 2:c58da9ec9ae3 | 169 | /** Control Square wave output port |
kenjiArai | 5:959683e91979 | 170 | * @param output_mode |
kenjiArai | 2:c58da9ec9ae3 | 171 | * @return none |
kenjiArai | 2:c58da9ec9ae3 | 172 | */ |
kenjiArai | 5:959683e91979 | 173 | void set_sq_wave(sq_wave_t); |
kenjiArai | 0:2919f8bd90f3 | 174 | |
kenjiArai | 0:2919f8bd90f3 | 175 | protected: |
kenjiArai | 4:0c07690cb24f | 176 | I2C *_i2c_p; |
kenjiArai | 4:0c07690cb24f | 177 | I2C &_i2c; |
kenjiArai | 0:2919f8bd90f3 | 178 | |
kenjiArai | 5:959683e91979 | 179 | uint8_t bin2bcd(uint8_t); |
kenjiArai | 5:959683e91979 | 180 | uint8_t bcd2bin(uint8_t); |
kenjiArai | 5:959683e91979 | 181 | void set_alarm_reg (uint16_t time); |
kenjiArai | 5:959683e91979 | 182 | |
kenjiArai | 0:2919f8bd90f3 | 183 | private: |
kenjiArai | 0:2919f8bd90f3 | 184 | uint8_t M41T62_addr; |
kenjiArai | 5:959683e91979 | 185 | uint8_t rtc_buf[RTC_BUF_SIZ]; // buffer for RTC |
kenjiArai | 5:959683e91979 | 186 | |
kenjiArai | 0:2919f8bd90f3 | 187 | }; |
kenjiArai | 0:2919f8bd90f3 | 188 | |
kenjiArai | 2:c58da9ec9ae3 | 189 | #endif // M41T62_H |