AnsweredAssumed Answered

K82 : how to identify if RTC initialised already ?

Question asked by Eugene Hiihtaja on Aug 6, 2018
Latest reply on Aug 8, 2018 by Mark Butcher

Hello !

 

In SDK examples, RTC is always initialized with some date/time after reboot.

 

But on practice, default time should be set only if RTC is not initialized yet or lost time and etc.

 

What is correct initialization sequence for enable all what is need and set default time if RTC dosn't have correct time yet ?

 

After some experiments, I can see that  kRTC_AlarmFlag is set if RTC dosn't have any initialisation values yet.

 

Could you suggest right sequence what should be executed every time when MCU has SW or HW reset ?

 

Now I have someting like this:

 

/* Init RTC */

rtc_config_t rtcConfig;

/*
* rtcConfig.wakeupSelect = false;
* rtcConfig.updateMode = false;
* rtcConfig.supervisorAccess = false;
* rtcConfig.compensationInterval = 0;
* rtcConfig.compensationTime = 0;
*/
RTC_GetDefaultConfig(&rtcConfig);
RTC_Init(RTC, &rtcConfig);

/* Select RTC clock source, RTC 32KHz oscillator. */
RTC_SetClockSource(RTC);

/* Start the RTC time counter */
RTC_StartTimer(RTC);

/* Get RTC status. If it set already. */
uint32_t status = RTC_GetStatusFlags(RTC);

 

if (status & kRTC_AlarmFlag /*kRTC_TimeInvalidFlag*/)
{
   RTC_ClearStatusFlags(RTC, kRTC_AlarmFlag);

   rtc_datetime_t date;
   /* Set a start date time and start RT */   
   date.year = 2018U;
   date.month = 8U;
   date.day = 6U;
   date.hour = 19U;
   date.minute = 0;
   date.second = 0;

   /* RTC time counter has to be stopped before setting the date & time in the TSR register. */
   RTC_StopTimer(RTC);

   / * Set RTC time to default */
   RTC_SetDatetime(RTC, &date);

   /* Start the RTC time counter */
   RTC_StartTimer(RTC);
}

Regards,

Eugene

Outcomes