diff --git a/targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_rtc.c b/targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_rtc.c index d0da8f5235..f2f6359287 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_rtc.c +++ b/targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_rtc.c @@ -188,8 +188,6 @@ /** @defgroup RTC_Private_Functions RTC Private Functions * @{ */ -static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc); -static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter); static uint32_t RTC_ReadAlarmCounter(RTC_HandleTypeDef* hrtc); static HAL_StatusTypeDef RTC_WriteAlarmCounter(RTC_HandleTypeDef* hrtc, uint32_t AlarmCounter); static HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc); @@ -1355,7 +1353,7 @@ HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc) * the configuration information for RTC. * @retval Time counter */ -static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc) +uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc) { uint16_t high1 = 0U, high2 = 0U, low = 0U; uint32_t timecounter = 0U; @@ -1385,10 +1383,10 @@ static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc) * @param TimeCounter: Counter to write in RTC_CNT registers * @retval HAL status */ -static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter) +HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter) { HAL_StatusTypeDef status = HAL_OK; - + /* Set Initialization mode */ if(RTC_EnterInitMode(hrtc) != HAL_OK) { diff --git a/targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_rtc.h b/targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_rtc.h index 792a9338db..8752c73dd4 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_rtc.h +++ b/targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_rtc.h @@ -518,6 +518,9 @@ HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTim HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format); HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format); HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format); + +uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc); +HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter); /** * @} */ diff --git a/targets/TARGET_STM/rtc_api.c b/targets/TARGET_STM/rtc_api.c index a27f774598..7895a4c5f9 100644 --- a/targets/TARGET_STM/rtc_api.c +++ b/targets/TARGET_STM/rtc_api.c @@ -141,51 +141,15 @@ Information about STM32F1: For date, there is no specific register, only a software structure. It is then not a problem to not use shifts. */ -#if TARGET_STM32F1 time_t rtc_read(void) { - RTC_DateTypeDef dateStruct = {0}; - RTC_TimeTypeDef timeStruct = {0}; - struct tm timeinfo; +#if TARGET_STM32F1 RtcHandle.Instance = RTC; - - // Read actual date and time - // Warning: the time must be read first! - HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN); - HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN); - - /* date information is null before first write procedure */ - /* set 01/01/1970 as default values */ - if (dateStruct.Year == 0) { - dateStruct.Year = 2 ; - dateStruct.Month = 1 ; - dateStruct.Date = 1 ; - } - - // Setup a tm structure based on the RTC - /* tm_wday information is ignored by _rtc_maketime */ - /* tm_isdst information is ignored by _rtc_maketime */ - timeinfo.tm_mon = dateStruct.Month - 1; - timeinfo.tm_mday = dateStruct.Date; - timeinfo.tm_year = dateStruct.Year + 68; - timeinfo.tm_hour = timeStruct.Hours; - timeinfo.tm_min = timeStruct.Minutes; - timeinfo.tm_sec = timeStruct.Seconds; - - // Convert to timestamp - time_t t; - if (_rtc_maketime(&timeinfo, &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) { - return 0; - } - - return t; -} + return RTC_ReadTimeCounter(&RtcHandle); #else /* TARGET_STM32F1 */ -time_t rtc_read(void) -{ struct tm timeinfo; /* Since the shadow registers are bypassed we have to read the time twice and compare them until both times are the same */ @@ -223,12 +187,23 @@ time_t rtc_read(void) } return t; -} #endif /* TARGET_STM32F1 */ +} + + void rtc_write(time_t t) { +#if TARGET_STM32F1 + + RtcHandle.Instance = RTC; + if (RTC_WriteTimeCounter(&RtcHandle, t) != HAL_OK) { + error("RTC_WriteTimeCounter error\n"); + } + +#else /* TARGET_STM32F1 */ + RTC_DateTypeDef dateStruct = {0}; RTC_TimeTypeDef timeStruct = {0}; @@ -253,12 +228,9 @@ void rtc_write(time_t t) timeStruct.Hours = timeinfo.tm_hour; timeStruct.Minutes = timeinfo.tm_min; timeStruct.Seconds = timeinfo.tm_sec; - -#if !(TARGET_STM32F1) timeStruct.TimeFormat = RTC_HOURFORMAT_24; timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; -#endif /* TARGET_STM32F1 */ #if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM /* Before setting the new time, we need to update the LPTICKER_counter value */ @@ -280,6 +252,7 @@ void rtc_write(time_t t) } core_util_critical_section_exit(); +#endif /* TARGET_STM32F1 */ } int rtc_isenabled(void)