2023-05-24 2:54 AM - edited 2023-11-20 4:43 AM
Hi I am having lags in the RTC after one day.
I am currently using the Nucleo L476RG board, the RTC oscillator is LSE and my CubeIDE version is 1.6.1. It seems as if in the time intervals where I do not read the RTC it is not updated. The next table shows the difference between the PC hour and the RTC hour. Here is a part of my code: 
A brief explanation of my code. It compares the date and time of the RTC with a file stored on the SD card. Then, if it is at the right timae, it writes the date and time to a data log and sends a data through the UART 4. Then it waits X time (using the getTicks function line 47).
My interest is that there is no delay between the RTC and the real time.
Help me!
while (	(RTC_info.year  == Line_info.start_year)  &&
        (RTC_info.month == Line_info.start_month) &&
        (RTC_info.day 	==	Line_info.start_day))
{
    // Read RTC
 
    rtc_get_time_normal(&RTC_info);
    //rtc_print(&RTC_info);
    int check_hour = hour_is_between(RTC_info, Line_info);
 
    if (check_hour == -1)
    {
        //deb_println("Waiting for the time interval");
        {};
    }
    else if (check_hour == 1)
    {
        //! Hour are equals
        rtc_get_time_normal(&RTC_info);
 
        while (hour_is_between(RTC_info, Line_info) == 1)
        {
            // Change the random value
            if (cnt_random >= 5)
            {
                cnt_random = 0;
                generate_random_float(&random_value);
            }
 
            sd_create_file(dossier_log);
 
            //! Generated random vale size 10 characters
            float_to_string(random_value, str_random_val);
 
            rtc_get_time_normal(&RTC_info);
            char* str = string_csv_file(str_random_val, RTC_info, Line_info);
            deb_println("Writing in log.txt");
            sd_write_line(dossier_log, str);
 
            send_KIM_data_msg(KIM, str_random_val, strlen(str_random_val));
            
            free(str);
 
            uint32_t now = HAL_GetTick();
            while( (HAL_GetTick() - now) < (Trans_Period * 1000) )
            {
                {};
            }
        }
    }
}Here is the function that allows to store the RTC value in a structure that I created.
uint8_t rtc_get_time_normal(struct time_info *time_param)
{
	/**
	 *  This function allows to obtain the time in a normal
	 *  format (human). The values are stored in a structure
	 *  pointer
	 */
	uint8_t ack = false;
	RTC_DateTypeDef gDate;
	RTC_TimeTypeDef gTime;
 
	__HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc);
	if (HAL_RTC_WaitForSynchro(&hrtc) != HAL_OK)
	{
		return false;
	}
 
	HAL_RTC_GetTime(&hrtc, &gTime, RTC_FORMAT_BIN);
	HAL_RTC_GetDate(&hrtc, &gDate, RTC_FORMAT_BIN);
 
	time_param->year = gDate.Year;
	time_param->month = gDate.Month;
	time_param->day = gDate.Date;
	time_param->hour = gTime.Hours;
	time_param->minute = gTime.Minutes;
	time_param->second = gTime.Seconds;
 
	return ack;
}2023-05-24 4:50 AM
Did you reset the board between these readouts?
> the RTC oscillator is LSE
Read out and check content of RCC.BDCR register to find out if it's true.
JW
2023-05-24 6:54 AM
No, the board has not been reset. In the log file every time the board starts it prints a "Hello project start". This message appears only once.
2023-05-24 12:19 PM
Read out and check content of RCC.BDCR register to find out if you indeed run it from LSE.
JW
2023-05-25 3:44 AM - edited 2023-11-20 4:44 AM
Hi,
These are the values. The bits are well configured, as I read in the manual.
Can you help me ?
 
2023-05-25 4:50 AM
Yes, RTCSEL=1 is OK.
This means that your LSE runs erratically.
JW
