mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Revision:
181:57724642e740
Parent:
180:96ed750bd169
Child:
182:a56a73fd2a6f
--- a/targets/TARGET_STM/rtc_api.c	Wed Jan 17 15:23:54 2018 +0000
+++ b/targets/TARGET_STM/rtc_api.c	Fri Feb 16 16:09:33 2018 +0000
@@ -31,15 +31,18 @@
 #if DEVICE_RTC
 
 #include "rtc_api_hal.h"
-#include "mbed_error.h"
 #include "mbed_mktime.h"
 
 static RTC_HandleTypeDef RtcHandle;
 
-#if DEVICE_LOWPOWERTIMER
+#if DEVICE_LOWPOWERTIMER && !MBED_CONF_TARGET_LOWPOWERTIMER_LPTIM
+
+#define GET_TICK_PERIOD(VALUE) (2048 * 1000000 / VALUE) /* 1s / SynchPrediv value * 2^11 (value to get the maximum precision value with no u32 overflow) */
+
 static void (*irq_handler)(void);
 static void RTC_IRQHandler(void);
-#endif
+static uint32_t lp_TickPeriod_us = GET_TICK_PERIOD(4095); /* default SynchPrediv value = 4095 */
+#endif /* DEVICE_LOWPOWERTIMER && !MBED_CONF_TARGET_LOWPOWERTIMER_LPTIM */
 
 void rtc_init(void)
 {
@@ -55,7 +58,7 @@
     }
 
 #if MBED_CONF_TARGET_LSE_AVAILABLE
-    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_LSE;
+    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
     RCC_OscInitStruct.PLL.PLLState   = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
     RCC_OscInitStruct.LSEState       = RCC_LSE_ON;
     RCC_OscInitStruct.LSIState       = RCC_LSI_OFF;
@@ -78,7 +81,7 @@
     __HAL_RCC_BACKUPRESET_RELEASE();
 
     // Enable LSI clock
-    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_LSE;
+    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
     RCC_OscInitStruct.PLL.PLLState   = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
     RCC_OscInitStruct.LSEState       = RCC_LSE_OFF;
     RCC_OscInitStruct.LSIState       = RCC_LSI_ON;
@@ -108,7 +111,7 @@
     RtcHandle.Init.HourFormat     = RTC_HOURFORMAT_24;
 
     /* PREDIV_A : 7-bit asynchronous prescaler */
-#if DEVICE_LOWPOWERTIMER
+#if DEVICE_LOWPOWERTIMER && !MBED_CONF_TARGET_LOWPOWERTIMER_LPTIM
     /* PREDIV_A is set to a small value to improve the SubSeconds resolution */
     /* with a 32768Hz clock, PREDIV_A=7 gives a precision of 244us */
     RtcHandle.Init.AsynchPrediv = 7;
@@ -124,6 +127,10 @@
     RtcHandle.Init.OutPutType     = RTC_OUTPUT_TYPE_OPENDRAIN;
 #endif /* TARGET_STM32F1 */
 
+#if DEVICE_LOWPOWERTIMER && !MBED_CONF_TARGET_LOWPOWERTIMER_LPTIM
+    lp_TickPeriod_us = GET_TICK_PERIOD(RtcHandle.Init.SynchPrediv);
+#endif
+
     if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
         error("RTC initialization failed");
     }
@@ -294,7 +301,7 @@
     }
 }
 
-#if DEVICE_LOWPOWERTIMER
+#if DEVICE_LOWPOWERTIMER && !MBED_CONF_TARGET_LOWPOWERTIMER_LPTIM
 
 static void RTC_IRQHandler(void)
 {
@@ -307,9 +314,27 @@
     }
 }
 
-uint32_t rtc_read_subseconds(void)
+uint32_t rtc_read_us(void)
 {
-    return 1000000.f * ((double)((RTC->PRER & RTC_PRER_PREDIV_S) - RTC->SSR) / ((RTC->PRER & RTC_PRER_PREDIV_S) + 1));
+    RTC_TimeTypeDef timeStruct = {0};
+    RTC_DateTypeDef dateStruct = {0};
+
+    RtcHandle.Instance = RTC;
+    HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
+
+    /* Reading RTC current time locks the values in calendar shadow registers until Current date is read
+    to ensure consistency between the time and date values */
+    HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
+
+    if (timeStruct.SubSeconds > timeStruct.SecondFraction) {
+        /* SS can be larger than PREDIV_S only after a shift operation. In that case, the correct
+           time/date is one second less than as indicated by RTC_TR/RTC_DR. */
+        timeStruct.Seconds -= 1;
+    }
+    uint32_t RTCTime = timeStruct.Seconds + timeStruct.Minutes * 60 + timeStruct.Hours * 60 * 60;
+    uint32_t Time_us = ((timeStruct.SecondFraction - timeStruct.SubSeconds) * lp_TickPeriod_us) >> 11;
+
+    return (RTCTime * 1000000) + Time_us ;
 }
 
 void rtc_set_wake_up_timer(uint32_t delta)
@@ -342,7 +367,7 @@
     NVIC_EnableIRQ(RTC_WKUP_IRQn);
 
     RtcHandle.Instance = RTC;
-    if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 0xFFFF & WakeUpCounter, WakeUpClock[DivIndex-1]) != HAL_OK) {
+    if (HAL_RTCEx_SetWakeUpTimer_IT(&RtcHandle, 0xFFFF & WakeUpCounter, WakeUpClock[DivIndex - 1]) != HAL_OK) {
         error("rtc_set_wake_up_timer init error (%d)\n", DivIndex);
     }
 }
@@ -353,6 +378,6 @@
     HAL_RTCEx_DeactivateWakeUpTimer(&RtcHandle);
 }
 
-#endif /* DEVICE_LOWPOWERTIMER */
+#endif /* DEVICE_LOWPOWERTIMER && !MBED_CONF_TARGET_LOWPOWERTIMER_LPTIM */
 
 #endif /* DEVICE_RTC */