/****************************************************************************** *File name : MM_CRC.c *Version.Revision: 1.0 *Purpose: * This module contains the code to initialize the * CRc_table used by the checksum functions. In order to keep CRc_table static, * we put here also the crc functions, even if they are called continuously * during the program execution *Public Functions: * unsigned int crc8(unsigned int, unsigned int) * unsigned int crc16(unsigned int, unsigned int) * unsigned int crc32(unsigned int, unsigned int) * unsigned int memcrc8(unsigned int*, unsigned int, unsigned int) * unsigned int memcrc16(unsigned int*, unsigned int, unsigned int) * unsigned int memcrc32(unsigned int*, unsigned int, unsigned int) * unsigned int memcrc32_pm(unsigned int*, unsigned int, unsigned int) *Description: * The code is straightforward *Creation Date & Author: 10-05-2005, SP DS *Version, Update date & Author: ******************************************************************************/ #include "MM_crc.h" /*========================= STATIC VAR =========================*/ static unsigned int CRc_table[256]={ 0x00000000, 0x00001021, 0x00002042, 0x00003063, 0x00004084, 0x000050a5, 0x000060c6, 0x000070e7, 0x00008108, 0x00009129, 0x0000a14a, 0x0000b16b, 0x0000c18c, 0x0000d1ad, 0x0000e1ce, 0x0000f1ef, 0x00001231, 0x00000210, 0x00003273, 0x00002252, 0x000052b5, 0x00004294, 0x000072f7, 0x000062d6, 0x00009339, 0x00008318, 0x0000b37b, 0x0000a35a, 0x0000d3bd, 0x0000c39c, 0x0000f3ff, 0x0000e3de, 0x00002462, 0x00003443, 0x00000420, 0x00001401, 0x000064e6, 0x000074c7, 0x000044a4, 0x00005485, 0x0000a56a, 0x0000b54b, 0x00008528, 0x00009509, 0x0000e5ee, 0x0000f5cf, 0x0000c5ac, 0x0000d58d, 0x00003653, 0x00002672, 0x00001611, 0x00000630, 0x000076d7, 0x000066f6, 0x00005695, 0x000046b4, 0x0000b75b, 0x0000a77a, 0x00009719, 0x00008738, 0x0000f7df, 0x0000e7fe, 0x0000d79d, 0x0000c7bc, 0x000048c4, 0x000058e5, 0x00006886, 0x000078a7, 0x00000840, 0x00001861, 0x00002802, 0x00003823, 0x0000c9cc, 0x0000d9ed, 0x0000e98e, 0x0000f9af, 0x00008948, 0x00009969, 0x0000a90a, 0x0000b92b, 0x00005af5, 0x00004ad4, 0x00007ab7, 0x00006a96, 0x00001a71, 0x00000a50, 0x00003a33, 0x00002a12, 0x0000dbfd, 0x0000cbdc, 0x0000fbbf, 0x0000eb9e, 0x00009b79, 0x00008b58, 0x0000bb3b, 0x0000ab1a, 0x00006ca6, 0x00007c87, 0x00004ce4, 0x00005cc5, 0x00002c22, 0x00003c03, 0x00000c60, 0x00001c41, 0x0000edae, 0x0000fd8f, 0x0000cdec, 0x0000ddcd, 0x0000ad2a, 0x0000bd0b, 0x00008d68, 0x00009d49, 0x00007e97, 0x00006eb6, 0x00005ed5, 0x00004ef4, 0x00003e13, 0x00002e32, 0x00001e51, 0x00000e70, 0x0000ff9f, 0x0000efbe, 0x0000dfdd, 0x0000cffc, 0x0000bf1b, 0x0000af3a, 0x00009f59, 0x00008f78, 0x00009188, 0x000081a9, 0x0000b1ca, 0x0000a1eb, 0x0000d10c, 0x0000c12d, 0x0000f14e, 0x0000e16f, 0x00001080, 0x000000a1, 0x000030c2, 0x000020e3, 0x00005004, 0x00004025, 0x00007046, 0x00006067, 0x000083b9, 0x00009398, 0x0000a3fb, 0x0000b3da, 0x0000c33d, 0x0000d31c, 0x0000e37f, 0x0000f35e, 0x000002b1, 0x00001290, 0x000022f3, 0x000032d2, 0x00004235, 0x00005214, 0x00006277, 0x00007256, 0x0000b5ea, 0x0000a5cb, 0x000095a8, 0x00008589, 0x0000f56e, 0x0000e54f, 0x0000d52c, 0x0000c50d, 0x000034e2, 0x000024c3, 0x000014a0, 0x00000481, 0x00007466, 0x00006447, 0x00005424, 0x00004405, 0x0000a7db, 0x0000b7fa, 0x00008799, 0x000097b8, 0x0000e75f, 0x0000f77e, 0x0000c71d, 0x0000d73c, 0x000026d3, 0x000036f2, 0x00000691, 0x000016b0, 0x00006657, 0x00007676, 0x00004615, 0x00005634, 0x0000d94c, 0x0000c96d, 0x0000f90e, 0x0000e92f, 0x000099c8, 0x000089e9, 0x0000b98a, 0x0000a9ab, 0x00005844, 0x00004865, 0x00007806, 0x00006827, 0x000018c0, 0x000008e1, 0x00003882, 0x000028a3, 0x0000cb7d, 0x0000db5c, 0x0000eb3f, 0x0000fb1e, 0x00008bf9, 0x00009bd8, 0x0000abbb, 0x0000bb9a, 0x00004a75, 0x00005a54, 0x00006a37, 0x00007a16, 0x00000af1, 0x00001ad0, 0x00002ab3, 0x00003a92, 0x0000fd2e, 0x0000ed0f, 0x0000dd6c, 0x0000cd4d, 0x0000bdaa, 0x0000ad8b, 0x00009de8, 0x00008dc9, 0x00007c26, 0x00006c07, 0x00005c64, 0x00004c45, 0x00003ca2, 0x00002c83, 0x00001ce0, 0x00000cc1, 0x0000ef1f, 0x0000ff3e, 0x0000cf5d, 0x0000df7c, 0x0000af9b, 0x0000bfba, 0x00008fd9, 0x00009ff8, 0x00006e17, 0x00007e36, 0x00004e55, 0x00005e74, 0x00002e93, 0x00003eb2, 0x00000ed1, 0x00001ef0 }; /****************************************************************************** *Function name : crc8, crc16, crc32 *Purpose: * These three functions compute the checksum on one word of 8, 16 or 32 bits, * respectively. They are used for the checksum of one or few words, in this * case using cycle. In case a more than 5 words are to be checked, it is * better to use the routines memcrc *Syntax: * unsigned int crc_out = crci(unsigned int datum, unsigned int crc_in); * where crci stands for one of crc8, crc16, crc32 *Input: * datum : the word to be checked * crc_in : to check one single word, this variable is set to 0xFFFFFFFF, * to check more than one word, the first time it is set to * 0xFFFFFFFF, then it is assigned the value crc_out from the * previous call *Output: * none *Return: * crc_out: the computed checksum ******************************************************************************/ unsigned int crc8 (unsigned int datum, unsigned int crc) { return ((crc << 8) ^ CRc_table[(((crc >> 8) ^ (datum)) & 0x00FF)]) & 0xFFFF; } unsigned int crc16 (unsigned int datum, unsigned int crc) { crc = (crc << 8) ^ CRc_table[(((crc ^ datum) >> 8) & 0x00FF)]; return ((crc << 8) ^ CRc_table[(((crc >> 8) ^ (datum)) & 0x00FF)]) & 0xFFFF; } unsigned int crc32 (unsigned int datum, unsigned int crc) { crc = (crc << 8) ^ CRc_table[(((crc >> 8) ^ (datum >> 24)) & 0x00FF)]; crc = (crc << 8) ^ CRc_table[(((crc >> 8) ^ (datum >> 16)) & 0x00FF)]; crc = (crc << 8) ^ CRc_table[(((crc ^ datum) >> 8 ) & 0x00FF)]; return ((crc << 8) ^ CRc_table[(((crc >> 8) ^ (datum)) & 0x00FF)]) & 0xFFFF; } /****************************************************************************** *Function name : memcrc8, memcrc16, memcrc32 *Purpose: * These three functions compute the checksum on a stream of 8, 16 or 32 bits * words, respectively. They are used when the words are more than 5, otherwise * it is better to use the routines crc. The corresponding _pm routines are * used for the DM segment mapped in PM *Syntax: * unsigned int crc_out = memcrci( unsigned int * p_data, unsigned int len, * unsigned int crc_in); * where memcrci stands for one of memcrc8, memcrc16, memcrc32 *Input: * p_data : pointer to the (array of) data to be checked * len : number of data memory words to be checked * crc_in : set to 0xFFFFFFFF the first time; to check more than one array, * the first time it is set to 0xFFFFFFFF, then it is assigned the * value crc_out from the previous call *Output: * none *Return: * crc_out: the computed checksum ******************************************************************************/ unsigned int memcrc8 (unsigned int *p_data, unsigned int len, unsigned int crc) { int i; for (i=0; i> 8) ^ (*(p_data))) & 0x00FF)]; p_data++; return (crc & 0xFFFF); } unsigned int memcrc16 (unsigned int *p_data, unsigned int len, unsigned int crc) { int i; for (i=0; i> 8) & 0x00FF)]; crc = ((crc << 8)) ^ CRc_table[(((crc >> 8) ^ (*(p_data))) & 0x00FF)]; p_data++; } return (crc & 0xFFFF); } unsigned int memcrc32 (unsigned int *p_data, unsigned int len, unsigned int crc) { int i, dataH; for (i=0; i> 16; crc = (crc << 8) ^ CRc_table[(((crc ^ dataH )>> 8) & 0x00FF)]; crc = (crc << 8) ^ CRc_table[(((crc >> 8) ^ (dataH ) ) & 0x00FF)]; crc = (crc << 8) ^ CRc_table[(((crc ^ *(p_data))>> 8) & 0x00FF)]; crc = (crc << 8) ^ CRc_table[(((crc >> 8) ^ *(p_data) ) & 0x00FF)]; p_data++; } return (crc & 0xFFFF); } unsigned int memcrc32_pm (unsigned int pm *p_data, unsigned int len, unsigned int crc) { int i, dataH; for (i=0; i> 16; crc = (crc << 8) ^ CRc_table[(((crc ^ dataH )>> 8) & 0x00FF)]; crc = (crc << 8) ^ CRc_table[(((crc >> 8) ^ (dataH ) ) & 0x00FF)]; crc = (crc << 8) ^ CRc_table[(((crc ^ *(p_data))>> 8) & 0x00FF)]; crc = (crc << 8) ^ CRc_table[(((crc >> 8) ^ *(p_data) ) & 0x00FF)]; p_data++; } return (crc & 0xFFFF); }