1 #include <lunaix/clock.h>
2 #include <lunaix/timer.h>
4 #include <lunaix/spike.h>
6 static volatile time_t sys_time;
8 void clock_systime_counter(void* arg);
12 if (!timer_context()) {
13 panick("Systimer not initialized");
17 timer_run_ms(1, clock_systime_counter, NULL, TIMER_MODE_PERIODIC);
20 void clock_systime_counter(void* arg) {
25 clock_datatime_eq(datetime_t* a, datetime_t* b) {
26 return a->year == b->year &&
27 a->month == b->month &&
29 a->weekday == b->weekday &&
30 a->minute == b->minute &&
31 a->second == b->second;
35 clock_walltime(datetime_t* datetime)
41 while (rtc_read_reg(RTC_REG_A) & 0x80);
42 memcpy(¤t, datetime, sizeof(datetime_t));
44 datetime->year = rtc_read_reg(RTC_REG_YRS);
45 datetime->month = rtc_read_reg(RTC_REG_MTH);
46 datetime->day = rtc_read_reg(RTC_REG_DAY);
47 datetime->weekday = rtc_read_reg(RTC_REG_WDY);
48 datetime->hour = rtc_read_reg(RTC_REG_HRS);
49 datetime->minute = rtc_read_reg(RTC_REG_MIN);
50 datetime->second = rtc_read_reg(RTC_REG_SEC);
51 } while (!clock_datatime_eq(datetime, ¤t));
53 uint8_t regbv = rtc_read_reg(RTC_REG_B);
55 // Convert from bcd to binary when needed
56 if (!RTC_BIN_ENCODED(regbv)) {
57 datetime->year = bcd2dec(datetime->year);
58 datetime->month = bcd2dec(datetime->month);
59 datetime->day = bcd2dec(datetime->day);
60 datetime->hour = bcd2dec(datetime->hour);
61 datetime->minute = bcd2dec(datetime->minute);
62 datetime->second = bcd2dec(datetime->second);
67 if (!RTC_24HRS_ENCODED(regbv) && (datetime->hour >> 7)) {
68 datetime->hour = (12 + datetime->hour & 0x80);
71 datetime->year += RTC_CURRENT_CENTRY * 100;