|
33 | 33 | #include "supervisor/shared/translate.h"
|
34 | 34 |
|
35 | 35 | #include "nrfx_rtc.h"
|
| 36 | +#include "nrf_clock.h" |
36 | 37 |
|
37 |
| -static uint32_t _rtc_seconds = 0; |
| 38 | +#define RTC_CLOCK_HZ (8) |
38 | 39 |
|
39 |
| -void rtc_handler(nrfx_rtc_int_type_t int_type) { |
| 40 | +static uint32_t rtc_offset = 0; |
| 41 | + |
| 42 | +const nrfx_rtc_t rtc_instance = NRFX_RTC_INSTANCE(0); |
40 | 43 |
|
| 44 | +const nrfx_rtc_config_t rtc_config = { |
| 45 | + .prescaler = RTC_FREQ_TO_PRESCALER(RTC_CLOCK_HZ), |
| 46 | + .reliable = 0, |
| 47 | + .tick_latency = 0, |
| 48 | + .interrupt_priority = 6 |
| 49 | +}; |
| 50 | + |
| 51 | +void rtc_handler(nrfx_rtc_int_type_t int_type) { |
| 52 | + // do nothing |
41 | 53 | }
|
42 | 54 |
|
43 | 55 | void rtc_init(void) {
|
| 56 | + if (!nrf_clock_lf_is_running()) { |
| 57 | + nrf_clock_task_trigger(NRF_CLOCK_TASK_LFCLKSTART); |
| 58 | + } |
| 59 | + nrfx_rtc_counter_clear(&rtc_instance); |
| 60 | + nrfx_rtc_init(&rtc_instance, &rtc_config, rtc_handler); |
| 61 | + nrfx_rtc_enable(&rtc_instance); |
44 | 62 | }
|
45 | 63 |
|
46 | 64 | void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
|
47 |
| - timeutils_seconds_since_2000_to_struct_time(_rtc_seconds, tm); |
| 65 | + uint32_t t = rtc_offset + (nrfx_rtc_counter_get(&rtc_instance) / RTC_CLOCK_HZ ); |
| 66 | + timeutils_seconds_since_2000_to_struct_time(t, tm); |
48 | 67 | }
|
49 | 68 |
|
50 | 69 | void common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
|
51 |
| - _rtc_seconds = timeutils_seconds_since_2000( |
| 70 | + rtc_offset = timeutils_seconds_since_2000( |
52 | 71 | tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec
|
53 | 72 | );
|
| 73 | + nrfx_rtc_counter_clear(&rtc_instance); |
54 | 74 | }
|
55 | 75 |
|
56 | 76 | // A positive value speeds up the clock by removing clock cycles.
|
|
0 commit comments