📄 crc.c
字号:
//###########################################################################
// FILE: crc.c
// TITLE: Calculation CRC value of Uart data stream,and do code fore correct.
//
// Description: CRC generation equation is g(x)=x^16+x^12+x^5+1
//
// FEC generation equation is g(x)=x^10+x^8+x^7+x^5+x^4+x^3+1
//
// History: Ver1.0,Octomber 7,2008,by Alex Zhang
//
//###########################################################################
#include "crc.h"
#define GCRC 0x11021
#define GCORRECT 0x5b9//101,1011,1001
unsigned int CrcTable[256];
unsigned int CorrectErfSample[26];
unsigned long CorrectTable[26];
void CrcInit(void)
{
CreateCrcTable();
CreateCorrectTable();
}
//Create CRC Table
void CreateCrcTable(void)
{
unsigned int i,j;
unsigned long t;
for(i=0;i<256;i++){
t = i<<8;
for(j=0;j<8;j++){
t <<= 1;
if((t & (unsigned long)0x10000) != 0)
t ^= (unsigned long)GCRC;
}
CrcTable[i] = t & (unsigned long)0x0000ffff;
}
}
//Create FEC Corect table
void CreateCorrectTable(void)
{
unsigned int i,j;
unsigned long t;
j = 0x80;
for(i=0;i<8;i++){
CorrectErfSample[i] = CorrectCreate(j,0);
j >>= 1;
}
j = 0x80;
for(i=0;i<8;i++){
CorrectErfSample[i+8] = CorrectCreate(0,j);
j >>= 1;
}
j = 0x200;
for(i=0;i<10;i++){
CorrectErfSample[i+16] = j;
j >>= 1;
}
t = (unsigned long)1<<25;
for(i=0;i<26;i++){
CorrectTable[i] = t;
t >>= 1;
}
}
//Create Crc Code
unsigned int CrcCreate(unsigned int *Buf,unsigned int BufLen)//int as byte
{
unsigned int i,t1,t2,t3;
unsigned int crcval;
t1 = Buf[0];
t2 = Buf[1];
for(i=2;i<BufLen;i++){
t3 = Buf[i];
crcval = CrcTable[t1];
crcval ^= (t2<<8)+t3;
t2 = crcval & 0x00ff;//LOW(crcval);
t1 = crcval >> 8 ;//HIGH(crcval);
}
crcval = CrcTable[t1];
crcval ^= t2<<8;
t2 = crcval & 0x00ff;//LOW(crcval);
t1 = crcval >> 8;//HIGH(crcval);
crcval = CrcTable[t1];
crcval ^= t2<<8;
return crcval;
}
//Create FEC Code
unsigned int CorrectCreate(unsigned int CodeByte1,unsigned int CodeByte2)//int as byte
{
unsigned long code;
unsigned long gc;
int i;
code = (unsigned long)((CodeByte1<<8)+CodeByte2)<<10;
gc = (unsigned long)GCORRECT << (26-11);
for(i=0;i<16;i++){
if((code & (unsigned long)0x2000000)!=0)
code ^= gc;
code <<= 1;
}
return (code >> (26-10));
}
//return 0:no error,1:Correct error,2:error
unsigned int CorrectDeal(unsigned long *CorrectCode)
{
unsigned long code;
unsigned long gc;
unsigned int i,t;
code = *CorrectCode ;
gc = (unsigned long)GCORRECT << (26-11);
for(i=0;i<16;i++){
if((code & (unsigned long)0x2000000)!=0)
code ^= gc;
code <<= 1;
}
t = code >> (26-10);
if(t==0)
return 0;
for(i=0;i<26;i++){
if(t==CorrectErfSample[i]){
*CorrectCode ^= CorrectTable[i];
return 1;
}
}
return 2;
}
//End of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -