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,

1 ACCEPTED SOLUTION

Accepted Solutions
crc=E57F / 1A80 LEFT 3D65
crc=E43A / 1BC5 RIGHT A6BC

I would suggest keep using the 0x3D65, bit reverse the input, and bit reverse the output.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

11 REPLIES 11
TDK
Guru

Perhaps try it with only 2-3 bytes first. Going to be a lot easier to work with.

The link you shared doesn't have the last few zero bytes on it.

TDK_0-1692882906327.png

 

If you feel a post has answered your question, please click "Accept as Solution".
TDK
Guru

> LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_CRC);

This disables the CRC clock. Don't do that until you're done with it.

If you feel a post has answered your question, please click "Accept as Solution".

0x30D2 seems consistent with your presentation and the CRC polynomial given, so hardware is working properly / as expected. Perhaps you have documentation for this CRC and some actual test vectors or exemplars.

The problem with on-line CRC calculators, and how people use them, masks a lot of the important details. Like how it manages your input data. I'm from a background that deals with how data is stored and moved around in computer systems, its much easier for me to code a model in C, without any ambiguity about how the data is presented.

Assume the on-line calculator is wrong, or at least incorrectly interpreted. Find some example from the standard specification, or the hardware generating and using them in-real-life.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Dosan
Associate III

Sorry, your rigth, the correct Data in code is without the last 2 bytes 0x00, 0x00.

Remaking the calcs, the new value that i get is 0x1A80 with XOR 0xFFFF applied and its still wrong.

I have different data examples with CRC cals that are the same that diferent online calculators, so in this case i can asume that the online calculators are ok. I have tried with 3 different online calculators and all of them give me the same CRC 16 DNP value.

Dosan
Associate III

I dont disable the clock until i have finished the calcs.

Ok, but now you've changed the data and the first post.

The STM32 HW generated number matched a SW side computation I did on a PC, independently.

The STM32 is doing what it is supposed to be doing, now it could be that the shift direction or MSB sense of the CRC or the data is not configured correctly, but then you'd need to get into specifics about that.

Do you have example data from an actual device?

Other examples for CRC-16/DNP 0xA6BC Right Shifting, where as you have the STM32 using 0x3D65 Left Shifting.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TDK
Guru

If you do an example with 2 bytes, or some other small amount, I'll check it.

Changing the OP doesn't help us here.

> I dont disable the clock until i have finished the calcs.

This disables the clock after initialization, no?

TDK_0-1692977324253.png

 

If you feel a post has answered your question, please click "Accept as Solution".
crc=E57F / 1A80 LEFT 3D65
crc=E43A / 1BC5 RIGHT A6BC

I would suggest keep using the 0x3D65, bit reverse the input, and bit reverse the output.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..