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?
Solved! Go to Solution.
Apparently I was not allowing changes to take place before checking them.
Apparently I was not allowing changes to take place before checking them.
Hi Mike,
Could you please tell me which K22 MCU you are using? (K22_50MHz, 100MHz, etc)
Best Regards,
Isaac
It's the MK22FN512VLH12. I am running at 120MHz. Also the MKL26Z64VFM4 at 96MHz.
Hello Mike,
Do you still have this problem?
Regards,
Isaac
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