/*! * @brief LLWU ISR function */ void llwu_isr(void) { uint8_t pinEn; NVIC_ClearPendingIRQ(LLW_IRQn); /* Print LLWU acknowledgement only if UART is enabled */ for(pinEn = 0; pinEn < FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN; pinEn++) { if (LLWU_HAL_GetExternalPinWakeupFlag(LLWU_BASE, (llwu_wakeup_pin_t)pinEn)) { LLWU_HAL_ClearExternalPinWakeupFlag(LLWU_BASE, (llwu_wakeup_pin_t)pinEn); /* write one to clear the flag */ } } /* * Note: This ISR does not write to the LLWU_F3 register because these * are peripheral module wakeups. The flags contained in the LLWU_F3 * register should be cleared through the associated module interrupt * and not through the LLWU_F3 per the Kinetis L Family Reference * Manual (LLWU Chapter) */ if (LLWU_HAL_GetInternalModuleWakeupFlag(LLWU_BASE, kLlwuWakeupModule0)) { CLOCK_SYS_EnableLptimerClock(0); LPTMR_HAL_ClearIntFlag(LPTMR0_BASE); /* write 1 to TCF to clear the LPT timer compare flag */ LPTMR_HAL_IsEnabled(LPTMR0_BASE); LPTMR_HAL_SetIntCmd(LPTMR0_BASE, 1); LPTMR_HAL_IsIntPending(LPTMR0_BASE); } if(LLWU_HAL_GetFilterDetectFlag(LLWU_BASE, 0)){ LLWU_HAL_ClearFilterDetectFlag(LLWU_BASE, 0); } if(LLWU_HAL_GetFilterDetectFlag(LLWU_BASE, 1)){ LLWU_HAL_ClearFilterDetectFlag(LLWU_BASE, 1); } }
/*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 }
/*! * @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))); }
/*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); }
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 : LPTMR_DRV_Init * Description : initializes the LPTMR driver. * This function will initialize the LPTMR driver according to user configure * strcuture. * *END*/ lptmr_status_t LPTMR_DRV_Init(uint32_t instance, lptmr_state_t *userStatePtr, const lptmr_user_config_t* userConfigPtr) { assert(instance < LPTMR_INSTANCE_COUNT); LPTMR_Type * base = g_lptmrBase[instance]; lptmr_prescaler_user_config_t prescalerUserConfig; lptmr_working_mode_user_config_t workingModeUserConfig; if ((!userConfigPtr) || (!userStatePtr)) { return kStatus_LPTMR_NullArgument; } /** prescaler value 0 is invalid while working as pulse counter */ if ((kLptmrTimerModePulseCounter == userConfigPtr->timerMode) && (true == userConfigPtr->prescalerEnable) && (kLptmrPrescalerDivide2 == userConfigPtr->prescalerValue)) { return kStatus_LPTMR_InvalidPrescalerValue; } /** Enable clock for lptmr */ CLOCK_SYS_EnableLptmrClock(instance); /** Disable lptmr and reset lptmr logic */ LPTMR_HAL_Disable(base); /** LPTMR prescaler configure */ prescalerUserConfig.prescalerClockSelect = (lptmr_prescaler_clock_select_t)userConfigPtr->prescalerClockSource; prescalerUserConfig.prescalerBypass = (uint8_t)(userConfigPtr->prescalerEnable == false); prescalerUserConfig.prescalerValue = userConfigPtr->prescalerValue; LPTMR_HAL_SetPrescalerMode(base, prescalerUserConfig); /** Working Mode configure */ workingModeUserConfig.timerModeSelect = userConfigPtr->timerMode; workingModeUserConfig.freeRunningEnable = userConfigPtr->freeRunningEnable; workingModeUserConfig.pinPolarity = userConfigPtr->pinPolarity; workingModeUserConfig.pinSelect = userConfigPtr->pinSelect; LPTMR_HAL_SetTimerWorkingMode(base,workingModeUserConfig); /** Internal context */ lptmr_state_ptrs[instance] = userStatePtr; userStatePtr->userCallbackFunc = NULL; /** LPTMR interrupt */ if (userConfigPtr->isInterruptEnabled) { LPTMR_HAL_SetIntCmd(base,true); INT_SYS_EnableIRQ(g_lptmrIrqId[instance]); } else { LPTMR_HAL_SetIntCmd(base,false); INT_SYS_DisableIRQ(g_lptmrIrqId[instance]); } /** Caculate prescaler clock frequency */ if ( kLptmrTimerModeTimeCounter == userConfigPtr->timerMode) { userStatePtr->prescalerClockHz = CLOCK_SYS_GetLptmrFreq(instance, userConfigPtr->prescalerClockSource); if (userConfigPtr->prescalerEnable) { userStatePtr->prescalerClockHz = (userStatePtr->prescalerClockHz >> ((uint32_t)(userConfigPtr->prescalerValue+1))); }