AnsweredAssumed Answered

RTC Seconds, prescalar must be set twice before they set.

Question asked by Mike Litster on Jul 28, 2015
Latest reply on Aug 20, 2015 by Mike Litster

I am programming a K22 processor and about half the time when I change the RTC seconds and prescalar, they do not change on the first pass. They have always changed by the second pass. Here is my code:

 

void SetTime(uint32_t milliseconds)

{

    RTC_Type *rtc_base = g_rtcBase[0];

    uint32_t seconds;

    uint16_t prescaler;

 

    // disable RTC counter

    RTC_HAL_EnableCounter(rtc_base, false);

 

    while (RTC_HAL_IsCounterEnabled(rtc_base)) {};

 

    // Datasheet: always write to prescaler register before seconds register.

    // Change the prescaler(milliseconds)

    prescaler = RTC_PRESCALER_RESET + MillisecondsToTicks(milliseconds % 1000);

    RTC_HAL_SetPrescaler(rtc_base, prescaler);

    delay_ms(50);

 

    // Change the seconds register

    seconds = milliseconds/1000;

    RTC_HAL_SetSecsReg(rtc_base, seconds);

    delay_ms(50);

 

    // Enable RTC counter

    RTC_HAL_EnableCounter(rtc_base, true);

    while (!RTC_HAL_IsCounterEnabled(rtc_base)) {};

}

 

I added the delays and the "while (RTC_HAL_IsCounterEnabled)" statements for troubleshooting when I noticed it wasn't setting. They had little effect upon the outcome.

 

Why does it take two passes to effectively set the RTC?

 

Also, when I run the same code on a KL26 processor, the seconds/prescalar don't get set at all. Any ideas on this one?

Outcomes