/*FUNCTION********************************************************************** * * Function Name : PIT_DRV_GetUs * Description : Get an absolute time stamp. * This function is useful to get elapsed time through calling this function in * time A, and then call it in time B. The elapsed time could be get by B-A, the * result may have 3-5 microseconds error depends on system clock frequency. * *END**************************************************************************/ uint32_t PIT_DRV_GetUs(void) { PIT_Type * base = g_pitBase[g_pitUsInstance]; /* Get current timer count, and reverse it to up-counting.*/ uint64_t currentTime = ~(((uint64_t)PIT_HAL_ReadTimerCount(base, g_pitUsChannel) << 32U) + PIT_HAL_ReadTimerCount(base, g_pitUsChannel - 1U)); /* Convert count numbers to microseconds unit.*/ return currentTime = (currentTime * 1000U) / (g_pitSourceClock / 1000U); }
/*FUNCTION********************************************************************** * * Function Name : PIT_DRV_DelayUs * Description : Delay specific microseconds. * The delay will have a 3-5 microseconds error depends on system clock frequency. * *END**************************************************************************/ void PIT_DRV_DelayUs(uint32_t us) { PIT_Type * base = g_pitBase[g_pitUsInstance]; uint64_t x = us * g_pitSourceClock / 1000000; uint64_t timeToBe = ((uint64_t)PIT_HAL_ReadTimerCount(base, g_pitUsChannel) << 32U) + PIT_HAL_ReadTimerCount(base, g_pitUsChannel - 1U) - x; while (((uint64_t)PIT_HAL_ReadTimerCount(base, g_pitUsChannel) << 32U) + PIT_HAL_ReadTimerCount(base, g_pitUsChannel - 1U) >= timeToBe) {} }
/*! * @cond DOXYGEN_PRIVATE * * @brief Atomically captures current time into HWTIMER_TIME_STRUCT structure * * Corrects/normalizes the values if necessary (interrupt pending, etc.) * * @param hwtimer[in] Pointer to hwtimer structure. * @param time[out] Pointer to time structure. This value is filled with current value of the timer. * * @return kHwtimerSuccess Success. * * @see HWTIMER_SYS_PitInit * @see HWTIMER_SYS_PitDeinit * @see HWTIMER_SYS_PitSetDiv * @see HWTIMER_SYS_PitStart * @see HWTIMER_SYS_PitStop * @see HWTIMER_SYS_PitIsr * @see HWTIMER_SYS_PitIsrShared */ static _hwtimer_error_code_t HWTIMER_SYS_PitGetTime(hwtimer_t *hwtimer, hwtimer_time_t *time) { uint32_t pitChannel; uint32_t tempCval; uint32_t baseAddr = g_pitBaseAddr[0]; assert(NULL != hwtimer); assert(NULL != time); pitChannel = hwtimer->llContext[0U]; assert(pitChannel < FSL_FEATURE_PIT_TIMER_COUNT); /* Enter critical section to avoid disabling interrupt from pit for very long time */ OSA_EnterCritical(kCriticalDisableInt); PIT_HAL_SetIntCmd(baseAddr, pitChannel, false); time->ticks = hwtimer->ticks; tempCval = PIT_HAL_ReadTimerCount(baseAddr, pitChannel); /* Check pending interrupt flag */ if (PIT_HAL_IsIntPending(baseAddr, pitChannel)) { PIT_HAL_SetIntCmd(baseAddr, pitChannel, true); OSA_ExitCritical(kCriticalDisableInt); time->subTicks = hwtimer->modulo - 1U; } else { PIT_HAL_SetIntCmd(baseAddr, pitChannel, true); OSA_ExitCritical(kCriticalDisableInt); /* todo: following line should be updated when HAL will be updated with this functionality. */ time->subTicks = HW_PIT_LDVALn_RD(baseAddr, pitChannel) - tempCval; } return kHwtimerSuccess; }
/*FUNCTION********************************************************************** * * Function Name : PIT_DRV_ReadTimerCount * Description : Reads the current timer counting value. * This function returns the real-time timer counting value, in a range from 0 * to a timer period. * *END**************************************************************************/ uint32_t PIT_DRV_ReadTimerCount(uint32_t instance, uint32_t channel) { assert(instance < PIT_INSTANCE_COUNT); PIT_Type * base = g_pitBase[instance]; return PIT_HAL_ReadTimerCount(base, channel); }
/*! * @brief Function ltc_timer_stop halt pit timer * and returns elapsed ticks. */ static unsigned int ltc_timer_stop(void) { uint32_t curr_ticks = PIT_HAL_ReadTimerCount(pitBase[0], LTC_TIMER_PIT_CHANNEL); PIT_HAL_StopTimer(pitBase[0], LTC_TIMER_PIT_CHANNEL); return (0xFFFFFFFFu - curr_ticks); }