Skip to main content
arnaud2399
Associate
November 29, 2011
Question

RTC initialization

  • November 29, 2011
  • 8 replies
  • 6453 views
Posted on November 29, 2011 at 17:56

Our design is made with  a STM32F207ZG microcontroller and we have some problems with the RTC peripheral.

We have an external 32.768 kHz oscillator (LSE).        

Using the ST library, the initialization of the RTC goes out with ERROR on test of ISR.RSF bit and ISR.INITF bit in

RTC_WaitForSynchro() and RTC_Init(&RTC_InitStructure) functions.

We checked the signal 32 KHz on the MC01 (PA8) pin and it is OK.

If we try with LSI clock source, it is OK : no ERROR.

What happens ? Do you have an idea ?

This is our source code (with RTX kernel) :

int main (void)

{

    SystemInit();

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

    PWR_BackupAccessCmd(ENABLE);       

    RCC_LSEConfig(RCC_LSE_ON);

           …

}

__task void _HORLOGE_Initialiser (void)

{

while (1)

 {           

    while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);

    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

    RCC_RTCCLKCmd(ENABLE);

    RTC_WaitForSynchro();

    // Calendar Configuration 

    RTC_InitStructure.RTC_AsynchPrediv = 0x7F;

    RTC_InitStructure.RTC_SynchPrediv =  0xFF;

    RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;

    RTC_Init(&RTC_InitStructure);

     os_tsk_delete_self ();

    } ;

}

    This topic has been closed for replies.

    8 replies

    danieldueringer9
    Visitor II
    January 6, 2012
    Posted on January 06, 2012 at 14:13

    Could you solve your problem? I also have a STM32F207 and initialize the RTC like you (LSE), but I have no problem with the initialization itself. My problem is that when I run power cycles (I have a battery) the RTC looses seconds!

    balmukund66
    Associate III
    January 10, 2012
    Posted on January 10, 2012 at 12:13

    Use code like this:

    int main (void)

    {

     SystemInit(); 

    /* Enable the PWR clock */

      RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

      /* Allow access to RTC */

      PWR_BackupAccessCmd(ENABLE);

    /***Configures the External Low Speed oscillator (LSE)****/

     

      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);

      

      /* Enable the RTC Clock */

      RCC_RTCCLKCmd(ENABLE);

      /* Wait for RTC APB registers synchronisation */

      RTC_WaitForSynchro();

      /* Calendar Configuration with LSI supposed at 32KHz */

      RTC_InitStructure.RTC_AsynchPrediv = 0x7F;

      RTC_InitStructure.RTC_SynchPrediv =  0xFF; 

      RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;

      RTC_Init(&RTC_InitStructure);

      .......

           while(1)

           {

            .....  //Read RTC Time Register

            ......

           }

               …

    }

    __task void _HORLOGE_Initialiser (void)

    {

    while (1)

     {          

     

         os_tsk_delete_self ();

        } ;

    }
    rombeusz
    Visitor II
    August 21, 2012
    Posted on August 21, 2012 at 14:37

    I have the same problem. I'm working on STM32F217VGT6 and when I want to configure LSE as the source clock of RTC it returns ERROR on synchonization and prescalers initialization. Did You found solution for this problem? Would be grateful for any help.

    Tesla DeLorean
    Guru
    August 21, 2012
    Posted on August 21, 2012 at 15:01

    PWR/BKP domain needs to be clocked, and unlocked.

    32KHz crystal needs to be 6pF or less.

    Verify clock is running.

    Show code.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    rombeusz
    Visitor II
    August 21, 2012
    Posted on August 21, 2012 at 16:03

    I'm using a 6pF cristal with 12pF external capacitors.

    LSE clock is running, I've checked it on MCO1 output and it's stable.

    Here is code:

    (I'm using stm32f2xx_stdperiph_lib)

    int main(void)

    {

    SystemInit();

    rcc_init();

    RTC_init();

    IO_init();

    while(1){

    }

    }

    static void rcc_init(void)

    {

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD |

    RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_BKPSRAM,ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_SPI1 | RCC_APB2Periph_TIM1 | RCC_APB2Periph_SYSCFG,ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB1Periph_USART3 | RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |

    RCC_APB1Periph_TIM4 | RCC_APB1Periph_TIM5 | RCC_APB1Periph_SPI3 | RCC_APB1Periph_PWR , ENABLE);

    }

    void RTC_init(void){

    # Reset power domain

    PWR_DeInit();

    # Allow access to RTC

    PWR_BackupAccessCmd(ENABLE);

    # Write protection DISABLE

    RTC_WriteProtectionCmd(DISABLE);

    # Enable LSE clock

    RCC_LSEConfig(RCC_LSE_ON);

    # Wait till LSE is ready

    while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);

    # COnfig MCO1 output

    RCC_MCO1Config(RCC_MCO1Source_LSE,RCC_MCO1Div_1);

    # Select LSE as RTC Clock Source

    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

    # Enable RTC Clock

    RCC_RTCCLKCmd(ENABLE);

    # Wait for RTC registers synchronization

    uint8_t odp = RTC_WaitForSynchro();

    if(odp != 0){

    #synchro OK

    LED1_ON();LED2_OFF();

    }else{

    #synchro ERROR

    LED2_ON();LED1_OFF();

    }

    }

    Maybe i'm missing some thing obvious.

    subscriptions
    Associate III
    September 16, 2014
    Posted on September 16, 2014 at 12:10

    We had this problem with our system too.

    If we couldn't get synchronization (INITF set true), then we issued

    RCC_BackupResetCmd(

    ENABLE

    );

    followed by

    RCC_BackupResetCmd(DIS

    ABLE

    );

    which made it work next time we tried.  We think it was caused by our battery backup voltage dropping too low.

    vneff
    Associate III
    October 14, 2014
    Posted on October 14, 2014 at 15:35

    I had a similar problem.

    I found that I had to increase the timeout (SYNCHRO_TIMEOUT) for the RTC_WaitForSynchro() function from 0x008000 to 0x0F0000.  I tried 0x010000 & 0x040000, but they did not consistently solve the problem, though 0x040000 was better.

    Vance

    signx
    Associate
    November 27, 2014
    Posted on November 27, 2014 at 12:02

    I has the same problem with RTC_ISR_INITF flag. 

    I found error in usb_bsp.c file.

      /* enable the PWR clock */

      RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE); 

      ----->>>

      RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);