diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 3681ac49e7..3cb4b52aac 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -140,6 +140,7 @@ SRC_NRFX = $(addprefix nrfx/,\ drivers/src/nrfx_twim.c \ drivers/src/nrfx_uarte.c \ drivers/src/nrfx_gpiote.c \ + drivers/src/nrfx_rtc.c \ ) ifdef EXTERNAL_FLASH_DEVICES diff --git a/ports/nrf/common-hal/rtc/RTC.c b/ports/nrf/common-hal/rtc/RTC.c index 7690b3bd3a..85cdb4c373 100644 --- a/ports/nrf/common-hal/rtc/RTC.c +++ b/ports/nrf/common-hal/rtc/RTC.c @@ -33,24 +33,44 @@ #include "supervisor/shared/translate.h" #include "nrfx_rtc.h" +#include "nrf_clock.h" -static uint32_t _rtc_seconds = 0; +#define RTC_CLOCK_HZ (8) + +static uint32_t rtc_offset = 0; + +const nrfx_rtc_t rtc_instance = NRFX_RTC_INSTANCE(0); + +const nrfx_rtc_config_t rtc_config = { + .prescaler = RTC_FREQ_TO_PRESCALER(RTC_CLOCK_HZ), + .reliable = 0, + .tick_latency = 0, + .interrupt_priority = 6 +}; void rtc_handler(nrfx_rtc_int_type_t int_type) { - + // do nothing } void rtc_init(void) { + if (!nrf_clock_lf_is_running()) { + nrf_clock_task_trigger(NRF_CLOCK_TASK_LFCLKSTART); + } + nrfx_rtc_counter_clear(&rtc_instance); + nrfx_rtc_init(&rtc_instance, &rtc_config, rtc_handler); + nrfx_rtc_enable(&rtc_instance); } void common_hal_rtc_get_time(timeutils_struct_time_t *tm) { - timeutils_seconds_since_2000_to_struct_time(_rtc_seconds, tm); + uint32_t t = rtc_offset + (nrfx_rtc_counter_get(&rtc_instance) / RTC_CLOCK_HZ ); + timeutils_seconds_since_2000_to_struct_time(t, tm); } void common_hal_rtc_set_time(timeutils_struct_time_t *tm) { - _rtc_seconds = timeutils_seconds_since_2000( + rtc_offset = timeutils_seconds_since_2000( tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec ); + nrfx_rtc_counter_clear(&rtc_instance); } // A positive value speeds up the clock by removing clock cycles. diff --git a/ports/nrf/nrfx_config.h b/ports/nrf/nrfx_config.h index 57a2727aa7..b8a0a7f60d 100644 --- a/ports/nrf/nrfx_config.h +++ b/ports/nrf/nrfx_config.h @@ -70,6 +70,9 @@ #define NRFX_PWM3_ENABLED 0 #endif +#define NRFX_RTC_ENABLED 1 +#define NRFX_RTC0_ENABLED 1 + // TIMERS #define NRFX_TIMER_ENABLED 1 // Don't enable TIMER0: it's used by the SoftDevice. diff --git a/shared-bindings/rtc/RTC.c b/shared-bindings/rtc/RTC.c index 474d4a399a..97265d601a 100644 --- a/shared-bindings/rtc/RTC.c +++ b/shared-bindings/rtc/RTC.c @@ -36,6 +36,7 @@ #include "shared-bindings/time/__init__.h" #include "supervisor/shared/translate.h" +/* void MP_WEAK common_hal_rtc_get_time(timeutils_struct_time_t *tm) { mp_raise_NotImplementedError(translate("RTC is not supported on this board")); } @@ -51,6 +52,7 @@ int MP_WEAK common_hal_rtc_get_calibration(void) { void MP_WEAK common_hal_rtc_set_calibration(int calibration) { mp_raise_NotImplementedError(translate("RTC calibration is not supported on this board")); } +*/ const rtc_rtc_obj_t rtc_rtc_obj = {{&rtc_rtc_type}};