GRPS LLC层FCS CRC-24校验算法

#include <stdio.h>
#include <stdlib.h>

typedef	char           Int8;
typedef unsigned char  Uint8;
typedef unsigned short Uint16;
typedef short          Int16;
typedef unsigned int   Uint32;
typedef int            Int32;
typedef float          Float32;
typedef long double    Float64;
typedef long long      Int64;
typedef unsigned long long Uint64;


//Uint8  tx_data1[] = { 0x03,0xfb,0x01,0x00,0x0e,0x00,0x32,0x11,0x03,0x16,0x05,0xf0,0x1a,0x05,0xf0,0x1e,0x00,0x00,0x22,0x00,
//		              0x00,0x25,0x0c,0x29,0x10,0x0c,0xb6,0xa9};  //0xa9b60c
//Uint8 tx_data1[] = { 0x41,0xfb,0x01,0x00,0x0e,0x00,0x32,0x11,0x03,0x16,0x01,0x90,0x63,0x28,0x0b}; // 0x0b2863
//Uint8 tx_data1[] = { 0x41,0xfb,0x01,0x00,0x0e,0x00,0x32,0x11,0x03,0x16,0x01,0xf0,0x14,0x49,0x20}; // 0x204914
Uint8 tx_data1[] = { 0x41,0xfb,0x01,0x00,0x0e,0x00,0x32,0x11,0x03,0x16,0x05,0xf0,0xd4,0x29,0xcb};   // 0x29cbd4
//位翻转函数
Uint32 Reflect(Uint32 ref,Uint8 ch)
{
	int i;
	Uint32 value = 0;
	for( i = 1; i < ( ch + 1 ); i++ )
	{
		if( ref & 1 )
			value |= 1 << ( ch - i );
		ref >>= 1;
	}
	return value;
}


Uint32 crc24_bit(Uint8 *ptr, Uint32 len, Uint32 gx)
{
    Uint8 i;
	Uint32 crc = 0xffffffff;
    while( len-- )
    {
        for( i = 1; i != 0; i <<= 1 )
        {
            if( ( crc & 0x800000 ) != 0 )
			{
				crc <<= 1;
				crc ^= gx;
			}
            else
				crc <<= 1;
            if( ( *ptr & i ) != 0 )
				crc ^= gx;
        }
        ptr++;
    }
    return ( Reflect(crc,24) ^ 0xffffff );
}

int main()
{
	Uint8 *data = tx_data1;
    Uint8 dataLen = sizeof(tx_data1) -3;

	printf("Slow CRC by bit          : %08x\n",crc24_bit( data, dataLen, 0xbba1b5 ));//多项式0xbba1b5

}


posted @ 2014-07-14 22:31  海阔天空84  阅读(529)  评论(0编辑  收藏  举报