Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching CRC32 from STM32F0 and zlib

I'm working on a communication link between a computer running Linux and a STM32F0. I want to use some kind of error detection for my packets and since the STM32F0 has CRC32 hw and I have zlib with CRC32 on Linux I thought it would be a good idea to use CRC32 for my project. The problem is that I won't get the same CRC value for the same data on the different platforms.

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>

int
main(void)
{
  uint8_t byte0 = 0x00;
  uint32_t crc0 = crc32(0L, Z_NULL, 0);
  crc0 = crc32(crc0, &byte0, 1);
  printf("CRC32 value of %" PRIu8 " is: %08" PRIx32 "\n", byte0, crc0);
}

outputs CRC32 value of 0 is: d202ef8d which matches the result on several online calculators.

It seems though that whatever settings I use on the STM32 I can't get the same CRC. I have found a flowchart on how the CRC hw calculates its value in an application note from ST but I can't figure out how it's done in zlib.

Does anyone know if they are compatible?

[edit 1] They both uses the same init value and polynomial.

[edit 2] The STM32 code is relatively uninteresging since it's using the hw.

...
/* Default values are used for init value and polynomial, see edit 1 */
CRC->CR |= CRC_CR_RESET;
CRC->DR = (uint8_t)0x00;
uint32_t crc = CRC->DR;
...
like image 944
evading Avatar asked Dec 09 '22 05:12

evading


1 Answers

The CRC32 implementation on STM32Fx seems to be not the standard CRC32 implementation you find on many online CRC calculators and the one used in zip.

STM32 implements CRC32-MPEG2 which uses big endian and no final flip mask compared to the zip CRC32 which uses little endian and a final flip mask.

I found this online calculator which supports CRC32-MPEG2.

If you are more interested on other CRC algorithms and their implementation, look at this link.

PS: The HAL driver from STM supports input in byte, half word and word formats and they seem to work fine for STM32F0x in v1.3.1

like image 52
Ayyala Avatar answered Dec 11 '22 08:12

Ayyala