Skip to content

Commit f846fa1

Browse files
committed
enable NRFX RTC adafruit#1046
1 parent f88f9fd commit f846fa1

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

ports/nrf/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ SRC_NRFX = $(addprefix nrfx/,\
140140
drivers/src/nrfx_twim.c \
141141
drivers/src/nrfx_uarte.c \
142142
drivers/src/nrfx_gpiote.c \
143+
drivers/src/nrfx_rtc.c \
143144
)
144145

145146
ifdef EXTERNAL_FLASH_DEVICES

ports/nrf/common-hal/rtc/RTC.c

+24-4
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,44 @@
3333
#include "supervisor/shared/translate.h"
3434

3535
#include "nrfx_rtc.h"
36+
#include "nrf_clock.h"
3637

37-
static uint32_t _rtc_seconds = 0;
38+
#define RTC_CLOCK_HZ (8)
3839

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);
4043

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
4153
}
4254

4355
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);
4462
}
4563

4664
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);
4867
}
4968

5069
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(
5271
tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec
5372
);
73+
nrfx_rtc_counter_clear(&rtc_instance);
5474
}
5575

5676
// A positive value speeds up the clock by removing clock cycles.

ports/nrf/nrfx_config.h

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
#define NRFX_PWM3_ENABLED 0
7171
#endif
7272

73+
#define NRFX_RTC_ENABLED 1
74+
#define NRFX_RTC0_ENABLED 1
75+
7376
// TIMERS
7477
#define NRFX_TIMER_ENABLED 1
7578
// Don't enable TIMER0: it's used by the SoftDevice.

shared-bindings/rtc/RTC.c

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "shared-bindings/time/__init__.h"
3737
#include "supervisor/shared/translate.h"
3838

39+
/*
3940
void MP_WEAK common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
4041
mp_raise_NotImplementedError(translate("RTC is not supported on this board"));
4142
}
@@ -51,6 +52,7 @@ int MP_WEAK common_hal_rtc_get_calibration(void) {
5152
void MP_WEAK common_hal_rtc_set_calibration(int calibration) {
5253
mp_raise_NotImplementedError(translate("RTC calibration is not supported on this board"));
5354
}
55+
*/
5456

5557
const rtc_rtc_obj_t rtc_rtc_obj = {{&rtc_rtc_type}};
5658

0 commit comments

Comments
 (0)