iMXRT1021 Keep RTC counter when device reboot

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iMXRT1021 Keep RTC counter when device reboot

1,077 Views
huy_tv
Contributor I

Dear all,

I tried to store RTC counter each time device reset (software reset only).
But everytime device boot again from software reset, RTC counter always count from 1970 (monitor by debug breakpoint)

This is my code to write and read RTC time. Could you please review it and give me your suggestion?
Thank you.

1. RTC initialize

void rtc_intialize(void)
{
    snvs_hp_rtc_datetime_t rtcDate;
    snvs_hp_rtc_config_t snvsRtcConfig;

    SNVS_HP_RTC_GetDatetime(SNVS, &rtcDate);
    /* Init SNVS */
    /*
     * snvsConfig->rtccalenable = false;
     * snvsConfig->rtccalvalue = 0U;
     * snvsConfig->srtccalenable = false;
     * snvsConfig->srtccalvalue = 0U;
     * snvsConfig->PIFreq = 0U;
     */
    SNVS_HP_RTC_GetDefaultConfig(&snvsRtcConfig);
    SNVS_HP_RTC_Init(SNVS, &snvsRtcConfig);

    if (rtcDate.year < 2000 || rtcDate.year > 2049)
    {
    	/* Set a start date time and start RT */
       rtcDate.year   = 2000U;
       rtcDate.month  = 0;
       rtcDate.day    = 0;
       rtcDate.hour   = 0;
       rtcDate.minute = 0;
       rtcDate.second = 0;

       /* Set RTC time to default time and date and start the RTC */
       SNVS_HP_RTC_SetDatetime(SNVS, &rtcDate);
    }
    SNVS_HP_RTC_StartTimer(SNVS);
}

 2. RTC set

bool rtc_set_time(rtc_date_time_t *time)
{
    snvs_hp_rtc_datetime_t rtc_date;
	/* Set a start date time and start RT */
    rtc_date.year   = time->year+2000;
    rtc_date.month  = time->month;
    rtc_date.day    = time->day;
    rtc_date.hour   = time->hour;
    rtc_date.minute = time->minute;
    rtc_date.second = time->second;

	/* Set RTC time to default time and date and start the RTC */
	if (kStatus_Success != SNVS_HP_RTC_SetDatetime(SNVS, &rtc_date))
	{
		return false;
	}

	return true;
}

3. RTC read

{
	snvs_hp_rtc_datetime_t date_time;
	SNVS_HP_RTC_GetDatetime(SNVS, &date_time);

	time->year = date_time.year;
	time->month = date_time.month;
	time->day = date_time.day;
	time->hour = date_time.hour + 7;		// gsm + 7
	time->minute = date_time.minute;
	time->second = date_time.second;
	get_weekday(time);
}

 

0 Kudos
5 Replies

805 Views
microcris
Contributor III

Hello @huy_tv 

Did you find the solution for this?

I have the same problem (with a RT1062). Every time I reset or power up the board, the year starts from 1970.

My RTC init code is basically the same as yours

 

void RTC_init(void)
{
	snvs_hp_rtc_config_t snvsRtcConfig;
	snvs_hp_rtc_datetime_t rtcDate;

	SNVS_HP_RTC_GetDefaultConfig(&snvsRtcConfig);
	SNVS_HP_RTC_Init(SNVS, &snvsRtcConfig);
	SNVS_HP_RTC_StartTimer(SNVS);
	SNVS_HP_RTC_GetDatetime(SNVS, &rtcDate);

	print_log(LOG_WARN, "RTC:\tStart OK\n");

	if(rtcDate.year == 1970)
	{
		rtcDate.year = 2022;
		rtcDate.month = 8;
		rtcDate.day = 2;
		rtcDate.hour = 16;
		rtcDate.minute = 0;
		rtcDate.second = 0;

		SNVS_HP_RTC_SetDatetime(SNVS, &rtcDate);

		print_log(LOG_ERROR, "RTC:\tReset Time\n");
	}
}

 

 

0 Kudos

799 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi @microcris ,

  If you have any issues, please create your own question post, then we can help you in your own question post directly.

  You can test it based on the newest SDK 2.12.0, thanks.

Best Regards,

kerry

1,068 Views
huy_tv
Contributor I

Dear @kerryzhou ,

Ok let me check my code, but can IMXRT hardware support stored RTC counter when device reset by software?
In some case, because the fault or exception, we can't save RTC counter to flash.
I would like RTC keep value, like other MCU (STM32...), RTC backup register, RTC counter still keep the old value before reset

0 Kudos

1,063 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi @huy_tv ,

  You can check the SNVS_LPGPR register, that register will contains the data when you reset it, you can save your data in that register.

 

Wish it helps you!

Best Regards,

kerry

0 Kudos

1,073 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi @huy_tv 

   Do you want to reset also use RTC counter before it do the software reset?

   If yes, I think you can save the RTC counter in some flash or specific area, then after you reset, you can copy the previous counter data and write back to your RTC related register again.

  Otherwise, after reset, your code will use your own defined default counter data, and counter based on that data.

 

Wish it helps you!

Best Regards,

Kerry

0 Kudos