Skip to main content
Nimit Vachhani
Associate III
December 12, 2016
Question

Calculate 16-bit CRC using STM32F0 Peripheral

  • December 12, 2016
  • 2 replies
  • 1870 views
Posted on December 12, 2016 at 08:26

Hello All,

I want to implement MODBUS RTU Slave for my Discovery STM32F0. I have worked on this protocol in 8051 controller. As i was going through CUBE i saw CRC peripheral in STM32F0 so i was curious and want to implement it on hardware side. I have implemented as below

 /*##-1- Configure the CRC peripheral #######################################*/
 CrcHandle.Instance = CRC;
 /* The default polynomial is used */
 CrcHandle.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE;
 /* The default init value is used */
 CrcHandle.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
 /* The input data are not inverted */
 CrcHandle.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
 /* The output data are not inverted */
 CrcHandle.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
 /* The input data are 8 bits lenght */
 CrcHandle.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
 if (HAL_CRC_Init(&CrcHandle) != HAL_OK)
 {
 /* Initialization Error */
 Error_Handler();
 }
 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_USART1_UART_Init();
 /* Initialize interrupts */
 MX_NVIC_Init();
HAL_GPIO_WritePin(ADC_DATA_Rdy_GPIO_Port , ADC_DATA_Rdy_Pin, GPIO_PIN_SET); 
HAL_GPIO_WritePin(ADC_Clk_GPIO_Port , ADC_Clk_Pin, GPIO_PIN_RESET); 
 
tx_buf[0] = 0x01; 
tx_buf[1] = 0x03;
tx_buf[2] = 0x00;
tx_buf[3] = 0x00;
tx_buf[4] = 0x00;
tx_buf[5] = 0x02;
uwCRCValue = HAL_CRC_Accumulate(&CrcHandle,(uint32_t *) tx_buf, 6);�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

But i am receiving wrong value for crc. My input is bytes of len 6 . Kindly help me if possible.

#crc-16
This topic has been closed for replies.

2 replies

Tesla DeLorean
Guru
December 12, 2016
Posted on December 12, 2016 at 14:09

Well it is not going to use the default polynomial, so you'll need to set one appropriate for MODBUS.

Which F0 part specifically are we talking about? I'm not sure they all support a programmable CRC.

I have posted MODBUS CRC examples, using the SPL and hardware and software computations. Search and Review.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
December 12, 2016
Posted on December 12, 2016 at 15:01

https://community.st.com/0D50X00009XkgXtSAJ

 
Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
December 12, 2016
Posted on December 12, 2016 at 14:19

RM0090, rev.8 says:

The programmable polynomial feature applies to STM32F07x and STM32F09x devices

only.