From 11c9df19ab1db825273bc397d252d9891e5b84ca Mon Sep 17 00:00:00 2001 From: Felipe Martinez Date: Thu, 24 Oct 2024 23:03:08 +0000 Subject: [PATCH] Use LFCLK in RTC --- include/fault.h | 2 ++ src/peripherals/nrf52832/rtc.c | 48 +++++++++++--------------------- src/peripherals/nrf52832/timer.c | 2 ++ src/ticker.c | 2 +- 4 files changed, 21 insertions(+), 33 deletions(-) diff --git a/include/fault.h b/include/fault.h index ec2414f..cfec052 100644 --- a/include/fault.h +++ b/include/fault.h @@ -37,6 +37,8 @@ typedef enum FAULT_PPI_DUPLICATE_PERIPHERAL, + FAULT_RTC_INVALID_STATE, + FAULT_ST7789_INVALID_COORDS, } fault_type_t; diff --git a/src/peripherals/nrf52832/rtc.c b/src/peripherals/nrf52832/rtc.c index 3ccb943..7102353 100644 --- a/src/peripherals/nrf52832/rtc.c +++ b/src/peripherals/nrf52832/rtc.c @@ -4,6 +4,7 @@ #include #include +#include "fault.h" #include "ie_time.h" #include "peripherals/nrf52832/ppi.h" @@ -34,9 +35,6 @@ struct RTC_inst_t bool running; - double tick_interval_us; - size_t last_check_us; - inten_t inten, evten; uint32_t prescaler, counter, prescaler_counter; }; @@ -45,34 +43,21 @@ void rtc_tick(void *userdata) { RTC_t *rtc = userdata; - size_t now = microseconds_now(); + rtc->counter++; - size_t elapsed = now - rtc->last_check_us; - size_t elapsed_ticks = elapsed / rtc->tick_interval_us; + ppi_fire_event(current_ppi, rtc->id, EVENT_ID(RTC_EVENTS_TICK), rtc->inten.TICK); - while (elapsed_ticks--) + if (rtc->counter == (1 << 24)) { - rtc->last_check_us = now; - - // if (elapsed_ticks > 1) - // printf("Warning: skipped %ld ticks\n", elapsed_ticks - 1); - - rtc->counter++; - - ppi_fire_event(current_ppi, rtc->id, EVENT_ID(RTC_EVENTS_TICK), rtc->inten.TICK); - - if (rtc->counter == (1 << 24)) - { - rtc->counter = 0; + rtc->counter = 0; - ppi_fire_event(current_ppi, rtc->id, EVENT_ID(RTC_EVENTS_OVRFLW), rtc->inten.OVRFLW); - } + ppi_fire_event(current_ppi, rtc->id, EVENT_ID(RTC_EVENTS_OVRFLW), rtc->inten.OVRFLW); + } - for (size_t i = 0; i < rtc->cc_num; i++) - { - if (rtc->counter == rtc->cc[i]) - ppi_fire_event(current_ppi, rtc->id, EVENT_ID(RTC_EVENTS_COMPARE0) + i, rtc->inten.COMPARE & (1 << i)); - } + for (size_t i = 0; i < rtc->cc_num; i++) + { + if (rtc->counter == rtc->cc[i]) + ppi_fire_event(current_ppi, rtc->id, EVENT_ID(RTC_EVENTS_COMPARE0) + i, rtc->inten.COMPARE & (1 << i)); } } @@ -88,7 +73,6 @@ OPERATION(rtc) rtc->prescaler = 0; rtc->counter = 0; rtc->prescaler_counter = 0; - rtc->tick_interval_us = 0; return MEMREG_RESULT_OK; } @@ -128,8 +112,10 @@ OPERATION(rtc) } else { + if (rtc->running) + fault_take(FAULT_RTC_INVALID_STATE); + rtc->prescaler = *value; - rtc->tick_interval_us = ((double)(rtc->prescaler + 1) * 1e6) / 32768.0; } return MEMREG_RESULT_OK; @@ -154,9 +140,7 @@ PPI_TASK_HANDLER(rtc_task_handler) case TASK_ID(RTC_TASKS_START): if (!rtc->running) { - ticker_add(rtc->ticker, rtc_tick, rtc, TICK_INTERVAL, true); - - rtc->last_check_us = microseconds_now(); + ticker_add(rtc->ticker, rtc_tick, rtc, 32768 / (rtc->prescaler + 1), true); rtc->running = true; } @@ -205,5 +189,5 @@ uint32_t rtc_get_counter(RTC_t *rtc) double rtc_get_tick_interval_us(RTC_t *rtc) { - return rtc->tick_interval_us; + return ((double)(rtc->prescaler + 1) * 1e6) / 32768.0; } diff --git a/src/peripherals/nrf52832/timer.c b/src/peripherals/nrf52832/timer.c index 88c8355..848a7c8 100644 --- a/src/peripherals/nrf52832/timer.c +++ b/src/peripherals/nrf52832/timer.c @@ -167,6 +167,8 @@ PPI_TASK_HANDLER(timer_task_handler) switch (task) { case TASK_ID(TIMER_TASKS_START): + fault_take(FAULT_NOT_IMPLEMENTED); // TODO: Make timer use HFCLK ticker + if (timer->mode == MODE_TIMER) { if (!timer->running) diff --git a/src/ticker.c b/src/ticker.c index c362db0..08f5767 100644 --- a/src/ticker.c +++ b/src/ticker.c @@ -44,7 +44,7 @@ void ticker_free(ticker_t *ticker) void ticker_reset(ticker_t *ticker) { - memset(ticker, 0, sizeof(ticker_t)); + ticker->entries_count = 0; } void ticker_add(ticker_t *ticker, ticker_cb_t cb, void *userdata, uint32_t interval, bool auto_reload)