Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: LPC1114_data_logger Check_external_RTC LPC1114_barometer_with_data_logging
Revision 0:2919f8bd90f3, committed 2014-06-21
- Comitter:
- kenjiArai
- Date:
- Sat Jun 21 04:37:56 2014 +0000
- Child:
- 1:9d7702a887d3
- Commit message:
- 1st fixed version
Changed in this revision
| m41t62_rtc.cpp | Show annotated file Show diff for this revision Revisions of this file |
| m41t62_rtc.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/m41t62_rtc.cpp Sat Jun 21 04:37:56 2014 +0000
@@ -0,0 +1,118 @@
+/*
+ * mbed library program
+ * Control M41T62 RTC Module
+ *
+ * Copyright (c) 2014 Kenji Arai / JH1PJL
+ * http://www.page.sannet.ne.jp/kenjia/index.html
+ * http://mbed.org/users/kenjiArai/
+ * Created: June 21st, 2014
+ * Revised: June 21st, 2014
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
+ * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+/*
+ *---------------- REFERENCE ----------------------------------------------------------------------
+ * http://www.st-japan.co.jp/web/jp/catalog/sense_power/FM151/CL1410/SC403/PF82507
+ */
+
+#include "mbed.h"
+#include "m41t62_rtc.h"
+
+
+// Register definition
+#define M41T62_REG_SSEC 0
+#define M41T62_REG_SEC 1
+#define M41T62_REG_MIN 2
+#define M41T62_REG_HOUR 3
+#define M41T62_REG_WDAY 4
+#define M41T62_REG_DAY 5
+#define M41T62_REG_MON 6
+#define M41T62_REG_YEAR 7
+#define M41T62_REG_ALARM_MON 0xa
+#define M41T62_REG_ALARM_DAY 0xb
+#define M41T62_REG_ALARM_HOUR 0xc
+#define M41T62_REG_ALARM_MIN 0xd
+#define M41T62_REG_ALARM_SEC 0xe
+#define M41T62_REG_FLAGS 0xf
+
+M41T62::M41T62 (PinName p_sda, PinName p_scl) : i2c(p_sda, p_scl) {
+ M41T62_addr = M41T62ADDR;
+}
+
+M41T62::M41T62 (I2C& p_i2c) : i2c(p_i2c) {
+ M41T62_addr = M41T62ADDR;
+}
+
+void M41T62::read_rtc (rtc_time *tm) {
+uint8_t eep_dt;
+
+ eep_dt = M41T62_REG_SSEC;
+ i2c_write_n_bytes((int)M41T62_addr, (char *)eep_dt, 1);
+ i2c_read_n_bytes((int)M41T62_addr, (char *)rtc_buf, 8);
+ tm->rtc_seconds = bcd2bin(rtc_buf[M41T62_REG_SEC] & 0x7f);
+ tm->rtc_minutes = bcd2bin(rtc_buf[M41T62_REG_MIN] & 0x7f);
+ tm->rtc_hours = bcd2bin(rtc_buf[M41T62_REG_HOUR] & 0x3f);
+ tm->rtc_date = bcd2bin(rtc_buf[M41T62_REG_DAY] & 0x3f);
+ tm->rtc_weekday = rtc_buf[M41T62_REG_WDAY] & 0x07;
+ tm->rtc_month = bcd2bin(rtc_buf[M41T62_REG_MON] & 0x1f);
+ tm->rtc_year = bcd2bin(rtc_buf[M41T62_REG_YEAR]) + 100 + 1900;
+}
+
+void M41T62::write_rtc (rtc_time *tm) {
+uint8_t eep_dt;
+
+ eep_dt = M41T62_REG_SSEC;
+ i2c_write_n_bytes((int)M41T62_addr, (char *)eep_dt, 1);
+ i2c_read_n_bytes((int)M41T62_addr, (char *)rtc_buf, 8);
+ rtc_buf[0] = M41T62_REG_SSEC;
+ rtc_buf[M41T62_REG_SSEC + 1] = 0;
+ rtc_buf[M41T62_REG_YEAR + 1] = bin2bcd((uint8_t)(tm->rtc_year % 100));
+ rtc_buf[M41T62_REG_MON + 1] = bin2bcd(tm->rtc_month) | (rtc_buf[M41T62_REG_MON] & ~0x1f);
+ rtc_buf[M41T62_REG_DAY + 1] = bin2bcd(tm->rtc_date) | (rtc_buf[M41T62_REG_DAY] & ~0x3f);
+ rtc_buf[M41T62_REG_WDAY + 1] = (tm->rtc_weekday & 0x07) | (rtc_buf[M41T62_REG_WDAY] & ~0x07);
+ rtc_buf[M41T62_REG_HOUR + 1] = bin2bcd(tm->rtc_hours) | (rtc_buf[M41T62_REG_HOUR] & ~0x3f);
+ rtc_buf[M41T62_REG_MIN + 1] = bin2bcd(tm->rtc_minutes) | (rtc_buf[M41T62_REG_MIN] & ~0x7f);
+ rtc_buf[M41T62_REG_SEC + 1] = bin2bcd(tm->rtc_seconds) | (rtc_buf[M41T62_REG_SEC] & ~0x7f);
+ i2c_write_n_bytes((int)M41T62_addr, (char *)rtc_buf, 9);
+}
+
+void M41T62::set_sq_wave (uint8_t sqw_dt) {
+uint8_t eep_dt[2];
+
+ eep_dt[0] = M41T62_REG_WDAY;
+ i2c_write_n_bytes((int)M41T62_addr, (char *)eep_dt, 1);
+ i2c_read_n_bytes((int)M41T62_addr, (char *)eep_dt, 1);
+ eep_dt[1] = (eep_dt[0] & 0x07) | (sqw_dt << 4);
+ eep_dt[0] = M41T62_REG_WDAY;
+ i2c_write_n_bytes((int)M41T62_addr, (char *)rtc_buf, 2);
+}
+
+void M41T62::i2c_read_n_bytes (int addr, char *dt, int n) {
+ i2c.read(addr, dt, n);
+}
+
+void M41T62::i2c_write_n_bytes (int addr, char *dt, int n) {
+ i2c.write(addr, dt, n);
+}
+
+uint8_t M41T62::bin2bcd (uint8_t dt){
+uint8_t bcdhigh = 0;
+
+ while (dt >= 10) {
+ bcdhigh++;
+ dt -= 10;
+ }
+ return ((uint8_t)(bcdhigh << 4) | dt);
+}
+
+uint8_t M41T62::bcd2bin (uint8_t dt){
+uint8_t tmp = 0;
+
+ tmp = ((uint8_t)(dt & (uint8_t)0xf0) >> (uint8_t)0x4) * 10;
+ return (tmp + (dt & (uint8_t)0x0f));
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/m41t62_rtc.h Sat Jun 21 04:37:56 2014 +0000
@@ -0,0 +1,84 @@
+/*
+ * mbed library program
+ * Control M41T62 RTC Module
+ *
+ * Copyright (c) 2014 Kenji Arai / JH1PJL
+ * http://www.page.sannet.ne.jp/kenjia/index.html
+ * http://mbed.org/users/kenjiArai/
+ * Created: June 21st, 2014
+ * Revised: June 21st, 2014
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
+ * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef M41T62_H
+#define M41T62_H
+
+#include "mbed.h"
+
+// RTC STmicro M41T62
+// Address b7=1,b6=1,b5=0,b4=1,b3=0,b2=0,b1=0, b0=R/W
+#define M41T62ADDR 0xd0 // No other choice
+
+#define RTC_Wk_Monday ((uint8_t)0x01)
+#define RTC_Wk_Tuesday ((uint8_t)0x02)
+#define RTC_Wk_Wednesday ((uint8_t)0x03)
+#define RTC_Wk_Thursday ((uint8_t)0x04)
+#define RTC_Wk_Friday ((uint8_t)0x05)
+#define RTC_Wk_Saturday ((uint8_t)0x06)
+#define RTC_Wk_Sunday ((uint8_t)0x07)
+
+#define RTC_SQW_NONE ((uint8_t)0x0)
+#define RTC_SQW_32KHZ ((uint8_t)0x1)
+#define RTC_SQW_8KHZ ((uint8_t)0x2)
+#define RTC_SQW_4KHZ ((uint8_t)0x3)
+#define RTC_SQW_2KHZ ((uint8_t)0x4)
+#define RTC_SQW_1KHZ ((uint8_t)0x5)
+#define RTC_SQW_512HZ ((uint8_t)0x6)
+#define RTC_SQW_256HZ ((uint8_t)0x7)
+#define RTC_SQW_128HZ ((uint8_t)0x8)
+#define RTC_SQW_64HZ ((uint8_t)0x9)
+#define RTC_SQW_32HZ ((uint8_t)0xa)
+#define RTC_SQW_16HZ ((uint8_t)0xb)
+#define RTC_SQW_8HZ ((uint8_t)0xc)
+#define RTC_SQW_4HZ ((uint8_t)0xd)
+#define RTC_SQW_2HZ ((uint8_t)0xe)
+#define RTC_SQW_1HZ ((uint8_t)0xf)
+
+typedef struct { // BCD format
+ uint8_t rtc_seconds;
+ uint8_t rtc_minutes;
+ uint8_t rtc_hours;
+ uint8_t rtc_weekday;
+ uint8_t rtc_date;
+ uint8_t rtc_month;
+ uint16_t rtc_year;
+}rtc_time;
+
+class M41T62 {
+public:
+ M41T62(PinName p_sda, PinName p_scl);
+ M41T62(I2C& p_i2c);
+
+ void read_rtc(rtc_time *);
+ void write_rtc(rtc_time *);
+ void set_sq_wave(uint8_t);
+
+protected:
+ void i2c_read_n_bytes(int, char*, int);
+ void i2c_write_n_bytes(int, char*, int);
+ uint8_t bin2bcd(uint8_t);
+ uint8_t bcd2bin(uint8_t);
+
+ I2C i2c;
+
+private:
+ uint8_t M41T62_addr;
+ uint8_t rtc_buf[8 + 2]; // buffer for RTC
+};
+
+#endif // M41T62_H
\ No newline at end of file