AnsweredAssumed Answered

LSE not starting when used for RTC

Question asked by grealish.ashley on Jul 5, 2013
Latest reply on Jul 5, 2013 by waclawek.jan
I am trying to use a 32.768kHz Xtal with 7pF load capacitors with an STM32F050C6. The problem I am having is that I cannot get the XTAL to reliably start up. I have seen it start a few times but most of the time it will not start.

I have attempted to use the same code with the recommended XTAL and caps on the STM32F0 Discovery board and I have the same problem. 

Has anyone got the LSE working with the RTC and if so would you be so kind to point out any issues with my code. 

The code gets stuck on line 24 until the timeout forces it to stop. Without the timeout the code gets stuck in the loop indefinitely.

01.int main (void)
02.{
03.    uint32_t count=0x200000;
04.     
05.    RTC_TimeTypeDef   RTC_TimeStructure;
06.    RTC_DateTypeDef     RTC_DateStructure;
07.    RTC_InitTypeDef   RTC_InitStructure;
08.         
09.    // Turn on PWR clock
10.    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
11. 
12.    // Allow access to RTC
13.  PWR_BackupAccessCmd(ENABLE);
14.     
15.    //Forces reset of RTC
16.    RCC_BackupResetCmd(ENABLE);
17.  RCC_BackupResetCmd(DISABLE);
18.         
19.    //PWR_DeInit();
20.     
21.  // Enable the LSE OSC
22.    //32.768 External Osc
23.  RCC_LSEConfig(RCC_LSE_ON);
24.    //RCC_LSEDriveConfig(RCC_LSEDrive_High);
25.     
26.    //Check for clock stability
27.    while ( (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) )
28.        if (--count==0)
29.        {
30.            return -1;
31.        }
32.     
33.  // Select the RTC Clock Source
34.  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
35.   
36.  // Enable the RTC Clock
37.  RCC_RTCCLKCmd(ENABLE);
38.   
39.  // Wait for RTC APB registers synchronisation
40.  if ( RTC_WaitForSynchro() == ERROR )
41.        return -2;
42.   
43.  RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
44.    //32.768kHz Clock is divided by (0x40 * 0x200) which
45.    //gives a 1Hz output from 32.768kHz crystal
46.  RTC_InitStructure.RTC_AsynchPrediv = 0x40;    //Max 0x7F
47.  RTC_InitStructure.RTC_SynchPrediv = 0x0200;   //Max 0x1FFF
48.   
49.  if ( RTC_Init(&RTC_InitStructure) == ERROR )
50.        return -3;
51.     
52.    while(1)
53.    {
54.        RTC_TimeStructInit(&RTC_TimeStructure);
55.        RTC_DateStructInit(&RTC_DateStructure);
56. 
57.        RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
58.        RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);
59.    }
60.}

Outcomes