Index: Utilities.c =================================================================== diff -u -r5846ff450c26de04b388c2636964a9116ccb6e9d -r7e186bb97310fb59f525e5399ec7a74711bea571 --- Utilities.c (.../Utilities.c) (revision 5846ff450c26de04b388c2636964a9116ccb6e9d) +++ Utilities.c (.../Utilities.c) (revision 7e186bb97310fb59f525e5399ec7a74711bea571) @@ -33,8 +33,14 @@ #define ASCII_CODE_NUMBER_ZERO 0x30 ///< ASCII code in hex for number zero. #define ASCII_CODE_NUMBER_SEVEN 0x37 ///< ASCII code in hex for number seven. -// ********** private data ********** +// ********** private data ********** +/// Semaphore data structure +typedef struct +{ + volatile BOOL isSemaphoreTaken; ///< Flag to indicate whether the semaphore is taken or not. +} SEMAPHORE_STATUS_T; + /// CRC-32 look-up table. const U32 CRC32_TABLE[] = { @@ -136,6 +142,7 @@ static U32 timeWindowedCounts[ NUM_OF_TIME_WINDOWED_COUNTS ][ MAX_TIME_WINDOWED_COUNT ]; ///< Time stamps for instances for time windowed counts static U32 timeWindowedCountIndexes[ NUM_OF_TIME_WINDOWED_COUNTS ]; ///< List indexes for time windowed counts static U32 timeWindowedCountCounts[ NUM_OF_TIME_WINDOWED_COUNTS ]; ///< Current counts for time windowed counts +static SEMAPHORE_STATUS_T sempahoreStatus[ NUM_OF_SEMAPHORES ]; /*********************************************************************//** * @brief @@ -207,13 +214,23 @@ return crc; } -U08 crc4( U08* buffer ) +/*********************************************************************//** + * @brief + * The crc4 function calculates a 4-bit CRC for a given range of bytes + * in memory. + * @details Inputs: none + * @details Outputs: none + * @param address pointer to start address of memory range to calculate CRC for + * @param len number of bytes in the memory range to calculate CRC for + * @return CRC as a U08 + *************************************************************************/ +U08 crc4( U16* buffer, U32 byteCount ) { U32 count; U08 nBit; U32 nRem = 0; - for ( count = 0; count < 16; count++ ) + for ( count = 0; count < byteCount; count++ ) { if ( 1 == ( count % 2 ) ) { @@ -568,6 +585,38 @@ data->data.uInt = data->defValue.uInt; data->comp = ~data->data.uInt; _enable_IRQ(); +} + +/*********************************************************************//** + * @brief + * The hexStrToDec function convert hex string to decimal value. + * @details Inputs: none + * @details Outputs: none + * @param valuePtr pointer to hex string to convert + * @param size size of the hex string to convert + * @return converted value of hex string + *************************************************************************/ +U32 hexStrToDec( U08 const * const valuePtr, U08 size ) +{ + U08 ii; + U08 value; + U32 result = 0; + + for ( ii = 0; ii < size; ++ii ) + { + if ( valuePtr[ii] < ASCII_CODE_LETTER_A ) + { + value = ( valuePtr[ii] - ASCII_CODE_NUMBER_ZERO ); + result = ( result << 4 ) | value; + } + else + { + value = ( valuePtr[ii] - ASCII_CODE_NUMBER_SEVEN ); + result = ( result << 4 ) | value; + } + } + + return result; } /*********************************************************************//** @@ -591,39 +640,60 @@ { result = TRUE; } + return result; } /*********************************************************************//** * @brief - * The hexStrToDec function convert hex string to decimal value. + * The initSemaphores function initializes the semaphores * @details Inputs: none - * @details Outputs: none - * @param valuePtr pointer to hex string to convert - * @param size size of the hex string to convert - * @return converted value of hex string + * @details Outputs: sempahoreStatus + * @return none *************************************************************************/ -U32 hexStrToDec( U08 const * const valuePtr, U08 size ) +void initSemaphores( void ) { - U08 ii; - U08 value; - U32 result = 0; + memset( &sempahoreStatus, FALSE, sizeof( SEMAPHORE_STATUS_T ) ); +} - for ( ii = 0; ii < size; ++ii ) +/*********************************************************************//** + * @brief + * The getSemaphore function gets a semaphore for the requested semaphore. + * @details Inputs: none + * @details Outputs: sempahoreStatus + * @param s which is the semaphore to be taken + * @return TRUE if semaphore is available and now it is taken otherwise, FALSE + *************************************************************************/ +BOOL getSemaphore( SEMAPHORE_T s ) +{ + BOOL result = FALSE; + + _disable_IRQ(); + if ( FALSE == sempahoreStatus[ s ].isSemaphoreTaken ) { - if ( valuePtr[ii] < ASCII_CODE_LETTER_A ) - { - value = ( valuePtr[ii] - ASCII_CODE_NUMBER_ZERO ); - result = ( result << 4 ) | value; - } - else - { - value = ( valuePtr[ii] - ASCII_CODE_NUMBER_SEVEN ); - result = ( result << 4 ) | value; - } + sempahoreStatus[ s ].isSemaphoreTaken = TRUE; + result = TRUE; } + _enable_IRQ(); return result; } + +/*********************************************************************//** + * @brief + * The releaseSemaphore function releases a semaphore that was previously taken + * for a job. + * @details Inputs: none + * @details Outputs: sempahoreStatus + * @param s which is the semaphore to be released + * @return none + *************************************************************************/ +void releaseSemaphore( SEMAPHORE_T s ) +{ + if ( TRUE == sempahoreStatus[ s ].isSemaphoreTaken ) + { + sempahoreStatus[ s ].isSemaphoreTaken = FALSE; + } +} /**@}*/