/************************************************************************* $Archive: /PACS/OnBoard/crc.c $ $Revision: 1.12 $ $Date: 2009/04/23 13:51:12 $ $Author: amazy $ $Log: crc.c,v $ Revision 1.12 2009/04/23 13:51:12 amazy 6.029 * * 5 12/11/06 11:48a Amazy * introduced checksum in DMC HK * * 4 11/12/06 11:04 Amazy * * 11 3/14/06 4:31p Pacs Egse * Version 6.001 * Cleaned and commented *************************************************************************/ #ifdef SIMULATOR #include "VirtuosoSim.h" #endif #include "crc.h" #include "l_gendef.h" #include "l_memory.h" /* IMPORTANT NOTE : This file has been provided by IFSI. It has been slightly modified to follow our programming standards. */ /* AUTHOR : SPezzuto */ /* This is the definition of the polynomial for the checksum algorithm. */ /* According to the PS-ICD the polynomial is X^16+X^12+X^5+1 which */ /* corresponds to 69665 (decimal) or 11021 (hex). */ #define TM_CRC_GENERATOR 0x00011021 int aCrcTable[256]; /* AUTHOR : SPezzuto */ /* This is the function that initializes the table. It must be called once */ /* for all (probably at startup). By default the generator is equal to */ /* TM_CRC_GENERATOR */ void InitCrcTable() { int i; int j; int crc; for (i=0;i<256;i++) { crc = i << 8; for (j=0;j<8;j++) { if (crc & 0x00008000) { crc = (crc << 1) ^ TM_CRC_GENERATOR; } else { crc = (crc << 1); } } aCrcTable[i] = crc & 0x0000FFFF; } } int Crc8(int data, int crc) { crc = aCrcTable[((crc >> 8)^data) & 0x000000FF] ^ (crc << 8); crc = crc & 0x0000FFFF; return crc; } int Crc16(int data, int crc) { crc = Crc8(data >> 8, crc); return Crc8(data & 0x000000FF, crc); } int Crc32(int data, int crc) { crc = Crc16((data >> 16) & 0xFFFF, crc); return Crc16(data & 0xFFFF, crc); } /* FUNCTION : int GetChecksum(int* p_buffer, int size) *************************************************** AUTHOR : AMazy USE : Compute the checksum of the given buffer. PARAMETERS: int* p_buffer : a pointer on a 32bits buffer int size : size of buffer (in 32bits words) SEE ALSO : "DpuRec.c" */ int GetChecksum(int* p_buffer, int size) { int i=0; int crc = 0xFFFFFFFF; #ifdef VERBOSE int* p_initialBuffer = p_buffer; #endif for (; i < size; i++, p_buffer++) { crc = Crc8((*p_buffer >> 24) & 0x000000FF, crc); crc = Crc8((*p_buffer & 0x00FF0000) >> 16, crc); crc = Crc8((*p_buffer & 0x0000FF00) >> 8, crc); crc = Crc8(*p_buffer & 0x000000FF, crc); }; return crc; }; /* FUNCTION : int GetChecksumInPram(int* p_buffer, int size) ********************************************************* AUTHOR : AMazy USE : Compute the checksum of the given buffer. PARAMETERS: int* p_buffer : a pointer on a 48bits buffer int size : size of buffer (in 48bits words) SEE ALSO : "DpuRec.c" */ int GetChecksumInPram(int* p_buffer, int size) { int i=0; int crc = 0xFFFFFFFF; T_UNSIGNED_32 temp32; T_UNSIGNED_16 temp16; #ifdef VERBOSE int* p_initialBuffer = p_buffer; #endif for (; i < size; i++, p_buffer++) { #ifndef SIMULATOR temp32 = MEM_ReadPM32bitMSWord((T_UNSIGNED_32)p_buffer); temp16 = MEM_ReadPM16bitLSWord((T_UNSIGNED_32)p_buffer); #endif //SIMULATOR crc = Crc8((temp32 >> 24) & 0x000000FF, crc); crc = Crc8((temp32 & 0x00FF0000) >> 16, crc); crc = Crc8((temp32 & 0x0000FF00) >> 8, crc); crc = Crc8(temp32 & 0x000000FF, crc); crc = Crc8((temp16 & 0x0000FF00) >> 8, crc); crc = Crc8(temp16 & 0x000000FF, crc); }; return crc; };