#include <lunaix/clock.h>
-#include <lunaix/timer.h>
-#include <hal/rtc.h>
+#include <lunaix/device.h>
+#include <lunaix/fs/twifs.h>
#include <lunaix/spike.h>
-static volatile time_t sys_time;
+#include <klibc/string.h>
-void clock_systime_counter(void* arg);
-
-void
-clock_init() {
- if (!timer_context()) {
- panick("Systimer not initialized");
- }
-
- // 系统计时器每毫秒累加。
- timer_run_ms(1, clock_systime_counter, NULL, TIMER_MODE_PERIODIC);
+static void
+__twimap_read_systime(struct twimap* map)
+{
+ ticks_t sys_time = clock_systime();
+ twimap_printf(map, "%u", sys_time);
}
-void clock_systime_counter(void* arg) {
- sys_time++;
+static void
+__twimap_read_datetime(struct twimap* map)
+{
+ 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) {
- 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;
+static void
+__twimap_read_unixtime(struct twimap* map)
+{
+ twimap_printf(map, "%u", clock_unixtime());
}
-void
-clock_walltime(datetime_t* datetime)
+time_t
+clock_unixtime()
{
- datetime_t current;
-
- do
- {
- while (rtc_read_reg(RTC_REG_A) & 0x80);
- memcpy(¤t, 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, ¤t));
-
- uint8_t regbv = rtc_read_reg(RTC_REG_B);
+ datetime_t dt;
+ hwrtc_walltime(&dt);
+ return datetime_tounix(&dt);
+}
- // 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);
+time_t
+clock_systime()
+{
+ if (!systimer) {
+ return 0;
}
+ ticks_t t = hwtimer_current_systicks();
+ ticks_t tu = systimer->running_freq / 1000;
- // To 24 hour format
- if (!RTC_24HRS_ENCODED(regbv) && (datetime->hour >> 7)) {
- datetime->hour = (12 + datetime->hour & 0x80);
+ if (unlikely(!tu)) {
+ return t;
}
- datetime->year += RTC_CURRENT_CENTRY * 100;
+ return t / (tu);
}
+void
+clock_walltime(datetime_t* datetime)
+{
+ sysrtc->ops->get_walltime(sysrtc, datetime);
+}
+
+static void
+clock_build_mapping()
+{
+ struct twifs_node* root;
+ struct twimap* map;
-time_t
-clock_systime() {
- return sys_time;
-}
\ No newline at end of file
+ root = twifs_dir_node(NULL, "clock");
+
+ twimap_export_value(root, systime, FSACL_ugR, NULL);
+ twimap_export_value(root, unixtime, FSACL_ugR, NULL);
+ twimap_export_value(root, datetime, FSACL_ugR, NULL);
+}
+EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping);