2016-12-11 11:26 PM
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-162016-12-12 05:09 AM
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.
2016-12-12 05:19 AM
RM0090, rev.8 says:
The programmable polynomial feature applies to STM32F07x and STM32F09x devices
only.2016-12-12 07:01 AM
https://community.st.com/0D50X00009XkgXtSAJ
2016-12-12 10:21 PM
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.
2016-12-13 01:07 AM
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