[bsp] fix stm32 f1 series rtc bug

This commit is contained in:
tyustli 2020-05-12 14:08:24 +08:00
parent b2b8887cea
commit a7c293fac0
1 changed files with 55 additions and 7 deletions

View File

@ -12,13 +12,6 @@
#ifdef BSP_USING_ONCHIP_RTC #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 #ifndef RTC_BKP_DR1
#define RTC_BKP_DR1 RT_NULL #define RTC_BKP_DR1 RT_NULL
#endif #endif
@ -33,6 +26,16 @@ static struct rt_device rtc;
static RTC_HandleTypeDef RTC_Handler; 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) static time_t get_rtc_timestamp(void)
{ {
RTC_TimeTypeDef RTC_TimeStruct = {0}; 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."); LOG_D("set rtc time.");
HAL_RTCEx_BKUPWrite(&RTC_Handler, RTC_BKP_DR1, BKUP_REG_DATA); 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; return RT_EOK;
} }
@ -108,6 +118,27 @@ static void rt_rtc_init(void)
HAL_RCC_OscConfig(&RCC_OscInitStruct); 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) static rt_err_t rt_rtc_config(struct rt_device *dev)
{ {
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
@ -167,6 +198,23 @@ static rt_err_t rt_rtc_config(struct rt_device *dev)
return -RT_ERROR; 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; return RT_EOK;
} }