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); }
/*FUNCTION********************************************************************** * * Function Name : OSA_TimeInit * Description : This function initializes the timer used in BM OSA, the * functions such as OSA_TimeDelay, OSA_TimeGetMsec, and the timeout are all * based on this timer. * *END**************************************************************************/ __WEAK_FUNC void OSA_TimeInit(void) { #if (FSL_OSA_BM_TIMER_CONFIG == FSL_OSA_BM_TIMER_LPTMER) lptmr_prescaler_user_config_t prescaler_config; lptmr_working_mode_user_config_t lptmr_config; /* * Setup LP Timer for timeout and delay. * Use 1kHz LPO as clock source, disable prescaler, freerun mode. */ CLOCK_SYS_EnableLptmrClock(BM_LPTMR_INSTANCE); LPTMR_HAL_Disable(BM_LPTMR_BASE); prescaler_config.prescalerBypass = true; prescaler_config.prescalerClockSelect = (lptmr_prescaler_clock_select_t)kClockLptmrSrcLpoClk; LPTMR_HAL_SetPrescalerMode(BM_LPTMR_BASE, prescaler_config); lptmr_config.freeRunningEnable = true; lptmr_config.timerModeSelect = kLptmrTimerModeTimeCounter; LPTMR_HAL_SetTimerWorkingMode(BM_LPTMR_BASE, lptmr_config); LPTMR_HAL_SetIntCmd(BM_LPTMR_BASE,false); LPTMR_HAL_Enable(BM_LPTMR_BASE); #endif }
static void lp_ticker_schedule_lptmr(void) { // schedule LPTMR, restart counter and set compare LPTMR_HAL_Disable(LPTMR0_BASE); LPTMR_HAL_SetCompareValue(LPTMR0_BASE, lp_lptmr_schedule_ticks); LPTMR_HAL_Enable(LPTMR0_BASE); lp_lptmr_schedule_ticks = 0; }
/*! * @brief ISR Routine for Low Power Timer */ void demo_lptmr_isr(void) { volatile uint32_t lptmrCsrTemp; CLOCK_SYS_EnableLptimerClock(0); LPTMR_HAL_ClearIntFlag(LPTMR0_BASE); /* write 1 to TCF to clear the LPT timer compare flag */ LPTMR_HAL_Enable(LPTMR0_BASE); /* enable timer */ LPTMR_HAL_SetIntCmd(LPTMR0_BASE, true); /* enable interrupts */ LPTMR_HAL_ClearIntFlag(LPTMR0_BASE); /* clear the flag */ /*wait for write to complete to before returning */ while(!(LPTMR_HAL_IsEnabled(LPTMR0_BASE) && LPTMR_HAL_GetIntCmd(LPTMR0_BASE))); }