How to set STM32 to generate standard CRC32

David Molnar picture David Molnar · Sep 22, 2016 · Viewed 18.1k times · Source

I am trying to generate CRC with STM32L4 hardware modul. I would like to validate fatfs files so basically I have byte arrays. I am using this CRC generator.

Unfortunately I cannot figure out how to set STM32L4 to generate the same result. I need CRC32 and I have

configuration:

hcrc.Instance = CRC;

/* The default polynomial is not used. It is required to defined it in CrcHandle.Init.GeneratingPolynomial*/
hcrc.Init.DefaultPolynomialUse    = DEFAULT_POLYNOMIAL_DISABLE;
/* Set the value of the polynomial */
hcrc.Init.GeneratingPolynomial    = 0x4C11DB7;
//hcrc.Init.GeneratingPolynomial    = 0xFB3EE248;
hcrc.Init.CRCLength= CRC_POLYLENGTH_32B;
/* The default init value is used */
/* The default init value is not used */
hcrc.Init.DefaultInitValueUse     = DEFAULT_INIT_VALUE_ENABLE;

/* User init value is used instead */
//hcrc.Init.InitValue               = 0;
hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
//hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_BYTE;
/* The input data are inverted by word */
//hcrc.Init.InputDataInversionMode  = CRC_INPUTDATA_INVERSION_WORD;

//hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_ENABLE;
hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
HAL_CRC_Init(&hcrc);

testing:

uint8_t test[] = {49,50,51,52};
uint32_t uwCRCValue = HAL_CRC_Calculate(&hcrc,(uint32_t *) test, 4);

result: A695C4AA

I am out of ideas. There is one way that I am succeeded with it to have uint32_t test[] and input is set to hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES; Unfortunately I have uint8_t...

Answer

Metin Celik picture Metin Celik · Feb 20, 2018

Using CubeMX, I generated with these settings:

hcrc.Instance = CRC;
hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE;
hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_BYTE;
hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_ENABLE;
hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;

Calculate the CRC like so:

uint32_t crc = HAL_CRC_Calculate(&hcrc, (uint32_t *)address, length);

And finally invert:

crc = ~crc;