Kenji Arai / RX8025NB

Dependents:   TYBLE16_simple_data_logger Check_external_RTC

Files at this revision

API Documentation at this revision

Comitter:
kenjiArai
Date:
Fri Jun 05 10:43:42 2015 +0000
Child:
1:817e81048235
Commit message:
RX-8025NB Real Time Clock Module by EPSON, 1st revision.

Changed in this revision

RX8025NB.cpp Show annotated file Show diff for this revision Revisions of this file
RX8025NB.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RX8025NB.cpp	Fri Jun 05 10:43:42 2015 +0000
@@ -0,0 +1,157 @@
+/*
+ * mbed library program
+ *  Control RX-8025NB Real Time Clock Module
+ *  EPSON
+ *
+ * Copyright (c) 2015 Kenji Arai / JH1PJL
+ *  http://www.page.sannet.ne.jp/kenjia/index.html
+ *  http://mbed.org/users/kenjiArai/
+ *      Created: June       3rd, 2015
+ *      Revised: June       5th, 2015
+ *
+ * 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.
+ */
+
+#include "mbed.h"
+#include "RX8025NB.h"
+
+RX8025::RX8025 (PinName p_sda, PinName p_scl) : _i2c(p_sda, p_scl)
+{
+    RX8025_addr = RX8025ADDR;
+}
+
+RX8025::RX8025 (I2C& p_i2c) : _i2c(p_i2c)
+{
+    RX8025_addr = RX8025ADDR;
+}
+
+/////////////// Read RTC data /////////////////////////////
+void RX8025::get_time_rtc (tm *t)
+{
+    read_rtc_std(t);
+}
+
+void RX8025::read_rtc_std (tm *t)
+{
+    rtc_time time;
+
+    read_rtc_direct(&time);
+    t->tm_sec  = time.rtc_seconds;
+    t->tm_min  = time.rtc_minutes;
+    t->tm_hour = time.rtc_hours;
+    t->tm_mday = time.rtc_date;
+    if ( time.rtc_weekday == RTC_Wk_Sunday) {
+        t->tm_wday = 0; // Sun is not 7 but 0
+    } else {
+        t->tm_wday = time.rtc_weekday;
+    }
+    t->tm_mon  = time.rtc_month - 1;
+    t->tm_year = time.rtc_year_raw + 100;
+    t->tm_isdst= 0;
+}
+
+/////////////// Write data to RTC /////////////////////////
+void RX8025::set_time_rtc (tm *t)
+{
+    write_rtc_std(t);
+}
+
+void RX8025::write_rtc_std (tm *t)
+{
+    rtc_time time;
+
+    time.rtc_seconds  = t->tm_sec;
+    time.rtc_minutes  = t->tm_min;
+    time.rtc_hours    = t->tm_hour;
+    time.rtc_date     = t->tm_mday;
+    if ( t->tm_wday == 0) {
+        time.rtc_weekday = RTC_Wk_Sunday;
+    } else {
+        time.rtc_weekday = t->tm_wday;
+    }
+    time.rtc_month    = t->tm_mon + 1;
+    time.rtc_year_raw = t->tm_year - 100;
+    write_rtc_direct(&time);
+}
+
+/////////////// Read/Write specific register //////////////
+uint8_t RX8025::read_reg_byte(uint8_t reg)
+{
+    uint8_t dt[2];
+
+    dt[0] = reg;
+    _i2c.write((int)RX8025_addr, (char *)dt, 1, true);
+    _i2c.read((int)RX8025_addr, (char *)dt, 1, false);
+    return dt[0];
+}
+
+uint8_t RX8025::write_reg_byte(uint8_t reg, uint8_t data)
+{
+    uint8_t dt[2];
+
+    dt[0] = reg;
+    dt[1] = data;
+    _i2c.write((int)RX8025_addr, (char *)dt, 2, false);
+    dt[1] = read_reg_byte(reg);
+    return dt[1];
+}
+
+/////////////// I2C Freq. /////////////////////////////////
+void RX8025::frequency(int hz)
+{
+    _i2c.frequency(hz);
+}
+
+/////////////// Read/Write RTC another format /////////////
+void RX8025::read_rtc_direct (rtc_time *tm)
+{
+    uint8_t dt;
+
+    dt = RX8025_REG_SEC;
+    _i2c.write((int)RX8025_addr, (char *)dt, 1, true);
+    _i2c.read((int)RX8025_addr, (char *)rtc_buf, RX8025_REG_CONTL2 + 1, false);
+    tm->rtc_seconds = bcd2bin(rtc_buf[RX8025_REG_SEC]  & 0x7f);
+    tm->rtc_minutes = bcd2bin(rtc_buf[RX8025_REG_MIN]  & 0x7f);
+    tm->rtc_hours   = bcd2bin(rtc_buf[RX8025_REG_HOUR] & 0x3f);
+    tm->rtc_date    = bcd2bin(rtc_buf[RX8025_REG_DAY]  & 0x3f);
+    tm->rtc_weekday = rtc_buf[RX8025_REG_WDAY] & 0x07;
+    tm->rtc_month   = bcd2bin(rtc_buf[RX8025_REG_MON]  & 0x1f);
+    tm->rtc_year_raw= bcd2bin(rtc_buf[RX8025_REG_YEAR]);
+    tm->rtc_year = tm->rtc_year_raw + 100 + 1900;
+}
+
+void RX8025::write_rtc_direct (rtc_time *tm)
+{
+    rtc_buf[RX8025_REG_YEAR + 1] = bin2bcd(tm->rtc_year_raw);
+    rtc_buf[RX8025_REG_MON + 1]  = bin2bcd(tm->rtc_month);
+    rtc_buf[RX8025_REG_WDAY + 1] = (tm->rtc_weekday & 0x07);
+    rtc_buf[RX8025_REG_DAY + 1]  = bin2bcd(tm->rtc_date);
+    rtc_buf[RX8025_REG_HOUR + 1] = bin2bcd(tm->rtc_hours);
+    rtc_buf[RX8025_REG_MIN + 1]  = bin2bcd(tm->rtc_minutes);
+    rtc_buf[RX8025_REG_SEC + 1]  = bin2bcd(tm->rtc_seconds);
+    rtc_buf[0] = RX8025_REG_SEC;
+    _i2c.write((int)RX8025_addr, (char *)rtc_buf, 8, false);
+}
+
+uint8_t RX8025::bin2bcd (uint8_t dt)
+{
+    uint8_t bcdhigh = 0;
+
+    while (dt >= 10) {
+        bcdhigh++;
+        dt -= 10;
+    }
+    return  ((uint8_t)(bcdhigh << 4) | dt);
+}
+
+uint8_t RX8025::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/RX8025NB.h	Fri Jun 05 10:43:42 2015 +0000
@@ -0,0 +1,171 @@
+/*
+ * mbed library program
+ *  Control RX-8025NB Real Time Clock Module
+ *  EPSON
+ *
+ * Copyright (c) 2015 Kenji Arai / JH1PJL
+ *  http://www.page.sannet.ne.jp/kenjia/index.html
+ *  http://mbed.org/users/kenjiArai/
+ *      Created: June       3rd, 2015
+ *      Revised: June       5th, 2015
+ *
+ * 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 ----------------------------------------------------------------------
+ * Original Information
+ *  http://www.epsondevice.com/docs/qd/ja/DownloadServlet?id=ID000515
+ * Sensor board
+ *  http://akizukidenshi.com/catalog/g/gK-08585/
+ */
+
+#ifndef RX8025_H
+#define RX8025_H
+
+#include "mbed.h"
+
+// RTC EPSON RX8025
+//  7bit address = 0b1010001(No other choice)
+#define RX8025ADDR  (0x32 << 1)
+
+#define RTC_Wk_Sunday          ((uint8_t)0x00)
+#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)
+
+// Register definition
+#define RX8025_REG_SEC         0
+#define RX8025_REG_MIN         1
+#define RX8025_REG_HOUR        2
+#define RX8025_REG_WDAY        3
+#define RX8025_REG_DAY         4
+#define RX8025_REG_MON         5
+#define RX8025_REG_YEAR        6
+#define RX8025_REG_OFFSET      7
+#define RX8025_REG_ALARMW_MIN  8
+#define RX8025_REG_ALARMW_HOUR 9
+#define RX8025_REG_ALARMW_WDAY 0xa
+#define RX8025_REG_ALARMD_MIN  0xb
+#define RX8025_REG_ALARMD_HOUR 0xc
+#define RX8025_REG_RESERVED    0xd
+#define RX8025_REG_CONTL1      0xe
+#define RX8025_REG_CONTL2      0xf
+
+// Buffer size
+#define RTC_BUF_SIZ            (RX8025_REG_CONTL2 + 5)
+
+/** Interface for RTC (I2C Interface)  EPSON RX8025
+ *
+ *  Standalone type RTC via I2C interface
+ *
+ * @code
+ * #include "mbed.h"
+ *
+ * // I2C Communication
+ *  RX8025     RX8025(dp5,dp27); // RTC(RX8025) SDA, SCL (Fixed address)
+ * // If you connected I2C line not only this device but also other devices,
+ * //     you need to declare following method.
+ *  I2C        i2c(dp5,dp27);    // SDA, SCL
+ *  RX8025     RX8025(i2c);      // RTC(RX8025) (Fixed address)
+ *
+ * int main() {
+ * tm t;
+ * time_t seconds;
+ * char buf[40];
+ *
+ *   RX8025.get_time_rtc(&t);   // read RTC data
+ *   seconds = mktime(&t);
+ *   strftime(buf, 40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds));
+ *   printf("Date: %s\r\n", buf);
+ * }
+ * @endcode
+ */
+
+class RX8025
+{
+public:
+
+    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;
+        uint8_t rtc_year_raw;
+        uint16_t rtc_year;
+    } rtc_time;
+
+    /** Configure data pin
+      * @param data SDA and SCL pins
+      */
+    RX8025(PinName p_sda, PinName p_scl);
+
+    /** Configure data pin (with other devices on I2C line)
+      * @param I2C previous definition
+      */
+    RX8025(I2C& p_i2c);
+
+    /** Read RTC data with Standard C "struct tm" format
+      * @param tm (data save area)
+      * @return none but all data in tm
+      */
+    void read_rtc_std(tm *);
+    void get_time_rtc(tm *);
+
+    /** Write data to RTC data with Standard C "struct tm" format
+      * @param tm (save writing data)
+      * @return none but all data in tm
+      */
+    void write_rtc_std(tm *);
+    void set_time_rtc(tm *);
+
+    /** Read one byte from specific register
+      * @param register address
+      * @return register data
+      */
+    uint8_t read_reg_byte(uint8_t reg);
+
+    /** Write one byte into specific register
+      * @param register address, data
+      * @return register saved data
+      */
+    uint8_t write_reg_byte(uint8_t reg, uint8_t data);
+
+    /** Read RTC data with own format
+      * @param tm (data save area)
+      * @return none but all data in tm
+      */
+    void read_rtc_direct(rtc_time *);
+
+    /** Read RTC data with own format
+      * @param tm (save writing data)
+      * @return none but all data in tm
+      */
+    void write_rtc_direct(rtc_time *);
+
+    /** Set I2C clock frequency
+      * @param freq.
+      * @return none
+      */
+    void frequency(int hz);
+
+protected:
+    uint8_t bin2bcd(uint8_t);
+    uint8_t bcd2bin(uint8_t);
+
+    I2C _i2c;
+
+private:
+    uint8_t RX8025_addr;
+    uint8_t rtc_buf[RTC_BUF_SIZ];   // buffer for RTC
+};
+
+#endif      // RX8025_H