AnsweredAssumed Answered

Calculate 16-bit CRC using STM32F0 Peripheral

Question asked by Nimit Vachhani on Dec 12, 2016
Latest reply on Dec 13, 2016 by Nimit Vachhani

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.

Outcomes