/**********************************************************************//** * * Copyright (c) 2019-2020 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 * * @date 25-Sep-2019 * @author S. Nash * * @brief Timers service module. Provides miscellaneous timer functions. * **************************************************************************/ #include "Timers.h" /** * @addtogroup Timers * @{ */ // ********** private definitions ********** // ********** private data ********** static U32 msTimerCount = 0; ///< 1ms timer counter incremented by TaskTimer.c. /*********************************************************************//** * @brief * The initTimers function initializes the Timers module. * @details * Inputs : none * Outputs : Timers module initialized. * @return none *************************************************************************/ void initTimers( void ) { msTimerCount = 0; } /*********************************************************************//** * @brief * The incMSTimerCount function increments the ms timer count. * @details * Inputs : none * Outputs : msTimerCount incremented * @return none *************************************************************************/ void incMSTimerCount( void ) { msTimerCount++; } /*********************************************************************//** * @brief * The getMSTimerCount function returns the current ms timer count. * @details * Inputs : msTimerCount * Outputs : none * @return msTimerCount *************************************************************************/ U32 getMSTimerCount( void ) { return msTimerCount; } /*********************************************************************//** * @brief * The didTimeout function determines whether a timeout has occurred between \n * a given start count and a given timeout period (in ms). * @details * Inputs : msTimerCount * 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 \n * time until now. * @details * Inputs : msTimerCount * 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 \n * time until a given end time. * @details * Inputs : none * 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; } /**@}*/