void RTC_Config(void)
{
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
#if defined (RTC_CLOCK_SOURCE_LSI) /* LSI used as RTC source clock*/
/* The RTC Clock may varies due to LSI frequency dispersion. */
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{
}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
SynchPrediv = 0xFF;
AsynchPrediv = 0x7F;
#elif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock */
/* Enable the LSE OSC */
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
SynchPrediv = 0xFF;
AsynchPrediv = 0x7F;
#else
#error Please select the RTC Clock source inside the main.c file
#endif /* RTC_CLOCK_SOURCE_LSI */
/* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
}
void RTC_init(void)
{
RTC_InitTypeDef RTC_InitStructure;
TFT_Show4Hex(300, 200, RTC->BKP0R, 16, 0);
TFT_Show4Hex(300, 220, RTC->BKP19R, 16, 0);
//if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2)
if(RTC->BKP0R != 0x32F2 && RTC->BKP19R != 0x32F2)
{
/* RTC configuration */
RTC_Config();
RTC_TamperCmd(RTC_Tamper_1, DISABLE);
/* Configure the RTC data register and RTC prescaler */
RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
/* Check on RTC init */
if (RTC_Init(&RTC_InitStructure) == ERROR)
{
TFT_ShowString(0,176,(uint8_t*)"***** RTC Prescaler Config failed ********");
}
/* Configure the time register */
RTC_TimeRegulate(0x12,0x12,0x12,0x03,0,0,0);
PWR_BackupAccessCmd(DISABLE);
}
else
{
/* Check if the Power On Reset flag is set */
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
TFT_ShowString(0,196,(uint8_t*)"Power On Reset occurred....");
}
/* Check if the Pin Reset flag is set */
else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
TFT_ShowString(0,196,(uint8_t*)"External Reset occurred....");
}
TFT_ShowString(0,196,(uint8_t*)"No need to configure RTC....");
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
RTC_TamperCmd(RTC_Tamper_1, DISABLE);
PWR_BackupAccessCmd(DISABLE);
RTC_TimeShow(600, 36);
RTC_DateShow(600, 16);
}
}
|