cancel
Showing results for 
Search instead for 
Did you mean: 

What is de CRC16 used on SPI peripheral? the Polynomial is configurable, but the init value and reverse or not, is not documented.

FMaro.1
Associate II

I have a SPI on STM32F746 configured:

/* SPI6 init function */

void MX_SPI6_Init(void)

{

 hspi6.Instance = SPI6;

 hspi6.Init.Mode = SPI_MODE_MASTER;

 hspi6.Init.Direction = SPI_DIRECTION_2LINES;

 hspi6.Init.DataSize = SPI_DATASIZE_16BIT;

 hspi6.Init.CLKPolarity = SPI_POLARITY_LOW;

 hspi6.Init.CLKPhase = SPI_PHASE_2EDGE;

 hspi6.Init.NSS = SPI_NSS_HARD_OUTPUT;

 hspi6.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;

 hspi6.Init.FirstBit = SPI_FIRSTBIT_MSB;

 hspi6.Init.TIMode = SPI_TIMODE_DISABLE;

 hspi6.Init.CRCCalculation = SPI_CRCCALCULATION_ENABLE;

 hspi6.Init.CRCPolynomial = 32773;

 hspi6.Init.CRCLength = SPI_CRC_LENGTH_16BIT;

 hspi6.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

 if (HAL_SPI_Init(&hspi6) != HAL_OK)

 {

  Error_Handler();

 }

}

void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 if(spiHandle->Instance==SPI2)

 {

 /* USER CODE BEGIN SPI2_MspInit 0 */

 /* USER CODE END SPI2_MspInit 0 */

  /* SPI2 clock enable */

  __HAL_RCC_SPI2_CLK_ENABLE();

  __HAL_RCC_GPIOI_CLK_ENABLE();

  /**SPI2 GPIO Configuration

  PI1   ------> SPI2_SCK

  PI2   ------> SPI2_MISO

  PI3   ------> SPI2_MOSI

  */

  GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;

  HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);

 /* USER CODE BEGIN SPI2_MspInit 1 */

 /* USER CODE END SPI2_MspInit 1 */

 }

 else if(spiHandle->Instance==SPI6)

 {

 /* USER CODE BEGIN SPI6_MspInit 0 */

 /* USER CODE END SPI6_MspInit 0 */

  /* SPI6 clock enable */

  __HAL_RCC_SPI6_CLK_ENABLE();

  __HAL_RCC_GPIOA_CLK_ENABLE();

  __HAL_RCC_GPIOG_CLK_ENABLE();

  /**SPI6 GPIO Configuration

  PA4   ------> SPI6_NSS

  PA5   ------> SPI6_SCK

  PA6   ------> SPI6_MISO

  PG14   ------> SPI6_MOSI

  */

  GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF8_SPI6;

  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = GPIO_PIN_14;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF5_SPI6;

  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

  /* SPI6 DMA Init */

  /* SPI6_RX Init */

  hdma_spi6_rx.Instance = DMA2_Stream6;

  hdma_spi6_rx.Init.Channel = DMA_CHANNEL_1;

  hdma_spi6_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;

  hdma_spi6_rx.Init.PeriphInc = DMA_PINC_DISABLE;

  hdma_spi6_rx.Init.MemInc = DMA_MINC_ENABLE;

  hdma_spi6_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;

  hdma_spi6_rx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;

  hdma_spi6_rx.Init.Mode = DMA_NORMAL;

  hdma_spi6_rx.Init.Priority = DMA_PRIORITY_LOW;

  hdma_spi6_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;

  if (HAL_DMA_Init(&hdma_spi6_rx) != HAL_OK)

  {

   Error_Handler();

  }

  __HAL_LINKDMA(spiHandle,hdmarx,hdma_spi6_rx);

  /* SPI6_TX Init */

  hdma_spi6_tx.Instance = DMA2_Stream5;

  hdma_spi6_tx.Init.Channel = DMA_CHANNEL_1;

  hdma_spi6_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;

  hdma_spi6_tx.Init.PeriphInc = DMA_PINC_DISABLE;

  hdma_spi6_tx.Init.MemInc = DMA_MINC_ENABLE;

  hdma_spi6_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;

  hdma_spi6_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;

  hdma_spi6_tx.Init.Mode = DMA_NORMAL;

  hdma_spi6_tx.Init.Priority = DMA_PRIORITY_LOW;

  hdma_spi6_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;

  if (HAL_DMA_Init(&hdma_spi6_tx) != HAL_OK)

  {

   Error_Handler();

  }

  __HAL_LINKDMA(spiHandle,hdmatx,hdma_spi6_tx);

 /* USER CODE BEGIN SPI6_MspInit 1 */

 /* USER CODE END SPI6_MspInit 1 */

 }

}

1 REPLY 1
TDK
Guru

https://community.st.com/s/question/0D50X00009XkfZPSAZ/unable-to-understand-how-the-crc-calculation-is-done-in-the-spi-module

Link in the post seems to be broken, but the code is there to give you the answers you're looking for.

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