IoT Morioka private / us_ticker_rtc

Files at this revision

API Documentation at this revision

Comitter:
YJKIYOKAWA
Date:
Tue Oct 11 05:11:40 2016 +0000
Commit message:
us_ticker base rtc

Changed in this revision

us_ticker_rtc.c Show annotated file Show diff for this revision Revisions of this file
us_ticker_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/us_ticker_rtc.c	Tue Oct 11 05:11:40 2016 +0000
@@ -0,0 +1,42 @@
+/* 
+ * Copyright (c) 2016 Delta Electronics(Japan), Inc
+ *
+ */
+#include "us_ticker_rtc.h"
+#include "us_ticker_api.h"
+
+static time_t  initTime = 0;
+static uint32_t init_us_tick = 0;
+
+void us_ticker_rtc_init(void)
+{
+    initTime = 0;
+    init_us_tick = us_ticker_read();
+}
+    
+void us_ticker_rtc_free(void)
+{
+}
+
+int us_ticker_rtc_isenabled(void)
+{
+    return 1;
+}
+
+time_t us_ticker_rtc_read(void)
+{
+    uint32_t now_us_tick = us_ticker_read();
+    
+    if (now_us_tick > init_us_tick) {
+        return initTime + ((now_us_tick - init_us_tick) / 1000000);
+    } else {
+        return initTime + ((0xFFFFFFFF - init_us_tick + now_us_tick + 1) / 1000000);
+    }
+}
+
+void us_ticker_rtc_write(time_t t)
+{
+    initTime = t;
+    init_us_tick = us_ticker_read();
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/us_ticker_rtc.h	Tue Oct 11 05:11:40 2016 +0000
@@ -0,0 +1,69 @@
+/* 
+ * Copyright (c) 2016 Delta Electronics(Japan), Inc
+ *
+ */
+#ifndef US_TICKER_RTC_H
+#define US_TICKER_RTC_H
+
+#include <time.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** 
+ *
+ * Provides Real-Time Clock (RTC) functions based on us_ticker.
+ * 
+ * us_ticker count up 32 bit value in microseconds.
+ * So it is overflow in 4294.967295 seconds = 71 minutes.
+ * Need to call set_time() before overflow.
+ *
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "us_ticker_rtc.h"
+ *
+ * int main() {
+ *
+ *     //  Attach us_ticker base rtc functions to be used for the C time functions
+ *     attach_rtc(us_ticker_rtc_read, us_ticker_rtc_write, us_ticker_rtc_init, us_ticker_rtc_isenabled);
+ *
+ *     time_t init_seconds = 1256729737;  // Set RTC time to Wed, 28 Oct 2009 11:35:37
+ *     set_time(init_seconds);
+ *
+ *     while(1) {
+ *         time_t seconds = time(NULL);
+ *         if ((seconds - init_seconds) > 3600)
+ *         {
+ *             set_time(seconds);
+ *             init_seconds = seconds;
+ *         }
+ *
+ *         printf("Time as seconds since January 1, 1970 = %d\n", seconds);
+ *
+ *         printf("Time as a basic string = %s", ctime(&seconds));
+ *
+ *         char buffer[32];
+ *         strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
+ *         printf("Time as a custom formatted string = %s", buffer);
+ *
+ *         wait(1);
+ *     }
+ * }
+ * @endcode
+ */
+
+void us_ticker_rtc_init(void);
+void us_ticker_rtc_free(void);
+int us_ticker_rtc_isenabled(void);
+
+time_t us_ticker_rtc_read(void);
+void us_ticker_rtc_write(time_t t);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif