From 6452eb317251dcd888f4ec8ef9a171b8b86c97b1 Mon Sep 17 00:00:00 2001 From: Laurent Meunier Date: Fri, 29 Mar 2019 14:51:14 +0100 Subject: [PATCH] LP ticker workaround 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. --- targets/TARGET_STM/lp_ticker.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_STM/lp_ticker.c b/targets/TARGET_STM/lp_ticker.c index 223bf42d3d..f58d20d2c5 100644 --- a/targets/TARGET_STM/lp_ticker.c +++ b/targets/TARGET_STM/lp_ticker.c @@ -226,8 +226,15 @@ void lp_ticker_set_interrupt(timestamp_t timestamp) /* Any successive write before the CMPOK flag be set, will lead to unpredictable results */ /* LPTICKER_DELAY_TICKS value prevents OS to call this set interrupt function before CMPOK */ MBED_ASSERT(__HAL_LPTIM_GET_FLAG(&LptimHandle, LPTIM_FLAG_CMPOK) == SET); - __HAL_LPTIM_CLEAR_FLAG(&LptimHandle, LPTIM_FLAG_CMPOK); - __HAL_LPTIM_COMPARE_SET(&LptimHandle, timestamp); + + if(timestamp < lp_ticker_read()) { + /* Workaround, because limitation */ + __HAL_LPTIM_CLEAR_FLAG(&LptimHandle, LPTIM_FLAG_CMPOK); + __HAL_LPTIM_COMPARE_SET(&LptimHandle, ~0); + } else { + __HAL_LPTIM_CLEAR_FLAG(&LptimHandle, LPTIM_FLAG_CMPOK); + __HAL_LPTIM_COMPARE_SET(&LptimHandle, timestamp); + } lp_ticker_clear_interrupt();