35
35
#include "nrfx_rtc.h"
36
36
#include "nrf_clock.h"
37
37
38
+ // We clock the RTC very slowly (8Hz) so that it won't overflow often.
39
+ // But the counter is only 24 bits, so overflow is about every 24 days ...
40
+ // For testing, set this to 32768 and it'll overflow every few minutes
41
+
38
42
#define RTC_CLOCK_HZ (8)
39
43
40
- static uint32_t rtc_offset = 0 ;
44
+ volatile static uint32_t rtc_offset = 0 ;
45
+ int8_t rtc_calibration = 0 ;
41
46
42
47
const nrfx_rtc_t rtc_instance = NRFX_RTC_INSTANCE (0 );
43
48
@@ -49,7 +54,9 @@ const nrfx_rtc_config_t rtc_config = {
49
54
};
50
55
51
56
void rtc_handler (nrfx_rtc_int_type_t int_type ) {
52
- // do nothing
57
+ if (int_type == NRFX_RTC_INT_OVERFLOW ) {
58
+ rtc_offset += (1L <<24 ) / RTC_CLOCK_HZ ;
59
+ }
53
60
}
54
61
55
62
void rtc_init (void ) {
@@ -59,6 +66,7 @@ void rtc_init(void) {
59
66
nrfx_rtc_counter_clear (& rtc_instance );
60
67
nrfx_rtc_init (& rtc_instance , & rtc_config , rtc_handler );
61
68
nrfx_rtc_enable (& rtc_instance );
69
+ nrfx_rtc_overflow_enable (& rtc_instance , 1 );
62
70
}
63
71
64
72
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) {
75
83
76
84
// A positive value speeds up the clock by removing clock cycles.
77
85
int common_hal_rtc_get_calibration (void ) {
78
- return 0 ;
86
+ return rtc_calibration ;
79
87
}
80
88
81
89
void common_hal_rtc_set_calibration (int calibration ) {
90
+ if (calibration > 127 || calibration < -127 )
91
+ mp_raise_ValueError (translate ("calibration value out of range +/-127" ));
92
+ rtc_calibration = calibration ;
82
93
}
0 commit comments