/************************************************************************** * * Copyright (c) 2020-2025 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * @file Timers.c * * @author (last) Dong Nguyen * @date (last) 27-Sep-2022 * * @author (original) Sean * @date (original) 17-Feb-2020 * ***************************************************************************/ #include "Timers.h" /** * @addtogroup Timers * @{ */ // ********** private definitions ********** // ********** private data ********** static volatile U32 msTimerCount = 0; ///< 1ms timer counter incremented by TaskTimer.c. /*********************************************************************//** * @brief * The initTimers function initializes the Timers module. * @details Inputs: msTimerCount * @details Outputs: msTimerCount * @return none *************************************************************************/ void initTimers( void ) { msTimerCount = 0; } /*********************************************************************//** * @brief * The incMSTimerCount function increments the ms timer count. * @details Inputs: msTimerCount * @details Outputs: msTimerCount * @return none *************************************************************************/ void incMSTimerCount( void ) { msTimerCount++; } /*********************************************************************//** * @brief * The getMSTimerCount function returns the current ms timer count. * @details Inputs: msTimerCount * @details Outputs: none * @return msTimerCount as a U32 *************************************************************************/ U32 getMSTimerCount( void ) { return msTimerCount; } /*********************************************************************//** * @brief * The didTimeout function determines whether a timeout has occurred between * a given start count and a given timeout period (in ms). * @details Inputs: msTimerCount * @details Outputs: none * @param startMSCount the ms count at the start of the timeout period * @param timeoutPeriod the period for the timeout (in ms) * @return TRUE if a timeout has occurred, FALSE if not *************************************************************************/ BOOL didTimeout( U32 startMSCount, U32 timeoutPeriod ) { BOOL result = FALSE; U32 currMSCount = msTimerCount; // no wrap if ( currMSCount >= startMSCount ) { if ( ( currMSCount - startMSCount ) >= timeoutPeriod ) { result = TRUE; } } // counter wrapped else { U32 deltaMSCount = ( 0xFFFFFFFF - startMSCount ) + currMSCount + 1; if ( deltaMSCount >= timeoutPeriod ) { result = TRUE; } } return result; } /*********************************************************************//** * @brief * The calcTimeSince function calculates the time (in ms) from a given start * time until now. * @details Inputs: msTimerCount * @details Outputs: none * @param startMSCount the ms count at the start of the period * @return ms since given start time *************************************************************************/ U32 calcTimeSince( U32 startMSCount ) { U32 result; U32 currMSCount = msTimerCount; // no wrap if ( currMSCount >= startMSCount ) { result = currMSCount - startMSCount; } else { result = ( 0xFFFFFFFF - startMSCount ) + currMSCount + 1; } return result; } /*********************************************************************//** * @brief * The calcTimeBetween function calculates the time (in ms) from a given start * time until a given end time. * @details Inputs: none * @details Outputs: none * @param startMSCount the ms count at the start of the period * @param endMSCount the ms count at the end of the period * @return ms between two given times *************************************************************************/ U32 calcTimeBetween( U32 startMSCount, U32 endMSCount ) { U32 result; // no wrap if ( endMSCount >= startMSCount ) { result = endMSCount - startMSCount; } else { result = ( 0xFFFFFFFF - startMSCount ) + endMSCount + 1; } return result; } /**@}*/