/*! * @brief override the RTC IRQ handler */ void RTC_IRQHandler(void) { if (RTC_HAL_HasAlarmOccured(RTC_BASE_PTR)) { RTC_HAL_SetAlarmIntCmd(RTC_BASE_PTR, false); } }
void lp_ticker_set_interrupt(uint32_t now, uint32_t time) { // On K64F, the compare value can be only set when the LPTMR is disabled // However, disabling the LPTMR will automatically clear the LPTMR counter // So we need to compensate for the time that already passed lp_compare_value = time; uint32_t ticks = time > now ? time - now : (uint32_t)((uint64_t)time + 0xFFFFFFFFu - now); lp_lptmr_schedule_ticks = 0; lp_ticker_lptmr_scheduled_flag = 0; RTC_HAL_EnableCounter(RTC_BASE, false); if (ticks > LPTMR_TIMER_MAX_VALUE) { ticks -= RTC_PRESCALER_MAX_VALUE + 1 - RTC_HAL_GetPrescaler(RTC_BASE); uint32_t seconds = RTC_HAL_GetSecsReg(RTC_BASE); while (ticks > LPTMR_TIMER_MAX_VALUE) { ticks -= RTC_PRESCALER_MAX_VALUE + 1; seconds++; } RTC_HAL_SetAlarmReg(RTC_BASE, seconds); RTC_HAL_SetAlarmIntCmd(RTC_BASE, true); // the lp timer will be triggered once RTC alarm is set lp_lptmr_schedule_ticks = ticks; } else { // restart counter, set compare LPTMR_HAL_Disable(LPTMR0_BASE); LPTMR_HAL_SetCompareValue(LPTMR0_BASE, ticks); LPTMR_HAL_Enable(LPTMR0_BASE); } RTC_HAL_EnableCounter(RTC_BASE, true); }
void LLWU_IRQHandler(void) { if (RTC_HAL_HasAlarmOccured(RTC)) { RTC_HAL_SetAlarmIntCmd(RTC_BASE_PTR, false); } LLWU_HAL_ClearExternalPinWakeupFlag(LLWU_BASE_PTR, (llwu_wakeup_pin_t)BOARD_SW_LLWU_EXT_PIN); }
static void rct_isr(void) { RTC_HAL_SetAlarmIntCmd(RTC_BASE, false); RTC_HAL_SetAlarmReg(RTC_BASE, 0); if (lp_lptmr_schedule_ticks) { lp_ticker_lptmr_scheduled_flag = 1; lp_ticker_schedule_lptmr(); } }
void RTC_DRV_AlarmIntAction(uint32_t instance) { uint32_t rtcBaseAddr = g_rtcBaseAddr[instance]; if (s_rtcRepeatAlarmState != NULL) { s_rtcRepeatAlarmState->alarmTime.year += s_rtcRepeatAlarmState->alarmRepTime .year; s_rtcRepeatAlarmState->alarmTime.month += s_rtcRepeatAlarmState->alarmRepTime.month; s_rtcRepeatAlarmState->alarmTime.day += s_rtcRepeatAlarmState->alarmRepTime.day; s_rtcRepeatAlarmState->alarmTime.hour += s_rtcRepeatAlarmState->alarmRepTime.hour; s_rtcRepeatAlarmState->alarmTime.minute += s_rtcRepeatAlarmState->alarmRepTime.minute; RTC_HAL_SetAlarm(rtcBaseAddr, &s_rtcRepeatAlarmState->alarmTime); } else { /* Writing to the alarm register clears the TAF flag in the Status register */ RTC_HAL_SetAlarmReg(rtcBaseAddr, 0x0); RTC_HAL_SetAlarmIntCmd(rtcBaseAddr, false); } }
/*! * @brief set alarm command. * * This function set the alarm which will be * trigerred x secs later. The alarm trigger * will print a notification on the console. */ static void cmd_alarm(uint8_t offsetSec) { uint32_t seconds = 0; if ((offsetSec < 1) || (offsetSec > 60)) { offsetSec = DEFAULT_ALARM_SECONDS; } RTC_HAL_GetDatetimeInSecs(RTC_BASE_PTR, &seconds); seconds += offsetSec; // set the datetime for alarm // set alarm in seconds RTC_HAL_SetAlarmReg(RTC_BASE_PTR, seconds); // Activate or deactivate the Alarm interrupt based on user choice RTC_HAL_SetAlarmIntCmd(RTC_BASE_PTR, true); }
/*FUNCTION********************************************************************** * * Function Name : RTC_DRV_SetAlarm * Description : sets the RTC alarm. * This function will first check if the date time has correct format. If yes, * convert the date time to seconds, and set the alarm in seconds. * *END**************************************************************************/ bool RTC_DRV_SetAlarm(uint32_t instance, rtc_datetime_t *alarmTime, bool enableAlarmInterrupt) { assert(alarmTime); uint32_t rtcBaseAddr = g_rtcBaseAddr[instance]; uint32_t srcClock = 0; uint32_t alrmSeconds = 0; uint32_t currSeconds = 0; /* Return error if the alarm time provided is not valid */ if (!(RTC_HAL_IsDatetimeCorrectFormat(alarmTime))) { return false; } RTC_HAL_ConvertDatetimeToSecs(alarmTime, &alrmSeconds); /* Get the current time */ currSeconds = RTC_HAL_GetSecsReg(rtcBaseAddr); if ((srcClock = CLOCK_SYS_GetExternalRefClock32kFreq()) != 32768U) { /* As the seconds register will not increment every second, we need to adjust the value * programmed to the alarm register */ alrmSeconds = alrmSeconds / (32768U / srcClock); } /* Make sure the alarm is for a future time */ if (alrmSeconds < currSeconds) { return false; } /* set alarm in seconds*/ RTC_HAL_SetAlarmReg(rtcBaseAddr, alrmSeconds); /* Activate or deactivate the Alarm interrupt based on user choice */ RTC_HAL_SetAlarmIntCmd(rtcBaseAddr, enableAlarmInterrupt); return true; }
void lp_ticker_init(void) { if (lp_ticker_inited) { return; } lp_ticker_inited = 1; // RTC might be configured already, don't reset it RTC_HAL_SetSupervisorAccessCmd(RTC_BASE, true); if (!RTC_HAL_IsCounterEnabled(RTC_BASE)) { // select RTC for OSC32KSEL CLOCK_HAL_SetSource(SIM_BASE, kClockOsc32kSel, 2); // configure RTC SIM_HAL_EnableRtcClock(SIM_BASE, 0U); RTC_HAL_Init(RTC_BASE); RTC_HAL_Enable(RTC_BASE); for (volatile uint32_t wait_count = 0; wait_count < 1000000; wait_count++); RTC_HAL_SetAlarmIntCmd(RTC_BASE, false); RTC_HAL_SetSecsIntCmd(RTC_BASE, false); RTC_HAL_SetAlarmReg(RTC_BASE, 0); RTC_HAL_EnableCounter(RTC_BASE, true); } vIRQ_ClearPendingIRQ(RTC_IRQn); vIRQ_SetVector(RTC_IRQn, (uint32_t)rct_isr); vIRQ_EnableIRQ(RTC_IRQn); // configure LPTMR CLOCK_SYS_EnableLptimerClock(0); LPTMR0_CSR = 0x00; LPTMR0_PSR = 0x00; LPTMR0_CMR = 0x00; LPTMR_HAL_SetTimerModeMode(LPTMR0_BASE, kLptmrTimerModeTimeCounter); LPTMR0_PSR |= LPTMR_PSR_PCS(0x2) | LPTMR_PSR_PBYP_MASK; LPTMR_HAL_SetIntCmd(LPTMR0_BASE, 1); LPTMR_HAL_SetFreeRunningCmd(LPTMR0_BASE, 0); IRQn_Type timer_irq[] = LPTMR_IRQS; vIRQ_SetVector(timer_irq[0], (uint32_t)lptmr_isr); vIRQ_EnableIRQ(timer_irq[0]); }
/*FUNCTION********************************************************************** * * Function Name : RTC_DRV_SetAlarm * Description : sets the RTC alarm. * This function will first check if the date time has correct format. If yes, * convert the date time to seconds, and set the alarm in seconds. * *END**************************************************************************/ bool RTC_DRV_SetAlarm(uint32_t instance, rtc_datetime_t *alarmTime, bool enableAlarmInterrupt) { assert(alarmTime); bool ret = false; uint32_t rtcBaseAddr = g_rtcBaseAddr[instance]; /* Return error if the alarm time provided is not valid */ if (!(RTC_HAL_IsDatetimeCorrectFormat(alarmTime))) { return ret; } ret = RTC_HAL_SetAlarm(rtcBaseAddr, alarmTime); if (ret) { /* Activate the Alarm interrupt if user wishes to use interrupts */ RTC_HAL_SetAlarmIntCmd(rtcBaseAddr, enableAlarmInterrupt); } return ret; }
void RTC_DRV_SetAlarmIntCmd(uint32_t instance, bool alarmEnable) { RTC_HAL_SetAlarmIntCmd(g_rtcBaseAddr[instance], alarmEnable); }