dvbbs
收藏本页
联系我们
论坛帮助
dvbbs

单片机在线编程网侃单片机-ISPSTM32系列芯片的ISP → 让STM32的CRC32与主流计算工具一致,Make STM32F CRC compatible with windows/winzip/winrar


  共有19076人关注过本帖树形打印

主题:让STM32的CRC32与主流计算工具一致,Make STM32F CRC compatible with windows/winzip/winrar

帅哥哟,离线,有人找我吗?
McuIsp
  1楼 个性首页 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:管理员 贴子:43 积分:626 威望:0 精华:1 注册:2009-05-13 03:09:30
让STM32的CRC32与主流计算工具一致,Make STM32F CRC compatible with windows/winzip/winrar  发贴心情 Post By:2009-05-13 20:45:22


  //CopyRight:www.mcuisp.com
 //版权: 单片机在线编程网
 /**********************
 reverse bits of DWORD
 Input:
   u32 data -- the input
 Output:
   u32 data -- the output
 **********************/
 u32 revbit(u32 data)
 {
   asm("rbit r0,r0");
   return data;
 };
 /**********************
 Calculate CRC32 of DWORD data array.
 Input:
   u32 *dworddata -- the array point
   u32 dwordcount -- the data len in DWORD
 Output:
   u32 CRC32 -- the result
 **********************/
 u32 CalcCRC32(u32 *dworddata,u32 dwordcount)
 {
   u32 ui32;
   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE);
   CRC->CR=1;
   asm("NOP");asm("NOP");asm("NOP");//貌似CRC复位后需等会送数
   for(;dwordcount>0;dwordcount--)
   {
     ui32=*dworddata;
     dworddata++;
     ui32=revbit(ui32);//输入位序颠倒一下
     CRC->DR=ui32;
   }
   ui32=CRC->DR;
   ui32=revbit(ui32);//输出位序颠倒一下
   ui32^=0xffffffff;//异或
   return ui32;
 };
 /**********************
 test the CalcCRC32.
 Input:
   None
 Output:
   None
 **********************/
 void testCRC(void)
 {
   u32 ui32;
   union{
     u32 databuf32[256/4];
     u8 databuf8[256];
   };
   databuf32[0]=0x41424344;//实际为44 43 42 41
   ui32=CalcCRC32(databuf32+0,1);
   Lcd_ClrRect(0,0,128,64);
   DisplayBytes("CRC,Input:",0,0,65535,16,16);
   DisplayAscii(toHex(databuf8[0]>>4),0,16);
   DisplayAscii(toHex(databuf8[0]>>0),8,16);
   DisplayAscii(toHex(databuf8[1]>>4),16,16);
   DisplayAscii(toHex(databuf8[1]>>0),24,16);
   DisplayAscii(toHex(databuf8[2]>>4),32,16);
   DisplayAscii(toHex(databuf8[2]>>0),40,16);
   DisplayAscii(toHex(databuf8[3]>>4),48,16);
   DisplayAscii(toHex(databuf8[3]>>0),56,16);//44434241
   DisplayBytes("CRC,Output:",0,32,65535,16,16);
   DisplayAscii(toHex(ui32>>28),0,48);
   DisplayAscii(toHex(ui32>>24),8,48);
   DisplayAscii(toHex(ui32>>20),16,48);
   DisplayAscii(toHex(ui32>>16),24,48);
   DisplayAscii(toHex(ui32>>12),32,48);
   DisplayAscii(toHex(ui32>>8),40,48);
   DisplayAscii(toHex(ui32>>4),48,48);
   DisplayAscii(toHex(ui32>>0),56,48);//847a7f6e
 }
 //可以看出,stm32f与主流计算工具有三点差别:
 //1、输入位序颠倒,只需数据输入前颠倒位序
 //2、输出位序颠倒,获得结果后再颠倒回来。
 //3、异或0xffffffff

支持(0中立(0反对(0单帖管理 | 引用 | 回复 回到顶部

返回版面帖子列表

让STM32的CRC32与主流计算工具一致,Make STM32F CRC compatible with windows/winzip/winrar








签名