cancel
Showing results for 
Search instead for 
Did you mean: 

RTC can't set date/time with LSE but with LSI works.

MNech.1
Associate

I'm using STM32IDE with MCU STM32L011F4 and LL libs. Something is wrong with RTC + LSE/LSI. Code is minimum generated by IDE. I set RTC with LSE and code is default generated by IDE, and can't change time/date, it's alwasy zero. But if I change source to LSI everything works. Board is handmade and LPTIM with LSE is working as expected.

Code is below as generated by IDE + lines with time/date check.

static void MX_RTC_Init(void)

{

 /* USER CODE BEGIN RTC_Init 0 */

 /* USER CODE END RTC_Init 0 */

 LL_RTC_InitTypeDef RTC_InitStruct = {0};

 LL_RTC_TimeTypeDef RTC_TimeStruct = {0};

 LL_RTC_DateTypeDef RTC_DateStruct = {0};

 /* Peripheral clock enable */

 LL_RCC_EnableRTC();

 /* USER CODE BEGIN RTC_Init 1 */

 /* USER CODE END RTC_Init 1 */

 /** Initialize RTC and set the Time and Date

 */

 RTC_InitStruct.HourFormat = LL_RTC_HOURFORMAT_24HOUR;

 RTC_InitStruct.AsynchPrescaler = 127;

 RTC_InitStruct.SynchPrescaler = 255;

 LL_RTC_Init(RTC, &RTC_InitStruct);

 /** Initialize RTC and set the Time and Date

 */

 RTC_TimeStruct.Hours = 0;

 RTC_TimeStruct.Minutes = 0;

 RTC_TimeStruct.Seconds = 0;

 LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BIN, &RTC_TimeStruct);

 RTC_DateStruct.WeekDay = LL_RTC_WEEKDAY_MONDAY;

 RTC_DateStruct.Month = LL_RTC_MONTH_JANUARY;

 RTC_DateStruct.Day = 12;

 RTC_DateStruct.Year = 0;

 LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BIN, &RTC_DateStruct);

 /* USER CODE BEGIN RTC_Init 2 */

 LL_RTC_WaitForSynchro(RTC);

 uint32_t current_time, current_date;

 current_time = LL_RTC_TIME_Get(RTC);

 current_date = LL_RTC_DATE_Get(RTC);

   //compare input and result

   while (__LL_RTC_CONVERT_BCD2BIN(__LL_RTC_GET_DAY(current_date)) != RTC_DateStruct.Day)

   {

      LL_GPIO_TogglePin(ImpulsOUT_GPIO_Port, ImpulsOUT_Pin);

      LL_mDelay(300);

   }

 /* USER CODE END RTC_Init 2 */

}

2 REPLIES 2
Amira
Associate III

Hello @MNech.1​ 

You can refer to the provided STM32L0Cube V1.12.0 firmware examples (NUCLEO-L010RB\Examples\RTC\RTC_Alarm , STM32L073Z-EVAL\Examples\RTC\RTC_Calendar and STM32L073Z-EVAL\Examples\RTC\RTC_LowPower_STANDBY) where the RTC is clocked by LSE, you can follow the same clock configuration.

I hope this would help you!

Regards,

Amira

Read out and check content of RCC_CSR register, mainly RTCSEL and RTCEN fields.

JW