tests-mbed_hal-sleep: add exception for STM LPTIM targets.

On some targets like STM family boards with LPTIM enabled an interrupt is triggered on counter rollover.
We need special handling for cases when next_match_timestamp < start_timestamp (interrupt is to be fired after rollover).
In such case after first wake-up we need to reset interrupt and go back to sleep waiting for the valid one.

On some targets like STM family boards with LPTIM enabled there is a required delay (~100 us) before we are able to reprogram LPTIM_COMPARE register back to back.
This is handled by the low level lp ticker wrapper which uses LPTIM_CMPOK interrupt. CMPOK fires when LPTIM_COMPARE register can be safely reprogrammed again.
This means that on these platforms we have additional interrupt (CMPOK) fired always ~100 us after programming lp ticker.
Since this interrupt wake-ups the board from the sleep we need to go to sleep after CMPOK is handled.

Background:
There is an errata in LPTIM specification that explains that CMP Flag
condition is not an exact match (COUNTER = MATCH) but rather a
comparison (COUNTER >= MATCH).

As a consequence the interrupt is firing early than expected when
programing a timestamp after the 0xFFFF wrap-around.

In order to
work-around this issue, we implement the below work-around.
In case timestamp is after the work-around, let's decide to program the
CMP value to 0xFFFF, which is the wrap-around value. There would anyway be
a wake-up at the time of wrap-around to let the OS update the system time.
When the wrap-around interrupt happen, OS will check the current time and
program again the timestamp to the proper value.
pull/11023/head
Przemyslaw Stekiel 2019-04-30 09:42:34 +02:00 committed by Evelyne Donnaes
parent 2679f2b6fc
commit f1044a7de5
1 changed files with 24 additions and 2 deletions

View File

@ -122,8 +122,27 @@ void deepsleep_lpticker_test()
lp_ticker_set_interrupt(next_match_timestamp);
/* On some targets like STM family boards with LPTIM enabled there is a required delay (~100 us) before we are able to
reprogram LPTIM_COMPARE register back to back. This is handled by the low level lp ticker wrapper which uses LPTIM_CMPOK interrupt.
CMPOK fires when LPTIM_COMPARE register can be safely reprogrammed again. During this period deep-sleep is locked.
This means that on these platforms we have additional interrupt (CMPOK) fired always ~100 us after programming lp ticker.
Since this interrupt wakes-up the board from the sleep we need to go to sleep after CMPOK is handled. */
TEST_ASSERT_TRUE(sleep_manager_can_deep_sleep_test_check());
sleep();
/* On some targets like STM family boards with LPTIM enabled an interrupt is triggered on counter rollover.
We need special handling for cases when next_match_timestamp < start_timestamp (interrupt is to be fired after rollover).
In such case after first wake-up we need to reset interrupt and go back to sleep waiting for the valid one.
NOTE: Above comment (CMPOK) applies also here.*/
#if MBED_CONF_TARGET_LPTICKER_LPTIM
if ((next_match_timestamp < start_timestamp) && lp_ticker_read() < next_match_timestamp) {
lp_ticker_set_interrupt(next_match_timestamp);
wait_ns(200000);
sleep();
}
#endif
const timestamp_t wakeup_timestamp = lp_ticker_read();
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()
TEST_ASSERT_TRUE_MESSAGE(sleep_manager_can_deep_sleep(), "deep sleep should not be locked");
const unsigned int us_ticks_before_sleep = us_ticker_read();
const timestamp_t wakeup_time = lp_ticker_read() + us_to_ticks(20000, lp_ticker_freq);
lp_ticker_set_interrupt(wakeup_time);
/* Wait for CMPOK */
TEST_ASSERT_TRUE(sleep_manager_can_deep_sleep_test_check());
const unsigned int us_ticks_before_sleep = us_ticker_read();
sleep();
const unsigned int us_ticks_after_sleep = us_ticker_read();