Skip to main content
drechsel
Associate
September 5, 2009
Question

CRC calculation in software

  • September 5, 2009
  • 15 replies
  • 4169 views
Posted on September 05, 2009 at 14:24

CRC calculation in software

    This topic has been closed for replies.

    15 replies

    Shang.Dawei
    Visitor II
    May 17, 2011
    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
    Visitor II
    October 11, 2011
    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;

    }

    Tesla DeLorean
    Guru
    October 11, 2011
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Tesla DeLorean
    Guru
    February 3, 2018
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    rdittmar
    Visitor II
    March 29, 2012
    Posted on March 29, 2012 at 16:45

    Hey swhite!

    Perfect - thank you - that helped me a lot!!!