cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G031F6 CRC calculate problem.

ABURM
Associate III

Hi everyone.

I am working on SMBus communication and i want to calculate PEC (packet error code) value. PEC value is using CRC-8 checksum with 0x07 polynomial. I want to use CRC peripheral in STM32G031F6 for PEC calculate. Cubemx settings are below.

0693W00000LwtMvQAJ.pngAnd code is here;

CRC_Data[0] = 0x16;	
CRC_Data[1] = 0x0f;	
CRC_Data[2] = 0x17;	
CRC_Data[3] = 0xe9;	
CRC_Data[4] = 0x03;
SMBus_PEC = HAL_CRC_Calculate (&hcrc, CRC_Data, 5);

SMBus_PEC value is 0x54 but this is wrong. It must be 0xE8. I am calculating with online CRC calculation from this site ==> https://www.lddgo.net/en/encrypt/crc

I read from the SMBus PEC value is also 0xE8. Also, when I calculate it manually, i get same value.

0693W00000LwtNeQAJ.pngWhat is the problem?

Have a nice day.

1 ACCEPTED SOLUTION

Accepted Solutions

Is CRC_Data[] an array of bytes?

Observe in debugger, single-stepping in disasm, how exactly does (does it use str or strb, what is the content of CRC registers exactly, etc.)

JW

PS I tried to calculate this CRC on 0x16 0x00 0x00 0x00 0x0f, what you get if you define CRC_Data[] as words, and it gave me 0x54

View solution in original post

4 REPLIES 4

Is CRC_Data[] an array of bytes?

Observe in debugger, single-stepping in disasm, how exactly does (does it use str or strb, what is the content of CRC registers exactly, etc.)

JW

PS I tried to calculate this CRC on 0x16 0x00 0x00 0x00 0x0f, what you get if you define CRC_Data[] as words, and it gave me 0x54

ABURM
Associate III

Hi Waclawek.jan.

CRC_Data is not byte. It is uint32_t. I changed to unsigned char. But HAL_CRC_Calculate function gave an error because data array is uint32_t defined. I changed the program as follows;

SMBus_PEC = HAL_CRC_Calculate (&hcrc, (uint32_t*)&CRC_Data, 5);

Before making this change, when I changed the variables in the CRC_Data array, the PEC value did not change. But now it is changing and working. :grinning_face_with_sweat:

Thanks for your help, mate. :thumbs_up:

Chloe Meunier
ST Employee

Hello,

I have tried to generate CRC with your parameters and it works on my side in a STM32H735-DK board : I find 0xE8.

Buffer declaration :

#define BUFFER_SIZE  5

static const uint8_t aDataBuffer[BUFFER_SIZE] =

{ 0x16, 0x0F , 0x17 , 0xE9 , 0x03};

CRC initialization :

hcrc.Instance = CRC;

 hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_DISABLE;

hcrc.Init.GeneratingPolynomial = 0X07;

 hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_DISABLE;

hcrc.Init.InitValue=0x00;

 hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;

 hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;

hcrc.Init.CRCLength=CRC_POLYLENGTH_8B;

hcrc.InputDataFormat =CRC_INPUTDATA_FORMAT_BYTES;

 if (HAL_CRC_Init(&hcrc) != HAL_OK)

 {

  Error_Handler();

 }

HAL function call :

 uwCRCValue = HAL_CRC_Calculate(&hcrc, (uint32_t *)aDataBuffer, 5);

Result : 0XE8

ABURM
Associate III

Hi Chloe.

CRC calculate function using "uint32_t" for data buffer.

HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength)

I defined data buffer as uint32_t. I explained in detail above. This is exactly the problem.

Thanks for your support. :thumbs_up: