AnsweredAssumed Answered

STM32F207VE problem with RTC

Question asked by Krzysztof on Jul 14, 2014

Hello

I’m using STM32F207VE and I have problems with RTC. I can configure it and write initialization data (time and date) and RTC seems to work ok. Time counter counts up every second but at 23:59:59 it does not reset but still counts up (24:00:00, 24:00:01 … and so on!)
The second problem is when I write data using function:
RTC_SetDate(RTC_Format_BIN, &RTC_DateStructure)
And after checking RTC registers with JTAG I can see new data inside them. But every time when I call function RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure) it returns old settings..:(
Only after reset RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure) returns valid date, but when I set new date situation repeats….

Bellow my initialization code:


/**
 * @brief  Configure the RTC peripheral by selecting the clock source.
 * @param  None
 * @retval None
 */
void RTC_ConfigX(void)
{
#if (_USE_RTC_)
    {
        RTC_InitTypeDef RTC_InitStructure;
        RTC_TimeTypeDef RTC_TimeStructure;
  
        /* Enable the PWR clock */
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  
        /* Allow access to RTC */
        PWR_BackupAccessCmd(ENABLE);
  
#if(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;
  
        debug_printf(_LVL_5, "RTC_Config LSI clock type setup\n");
  
#elif(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;
  
        debug_printf(_LVL_5, "RTC_Config LSE clock type setup\n");
  
#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();
  
        /* Write to the first RTC Backup Data Register */
        RTC_WriteBackupRegister(RTC_BKP_DR0, FIRST_DATA);
  
        /* Set the Time */
        RTC_TimeStructure.RTC_Hours = 0x00;
        RTC_TimeStructure.RTC_Minutes = 0x00;
        RTC_TimeStructure.RTC_Seconds = 0x00;
  
        /* Set the Date */
  
        /* Calendar Configuration */
        RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
        RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
        RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
        RTC_Init(&RTC_InitStructure);
  
        /* Set Current Time and Date */
        RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure);
        RTC_SetCompilationDate();
  
  
        /*  Backup SRAM ***************************************************************/
        /* Enable BKPRAM Clock */
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
  
      /* Enable the Backup SRAM low power Regulator to retain it's content in VBAT mode */
        PWR_BackupRegulatorCmd(ENABLE);
  
        /* Wait until the Backup SRAM low power Regulator is ready */
        while (PWR_GetFlagStatus(PWR_FLAG_BRR) == RESET)
        {
        }
        /* RTC Backup Data Registers **************************************************/
        /* Write to RTC Backup Data Register */
        RTC_WriteBackupRegister(RTC_BKP0R, FIRST_DATA); //store first data to bacup reg
    }
#endif
}
  
  
/**
 * @brief  Main RTC config function
 * First it chcecks if rtc was configured and is maintain by external
 * bacup voltage source
 * If not RTC_ConfigX is executed
 * @param  None
 * @retval None
 */
RTC_ConfigState_t RTC_Config(void)
{
    RTC_ConfigState_t RTC_ConfigState;
  
#if(_USE_RTC_)
  
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
  
    //read first bacup register and check it has test data valid
    if (RTC_ReadBackupRegister(RTC_BKP_DR0) != FIRST_DATA)
    {
        RTC_ConfigX();
        debug_printf(_LVL_5,"RTC jeszcze nie konfigurowane\r\n");
        RTC_ConfigState=RTC_ConfigState_FirstConfig;
    }
    else
    {
        debug_printf(_LVL_5,"RTC już raz włącznone i skonfigurowane\r\n");
        RTC_ConfigState=RTC_ConfigState_NoNeedToconfig;
  
                /* 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();
  
               /* Clear the RTC Alarm Flag */
               RTC_ClearFlag(RTC_FLAG_ALRAF);
  
               /* Clear the EXTI Line 17 Pending bit (Connected internally to RTC Alarm) */
               EXTI_ClearITPendingBit(EXTI_Line17);
          
               /* Enable the PWR APB1 Clock Interface */
               RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
    }
  
    ///-- Configure RTC Alarm A Interrupt
        EXTI_ClearITPendingBit(EXTI_Line17 );
        EXTI_InitStructure.EXTI_Line = EXTI_Line17;
        EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
        EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
        EXTI_InitStructure.EXTI_LineCmd = ENABLE;
        EXTI_Init(&EXTI_InitStructure);
  
        /* Enable the RTC Alarm Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
#endif
        return RTC_ConfigState;
}
  
/*******************************************************************************
 *
 * @fn RTC_SetCompilationDate
 *  @brief  Set compilation date to RTC
 *  Author: kawel
 * 06-06-2014
 * @warning none
 *
 * @param  void
 *
 * @return void
 *
 *******************************************************************************/
void RTC_SetCompilationDate(void)
{
    uint8_t day, month, year;
    int WeekDay;
    RTC_DateTypeDef RTC_DateStructure;
  
    debug_printf(_LVL_3, "Compilation date: %s \r\n", __DATE__);
  
    if (RTC_GetDateFromCompilationDate(&day, &month, &year) == SUCCESS)
    {
        RTC_DateStructure.RTC_Date=day;
        RTC_DateStructure.RTC_Month=month;
        RTC_DateStructure.RTC_Year=year;
  
        WeekDay=RTC_CalcDayOfWeek2(&RTC_DateStructure);
        RTC_DateStructure.RTC_WeekDay=WeekDay;
  
        RTC_SetDate(RTC_Format_BIN, &RTC_DateStructure);
    }
}

Outcomes