@@ -44,7 +44,7 @@ static uint64_t cyc_sys_compensated;
44
44
*/
45
45
static const struct device * counter_dev ;
46
46
/* Indicates if the counter is running. */
47
- bool counter_running ;
47
+ static bool counter_running ;
48
48
#endif
49
49
50
50
static uint64_t mcux_lpc_ostick_get_compensated_timer_value (void )
@@ -90,12 +90,10 @@ static uint32_t mcux_lpc_ostick_set_counter_timeout(int32_t curr_timeout)
90
90
uint32_t ret = 0 ;
91
91
92
92
if (counter_dev ) {
93
- uint32_t timeout ;
94
- int32_t ticks ;
93
+ uint32_t ticks ;
95
94
struct counter_top_cfg top_cfg = { 0 };
96
- timeout = k_ticks_to_us_near32 (curr_timeout );
97
95
98
- ticks = counter_us_to_ticks (counter_dev , timeout );
96
+ ticks = counter_us_to_ticks (counter_dev , curr_timeout );
99
97
ticks = CLAMP (ticks , 1 , counter_get_max_top_value (counter_dev ));
100
98
101
99
top_cfg .ticks = ticks ;
@@ -181,28 +179,42 @@ static uint32_t mcux_lpc_ostick_compensate_system_timer(void)
181
179
182
180
void sys_clock_set_timeout (int32_t ticks , bool idle )
183
181
{
184
- ARG_UNUSED (idle );
185
-
186
182
if (!IS_ENABLED (CONFIG_TICKLESS_KERNEL )) {
187
183
/* Only for tickless kernel system */
188
184
return ;
189
185
}
190
186
191
187
#if DT_NODE_HAS_STATUS_OKAY (DT_NODELABEL (standby )) && CONFIG_PM
192
- if (idle ) {
188
+ /* We intercept calls from idle with a 0 tick count */
189
+ if (idle && ticks == 0 ) {
193
190
/* OS Timer may not be able to wakeup in certain low power modes.
194
191
* For these cases, we start a counter that can wakeup
195
192
* from low power modes.
196
193
*/
197
194
if (pm_state_next_get (0 )-> state == PM_STATE_STANDBY ) {
198
- if (mcux_lpc_ostick_set_counter_timeout (ticks ) == 0 ) {
195
+ uint64_t timeout ;
196
+
197
+ /* Check the amount of time left and switch to a counter
198
+ * that is active in this power mode.
199
+ */
200
+ timeout = base -> MATCH_L ;
201
+ timeout |= (uint64_t )(base -> MATCH_H ) << 32 ;
202
+ timeout = OSTIMER_GrayToDecimal (timeout );
203
+ timeout -= OSTIMER_GetCurrentTimerValue (base );
204
+ /* Round up to the next tick boundary */
205
+ timeout += (CYC_PER_TICK - 1 );
206
+ /* Convert to microseconds and round up to the next value */
207
+ timeout = (((timeout / CYC_PER_TICK ) * CYC_PER_TICK ) * CYC_PER_US );
208
+ if (mcux_lpc_ostick_set_counter_timeout (timeout ) == 0 ) {
199
209
/* A low power counter has been started. No need to
200
210
* go further, simply return
201
211
*/
202
212
return ;
203
213
}
204
214
}
205
215
}
216
+ #else
217
+ ARG_UNUSED (idle );
206
218
#endif
207
219
208
220
ticks = ticks == K_TICKS_FOREVER ? MAX_TICKS : ticks ;
0 commit comments