cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate 16-bit CRC using STM32F0 Peripheral

Nimit Vachhani
Associate III
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
5 REPLIES 5
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 Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on December 12, 2016 at 14:19

RM0090, rev.8 says:

The programmable polynomial feature applies to STM32F07x and STM32F09x devices

only.
Posted on December 12, 2016 at 15:01

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

 
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on December 13, 2016 at 06:21

Hey Clive thanks for the link. I am using STM32F051R8. I have seen your code some how i am getting error with that code. I am using HAL functions and i cannot set value for crc polynomial. I do have my software code from 8051 which uses same approach as yours 'CRC_MODBUS16Fast'. Well if i think i will use software approach instead. 

Thanks for the advice.

Posted on December 13, 2016 at 09:07

Nimit Vachhani wrote:

I am using STM32F051R8. [...]  i cannot set value for crc polynomial.

RM0090, rev.8 says:

The programmable polynomial feature applies to STM32F07x and STM32F09x devices

only.

JW