54
54
* between is unreliable */
55
55
#define LP_TIMER_SAFE_GUARD 5
56
56
57
-
58
57
LPTIM_HandleTypeDef LptimHandle ;
59
58
60
59
const ticker_info_t * lp_ticker_get_info ()
@@ -73,6 +72,10 @@ const ticker_info_t *lp_ticker_get_info()
73
72
volatile uint8_t lp_Fired = 0 ;
74
73
/* Flag and stored counter to handle delayed programing at low level */
75
74
volatile bool lp_delayed_prog = false;
75
+
76
+ volatile bool future_event_flag = false;
77
+ volatile bool roll_over_flag = false;
78
+
76
79
volatile bool lp_cmpok = false;
77
80
volatile timestamp_t lp_delayed_counter = 0 ;
78
81
volatile bool sleep_manager_locked = false;
@@ -226,12 +229,33 @@ static void LPTIM1_IRQHandler(void)
226
229
if (__HAL_LPTIM_GET_IT_SOURCE (& LptimHandle , LPTIM_IT_CMPOK ) != RESET ) {
227
230
__HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK );
228
231
lp_cmpok = true;
229
- if (sleep_manager_locked ) {
232
+ if (sleep_manager_locked ) {
230
233
sleep_manager_unlock_deep_sleep ();
231
234
sleep_manager_locked = false;
232
235
}
233
- if (lp_delayed_prog ) {
234
- lp_ticker_set_interrupt (lp_delayed_counter );
236
+ if (lp_delayed_prog ) {
237
+ if (roll_over_flag ) {
238
+ /* If we were close to the roll over of the ticker counter
239
+ * change current tick so it can be compared with buffer.
240
+ * If this event got outdated fire interrupt right now,
241
+ * else schedule it normally. */
242
+ if (lp_delayed_counter <= ((lp_ticker_read () + LP_TIMER_SAFE_GUARD + 1 ) & 0xFFFF )) {
243
+ lp_ticker_fire_interrupt ();
244
+ } else {
245
+ lp_ticker_set_interrupt ((lp_delayed_counter - LP_TIMER_SAFE_GUARD - 1 ) & 0xFFFF );
246
+ }
247
+ roll_over_flag = false;
248
+ } else {
249
+ if (future_event_flag && (lp_delayed_counter <= lp_ticker_read ())) {
250
+ /* If this event got outdated fire interrupt right now,
251
+ * else schedule it normally. */
252
+ lp_ticker_fire_interrupt ();
253
+ future_event_flag = false;
254
+ } else {
255
+ lp_ticker_set_interrupt (lp_delayed_counter );
256
+ }
257
+ }
258
+
235
259
lp_delayed_prog = false;
236
260
}
237
261
}
@@ -261,6 +285,8 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
261
285
{
262
286
core_util_critical_section_enter ();
263
287
288
+ timestamp_t last_read_counter = lp_ticker_read ();
289
+
264
290
/* Always store the last requested timestamp */
265
291
lp_delayed_counter = timestamp ;
266
292
NVIC_EnableIRQ (LPTIM1_IRQn );
@@ -272,19 +298,43 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
272
298
if (lp_cmpok == false) {
273
299
/* if this is not safe to write, then delay the programing to the
274
300
* time when CMPOK interrupt will trigger */
301
+
302
+ /* If this target timestamp is close to the roll over of the ticker counter
303
+ * and current tick is also close to the roll over, then we are in danger zone.*/
304
+ if (((0xFFFF - LP_TIMER_SAFE_GUARD < timestamp ) || (timestamp < LP_TIMER_SAFE_GUARD )) && (0xFFFA < last_read_counter )) {
305
+ roll_over_flag = true;
306
+ /* Change the lp_delayed_counter buffer in that way so the value of (0xFFFF - LP_TIMER_SAFE_GUARD) is equal to 0.
307
+ * By doing this it is easy to check if the value of timestamp get outdated by delaying its programming
308
+ * For example if LP_TIMER_SAFE_GUARD is set to 5
309
+ * (0xFFFA + LP_TIMER_SAFE_GUARD + 1) & 0xFFFF = 0
310
+ * (0xFFFF + LP_TIMER_SAFE_GUARD + 1) & 0xFFFF = 5
311
+ * (0x0000 + LP_TIMER_SAFE_GUARD + 1) & 0xFFFF = 6
312
+ * (0x0005 + LP_TIMER_SAFE_GUARD + 1) & 0xFFFF = 11*/
313
+ lp_delayed_counter = (timestamp + LP_TIMER_SAFE_GUARD + 1 ) & 0xFFFF ;
314
+ } else {
315
+ roll_over_flag = false;
316
+ /* Check if event was meant to be in the past. */
317
+ if (lp_delayed_counter >= last_read_counter ) {
318
+ future_event_flag = true;
319
+ } else {
320
+ future_event_flag = false;
321
+ }
322
+ }
323
+
275
324
lp_delayed_prog = true;
276
325
} else {
277
- timestamp_t last_read_counter = lp_ticker_read ();
326
+
278
327
lp_ticker_clear_interrupt ();
279
328
280
329
/* HW is not able to trig a very short term interrupt, that is
281
330
* not less than few ticks away (LP_TIMER_SAFE_GUARD). So let's make sure it'
282
331
* s at least current tick + LP_TIMER_SAFE_GUARD */
283
- for (uint8_t i = 0 ; i < LP_TIMER_SAFE_GUARD ; i ++ ) {
332
+ for (uint8_t i = 0 ; i < LP_TIMER_SAFE_GUARD ; i ++ ) {
284
333
if (LP_TIMER_WRAP ((last_read_counter + i )) == timestamp ) {
285
334
timestamp = LP_TIMER_WRAP ((timestamp + LP_TIMER_SAFE_GUARD ));
286
335
}
287
336
}
337
+
288
338
/* Then check if this target timestamp is not in the past, or close to wrap-around
289
339
* Let's assume last_read_counter = 0xFFFC, and we want to program timestamp = 0x100
290
340
* The interrupt will not fire before the CMPOK flag is OK, so there are 2 cases:
@@ -296,7 +346,7 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
296
346
* There might be crossing cases where it would also fire @ 0xFFFE, but by the time we read the counter,
297
347
* it may already have moved to the next one, so for now we've taken this as margin of error.
298
348
*/
299
- if ((timestamp < last_read_counter ) && (last_read_counter <= (0xFFFF - LP_TIMER_SAFE_GUARD ))) {
349
+ if ((timestamp < last_read_counter ) && (last_read_counter <= (0xFFFF - LP_TIMER_SAFE_GUARD ))) {
300
350
/* Workaround, because limitation */
301
351
__HAL_LPTIM_COMPARE_SET (& LptimHandle , ~0 );
302
352
} else {
@@ -309,7 +359,7 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
309
359
lp_cmpok = false;
310
360
/* Prevent from sleeping after compare register was set as we need CMPOK
311
361
* interrupt to fire (in ~3x30us cycles) before we can safely enter deep sleep mode */
312
- if (!sleep_manager_locked ) {
362
+ if (!sleep_manager_locked ) {
313
363
sleep_manager_lock_deep_sleep ();
314
364
sleep_manager_locked = true;
315
365
}
@@ -332,14 +382,14 @@ void lp_ticker_disable_interrupt(void)
332
382
{
333
383
core_util_critical_section_enter ();
334
384
335
- if (!lp_cmpok ) {
385
+ if (!lp_cmpok ) {
336
386
while (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK ) == RESET ) {
337
387
}
338
388
__HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK );
339
389
lp_cmpok = true;
340
390
}
341
391
/* now that CMPOK is set, allow deep sleep again */
342
- if (sleep_manager_locked ) {
392
+ if (sleep_manager_locked ) {
343
393
sleep_manager_unlock_deep_sleep ();
344
394
sleep_manager_locked = false;
345
395
}
0 commit comments