3131
3232#define RTC_BITS 48u
3333#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)
3734
3835static bool rtc_inited = false;
3936
@@ -46,11 +43,6 @@ const ticker_info_t* lp_ticker_get_info()
4643 return & info ;
4744}
4845
49- void PRCM_IRQHandler (void )
50- {
51- lp_ticker_irq_handler ();
52- }
53-
5446void lp_ticker_init ()
5547{
5648 if (PRCMRTCInUseGet () == true)
@@ -59,7 +51,7 @@ void lp_ticker_init()
5951 return ;
6052 }
6153 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 );
6355 // Set priority to highest 0
6456 NVIC_SetPriority (INT_PRCM_IRQn , 0 );
6557 NVIC_ClearPendingIRQ (INT_PRCM_IRQn );
@@ -83,24 +75,10 @@ void lp_ticker_free()
8375void lp_ticker_set_interrupt (timestamp_t timestamp )
8476{
8577 // 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.
9078 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- }
10179 // Clear pending interrupt
10280 PRCMIntStatus ();
103- PRCMSlowClkCtrMatchSet (RTC_WRAP (( slowctr_now + timestamp )) );
81+ PRCMSlowClkCtrMatchSet (timestamp );
10482 PRCMIntEnable (PRCM_INT_SLOW_CLK_CTR );
10583 core_util_critical_section_exit ();
10684}
@@ -124,8 +102,15 @@ void lp_ticker_clear_interrupt()
124102
125103timestamp_t lp_ticker_read ()
126104{
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 ;
129114}
130115
131116#endif /* DEVICE_LPTICKER */
0 commit comments