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

19,057

Solution 1

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.

Solution 2

I can confirm that this works for the STM32F051 (STM32F0Discovery):

RTC_InitTypeDef R;
RTC_TimeTypeDef T;

// Enable PWR clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

/* Enable the Backup Domain Access */
PWR_BackupAccessCmd(ENABLE);

/* Disable RTC clock */
RCC_RTCCLKCmd(DISABLE);
/* Enable RTC clock */
RCC_RTCCLKCmd(ENABLE);


RCC_LSEDriveConfig(RCC_LSEDrive_High); // i think this is optional
/* LSE Enable */
RCC_LSEConfig(RCC_LSE_ON);

/* Wait until the LSE crystal is ready */
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){
}

/* Set RTC clock source to LSE */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
R.RTC_AsynchPrediv = 0x7F;
R.RTC_SynchPrediv = 0xFF;


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

/* Waits until the RTC Time and Date registers are synchronized with RTC APB clock.*/
RTC_WaitForSynchro();

/* Set hour format to 24hrs */
R.RTC_HourFormat = RTC_HourFormat_24;

/* initialize the RTC */
if (RTC_Init(&R) == ERROR){
    printf("RTC init failed \r\n");
}

printf("RTC done. \r\n");

while(1){
    RTC_GetTime(RTC_Format_BIN, &T);
    printf("the time is %02d : %02d : %02d \r\n", T.RTC_Hours, T.RTC_Minutes, T.RTC_Seconds);
}
Share:
19,057
Ishmeet
Author by

Ishmeet

I have worked on tcp ip stack and have knowledge of wlan and ported it on AVR, PIC, ATMEL, STM based platforms ...

Updated on June 04, 2022

Comments

  • Ishmeet
    Ishmeet almost 2 years

    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