/************************************************************************** * * 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 Utilities.c * * @author (last) Sean Nash * @date (last) 05-Aug-2020 * * @author (original) Sean * @date (original) 17-Feb-2020 * ***************************************************************************/ #include "Common.h" #include "Timers.h" #include "Utilities.h" /** * @addtogroup Utilities * @{ */ // ********** private definitions ********** #define INITIAL_CRC16_VAL 0xFFFF ///< Seed for 16-bit CRC function. #define INITIAL_CRC08_VAL 0x00 ///< Seed for 8-bit CRC function. // ********** private data ********** /// CRC-16 look-up table. const U16 crc16_table[] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 }; /// CRC-8 look-up table. const U08 crc8_table[] = { 0, 49, 98, 83, 196, 245, 166, 151, 185, 136, 219, 234, 125, 76, 31, 46, 67, 114, 33, 16, 135, 182, 229, 212, 250, 203, 152, 169, 62, 15, 92, 109, 134, 183, 228, 213, 66, 115, 32, 17, 63, 14, 93, 108, 251, 202, 153, 168, 197, 244, 167, 150, 1, 48, 99, 82, 124, 77, 30, 47, 184, 137, 218, 235, 61, 12, 95, 110, 249, 200, 155, 170, 132, 181, 230, 215, 64, 113, 34, 19, 126, 79, 28, 45, 186, 139, 216, 233, 199, 246, 165, 148, 3, 50, 97, 80, 187, 138, 217, 232, 127, 78, 29, 44, 2, 51, 96, 81, 198, 247, 164, 149, 248, 201, 154, 171, 60, 13, 94, 111, 65, 112, 35, 18, 133, 180, 231, 214, 122, 75, 24, 41, 190, 143, 220, 237, 195, 242, 161, 144, 7, 54, 101, 84, 57, 8, 91, 106, 253, 204, 159, 174, 128, 177, 226, 211, 68, 117, 38, 23, 252, 205, 158, 175, 56, 9, 90, 107, 69, 116, 39, 22, 129, 176, 227, 210, 191, 142, 221, 236, 123, 74, 25, 40, 6, 55, 100, 85, 194, 243, 160, 145, 71, 118, 37, 20, 131, 178, 225, 208, 254, 207, 156, 173, 58, 11, 88, 105, 4, 53, 102, 87, 192, 241, 162, 147, 189, 140, 223, 238, 121, 72, 27, 42, 193, 240, 163, 146, 5, 52, 103, 86, 120, 73, 26, 43, 188, 141, 222, 239, 130, 179, 224, 209, 70, 119, 36, 21, 59, 10, 89, 104, 255, 206, 157, 172 }; // variables for time windowed counts (counts # of instances in a specific window of time) static BOOL timeWindowedCountsInitialized[ NUM_OF_TIME_WINDOWED_COUNTS ]; ///< Initialized flags for time windowed counts. static U32 timeWindowedCountsMaxCount[ NUM_OF_TIME_WINDOWED_COUNTS ]; ///< Max. counts for time windowed counts. static U32 timeWindowedCountsWindowMs[ NUM_OF_TIME_WINDOWED_COUNTS ]; ///< Time windows (in ms) for time windowed counts. 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. /*********************************************************************//** * @brief * The crc16 function calculates a 16-bit CRC for a given range of bytes \n * in memory. Poly = 0x1021. Not reflected. Initial value = 0xFFFF. * @details * Inputs : none * 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 *************************************************************************/ U16 crc16( const U08 *address, U32 len ) { U16 crc = INITIAL_CRC16_VAL; while ( len-- > 0 ) { crc = ( crc << SHIFT_8_BITS_FOR_BYTE_SHIFT ) ^ crc16_table[ *address ^ ( ( crc >> SHIFT_8_BITS_FOR_BYTE_SHIFT ) & MASK_OFF_MSB ) ]; address++; } return crc; } /*********************************************************************//** * @brief * The crc8 function calculates a 8-bit CRC for a given range of bytes \n * in memory. * @details * Inputs : none * 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 *************************************************************************/ U08 crc8( const U08 *address, U32 len ) { U08 crc = INITIAL_CRC08_VAL; while ( len-- > 0 ) { crc = crc8_table[ (*address) ^ crc ]; address++; } return crc; } /*********************************************************************//** * @brief * The initTimeWindowedCount function initializes a given time windowed count. * @details * Inputs : none * Outputs : all time windowed count variables for a given time windowed count are initialized. * @param cnt ID of the time windowed count to initialize * @param maxCnt maximum number of instances in the time window for this count * @param winMs number of ms in the time window for this count * @return none *************************************************************************/ void initTimeWindowedCount( TIME_WINDOWED_COUNT_T cnt, U32 maxCnt, U32 winMs ) { if ( ( cnt < NUM_OF_TIME_WINDOWED_COUNTS ) && ( maxCnt <= MAX_TIME_WINDOWED_COUNT ) ) { U32 i; // initialize a time windowed count timeWindowedCountsMaxCount[ cnt ] = maxCnt; timeWindowedCountsWindowMs[ cnt ] = winMs; timeWindowedCountIndexes[ cnt ] = 0; timeWindowedCountCounts[ cnt ] = 0; for ( i = 0; i < MAX_TIME_WINDOWED_COUNT; i++ ) { timeWindowedCounts[ cnt ][ i ] = 0; } timeWindowedCountsInitialized[ cnt ] = TRUE; } else { if ( cnt < NUM_OF_TIME_WINDOWED_COUNTS ) { timeWindowedCountsInitialized[ cnt ] = FALSE; #ifdef _DG_ SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_UTIL_INVALID_WIN_MAX_COUNT, maxCnt ) #else SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_UTIL_INVALID_WIN_MAX_COUNT, maxCnt ) #endif } else { #ifdef _DG_ SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_UTIL_INVALID_WIN_COUNT, cnt ) #else SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_UTIL_INVALID_WIN_COUNT, cnt ) #endif } } } /*********************************************************************//** * @brief * The incTimeWindowedCount function adds a new instance to a given time \n * windowed count. Must call initTimeWindowedCount() prior to calling this \n * function for a given time windowed count. * *Note - thread protection not provided - assumed function will be called \n * by one task for a given time windowed count. * @details * Inputs : timeWindowedCounts[][], timeWindowedCountIndexes[], timeWindowedCountCounts[] * Outputs : timeWindowedCounts[][], timeWindowedCountIndexes[], timeWindowedCountCounts[] * @param cnt ID of the time windowed count to add an instance to * @return TRUE if this instances brings the count to the maximum within \n * this counts time window, otherwise FALSE *************************************************************************/ BOOL incTimeWindowedCount( TIME_WINDOWED_COUNT_T cnt ) { BOOL result = FALSE; U32 timeInMS; if ( ( cnt < NUM_OF_TIME_WINDOWED_COUNTS ) && ( TRUE == timeWindowedCountsInitialized[ cnt ] ) ) { // replace timestamp of oldest instance in list with this new one timeWindowedCounts[ cnt ][ timeWindowedCountIndexes[ cnt ] ] = getMSTimerCount(); // move index to next position in list (may wrap) timeWindowedCountIndexes[ cnt ] = INC_WRAP( timeWindowedCountIndexes[ cnt ], 0, timeWindowedCountsMaxCount[ cnt ] - 1 ); // update list count timeWindowedCountCounts[ cnt ] = INC_CAP( timeWindowedCountCounts[ cnt ], timeWindowedCountsMaxCount[ cnt ] ); // check if too many instances within time window timeInMS = calcTimeSince( timeWindowedCounts[ cnt ][ timeWindowedCountIndexes[ cnt ] ] ); if ( ( timeWindowedCountCounts[ cnt ] >= timeWindowedCountsMaxCount[ cnt ] ) && ( timeInMS <= timeWindowedCountsWindowMs[ cnt ] ) ) { result = TRUE; } } else { #ifdef _DG_ SET_ALARM_WITH_2_U32_DATA ( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_UTIL_TIME_WINDOWED_COUNT_ERROR, cnt ); #else SET_ALARM_WITH_2_U32_DATA ( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_UTIL_TIME_WINDOWED_COUNT_ERROR, cnt ); #endif } return result; } /*********************************************************************//** * @brief * The getCriticalData function gets the value for a given critical data \n * record. The integrity of the critical data is checked first. If the \n * critical data record fails the integrity check, a fault is triggered. * @details * Inputs : none * Outputs : given critical data record's value is retrieved. * @param data Ptr to a critical data record * @return The data from a critical data record *************************************************************************/ CRITICAL_DATAS_T getCriticalData( CRITICAL_DATA_T *data ) { CRITICAL_DATAS_T result; CRITICAL_DATA_T d; _disable_IRQ(); d = *data; _enable_IRQ(); result = d.data; // verify data integrity if ( ( ~d.data.uInt != d.comp ) || ( TRUE != d.set ) || ( data->typ >= NUM_OF_CRITICAL_DATA_TYPES ) ) { result.uInt = 0; #ifdef _DG_ SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_CRITICAL_DATA_ERROR, (U32)&data[0], 0 ) #else SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_CRITICAL_DATA_ERROR, (U32)&data[0], 0 ) #endif } return result; } /*********************************************************************//** * @brief * The setCriticalData function sets the value for a given critical data \n * record. * @details * Inputs : none * Outputs : given critical data record's value is set to given value * @param data Ptr to a critical data record * @param value a value to set * @return none *************************************************************************/ void setCriticalData( CRITICAL_DATA_T *data, CRITICAL_DATAS_T value ) { if ( data->typ < NUM_OF_CRITICAL_DATA_TYPES ) { _disable_IRQ(); data->data = value; data->comp = ~data->data.uInt; data->set = TRUE; _enable_IRQ(); } else { #ifdef _DG_ SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_CRITICAL_DATA_ERROR, (U32)&data[0], 1 ) #else SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_CRITICAL_DATA_ERROR, (U32)&data[0], 1 ) #endif } } /*********************************************************************//** * @brief * The resetCriticalData function resets a critical data record. * @details * Inputs : none * Outputs : Given critical data record is reset * @param data Ptr to a critical data record * @return none *************************************************************************/ void resetCriticalData( CRITICAL_DATA_T *data ) { _disable_IRQ(); data->set = FALSE; data->data.uInt = 0; data->comp = ~data->data.uInt; _enable_IRQ(); } /**@}*/