cancel
Showing results for 
Search instead for 
Did you mean: 

CRC calculation in software

drechsel
Associate II
Posted on September 05, 2009 at 14:24

CRC calculation in software

15 REPLIES 15
mcuisp
Associate II
Posted on May 17, 2011 at 13:17

Hi:

I had research about this issue before:

Title:

Make STM32F CRC compatible with windows/winzip/winrar

//CopyRight:www.mcuisp.com

//Compile With IAR

/**********************

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'');//delay for hardware ready

for(;dwordcount>0;dwordcount--)

{

ui32=*dworddata;

dworddata++;

ui32=revbit(ui32);//reverse the bit order of input data

CRC->DR=ui32;

}

ui32=CRC->DR;

ui32=revbit(ui32);//reverse the bit order of output data

ui32^=0xffffffff;//xor with 0xffffffff

return ui32;//now the output is compatible with windows/winzip/winrar

};

Best Regards!

James Peng

mcuisp AT gmail.com

[ This message was edited by: mcuisp on 05-09-2009 17:55 ]

Shang.Dawei
Associate II
Posted on May 17, 2011 at 13:17

[DEAD LINK /public/STe2ecommunities/mcu/Lists/ARM%20CortexM3%20STM32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/ARM CortexM3 STM32/CRC32 calculation mismatch&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000626BE2B829C32145B9EB5739142DC17E&currentviews=635]https://my.st.com/public/STe2ecommunities/mcu/Lists/ARM%20CortexM3%20STM32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/ARM%20CortexM3%20STM32/CRC32%20calculation%20mismatch&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000626BE2B829C32145B9EB5739142DC17E&currentviews=635

//****************************************************************************

DWORD CRC32WideFast(DWORD Crc, DWORD Size, BYTE *Buffer)

{

Size = Size >> 2; // /4

while(Size--)

{

static const DWORD CrcTable[16] = { // Nibble lookup table for 0x04C11DB7 polynomial

0x00000000,0x04C11DB7,0x09823B6E,0x0D4326D9,0x130476DC,0x17C56B6B,0x1A864DB2,0x1E475005,

0x2608EDB8,0x22C9F00F,0x2F8AD6D6,0x2B4BCB61,0x350C9B64,0x31CD86D3,0x3C8EA00A,0x384FBDBD };

Crc = Crc ^ *((DWORD *)Buffer); // Apply all 32-bits

Buffer += 4;

// Process 32-bits, 4 at a time, or 8 rounds

Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; // Assumes 32-bit reg, masking index to 4-bits

Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; //  0x04C11DB7 Polynomial used in STM32

Crc = (Crc << 4) ^ CrcTable[Crc >> 28];

Crc = (Crc << 4) ^ CrcTable[Crc >> 28];

Crc = (Crc << 4) ^ CrcTable[Crc >> 28];

Crc = (Crc << 4) ^ CrcTable[Crc >> 28];

Crc = (Crc << 4) ^ CrcTable[Crc >> 28];

Crc = (Crc << 4) ^ CrcTable[Crc >> 28];

}

return(Crc);

}

petter23
Associate II
Posted on October 11, 2011 at 14:53

I got this code to produce what seems to be CRC-32 on even 4-byte input using the CRC unit correctly, at least it produces the same CRC-32 as perl crc32 and some other CRC-32 according to the mentioned specifikation earler.

#include ''stm32f2xx.h''

uint32_t my_crc32(uint8_t* data, uint32_t len)

{

    uint32_t* pBuffer = (uint32_t*) data;

    uint32_t BufferLength = len/4;

    uint32_t index = 0;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);

    CRC_ResetDR();

      

    for(index = 0; index < BufferLength; index++)

    {

        CRC->DR = __RBIT(pBuffer[index]);

    }

    return __RBIT(CRC->DR) ^ 0xFFFFFFFF;

}

Posted on October 11, 2011 at 15:37

There have been quite a few discussions on CRC computation, including this one which adapts mcuisp's 32-bit algorithm to byte boundary conditions.

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/CRC computation&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=1034]CRC Computation

Unfortunately most of the pre May-2011 links are broken due to forum software failures.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rdittmar
Associate
Posted on March 29, 2012 at 16:45

Hey swhite!

Perfect - thank you - that helped me a lot!!!
Posted on February 03, 2018 at 17:03

Current working link

https://community.st.com/0D50X00009XkbNMSAZ

 
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..