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

cancel
Showing results for 
Search instead for 
Did you mean: 

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

Jump to solution
512 Views
mikelitster
Contributor II

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?

Labels (1)
0 Kudos
1 Solution
140 Views
mikelitster
Contributor II

Apparently I was not allowing changes to take place before checking them.

View solution in original post

0 Kudos
5 Replies
141 Views
mikelitster
Contributor II

Apparently I was not allowing changes to take place before checking them.

View solution in original post

0 Kudos
140 Views
isaacavila
NXP Employee
NXP Employee

Hi Mike,

Could you please tell me which K22 MCU you are using? (K22_50MHz, 100MHz, etc)

Best Regards,

Isaac

0 Kudos
140 Views
mikelitster
Contributor II

It's the MK22FN512VLH12. I am running at 120MHz.  Also the MKL26Z64VFM4 at 96MHz.

0 Kudos
140 Views
isaacavila
NXP Employee
NXP Employee

Hello Mike,

Do you still have this problem?

Regards,

Isaac

0 Kudos
140 Views
isaacavila
NXP Employee
NXP Employee

Hello Mike

How do you realize that changes aren't made? (debugging, printing...)

I ran RTC example located in <KSDK_1_2_0_PATH>\examples\twrk22f120m\driver_examples\rtc\kds and in function RTC_DRV_SetDatetime() it updates TSR and TPR values in the first attempt.

Could you please attach your code to see what is happening?

Best Regards,

Isaac

0 Kudos