5454 * between is unreliable */
5555#define LP_TIMER_SAFE_GUARD 5
5656
57-
5857LPTIM_HandleTypeDef LptimHandle ;
5958
6059const ticker_info_t * lp_ticker_get_info ()
@@ -73,6 +72,10 @@ const ticker_info_t *lp_ticker_get_info()
7372volatile uint8_t lp_Fired = 0 ;
7473/* Flag and stored counter to handle delayed programing at low level */
7574volatile bool lp_delayed_prog = false;
75+
76+ volatile bool future_event_flag = false;
77+ volatile bool roll_over_flag = false;
78+
7679volatile bool lp_cmpok = false;
7780volatile timestamp_t lp_delayed_counter = 0 ;
7881volatile bool sleep_manager_locked = false;
@@ -231,7 +234,28 @@ static void LPTIM1_IRQHandler(void)
231234 sleep_manager_locked = false;
232235 }
233236 if (lp_delayed_prog ) {
234- lp_ticker_set_interrupt (lp_delayed_counter );
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+
235259 lp_delayed_prog = false;
236260 }
237261 }
@@ -260,6 +284,8 @@ uint32_t lp_ticker_read(void)
260284void lp_ticker_set_interrupt (timestamp_t timestamp )
261285{
262286 core_util_critical_section_enter ();
287+
288+ timestamp_t last_read_counter = lp_ticker_read ();
263289
264290 /* Always store the last requested timestamp */
265291 lp_delayed_counter = timestamp ;
@@ -272,9 +298,33 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
272298 if (lp_cmpok == false) {
273299 /* if this is not safe to write, then delay the programing to the
274300 * 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+ {
306+ roll_over_flag = true;
307+ /* Change the lp_delayed_counter buffer in that way so the value of (0xFFFF - LP_TIMER_SAFE_GUARD) is equal to 0.
308+ * By doing this it is easy to check if the value of timestamp get outdated by delaying its programming
309+ * For example if LP_TIMER_SAFE_GUARD is set to 5
310+ * (0xFFFA + LP_TIMER_SAFE_GUARD + 1) & 0xFFFF = 0
311+ * (0xFFFF + LP_TIMER_SAFE_GUARD + 1) & 0xFFFF = 5
312+ * (0x0000 + LP_TIMER_SAFE_GUARD + 1) & 0xFFFF = 6
313+ * (0x0005 + LP_TIMER_SAFE_GUARD + 1) & 0xFFFF = 11*/
314+ lp_delayed_counter = (timestamp + LP_TIMER_SAFE_GUARD + 1 ) & 0xFFFF ;
315+ } else {
316+ roll_over_flag = false;
317+ /* Check if event was meant to be in the past. */
318+ if (lp_delayed_counter >= last_read_counter ) {
319+ future_event_flag = true;
320+ } else {
321+ future_event_flag = false;
322+ }
323+ }
324+
275325 lp_delayed_prog = true;
276326 } else {
277- timestamp_t last_read_counter = lp_ticker_read ();
327+
278328 lp_ticker_clear_interrupt ();
279329
280330 /* HW is not able to trig a very short term interrupt, that is
@@ -285,6 +335,7 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
285335 timestamp = LP_TIMER_WRAP ((timestamp + LP_TIMER_SAFE_GUARD ));
286336 }
287337 }
338+
288339 /* Then check if this target timestamp is not in the past, or close to wrap-around
289340 * Let's assume last_read_counter = 0xFFFC, and we want to program timestamp = 0x100
290341 * The interrupt will not fire before the CMPOK flag is OK, so there are 2 cases:
0 commit comments