//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
|