Computing > Embedded Computing

32 bit CRC - is it standard?

(1/7) > >>

I am programming a 32F417 which has a hardware CRC calculator, but the STM HAL function for it is too convoluted for me to understand. But also I need a version to run "on the other end" which doesn't have a CRC calculator.

So I am looking for a software algorithm. It does not need to be identical to the STM hardware one, which may be this one or this one

It doesn't need to be fast. I would like > 1MB/sec but even one which doesn't use a table should do that (168MHz CPU with a barrel shifter). It needs to take in 1 byte at a time, not a pointer to a buffer and a length (because I will be calculating it over varying amounts of data, and too much to fit into RAM in one go).

But I am finding various algorithms which seem to be different... I've been told that the 32 bit CRC has been standardised.

For example I found this one:

which is trivially modified for a byte at a time:

--- Code: ---static uint32_t crc = 0xFFFFFFFF;  // this value gets the accumulated CRC as you go along

void crc32b(uint8_t input_byte) {

   uint32_t mask, j;

      crc = crc ^ input_byte;
      for (j = 7; j >= 0; j--)
         mask = -(crc & 1);
         crc = (crc >> 1) ^ (0xEDB88320 & mask);
--- End code ---

Thank you in advance for any ideas. I have tons of CRC-16 code and have used these many times, some with big tables and pretty fast, but not CRC-32.

There are numerous standards that use a CRC. Some share the same polynomial, initial value and bit order.

List of some common ones here:

The results for a given CRC (such as those listed) will be the same no matter how it is calculated.

OK; thanks. The CRC-32 in that list is probably the one in my code in the 1st post:

CRC32 is a CRC on 32 bits. There are many possible polynomials. Refer to the Wikipedia article for the "standard" ones. There are several.

Two things to note: apart from the polynomial, of course you can select the start value. One thing that can vary as well is that the resulting CRC can be negated or not (logical NOT). If you're using a CRC that you control on both sides, picking all 1's for the start value and not negating the result is fine.

The second point is that you can use a pre-built array of 256 32-bit values that can replace the inner loop of 8 iterations. Usually faster (but depending on architecture.)

You can have a look at GCC's implementation to get some ideas:

That's a nice algorithm. It looks exactly the same CRC32 as my original code, however.

Clearly there are far fewer variations in the 32 bit CRC spectrum. Almost zero, actually :) This seems to be the "Ethernet" CRC, polynomial 0x04c11db7. And it is the same as the STC 32F4 hardware version

One thing which may vary is the byte order stored in the file, or sent in the serial data stream. For example Modbus swaps the two bytes in the CRC-16.

Interestingly this site lists just one CRC-32.


[0] Message Index

[#] Next page

There was an error while thanking
Go to full version