From 64405275275ed88ce4f4ba4341840d4e597eda62 Mon Sep 17 00:00:00 2001 From: Lephe Date: Sun, 31 Jan 2021 09:19:19 +0100 Subject: [PATCH] rtc: add an rtc_ticks() function similar to RTC_GetTicks() --- CMakeLists.txt | 1 + include/gint/rtc.h | 9 ++++++++- src/rtc/rtc.c | 1 + src/rtc/rtc_ticks.c | 14 ++++++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 src/rtc/rtc_ticks.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0aad734..f09f7bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ set(SOURCES_COMMON src/render/topti.c src/rtc/inth.s src/rtc/rtc.c + src/rtc/rtc_ticks.c src/spu/spu.c src/std/tinymt32/rand.c src/std/tinymt32/tinymt32.c diff --git a/include/gint/rtc.h b/include/gint/rtc.h index cb7250a..b7652aa 100644 --- a/include/gint/rtc.h +++ b/include/gint/rtc.h @@ -22,6 +22,7 @@ typedef struct uint8_t hours; /* Hour (0..23) */ uint8_t minutes; /* Minute (0..59) */ uint8_t seconds; /* Second (0..59) */ + uint8_t ticks; /* 128-Hz sub-second counter (0...127) */ } rtc_time_t; @@ -31,10 +32,16 @@ void rtc_get_time(rtc_time_t *time); /* rtc_set_time(): Set current time in the RTC If [time->week_day] is not in the valid range, it is set to 0. Other fields - are not checked. + are not checked. R64CNT cannot be set to [time->ticks] is ignored. @time Pointer to new time */ void rtc_set_time(rtc_time_t const *time); +/* rtc_ticks(): Get number of 128-Hz ticks elapsed since Midnight. + + Returns R64CNT + 128*RSECCNT + 128*60*RMINCNT + 128*60*60*RHRCNT. This can + be used as a 128-Hz counter, but it wraps around at midnight. */ +uint32_t rtc_ticks(void); + //--- // RTC timer // The real-time clock produces a regular interrupt which may be used as a diff --git a/src/rtc/rtc.c b/src/rtc/rtc.c index c118e29..5d92b29 100644 --- a/src/rtc/rtc.c +++ b/src/rtc/rtc.c @@ -61,6 +61,7 @@ void rtc_get_time(rtc_time_t *time) do { RTC->RCR1.CF = 0; + time->ticks = RTC->R64CNT; time->seconds = int8(RTC->RSECCNT.byte); time->minutes = int8(RTC->RMINCNT.byte); time->hours = int8(RTC->RHRCNT.byte); diff --git a/src/rtc/rtc_ticks.c b/src/rtc/rtc_ticks.c new file mode 100644 index 0000000..9185c59 --- /dev/null +++ b/src/rtc/rtc_ticks.c @@ -0,0 +1,14 @@ +#include + +uint32_t rtc_ticks(void) +{ + rtc_time_t t; + rtc_get_time(&t); + + uint32_t ticks = t.hours; + ticks = 60 * ticks + t.minutes; + ticks = 60 * ticks + t.seconds; + ticks = 128 * ticks + t.ticks; + + return ticks; +}