Skip to content

Safely initialize RTC on kinetis devices #4453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api/lp_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,26 @@ static int lptmr_schedule = 0;

static void rtc_isr(void)
{
RTC_DisableInterrupts(RTC, kRTC_AlarmInterruptEnable);
RTC->TAR = 0; /* Write clears the IRQ flag */

/* Wait subsecond remainder if any */
if (lptmr_schedule) {
LPTMR_SetTimerPeriod(LPTMR0, lptmr_schedule);
LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
LPTMR_StartTimer(LPTMR0);
} else {
lp_ticker_irq_handler();
uint32_t sr = RTC->SR;
if (sr & RTC_SR_TOF_MASK) {
// Reset RTC to 0 so it keeps counting
RTC_StopTimer(RTC);
RTC->TSR = 0;
RTC_StartTimer(RTC);
} else if (sr & RTC_SR_TAF_MASK) {
RTC_DisableInterrupts(RTC, kRTC_AlarmInterruptEnable);
RTC->TAR = 0; /* Write clears the IRQ flag */

/* Wait subsecond remainder if any */
if (lptmr_schedule) {
LPTMR_SetTimerPeriod(LPTMR0, lptmr_schedule);
LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
LPTMR_StartTimer(LPTMR0);
} else {
lp_ticker_irq_handler();
}
} else if (sr & RTC_SR_TIF_MASK) {
RTC_DisableInterrupts(RTC, kRTC_TimeOverflowInterruptEnable);
}
}

Expand Down Expand Up @@ -73,6 +83,7 @@ void lp_ticker_init(void)
RTC_StartTimer(RTC);
}

RTC->TAR = 0; /* Write clears the IRQ flag */
NVIC_ClearPendingIRQ(RTC_IRQn);
NVIC_SetVector(RTC_IRQn, (uint32_t)rtc_isr);
NVIC_EnableIRQ(RTC_IRQn);
Expand Down