13 years C/C++/C# programing, focus on embedded and mobile device development.

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

用于对Sector中数据的校验, read sector crc, 经过很多努力才找到正确的算法:

 

unsigned long distribute(unsigned char data)
{
 unsigned long tap,a;
 unsigned char data2;
 tap=0;

 data2=data<<7;
 a=data2>>7;
 a=a<<0;
 tap=tap|a;

 data2=(data<<(8-1-1));
 a=data2>>7;
 a=a<<3;
 tap=tap|a;

 data2=(data<<(8-2-1));
 a=data2>>7;
 a=a<<5;
 tap=tap|a;

 data2=(data<<(8-3-1));
 a=data2>>7;
 a=a<<8;
 tap=tap|a;

 data2=(data<<(8-4-1));
 a=data2>>7;
 a=a<<10;
 tap=tap|a;

 data2=(data<<(8-5-1));
 a=data2>>7;
 a=a<<13;
 tap=tap|a;

 data2=(data<<(8-6-1));
 a=data2>>7;
 a=a<<16;
 tap=tap|a;

 data2=(data<<(8-7-1));
 a=data2>>7;
 a=a<<18;
 tap=tap|a;

 return tap;
}

unsigned long crc32(unsigned char* buffer,int len)
{
 unsigned long crc,tap,crc_flag;
 int i;
 unsigned char b;

 crc=0;
 tap=0;
 crc_flag=0;

 for(i=0;i<len;i++){
  b=buffer[i];

  crc_flag=crc>>31;
  crc=crc<<1;

  tap=distribute(b);

  crc=crc^tap;

  if(crc_flag==0){
  }
  else{
   crc=crc^0x00400007;
  }
 }

 return crc;
}

posted on 2008-09-05 12:45  woaiusd  阅读(349)  评论(0编辑  收藏  举报