From a7c293fac078946ba544204a3fe67d930bdc82b7 Mon Sep 17 00:00:00 2001 From: tyustli <1225613647@qq.com> Date: Tue, 12 May 2020 14:08:24 +0800 Subject: [PATCH] [bsp] fix stm32 f1 series rtc bug --- bsp/stm32/libraries/HAL_Drivers/drv_rtc.c | 62 ++++++++++++++++++++--- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_rtc.c b/bsp/stm32/libraries/HAL_Drivers/drv_rtc.c index f62ed0b8f0..380cce7624 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_rtc.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_rtc.c @@ -12,13 +12,6 @@ #ifdef BSP_USING_ONCHIP_RTC - -#ifndef HAL_RTCEx_BKUPRead -#define HAL_RTCEx_BKUPRead(x1, x2) (~BKUP_REG_DATA) -#endif -#ifndef HAL_RTCEx_BKUPWrite -#define HAL_RTCEx_BKUPWrite(x1, x2, x3) -#endif #ifndef RTC_BKP_DR1 #define RTC_BKP_DR1 RT_NULL #endif @@ -33,6 +26,16 @@ static struct rt_device rtc; static RTC_HandleTypeDef RTC_Handler; +RT_WEAK uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister) +{ + return (~BKUP_REG_DATA); +} + +RT_WEAK void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data) +{ + return; +} + static time_t get_rtc_timestamp(void) { RTC_TimeTypeDef RTC_TimeStruct = {0}; @@ -84,6 +87,13 @@ static rt_err_t set_rtc_time_stamp(time_t time_stamp) LOG_D("set rtc time."); HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA); +#ifdef SOC_SERIES_STM32F1 + HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR2, RTC_DateStruct.Year); + HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR3, RTC_DateStruct.Month); + HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR4, RTC_DateStruct.Date); + HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR5, RTC_DateStruct.WeekDay); +#endif + return RT_EOK; } @@ -108,6 +118,27 @@ static void rt_rtc_init(void) HAL_RCC_OscConfig(&RCC_OscInitStruct); } +#ifdef SOC_SERIES_STM32F1 +/* update RTC_BKP_DRx*/ +static void rt_rtc_bkp_update(void) +{ + RTC_DateTypeDef RTC_DateStruct = {0}; + + HAL_PWR_EnableBkUpAccess(); + __HAL_RCC_BKP_CLK_ENABLE(); + + HAL_RTC_GetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN); + if (HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR4) != RTC_DateStruct.Date) + { + HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA); + HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR2, RTC_DateStruct.Year); + HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR3, RTC_DateStruct.Month); + HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR4, RTC_DateStruct.Date); + HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR5, RTC_DateStruct.WeekDay); + } +} +#endif + static rt_err_t rt_rtc_config(struct rt_device *dev) { RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; @@ -167,6 +198,23 @@ static rt_err_t rt_rtc_config(struct rt_device *dev) return -RT_ERROR; } } +#ifdef SOC_SERIES_STM32F1 + else + { + RTC_DateTypeDef RTC_DateStruct = {0}; + + RTC_DateStruct.Year = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR2); + RTC_DateStruct.Month = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR3); + RTC_DateStruct.Date = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR4); + RTC_DateStruct.WeekDay = HAL_RTCEx_BKUPRead(&RTC_Handler, RTC_BKP_DR5); + if (HAL_RTC_SetDate(&RTC_Handler, &RTC_DateStruct, RTC_FORMAT_BIN) != HAL_OK) + { + Error_Handler(); + } + rt_rtc_bkp_update(); + } +#endif + return RT_EOK; }