cancel
Showing results for 
Search instead for 
Did you mean: 

CRC on byte string (odd or even length)

gds23
Associate II
Posted on March 09, 2010 at 16:40

CRC on byte string (odd or even length)

3 REPLIES 3
swhite2
Associate III
Posted on May 17, 2011 at 13:42

No. It only processes 4-byte words at once. You'll have to use a byte-wise software calculation to do what you want. There are many implementations on the internet--just use Google.

gds23
Associate II
Posted on May 17, 2011 at 13:42

Seems like since I can receive incoming messages that are random length and verify the CRC, I should be able to generate a CRC for an outgoing message of random length also using the word-wide CRC generator.  Or maybe it's a one-way function?

Sure, doing it in code is always possible, but would prefer to utilize the h/w facilities if at all possible.

Posted on May 17, 2011 at 13:42

The nature of the CRC is that of a long division, if you feed in the anticipated remainder the result will be zero.

The problem with the STM32's CRC register is that it shifts the high order bit from a 32-bit word read from memory as a small-endian value. This means the padding bits needed to form a 32-bit word from residual byte(s) gets processed before the data. So either you need to send the padding data, or accept there will be a discontinuity.

Presume you have the 5 byte sequence AA, BB, CC, DD, EE

The CRC hardware will receive this data as

DDCCBBAA, 00000000EE

and in fact the EE will be processed as the eighth byte, not the fifth.

It is certainly possible to use the HW generator, in combination with some software to handle residual byte(s), but you'll need to get your head around how the hardware works, and if it is even appropriate for short strings.

Personally, I'd just use the bit reversed polynominal 0xEDB88320 (STM uses 0x04C11DB7) and do it quickly in software.

Slower 1-bit method

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

{

  while(Size--)

  {

    int i;

    Crc = Crc ^ (DWORD)*Buffer++;

    for(i=0; i<8; i++)

      if (Crc & 1)

        Crc = (Crc >> 1) ^ 0xEDB88320;

      else

        Crc = (Crc >> 1);

  }

  return(Crc);

}

Faster 4-bit method, could do 8-bit would require a bigger table

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

{

  while(Size--)

  {

    static const DWORD CrcTable[] = {

0x00000000,0x1DB71064,0x3B6E20C8,0x26D930AC,0x76DC4190,0x6B6B51F4,0x4DB26158,0x5005713C,

0xEDB88320,0xF00F9344,0xD6D6A3E8,0xCB61B38C,0x9B64C2B0,0x86D3D2D4,0xA00AE278,0xBDBDF21C };

    Crc = Crc ^ (DWORD)*Buffer++;

    Crc = (Crc >> 4) ^ CrcTable[Crc & 0x0F];

    Crc = (Crc >> 4) ^ CrcTable[Crc & 0x0F];

  }

  return(Crc);

}

-Clive

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