Index: Utilities.c =================================================================== diff -u -rec333e3d50b94aae15581da1d54d030ed8110a00 -r26bae5cd3ee7aa22dbe75e558e7c665323986a62 --- Utilities.c (.../Utilities.c) (revision ec333e3d50b94aae15581da1d54d030ed8110a00) +++ Utilities.c (.../Utilities.c) (revision 26bae5cd3ee7aa22dbe75e558e7c665323986a62) @@ -26,15 +26,24 @@ // ********** private definitions ********** -#define INITIAL_CRC16_VAL 0xFFFF ///< Seed for 16-bit CRC function -#define INITIAL_CRC08_VAL 0x00 ///< Seed for 8-bit CRC function +#define INITIAL_CRC16_VAL 0xFFFF ///< Seed for 16-bit CRC function +#define INITIAL_CRC08_VAL 0x00 ///< Seed for 8-bit CRC function -#define ASCII_CODE_LETTER_A 0x41 ///< ASCII code in hex for letter A. -#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. +#define ASCII_CODE_LETTER_A 0x41 ///< ASCII code in hex for letter A. +#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. + +#define SEMAPHORE_IN_USE_TIMEOUT_MS ( 10 * MS_PER_SECOND ) ///< Taken semaphore timeout in milliseconds. -// ********** private data ********** +// ********** private data ********** +/// Semaphore data structure +typedef struct +{ + U32 semaphoreStartTimeMS; ///< Semaphore start time in milliseconds. + 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 +145,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 +217,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 +588,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 +643,88 @@ { 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; + U08 i; - for ( ii = 0; ii < size; ++ii ) + for ( i = 0; i < NUM_OF_SEMAPHORES; i++ ) { - 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; - } + memset( &sempahoreStatus[ i ], 0x0, sizeof( SEMAPHORE_STATUS_T ) ); } +} +/*********************************************************************//** + * @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 ) + { + sempahoreStatus[ s ].semaphoreStartTimeMS = getMSTimerCount(); + sempahoreStatus[ s ].isSemaphoreTaken = TRUE; + result = TRUE; + } + else if ( TRUE == didTimeout( sempahoreStatus[ s ].semaphoreStartTimeMS, SEMAPHORE_IN_USE_TIMEOUT_MS ) ) + { +#ifdef _DG_ + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_SEMAPHORE_IN_USE_TIMEOUT, (U32)s ) +#endif +#ifdef _HD_ + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_SEMAPHORE_IN_USE_TIMEOUT, (U32)s ) +#endif + } + _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; + } +} + +/*********************************************************************//** + * @brief + * The isSemaphoreReleased function returns the status of a taken semaphore + * @details Inputs: none + * @details Outputs: sempahoreStatus + * @param s which is the semaphore to check + * @return status of the semaphore + *************************************************************************/ +BOOL isSemaphoreReleased( SEMAPHORE_T s ) +{ + return ( TRUE == sempahoreStatus[ s ].isSemaphoreTaken ? FALSE : TRUE ); +} /**@}*/