Products > Embedded Computing

32 bit CRC - is it standard?

(1/7) > >>

peter-h:
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
https://stm32f4-discovery.net/2015/07/hal-library-10-crc-for-stm32fxxx/ or this one
https://www.st.com/resource/en/application_note/dm00068118-using-the-crc-peripheral-in-the-stm32-family-stmicroelectronics.pdf

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:
https://stackoverflow.com/questions/21001659/crc32-algorithm-implementation-in-c-without-a-look-up-table-and-with-a-public-li

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.

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

List of some common ones here: https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Polynomial_representations_of_cyclic_redundancy_checks

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

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

SiliconWizard:
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: https://github.com/gcc-mirror/gcc/blob/master/libiberty/crc32.c

peter-h:
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 https://www.lammertbies.nl/comm/info/crc-calculation lists just one CRC-32.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod