/*! * @cond DOXYGEN_PRIVATE * * @brief This function initializes caller allocated structure according to given * numerical identifier of the timer. * * Called by hwtimer_init(). * Initializes the HWTIMER structure. * Sets interrupt priority and registers ISR. * * @param hwtimer[in] Returns initialized hwtimer structure handle. * @param pitId[in] Determines PIT module and pit channel. * @param isrPrior[in] Interrupt priority for PIT * @param data[in] Specific data. Not used in this timer. * * @return kHwtimerSuccess Success. * @return kHwtimerInvalidInput When channel number does not exist in pit module. * @return kHwtimerRegisterHandlerError When registration of the interrupt service routine failed. * * @see HWTIMER_SYS_PitDeinit * @see HWTIMER_SYS_PitSetDiv * @see HWTIMER_SYS_PitStart * @see HWTIMER_SYS_PitStop * @see HWTIMER_SYS_PitGetTime * @see HWTIMER_SYS_PitIsr * @see HWTIMER_SYS_PitIsrShared */ static _hwtimer_error_code_t HWTIMER_SYS_PitInit(hwtimer_t * hwtimer, uint32_t pitId, uint32_t isrPrior, void *data) { uint32_t pitChannel; uint32_t baseAddr = g_pitBaseAddr[0]; if (FSL_FEATURE_PIT_TIMER_COUNT < pitId) { return kHwtimerInvalidInput; } assert(NULL != hwtimer); /* We need to store pitId of timer in context struct */ hwtimer->llContext[0U] = pitId; pitChannel = hwtimer->llContext[0U]; /* Un-gate pit clock */ CLOCK_SYS_EnablePitClock(0U); /* Enable PIT module clock */ PIT_HAL_Enable(baseAddr); /* Allows the timers to be stopped when the device enters the Debug mode. */ PIT_HAL_SetTimerRunInDebugCmd(baseAddr, false); /* Disable timer and interrupt */ PIT_HAL_StopTimer(baseAddr, pitChannel); PIT_HAL_SetIntCmd(baseAddr, pitChannel, false); /* Clear any pending interrupt */ PIT_HAL_ClearIntFlag(baseAddr, pitChannel); /* Store hwtimer in global array */ g_hwtimersPit[pitChannel] = hwtimer; /* Enable PIT interrupt.*/ if (kStatus_OSA_Success != OSA_InstallIntHandler(g_pitIrqId[pitChannel], HWTIMER_SYS_PitIsr)) { return kHwtimerRegisterHandlerError; } PIT_HAL_SetIntCmd(baseAddr, pitChannel, true); INT_SYS_EnableIRQ(g_pitIrqId[pitChannel]); return kHwtimerSuccess; }
/*FUNCTION********************************************************************** * * Function Name : PIT_DRV_Init * Description : Initialize PIT module. * This function must be called before calling all the other PIT driver functions. * This function un-gates the PIT clock and enables the PIT module. The * isRunInDebug passed into function will affect all timer channels. * *END**************************************************************************/ pit_status_t PIT_DRV_Init(uint32_t instance, bool isRunInDebug) { assert(instance < PIT_INSTANCE_COUNT); PIT_Type * base = g_pitBase[instance]; /* Un-gate pit clock*/ CLOCK_SYS_EnablePitClock(instance); /* Enable PIT module clock*/ PIT_HAL_Enable(base); /* Set timer run or stop in debug mode*/ PIT_HAL_SetTimerRunInDebugCmd(base, isRunInDebug); /* Finally, update pit source clock frequency.*/ g_pitSourceClock = CLOCK_SYS_GetPitFreq(instance); return kStatus_PIT_Success; }
/*! * @cond DOXYGEN_PRIVATE * * @brief This function initializes caller allocated structure according to given * numerical identifier of the timer. * * Called by hwtimer_init(). * Initializes the HWTIMER structure. * Sets interrupt priority and registers ISR. * * @param hwtimer[in] Returns initialized hwtimer structure handle. * @param pitId[in] Determines PIT module and pit channel. * @param isrPrior[in] Interrupt priority for PIT * @param data[in] Specific data. Not used in this timer. * * @return kHwtimerSuccess Success. * @return kHwtimerInvalidInput When channel number does not exist in pit module. * @return kHwtimerLockError When Locking failed. * @return kHwtimerRegisterHandlerError When registration of the interrupt service routine failed. * * @see HWTIMER_SYS_PitDeinit * @see HWTIMER_SYS_PitSetDiv * @see HWTIMER_SYS_PitStart * @see HWTIMER_SYS_PitStop * @see HWTIMER_SYS_PitGetTime * @see HWTIMER_SYS_PitIsr * @see HWTIMER_SYS_PitIsrShared */ static _hwtimer_error_code_t HWTIMER_SYS_PitInit(hwtimer_t * hwtimer, uint32_t pitId, uint32_t isrPrior, void *data) { uint32_t pitChannel; uint32_t baseAddr = g_pitBaseAddr[0]; if (FSL_FEATURE_PIT_TIMER_COUNT < pitId) { return kHwtimerInvalidInput; } assert(NULL != hwtimer); /* We need to store pitId of timer in context struct */ hwtimer->llContext[0U] = pitId; pitChannel = hwtimer->llContext[0U]; /* Un-gate pit clock */ CLOCK_SYS_EnablePitClock(0U); /* Enable PIT module clock */ PIT_HAL_Enable(baseAddr); /* Allows the timers to be stopped when the device enters the Debug mode. */ PIT_HAL_SetTimerRunInDebugCmd(baseAddr, false); /* Disable timer and interrupt */ PIT_HAL_StopTimer(baseAddr, pitChannel); PIT_HAL_SetIntCmd(baseAddr, pitChannel, false); /* Clear any pending interrupt */ PIT_HAL_ClearIntFlag(baseAddr, pitChannel); /* Create lock if does not exists*/ /* Enter critical section to avoid interrupt create locking */ OSA_EnterCritical(kCriticalDisableInt); if (g_lock == NULL) { /* Initialize synchronization object */ if (kStatus_OSA_Success != OSA_MutexCreate(&g_lock_data)) { return kHwtimerLockError; } g_lock = &g_lock_data; } OSA_ExitCritical(kCriticalDisableInt); assert(g_lock == &g_lock_data); /* Critical section. Access to global variable */ if (kStatus_OSA_Success != OSA_MutexLock(g_lock, OSA_WAIT_FOREVER)) { return kHwtimerLockError; } /* Store hwtimer in global array */ g_hwtimersPit[pitChannel] = hwtimer; if (kStatus_OSA_Success != OSA_MutexUnlock(g_lock)) { return kHwtimerLockError; } /* Enable PIT interrupt.*/ if (kStatus_OSA_Success != OSA_InstallIntHandler(kpitIrqIds[pitChannel], HWTIMER_SYS_PitIsr)) { return kHwtimerRegisterHandlerError; } PIT_HAL_SetIntCmd(baseAddr, pitChannel, true); INT_SYS_EnableIRQ(kpitIrqIds[pitChannel]); return kHwtimerSuccess; }