RTC clock configuration in Stm32L in LSI / LSE / HSE only?

I use a real-time clock on the STM32L152RB detection board using the IAR compiler. I implemented Clock configuration on HSI and using PLL, I 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 setting the real time clock. I have to set the secondary LSE clock as the source of the RTC clock, which in my case is the original HSI clock. The remaining steps, which include turning on the PWR controller, turning on access to the rtc domain, the clock source rtc, rtc_init (), and then setting up and gettime, are fine, as 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); } 

The output I get is 0:0:0

+7
c embedded real-time stm32
source share
2 answers

Having decided 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 an external crystal or oscillator. For the internal crystal, LSI can be used.

+6
source share

I can confirm that this works for 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); } 
+1
source share

All Articles