cancel
Showing results for 
Search instead for 
Did you mean: 

RTC lags

JFlui.1
Associate II

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.
_legacyfs_online_stmicro_images_0693W00000bkl1QQAQ.png 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;
}

5 REPLIES 5

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

JFlui.1
Associate II

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.

Read out and check content of RCC.BDCR register to find out if you indeed run it from LSE.

JW

JFlui.1
Associate II

Hi,

These are the values. The bits are well configured, as I read in the manual.

Can you help me ?


_legacyfs_online_stmicro_images_0693W00000bkqomQAA.png

Yes, RTCSEL=1 is OK.

This means that your LSE runs erratically.

  • output LSE to MCO or LSCO and observe
  • try to increase oscillator drive in LSEDRV
  • read AN2867, mainly chapter 7 Tips for improving oscillator stability

JW