Modified for BG96

Fork of mbed-dev by mbed official

Revision:
187:0387e8f68319
Parent:
186:707f6e361f3e
diff -r 707f6e361f3e -r 0387e8f68319 targets/TARGET_STM/lp_ticker.c
--- a/targets/TARGET_STM/lp_ticker.c	Fri Jun 22 16:45:37 2018 +0100
+++ b/targets/TARGET_STM/lp_ticker.c	Thu Sep 06 13:40:20 2018 +0100
@@ -40,7 +40,7 @@
 
 LPTIM_HandleTypeDef LptimHandle;
 
-const ticker_info_t* lp_ticker_get_info()
+const ticker_info_t *lp_ticker_get_info()
 {
     static const ticker_info_t info = {
 #if MBED_CONF_TARGET_LSE_AVAILABLE
@@ -60,18 +60,11 @@
 
 void lp_ticker_init(void)
 {
-    NVIC_DisableIRQ(LPTIM1_IRQn);
-
     /* Check if LPTIM is already configured */
-#if (TARGET_STM32L0)
-    if (READ_BIT(RCC->APB1ENR, RCC_APB1ENR_LPTIM1EN) != RESET) {
+    if (__HAL_RCC_LPTIM1_IS_CLK_ENABLED()) {
+        lp_ticker_disable_interrupt();
         return;
     }
-#else
-    if (__HAL_RCC_LPTIM1_IS_CLK_ENABLED()) {
-        return;
-    }
-#endif
 
     RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct = {0};
     RCC_OscInitTypeDef RCC_OscInitStruct = {0};
@@ -153,6 +146,7 @@
     __HAL_LPTIM_ENABLE_IT(&LptimHandle, LPTIM_IT_CMPM);
     HAL_LPTIM_Counter_Start(&LptimHandle, 0xFFFF);
 
+    /* Need to write a compare value in order to get LPTIM_FLAG_CMPOK in set_interrupt */
     __HAL_LPTIM_COMPARE_SET(&LptimHandle, 0);
 }
 
@@ -201,7 +195,7 @@
     irq_handler = (void (*)(void))lp_ticker_irq_handler;
 
     /* CMPOK is set by hardware to inform application that the APB bus write operation to the LPTIM_CMP register has been successfully completed */
-    /* Any successive write before respectively the ARROK flag or the CMPOK flag be set, will lead to unpredictable results */
+    /* Any successive write before the CMPOK flag be set, will lead to unpredictable results */
     while (__HAL_LPTIM_GET_FLAG(&LptimHandle, LPTIM_FLAG_CMPOK) == RESET) {
     }
 
@@ -221,8 +215,11 @@
 
 void lp_ticker_disable_interrupt(void)
 {
+    NVIC_DisableIRQ(LPTIM1_IRQn);
     LptimHandle.Instance = LPTIM1;
-    NVIC_DisableIRQ(LPTIM1_IRQn);
+    /* Waiting last write operation completion */
+    while (__HAL_LPTIM_GET_FLAG(&LptimHandle, LPTIM_FLAG_CMPOK) == RESET) {
+    }
 }
 
 void lp_ticker_clear_interrupt(void)
@@ -241,10 +238,10 @@
 
 #include "rtc_api_hal.h"
 
-const ticker_info_t* lp_ticker_get_info()
+const ticker_info_t *lp_ticker_get_info()
 {
     static const ticker_info_t info = {
-        RTC_CLOCK/4, // RTC_WAKEUPCLOCK_RTCCLK_DIV4
+        RTC_CLOCK / 4, // RTC_WAKEUPCLOCK_RTCCLK_DIV4
         32
     };
     return &info;
@@ -263,6 +260,7 @@
 
 void lp_ticker_set_interrupt(timestamp_t timestamp)
 {
+    lp_ticker_disable_interrupt();
     rtc_set_wake_up_timer(timestamp);
 }