#include <hal/rtc.h>
#include <lunaix/clock.h>
+#include <lunaix/fs/twifs.h>
#include <lunaix/spike.h>
#include <lunaix/timer.h>
+#include <klibc/string.h>
+
static volatile time_t sys_time;
void
clock_systime_counter(void* arg);
+void
+__clock_read_systime(struct twimap* map)
+{
+ time_t save = sys_time;
+ twimap_printf(map, "%u", save);
+}
+
+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);
+}
+
+void
+__clock_read_unix(struct twimap* map)
+{
+ datetime_t dt;
+ clock_walltime(&dt);
+ twimap_printf(map, "%u", clock_tounixtime(&dt));
+}
+
+void
+clock_build_mapping()
+{
+ 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;
+}
+
void
clock_init()
{
// 系统计时器每毫秒累加。
timer_run_ms(1, clock_systime_counter, NULL, TIMER_MODE_PERIODIC);
+
+ clock_build_mapping();
}
void
datetime->second = rtc_read_reg(RTC_REG_SEC);
} while (!clock_datatime_eq(datetime, ¤t));
- uint8_t regbv = rtc_read_reg(RTC_REG_B);
+ u8_t regbv = rtc_read_reg(RTC_REG_B);
// Convert from bcd to binary when needed
if (!RTC_BIN_ENCODED(regbv)) {
// To 24 hour format
if (!RTC_24HRS_ENCODED(regbv) && (datetime->hour >> 7)) {
- datetime->hour = (12 + datetime->hour & 0x80);
+ datetime->hour = 12 + (datetime->hour & 0x80);
}
datetime->year += RTC_CURRENT_CENTRY * 100;