31
31
32
32
#define RTC_BITS 48u
33
33
#define RTC_FREQ 32768u
34
- #define RTC_COUNTER_MSK 0xFFFFFFFFFFFFULL
35
- /**< Macro for wrapping values to RTC capacity. */
36
- #define RTC_WRAP (val ) (val & RTC_COUNTER_MSK)
37
34
38
35
static bool rtc_inited = false;
39
36
@@ -46,11 +43,6 @@ const ticker_info_t* lp_ticker_get_info()
46
43
return & info ;
47
44
}
48
45
49
- void PRCM_IRQHandler (void )
50
- {
51
- lp_ticker_irq_handler ();
52
- }
53
-
54
46
void lp_ticker_init ()
55
47
{
56
48
if (PRCMRTCInUseGet () == true)
@@ -59,7 +51,7 @@ void lp_ticker_init()
59
51
return ;
60
52
}
61
53
if (!rtc_inited ) {
62
- NVIC_SetVector (INT_PRCM_IRQn , (uint32_t )PRCM_IRQHandler );
54
+ NVIC_SetVector (INT_PRCM_IRQn , (uint32_t )lp_ticker_irq_handler );
63
55
// Set priority to highest 0
64
56
NVIC_SetPriority (INT_PRCM_IRQn , 0 );
65
57
NVIC_ClearPendingIRQ (INT_PRCM_IRQn );
@@ -83,24 +75,10 @@ void lp_ticker_free()
83
75
void lp_ticker_set_interrupt (timestamp_t timestamp )
84
76
{
85
77
// timestamp is defined as 32b.
86
- unsigned long long slowctr_now ; // 64b counter
87
-
88
- // The 32KHz clock is always running. Say, to generate interrupt after 1ms or 33 ticks, if the counter is N, we need to write
89
- // N + 33 to the match register.
90
78
core_util_critical_section_enter ();
91
- slowctr_now = PRCMSlowClkCtrFastGet ();
92
-
93
- /* COMPARE occurs when a SC register is N and the COUNTER value transitions from N-1 to N.
94
- * If the COUNTER is N, writing N+2 to a CC register is guaranteed to trigger a
95
- * COMPARE event at N+2.
96
- */
97
- if (slowctr_now == timestamp || (slowctr_now + 1 ) == timestamp )
98
- {
99
- timestamp += 2 ;
100
- }
101
79
// Clear pending interrupt
102
80
PRCMIntStatus ();
103
- PRCMSlowClkCtrMatchSet (RTC_WRAP (( slowctr_now + timestamp )) );
81
+ PRCMSlowClkCtrMatchSet (timestamp );
104
82
PRCMIntEnable (PRCM_INT_SLOW_CLK_CTR );
105
83
core_util_critical_section_exit ();
106
84
}
@@ -124,8 +102,15 @@ void lp_ticker_clear_interrupt()
124
102
125
103
timestamp_t lp_ticker_read ()
126
104
{
127
- // Truncate to 32b. Is it okay?
128
- return (timestamp_t )PRCMSlowClkCtrFastGet ();
105
+ // Read forever until reaching two of the same
106
+ volatile unsigned long long read_previous , read_current ;
107
+ do
108
+ {
109
+ read_previous = PRCMSlowClkCtrFastGet ();
110
+ read_current = PRCMSlowClkCtrFastGet ();
111
+ } while (read_previous != read_current );
112
+
113
+ return read_current ;
129
114
}
130
115
131
116
#endif /* DEVICE_LPTICKER */
0 commit comments