From 6f1b50a4cc6c232ee505a543f006abb1c6cd33cf Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Fri, 19 Feb 2021 23:41:56 -0500 Subject: rtc fixes, time command --- src/user/include/knob/time.h | 13 ++++++++++ src/user/knob/time.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ src/user/time/time.c | 24 +++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 src/user/include/knob/time.h create mode 100644 src/user/knob/time.c create mode 100644 src/user/time/time.c (limited to 'src/user') diff --git a/src/user/include/knob/time.h b/src/user/include/knob/time.h new file mode 100644 index 0000000..4b84697 --- /dev/null +++ b/src/user/include/knob/time.h @@ -0,0 +1,13 @@ +#include + +struct time { + uint16_t year;//ad + uint8_t month;//jan = 1 + uint8_t day; + uint8_t hour;//24-hour, midnight = 0 + uint8_t minute; + uint8_t second; + uint32_t timestamp; +}; + +struct time get_time(); \ No newline at end of file diff --git a/src/user/knob/time.c b/src/user/knob/time.c new file mode 100644 index 0000000..e3bbb08 --- /dev/null +++ b/src/user/knob/time.c @@ -0,0 +1,57 @@ +#include +#include +#include + +uint8_t days_in_months[] = { + 31, 0, 31, 30, + 31, 30, 31, 31, + 30, 31, 30, 31 +}; + +struct time get_time() { + struct time t; + + const uint32_t ts = _get_timestamp(); + t.timestamp = ts; + + const uint32_t time_part = ts % 86400; + t.hour = time_part / 3600; + t.minute = (time_part / 60) % 60; + t.second = time_part % 60; + + uint32_t days = ts / 86400; + + uint32_t year = 2000; + while (days >= 1461) { + year += 4; + days -= 1461; + } + + days_in_months[1] = 28; + if (days >= 1096) { + t.year = year += 3; + days -= 1096; + } + else if (days >= 731) { + t.year = year += 2; + days -= 731; + } + else if (days >= 366) { + t.year = ++year; + days -= 366; + } + else { + t.year = year; + days_in_months[1] = 29; + } + + uint8_t m = 0; + while (days >= days_in_months[m]) { + days -= days_in_months[m]; + ++m; + } + + t.month = m + 1; + t.day = days + 1; + return t; +} \ No newline at end of file diff --git a/src/user/time/time.c b/src/user/time/time.c new file mode 100644 index 0000000..49f6cf5 --- /dev/null +++ b/src/user/time/time.c @@ -0,0 +1,24 @@ +#include +#include + +static const char *const month_names[] = { + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December" +}; + +void main() { + const struct time t = get_time(); + term_addf("TS: 0x%8h\n", t.timestamp); + term_addf("Date: %s %u, %u\n", month_names[t.month - 1], t.day, t.year); + term_addf("Time: %u:%2u:%2u\n", t.hour, t.minute, t.second); +} \ No newline at end of file -- cgit v1.2.3