userspace fun: maze game and a new device to support it
[lunaix-os.git] / lunaix-os / kernel / time / clock.c
index 15a6a32aaf5fbf67527457578d30ebf52f46f18c..3170731b842c340398227f33f898a61e2baf4060 100644 (file)
@@ -1,87 +1,97 @@
-#include <hal/rtc.h>
 #include <lunaix/clock.h>
+#include <lunaix/device.h>
+#include <lunaix/fs/twifs.h>
 #include <lunaix/spike.h>
-#include <lunaix/timer.h>
 
-static volatile time_t sys_time;
+#include <klibc/string.h>
 
 void
-clock_systime_counter(void* arg);
-
-void
-clock_init()
+__clock_read_systime(struct twimap* map)
 {
-    if (!timer_context()) {
-        panick("Systimer not initialized");
-    }
-
-    // 系统计时器每毫秒累加。
-    timer_run_ms(1, clock_systime_counter, NULL, TIMER_MODE_PERIODIC);
+    ticks_t sys_time = clock_systime();
+    twimap_printf(map, "%u", sys_time);
 }
 
 void
-clock_systime_counter(void* arg)
+__clock_read_datetime(struct twimap* map)
 {
-    sys_time++;
+    datetime_t dt;
+    clock_walltime(&dt);
+    twimap_printf(map,
+                  "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
+                  dt.year,
+                  dt.month,
+                  dt.day,
+                  dt.hour,
+                  dt.minute,
+                  dt.second);
 }
 
-int
-clock_datatime_eq(datetime_t* a, datetime_t* b)
+void
+__clock_read_unix(struct twimap* map)
 {
-    return a->year == b->year && a->month == b->month && a->day == b->day &&
-           a->weekday == b->weekday && a->minute == b->minute &&
-           a->second == b->second;
+    twimap_printf(map, "%u", clock_unixtime());
 }
 
 void
-clock_walltime(datetime_t* datetime)
+clock_build_mapping()
 {
-    datetime_t current;
-
-    do {
-        while (rtc_read_reg(RTC_REG_A) & 0x80)
-            ;
-        memcpy(&current, datetime, sizeof(datetime_t));
-
-        datetime->year = rtc_read_reg(RTC_REG_YRS);
-        datetime->month = rtc_read_reg(RTC_REG_MTH);
-        datetime->day = rtc_read_reg(RTC_REG_DAY);
-        datetime->weekday = rtc_read_reg(RTC_REG_WDY);
-        datetime->hour = rtc_read_reg(RTC_REG_HRS);
-        datetime->minute = rtc_read_reg(RTC_REG_MIN);
-        datetime->second = rtc_read_reg(RTC_REG_SEC);
-    } while (!clock_datatime_eq(datetime, &current));
-
-    uint8_t regbv = rtc_read_reg(RTC_REG_B);
-
-    // Convert from bcd to binary when needed
-    if (!RTC_BIN_ENCODED(regbv)) {
-        datetime->year = bcd2dec(datetime->year);
-        datetime->month = bcd2dec(datetime->month);
-        datetime->day = bcd2dec(datetime->day);
-        datetime->hour = bcd2dec(datetime->hour);
-        datetime->minute = bcd2dec(datetime->minute);
-        datetime->second = bcd2dec(datetime->second);
-    }
+    struct twifs_node* root = twifs_dir_node(NULL, "clock");
+    struct twimap* map;
 
-    // To 24 hour format
-    if (!RTC_24HRS_ENCODED(regbv) && (datetime->hour >> 7)) {
-        datetime->hour = (12 + datetime->hour & 0x80);
-    }
+    map = twifs_mapping(root, NULL, "systime");
+    map->read = __clock_read_systime;
+
+    map = twifs_mapping(root, NULL, "unix");
+    map->read = __clock_read_unix;
 
-    datetime->year += RTC_CURRENT_CENTRY * 100;
+    map = twifs_mapping(root, NULL, "datetime");
+    map->read = __clock_read_datetime;
 }
+EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping);
 
 time_t
 clock_unixtime()
 {
     datetime_t dt;
-    clock_walltime(&dt);
-    return clock_tounixtime(&dt);
+    hwrtc_walltime(&dt);
+    return datetime_tounix(&dt);
 }
 
 time_t
 clock_systime()
 {
-    return sys_time;
+    if (!systimer) {
+        return 0;
+    }
+
+    ticks_t t = hwtimer_current_systicks();
+    ticks_t tu = systimer->running_freq / 1000;
+
+    if (unlikely(!tu)) {
+        return t;
+    }
+
+    return t / (tu);
+}
+
+void
+clock_walltime(datetime_t* datetime)
+{
+    sysrtc->get_walltime(sysrtc, datetime);
+}
+
+void
+clock_init()
+{
+    int idx = 0;
+    struct device_def* pos;
+    foreach_exported_device_of(load_timedev, idx, pos)
+    {
+        if (pos->class.device != DEV_RTC) {
+            continue;
+        }
+
+        pos->init(pos);
+    }
 }
\ No newline at end of file