2 #include <lunaix/clock.h>
3 #include <lunaix/fs/twifs.h>
4 #include <lunaix/spike.h>
5 #include <lunaix/timer.h>
7 #include <klibc/string.h>
9 static volatile time_t sys_time;
12 clock_systime_counter(void* arg);
15 __clock_read_systime(struct twimap* map)
17 time_t save = sys_time;
18 twimap_printf(map, "%u", save);
22 __clock_read_datetime(struct twimap* map)
27 "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
37 __clock_read_unix(struct twimap* map)
41 twimap_printf(map, "%u", clock_tounixtime(&dt));
47 struct twifs_node* root = twifs_dir_node(NULL, "clock");
50 map = twifs_mapping(root, NULL, "systime");
51 map->read = __clock_read_systime;
53 map = twifs_mapping(root, NULL, "unix");
54 map->read = __clock_read_unix;
56 map = twifs_mapping(root, NULL, "datetime");
57 map->read = __clock_read_datetime;
63 if (!timer_context()) {
64 panick("Systimer not initialized");
68 timer_run_ms(1, clock_systime_counter, NULL, TIMER_MODE_PERIODIC);
70 clock_build_mapping();
74 clock_systime_counter(void* arg)
80 clock_datatime_eq(datetime_t* a, datetime_t* b)
82 return a->year == b->year && a->month == b->month && a->day == b->day &&
83 a->weekday == b->weekday && a->minute == b->minute &&
84 a->second == b->second;
88 clock_walltime(datetime_t* datetime)
93 while (rtc_read_reg(RTC_REG_A) & 0x80)
95 memcpy(¤t, datetime, sizeof(datetime_t));
97 datetime->year = rtc_read_reg(RTC_REG_YRS);
98 datetime->month = rtc_read_reg(RTC_REG_MTH);
99 datetime->day = rtc_read_reg(RTC_REG_DAY);
100 datetime->weekday = rtc_read_reg(RTC_REG_WDY);
101 datetime->hour = rtc_read_reg(RTC_REG_HRS);
102 datetime->minute = rtc_read_reg(RTC_REG_MIN);
103 datetime->second = rtc_read_reg(RTC_REG_SEC);
104 } while (!clock_datatime_eq(datetime, ¤t));
106 u8_t regbv = rtc_read_reg(RTC_REG_B);
108 // Convert from bcd to binary when needed
109 if (!RTC_BIN_ENCODED(regbv)) {
110 datetime->year = bcd2dec(datetime->year);
111 datetime->month = bcd2dec(datetime->month);
112 datetime->day = bcd2dec(datetime->day);
113 datetime->hour = bcd2dec(datetime->hour);
114 datetime->minute = bcd2dec(datetime->minute);
115 datetime->second = bcd2dec(datetime->second);
119 if (!RTC_24HRS_ENCODED(regbv) && (datetime->hour >> 7)) {
120 datetime->hour = 12 + (datetime->hour & 0x80);
123 datetime->year += RTC_CURRENT_CENTRY * 100;
131 return clock_tounixtime(&dt);