cancel
Showing results for 
Search instead for 
Did you mean: 

CRC16-DNP calculation error

Dosan
Associate III

Hi,

Im trying to get CRC16 - DNP from a string array that its the next:

char Message2[] =
		{
		0x00, 0x00, 0x00, 0x29, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x31, 0x30, 0x30, 0x30,
		0x30, 0x30, 0x30, 0x30, 0x32, 0x30, 0x36, 0x33, 0x57, 0x47, 0x31, 0x36, 0x53, 0x57, 0x54, 0x54, 0x20,
		0x57, 0x30, 0x2E, 0x31, 0x31, 0x30, 0x42, 0x42, 0x42, 0x52, 0x31, 0x30, 0x32, 0x01, 0x01, 0x00, 0x03,
		0xB6, 0x4D, 0x45, 0x4D, 0x42, 0x00, 0x00, 0x10, 0x53, 0x31, 0x35, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30,
		0x32, 0x35, 0x39, 0x39, 0x00, 0x61, 0x00, 0x00, 0x24, 0x00, 0x16, 0x65, 0xFF, 0x98, 0xEA, 0xA8, 0x40,
		0x62, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00, 0x01,
		0x10, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0xE9,
		0xF1, 0xE3};

 

 

 

And Im using STM32CubeIDE with a STM32G0 microcontroller that have a CRC peripheral unit, so i think that i config correctly the peripheral:

 

 

/* USER CODE BEGIN CRC_Init 1 */
  LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_CRC);
  /* USER CODE END CRC_Init 1 */
  LL_CRC_SetInputDataReverseMode(CRC, LL_CRC_INDATA_REVERSE_NONE);
  LL_CRC_SetOutputDataReverseMode(CRC, LL_CRC_OUTDATA_REVERSE_NONE);
  LL_CRC_SetPolynomialCoef(CRC, 0x3D65);
  LL_CRC_SetPolynomialSize(CRC, LL_CRC_POLYLENGTH_16B);
  LL_CRC_SetInitialData(CRC, 0);
  /* USER CODE BEGIN CRC_Init 2 */
  LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_CRC);

 

 

 When i send data to CRC i use the next code:

 

 

LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_CRC);

	LL_CRC_ResetCRCCalculationUnit(CRC);
	while (Len--)
		LL_CRC_FeedData8(CRC,*Adr++);

	CrcCalc=LL_CRC_ReadData16(CRC);

	LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_CRC);			// Peripheral clock disable

 

 

I check that all bytes are getting correctly into the  FeedData8() function but i dont get the correct CRC value, that in my case is 0xE57F and if i do the last operation XOR 0xFFFF the result is 0x1A80. 

Using online calculators, the correct value should be 0x1BC5, for example using: https://crccalc.com/?crc=0000002930000000000000000131303030303030303230363357473136535754542057302E3131304242425231303201010003B64D454D42000010533135313030303030323539390061000024001665FF98EAA84062FFFF0000FFFF00000000000110000001100101010000000000000000000000000000000000000000000000000000000008E9F1E...

I dont know if im doing something wrong (that is my best option) or the calcs are correct and the online calculators not.

Could anyone check my steps and confirm that are ok or not?

Thanks,

11 REPLIES 11

Your rigth and now its working.

Setting bit reverse the input and bit reverse the output the CRC calculation work.

Thanks so much.

Dosan
Associate III

Yes, i disable the clock to get better power consumption, but when i use the CRC is enable.

Thanks,

Best Regards