summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Moore <nick@zoic.org>2019-02-12 13:11:08 +1100
committerNick Moore <nick@zoic.org>2019-03-28 09:50:09 +1100
commit28254def0bf10ecb2690a9eb3af6efe4e515eea6 (patch)
treed09d5674fc707afc62ae222f065e2a6a9e33848d
parentff6395fa4eaf531abdf68719663895f4f2bf1870 (diff)
adafruit/circuitpython#1046 handle overflows in the RTC counter
-rw-r--r--ports/nrf/common-hal/rtc/RTC.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/ports/nrf/common-hal/rtc/RTC.c b/ports/nrf/common-hal/rtc/RTC.c
index 85cdb4c37..d62815b5a 100644
--- a/ports/nrf/common-hal/rtc/RTC.c
+++ b/ports/nrf/common-hal/rtc/RTC.c
@@ -35,9 +35,14 @@
#include "nrfx_rtc.h"
#include "nrf_clock.h"
+// We clock the RTC very slowly (8Hz) so that it won't overflow often.
+// But the counter is only 24 bits, so overflow is about every 24 days ...
+// For testing, set this to 32768 and it'll overflow every few minutes
+
#define RTC_CLOCK_HZ (8)
-static uint32_t rtc_offset = 0;
+volatile static uint32_t rtc_offset = 0;
+int8_t rtc_calibration = 0;
const nrfx_rtc_t rtc_instance = NRFX_RTC_INSTANCE(0);
@@ -49,7 +54,9 @@ const nrfx_rtc_config_t rtc_config = {
};
void rtc_handler(nrfx_rtc_int_type_t int_type) {
- // do nothing
+ if (int_type == NRFX_RTC_INT_OVERFLOW) {
+ rtc_offset += (1L<<24) / RTC_CLOCK_HZ;
+ }
}
void rtc_init(void) {
@@ -59,6 +66,7 @@ void rtc_init(void) {
nrfx_rtc_counter_clear(&rtc_instance);
nrfx_rtc_init(&rtc_instance, &rtc_config, rtc_handler);
nrfx_rtc_enable(&rtc_instance);
+ nrfx_rtc_overflow_enable(&rtc_instance, 1);
}
void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {
@@ -75,8 +83,11 @@ void common_hal_rtc_set_time(timeutils_struct_time_t *tm) {
// A positive value speeds up the clock by removing clock cycles.
int common_hal_rtc_get_calibration(void) {
- return 0;
+ return rtc_calibration;
}
void common_hal_rtc_set_calibration(int calibration) {
+ if (calibration > 127 || calibration < -127)
+ mp_raise_ValueError(translate("calibration value out of range +/-127"));
+ rtc_calibration = calibration;
}