Skip to content

Commit

Permalink
Use LFCLK in RTC
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Oct 24, 2024
1 parent 7f98d89 commit 11c9df1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 33 deletions.
2 changes: 2 additions & 0 deletions include/fault.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ typedef enum

FAULT_PPI_DUPLICATE_PERIPHERAL,

FAULT_RTC_INVALID_STATE,

FAULT_ST7789_INVALID_COORDS,
} fault_type_t;

Expand Down
48 changes: 16 additions & 32 deletions src/peripherals/nrf52832/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string.h>
#include <stdlib.h>

#include "fault.h"
#include "ie_time.h"
#include "peripherals/nrf52832/ppi.h"

Expand Down Expand Up @@ -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;
};
Expand All @@ -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));
}
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/peripherals/nrf52832/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 11c9df1

Please sign in to comment.