@@ -122,8 +122,27 @@ void deepsleep_lpticker_test()
122122
123123 lp_ticker_set_interrupt (next_match_timestamp);
124124
125+ /* On some targets like STM family boards with LPTIM enabled there is a required delay (~100 us) before we are able to
126+ reprogram LPTIM_COMPARE register back to back. This is handled by the low level lp ticker wrapper which uses LPTIM_CMPOK interrupt.
127+ CMPOK fires when LPTIM_COMPARE register can be safely reprogrammed again. During this period deep-sleep is locked.
128+ This means that on these platforms we have additional interrupt (CMPOK) fired always ~100 us after programming lp ticker.
129+ Since this interrupt wakes-up the board from the sleep we need to go to sleep after CMPOK is handled. */
130+ TEST_ASSERT_TRUE (sleep_manager_can_deep_sleep_test_check ());
131+
125132 sleep ();
126133
134+ /* On some targets like STM family boards with LPTIM enabled an interrupt is triggered on counter rollover.
135+ We need special handling for cases when next_match_timestamp < start_timestamp (interrupt is to be fired after rollover).
136+ In such case after first wake-up we need to reset interrupt and go back to sleep waiting for the valid one.
137+ NOTE: Above comment (CMPOK) applies also here.*/
138+ #if MBED_CONF_TARGET_LPTICKER_LPTIM
139+ if ((next_match_timestamp < start_timestamp) && lp_ticker_read () < next_match_timestamp) {
140+ lp_ticker_set_interrupt (next_match_timestamp);
141+ wait_ns (200000 );
142+ sleep ();
143+ }
144+ #endif
145+
127146 const timestamp_t wakeup_timestamp = lp_ticker_read ();
128147
129148 sprintf (info, " Delta ticks: %u, Ticker width: %u, Expected wake up tick: %d, Actual wake up tick: %d, delay ticks: %d, wake up after ticks: %d" ,
@@ -154,11 +173,14 @@ void deepsleep_high_speed_clocks_turned_off_test()
154173
155174 TEST_ASSERT_TRUE_MESSAGE (sleep_manager_can_deep_sleep (), " deep sleep should not be locked" );
156175
157- const unsigned int us_ticks_before_sleep = us_ticker_read ();
158-
159176 const timestamp_t wakeup_time = lp_ticker_read () + us_to_ticks (20000 , lp_ticker_freq);
160177 lp_ticker_set_interrupt (wakeup_time);
161178
179+ /* Wait for CMPOK */
180+ TEST_ASSERT_TRUE (sleep_manager_can_deep_sleep_test_check ());
181+
182+ const unsigned int us_ticks_before_sleep = us_ticker_read ();
183+
162184 sleep ();
163185
164186 const unsigned int us_ticks_after_sleep = us_ticker_read ();
0 commit comments