24
24
#include "sleep_api.h"
25
25
#include "sleepmodes.h"
26
26
27
- static bool rtc_inited = false;
28
- static time_t time_base = 0 ;
29
- static uint32_t useflags = 0 ;
27
+ static bool rtc_inited = false;
28
+ static time_t time_base = 0 ;
29
+ static uint32_t useflags = 0 ;
30
+ static uint32_t time_extend = 0 ;
30
31
31
32
static void (* comp0_handler )(void ) = NULL ;
32
33
33
- #define RTC_LEAST_ACTIVE_SLEEPMODE EM2
34
-
34
+ #define RTC_LEAST_ACTIVE_SLEEPMODE EM2
35
+ #define RTC_NUM_BITS (24)
35
36
36
37
void RTC_IRQHandler (void )
37
38
{
38
39
uint32_t flags ;
39
40
flags = RTC_IntGet ();
40
41
if (flags & RTC_IF_OF ) {
41
42
RTC_IntClear (RTC_IF_OF );
42
- /* RTC has overflowed (24 bits). Use time_base as software counter for upper 8 bits. */
43
- time_base += 1 << 24 ;
43
+ /* RTC has overflowed (24 bits). Use time_extend as software counter for 32 more bits. */
44
+ time_extend += 1 ;
44
45
}
45
46
if (flags & RTC_IF_COMP0 ) {
46
47
RTC_IntClear (RTC_IF_COMP0 );
@@ -50,6 +51,20 @@ void RTC_IRQHandler(void)
50
51
}
51
52
}
52
53
54
+ uint32_t rtc_get_32bit (void )
55
+ {
56
+ return (RTC_CounterGet () + (time_extend << RTC_NUM_BITS ));
57
+ }
58
+
59
+ uint64_t rtc_get_full (void )
60
+ {
61
+ uint64_t ticks = 0 ;
62
+ ticks += time_extend ;
63
+ ticks = ticks << RTC_NUM_BITS ;
64
+ ticks += RTC_CounterGet ();
65
+ return ticks ;
66
+ }
67
+
53
68
void rtc_set_comp0_handler (uint32_t handler )
54
69
{
55
70
comp0_handler = (void (* )(void )) handler ;
@@ -126,18 +141,23 @@ int rtc_isenabled(void)
126
141
127
142
time_t rtc_read (void )
128
143
{
129
- return (time_t ) ((RTC_CounterGet () + time_base ) >> RTC_FREQ_SHIFT );
144
+ return (time_t ) (rtc_get_full () >> RTC_FREQ_SHIFT ) + time_base ;
145
+ }
146
+
147
+ time_t rtc_read_uncompensated (void )
148
+ {
149
+ return (time_t ) (rtc_get_full () >> RTC_FREQ_SHIFT );
130
150
}
131
151
132
152
void rtc_write (time_t t )
133
153
{
134
154
/* We have to check that the RTC did not tick while doing this. */
135
155
/* If the RTC ticks we just redo this. */
136
- uint32_t rtc_count ;
156
+ uint32_t time ;
137
157
do {
138
- rtc_count = RTC_CounterGet ();
139
- time_base = ( t << RTC_FREQ_SHIFT ) - rtc_count ;
140
- } while (rtc_count != RTC_CounterGet ());
158
+ time = rtc_read_uncompensated ();
159
+ time_base = t - time ;
160
+ } while (time != rtc_read_uncompensated ());
141
161
}
142
162
143
163
#endif
0 commit comments