X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/ea77b9c3fc7fb9bf9d7f9604fc187c8049212a2a..b60166b327a9108b07e3069fa6568a451529ffd9:/lunaix-os/kernel/time/clock.c diff --git a/lunaix-os/kernel/time/clock.c b/lunaix-os/kernel/time/clock.c index 0f04575..3170731 100644 --- a/lunaix-os/kernel/time/clock.c +++ b/lunaix-os/kernel/time/clock.c @@ -1,19 +1,15 @@ -#include #include +#include #include #include -#include -static volatile time_t sys_time; - -void -clock_systime_counter(void* arg); +#include void __clock_read_systime(struct twimap* map) { - time_t save = sys_time; - twimap_printf(map, "%u", save); + ticks_t sys_time = clock_systime(); + twimap_printf(map, "%u", sys_time); } void @@ -34,9 +30,7 @@ __clock_read_datetime(struct twimap* map) void __clock_read_unix(struct twimap* map) { - datetime_t dt; - clock_walltime(&dt); - twimap_printf(map, "%u", clock_tounixtime(&dt)); + twimap_printf(map, "%u", clock_unixtime()); } void @@ -54,83 +48,50 @@ clock_build_mapping() map = twifs_mapping(root, NULL, "datetime"); map->read = __clock_read_datetime; } +EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping); -void -clock_init() -{ - if (!timer_context()) { - panick("Systimer not initialized"); - } - - // 系统计时器每毫秒累加。 - timer_run_ms(1, clock_systime_counter, NULL, TIMER_MODE_PERIODIC); - - clock_build_mapping(); -} - -void -clock_systime_counter(void* arg) -{ - sys_time++; -} - -int -clock_datatime_eq(datetime_t* a, datetime_t* b) +time_t +clock_unixtime() { - 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; + datetime_t dt; + hwrtc_walltime(&dt); + return datetime_tounix(&dt); } -void -clock_walltime(datetime_t* datetime) +time_t +clock_systime() { - 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); + if (!systimer) { + return 0; } - // To 24 hour format - if (!RTC_24HRS_ENCODED(regbv) && (datetime->hour >> 7)) { - datetime->hour = (12 + datetime->hour & 0x80); + ticks_t t = hwtimer_current_systicks(); + ticks_t tu = systimer->running_freq / 1000; + + if (unlikely(!tu)) { + return t; } - datetime->year += RTC_CURRENT_CENTRY * 100; + return t / (tu); } -time_t -clock_unixtime() +void +clock_walltime(datetime_t* datetime) { - datetime_t dt; - clock_walltime(&dt); - return clock_tounixtime(&dt); + sysrtc->get_walltime(sysrtc, datetime); } -time_t -clock_systime() +void +clock_init() { - return sys_time; + 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