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); }
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; }
/*FUNCTION********************************************************************** * * Function Name : LPTMR_HAL_Init * Description : Initialize LPTMR module to reset state. * *END**************************************************************************/ void LPTMR_HAL_Init(LPTMR_Type * base) { lptmr_working_mode_user_config_t working_mode_config; lptmr_prescaler_user_config_t prescaler_config; LPTMR_HAL_Disable(base); LPTMR_HAL_ClearIntFlag(base); working_mode_config.timerModeSelect = kLptmrTimerModeTimeCounter; working_mode_config.freeRunningEnable = false; working_mode_config.pinPolarity = kLptmrPinPolarityActiveHigh; working_mode_config.pinSelect = kLptmrPinSelectInput0; LPTMR_HAL_SetTimerWorkingMode(base, working_mode_config); prescaler_config.prescalerValue = kLptmrPrescalerDivide2; prescaler_config.prescalerBypass = true; prescaler_config.prescalerClockSelect = kLptmrPrescalerClock0; LPTMR_HAL_SetPrescalerMode(base, prescaler_config); LPTMR_HAL_SetCompareValue(base, 0U); LPTMR_HAL_SetIntCmd(base, false); }