Question
LSE not starting when used for RTC
Posted on July 05, 2013 at 13:21
I am trying to use a 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.
int
main (
void
)
{
uint32_t count=0x200000;
RTC_TimeTypeDef RTC_TimeStructure;
RTC_DateTypeDef RTC_DateStructure;
RTC_InitTypeDef RTC_InitStructure;
// Turn on PWR clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
// Allow access to RTC
PWR_BackupAccessCmd(ENABLE);
//Forces reset of RTC
RCC_BackupResetCmd(ENABLE);
RCC_BackupResetCmd(DISABLE);
//PWR_DeInit();
// Enable the LSE OSC
//768 External Osc
RCC_LSEConfig(RCC_LSE_ON);
//RCC_LSEDriveConfig(RCC_LSEDrive_High);
//Check for clock stability
while
( (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) )
if
(--count==0)
{
return
-1;
}
// Select the RTC Clock Source
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
// Enable the RTC Clock
RCC_RTCCLKCmd(ENABLE);
// Wait for RTC APB registers synchronisation
if
( RTC_WaitForSynchro() == ERROR )
return
-2;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
//768kHz Clock is divided by (0x40 * 0x200) which
//gives a 1Hz output from 768kHz crystal
RTC_InitStructure.RTC_AsynchPrediv = 0x40;
//Max 0x7F
RTC_InitStructure.RTC_SynchPrediv = 0x0200;
//Max 0x1FFF
if
( RTC_Init(&RTC_InitStructure) == ERROR )
return
-3;
while
(1)
{
RTC_TimeStructInit(&RTC_TimeStructure);
RTC_DateStructInit(&RTC_DateStructure);
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);
}
}
#lse #rtc