forked

Revision:
160:d5399cc887bb
Parent:
154:37f96f9d4de2
Child:
165:e614a9f1c9e2
--- a/targets/TARGET_STM/rtc_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_STM/rtc_api.c	Tue Mar 14 16:40:56 2017 +0000
@@ -82,9 +82,7 @@
     if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
         error("PeriphClkInitStruct RTC failed with LSE\n");
     }
-
 #else /* !RTC_LSI */
-
     __PWR_CLK_ENABLE();
 
     // Reset Backup domain
@@ -108,7 +106,6 @@
     if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
         error("PeriphClkInitStruct RTC failed with LSI\n");
     }
-
 #endif /* !RTC_LSI */
 
     // Enable RTC
@@ -173,11 +170,23 @@
 }
 
 /*
- RTC Registers
-   RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday
-   RTC_Month   1=january, 2=february, ..., 12=december
-   RTC_Date    day of the month 1-31
-   RTC_Year    year 0-99
+ ST RTC_DateTypeDef structure
+   WeekDay 1=monday, 2=tuesday, ..., 7=sunday
+   Month   0x1=january, 0x2=february, ..., 0x12=december
+   Date    day of the month 1-31
+   Year    year 0-99
+
+ ST RTC_TimeTypeDef structure
+  Hours           0-12 if the RTC_HourFormat_12 is selected during init
+                  0-23 if the RTC_HourFormat_24 is selected during init
+  Minutes         0-59
+  Seconds         0-59
+  TimeFormat      RTC_HOURFORMAT12_AM/RTC_HOURFORMAT12_PM
+  SubSeconds      time unit range between [0-1] Second with [1 Sec / SecondFraction +1] granularity
+  SecondFraction  range or granularity of Sub Second register content corresponding to Synchronous pre-scaler factor value (PREDIV_S)
+  DayLightSaving  RTC_DAYLIGHTSAVING_SUB1H/RTC_DAYLIGHTSAVING_ADD1H/RTC_DAYLIGHTSAVING_NONE
+  StoreOperation  RTC_STOREOPERATION_RESET/RTC_STOREOPERATION_SET
+
  struct tm
    tm_sec      seconds after the minute 0-61
    tm_min      minutes after the hour 0-59
@@ -189,6 +198,22 @@
    tm_yday     days since January 1 0-365
    tm_isdst    Daylight Saving Time flag
 */
+
+/*
+Information about STM32F0, STM32F2, STM32F3, STM32F4, STM32F7, STM32L0, STM32L1, STM32L4:
+BCD format is used to store the date in the RTC. The year is store on 2 * 4 bits.
+Because the first year is reserved to see if the RTC is init, the supposed range is 01-99.
+1st point is to cover the standard range from 1970 to 2038 (limited by the 32 bits of time_t).
+2nd point is to keep the year 1970 and the leap years synchronized.
+
+So by moving it 68 years forward from 1970, it become 1969-2067 which include 1970-2038.
+68 is also a multiple of 4 so it let the leap year synchronized.
+
+Information about STM32F1:
+32bit register is used (no BCD format) for the seconds and a software structure to store dates.
+It is then not a problem to not use shifts.
+*/
+
 time_t rtc_read(void)
 {
     RTC_DateTypeDef dateStruct;
@@ -199,11 +224,11 @@
 
     // Read actual date and time
     // Warning: the time must be read first!
-    HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
-    HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
+    HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
+    HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
 
     // Setup a tm structure based on the RTC
-    timeinfo.tm_wday = dateStruct.WeekDay;
+    /* tm_wday information is ignored by mktime */
     timeinfo.tm_mon  = dateStruct.Month - 1;
     timeinfo.tm_mday = dateStruct.Date;
     timeinfo.tm_year = dateStruct.Year + 68;
@@ -230,7 +255,11 @@
     struct tm *timeinfo = localtime(&t);
 
     // Fill RTC structures
-    dateStruct.WeekDay        = timeinfo->tm_wday;
+    if (timeinfo->tm_wday == 0) {
+        dateStruct.WeekDay    = 7;
+    } else {
+        dateStruct.WeekDay    = timeinfo->tm_wday;
+    }
     dateStruct.Month          = timeinfo->tm_mon + 1;
     dateStruct.Date           = timeinfo->tm_mday;
     dateStruct.Year           = timeinfo->tm_year - 68;
@@ -245,21 +274,17 @@
 #endif /* TARGET_STM32F1 */
 
     // Change the RTC current date/time
-    HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
-    HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
+    HAL_RTC_SetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
+    HAL_RTC_SetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
 }
 
 int rtc_isenabled(void)
 {
-#if DEVICE_LOWPOWERTIMER
-    if ((RTC->ISR & RTC_ISR_INITS) ==  RTC_ISR_INITS) {
-        return 1;
-    } else {
-        return 0;
-    }
-#else /* DEVICE_LOWPOWERTIMER */
-    return 1;
-#endif /* DEVICE_LOWPOWERTIMER */
+#if !(TARGET_STM32F1)
+    return ( ((RTC->ISR & RTC_ISR_INITS) ==  RTC_ISR_INITS) && ((RTC->ISR & RTC_ISR_RSF) ==  RTC_ISR_RSF) );
+#else /* TARGET_STM32F1 */
+    return ((RTC->CRL & RTC_CRL_RSF) ==  RTC_CRL_RSF);
+#endif /* TARGET_STM32F1 */
 }
 
 #if DEVICE_LOWPOWERTIMER
@@ -308,5 +333,4 @@
 }
 #endif /* DEVICE_LOWPOWERTIMER */
 
-
 #endif /* DEVICE_RTC */