Index: Utilities.c =================================================================== diff -u -rbe82a1c66a83e49efdf3ddbc288ae8560d678cc2 -r2932935752882cd7bc9808bfe238b03753d3e167 --- Utilities.c (.../Utilities.c) (revision be82a1c66a83e49efdf3ddbc288ae8560d678cc2) +++ Utilities.c (.../Utilities.c) (revision 2932935752882cd7bc9808bfe238b03753d3e167) @@ -176,6 +176,55 @@ return result; } +/*********************************************************************//** + * @brief + * The u16DiffWithWrap function calculates the difference between two given + * unsigned 16-bit numbers. If the second (ending) number is less than the + * first (starting) number, then the difference is calculated with a wrap past + * 16-bit full scale to zero. + * @details Inputs: none + * @details Outputs: none + * @param start first number to compute difference for + * @param end second number to compute difference for + * @return the difference between the two given numbers + *************************************************************************/ +U16 u16DiffWithWrap( U16 start, U16 end ) +{ + U16 result = ( end >= start ? end - start : ( HEX_64_K - start ) + end ); + + return result; +} + +/*********************************************************************//** + * @brief + * The u16BiDiffWithWrap function calculates the difference between two given + * unsigned 16-bit numbers. This version of function returns a signed value + * to allow for a negative difference. + * @details Inputs: none + * @details Outputs: none + * @param start first number to compute difference for + * @param end second number to compute difference for + * @return the difference between the two given numbers + *************************************************************************/ +S16 u16BiDiffWithWrap( U16 start, U16 end ) +{ + S16 result; + + U16 incDelta = ( end >= start ? end - start : ( HEX_64_K - start ) + end ); + U16 decDelta = HEX_64_K - incDelta; + + if ( incDelta < decDelta ) + { + result = (S16)incDelta; + } + else + { + result = (S16)decDelta * -1; + } + + return result; +} + /*********************************************************************//** * @brief * The initTimeWindowedCount function initializes a given time windowed count. Index: Utilities.h =================================================================== diff -u -r7c49409b1bfeee8fd788fe1cd2a6dbf2abda7fea -r2932935752882cd7bc9808bfe238b03753d3e167 --- Utilities.h (.../Utilities.h) (revision 7c49409b1bfeee8fd788fe1cd2a6dbf2abda7fea) +++ Utilities.h (.../Utilities.h) (revision 2932935752882cd7bc9808bfe238b03753d3e167) @@ -84,7 +84,9 @@ U16 crc16( const U08 *address, U32 len ); U08 crc8( const U08 *address, U32 len ); -U32 u32DiffWithWrap( U32 start, U32 end ); +U32 u32DiffWithWrap( U32 start, U32 end ); +U16 u16DiffWithWrap( U16 start, U16 end ); +S16 u16BiDiffWithWrap( U16 start, U16 end ); void initTimeWindowedCount( TIME_WINDOWED_COUNT_T cnt, U32 maxCnt, U32 winMs ); BOOL incTimeWindowedCount( TIME_WINDOWED_COUNT_T cnt ); CRITICAL_DATAS_T getCriticalData( CRITICAL_DATA_T *data );