adafruit/circuitpython#1046 handle overflows in the RTC counter
This commit is contained in:
parent
f846fa109e
commit
6206fa9a82
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue