Clock configuration of RTC in Stm32L in LSI/LSE/HSE only?

Ishmeet picture Ishmeet · Sep 2, 2013 · Viewed 17.3k times · Source

I am implementing Real Time Clock on STM32L152RB Discovery board using IAR compiler. I have implemented the Clock configuration on HSI and using PLL I have multiplied it by 4. Code -->

/* Enable HSI Clock */
RCC_HSICmd(ENABLE);

/*!< Wait till HSI is ready */
while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);

RCC_PLLConfig(RCC_PLLSource_HSI,RCC_PLLMul_4,RCC_PLLDiv_2);
RCC_PLLCmd(ENABLE); 
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

/* Set HSI as sys clock*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

The problem is while configuring Real Time clock I have to set the secondary clock LSE as the RTC Clock source, which in my case my source clock is HSI. Rest of the steps which includes enable PWR controller, enable rtc domain access, rtc clock source, rtc_init(), then settime and gettime, are alright as per I know. Here is the code I tried -->

/* Enable RTC clocks and rtc related functions */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
PWR_RTCAccessCmd(ENABLE);

RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);  //This part I think is wrong
RCC_RTCCLKCmd(ENABLE);
RTC_InitTypeStructure.RTC_HourFormat=RTC_HourFormat_12;
RTC_InitTypeStructure.RTC_AsynchPrediv=0x7F;
RTC_InitTypeStructure.RTC_SynchPrediv=0xFF;
RTC_Init(&RTC_InitTypeStructure);
/* End RTC Clock */
RTC_TimeTypeTime.RTC_Hours=18;
RTC_TimeTypeTime.RTC_Minutes=11;
RTC_TimeTypeTime.RTC_Seconds=4;
RTC_TimeTypeTime.RTC_H12=RTC_H12_PM;
RTC_SetTime(RTC_Format_BIN, &RTC_TimeTypeTime);
while(1){
    f_SleepMs(10);
    RTC_GetTime(RTC_Format_BIN, &RTC_TimeTypeTime);
    RELEASE_MSG("\r%d:%d:%d",RTC_TimeTypeTime.RTC_Hours,RTC_TimeTypeTime.RTC_Minutes,RTC_TimeTypeTime.RTC_Seconds);
}   

Output I get is 0:0:0

Answer

Ishmeet picture Ishmeet · Sep 2, 2013

Solved doing this,

/* Allow access to the RTC */
PWR_RTCAccessCmd(ENABLE);

/* Reset RTC Backup Domain */
RCC_RTCResetCmd(ENABLE);
RCC_RTCResetCmd(DISABLE);

/* LSE Enable */
RCC_LSEConfig(RCC_LSE_ON);

/* Wait until LSE is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);

/* RTC Clock Source Selection */ 
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); 

/* Enable the RTC */
RCC_RTCCLKCmd(ENABLE);   

LSE can only work with External Crystal or oscillator. For internal crystal LSI can be used.