X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/80890b99fec2630ef0a1a0805d894c3d86c16506..b60166b327a9108b07e3069fa6568a451529ffd9:/lunaix-os/kernel/time/clock.c?ds=sidebyside diff --git a/lunaix-os/kernel/time/clock.c b/lunaix-os/kernel/time/clock.c index e862f79..3170731 100644 --- a/lunaix-os/kernel/time/clock.c +++ b/lunaix-os/kernel/time/clock.c @@ -1,78 +1,97 @@ #include -#include -#include +#include +#include #include -static volatile time_t sys_time; - -void clock_systime_counter(void* arg); +#include void -clock_init() { - if (!timer_context()) { - panick("Systimer not initialized"); - } - - // 系统计时器每毫秒累加。 - timer_run_ms(1, clock_systime_counter, NULL, TIMER_MODE_PERIODIC); +__clock_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++; +void +__clock_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; +void +__clock_read_unix(struct twimap* map) +{ + 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(¤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); - - // 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; + + map = twifs_mapping(root, NULL, "systime"); + map->read = __clock_read_systime; + + map = twifs_mapping(root, NULL, "unix"); + map->read = __clock_read_unix; + + 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; + hwrtc_walltime(&dt); + return datetime_tounix(&dt); +} + +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->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; + } -time_t -clock_systime() { - return sys_time; + pos->init(pos); + } } \ No newline at end of file