cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3 Problem when I use the HAL_SPI_Receive () function with SPI configured in SPI_DIRECTION_1LINE, 8 bits

gzazz.2041
Associate

Good evening,

I had a problem using the HAL_SPI_Receive () function with the SPI configured in SPI_DIRECTION_1LINE in 8 bits mode.

this function generated 16 dummy clock periods instead of 8.

the SPI was configured like this:

static void MX_SPI3_Init(void)

{

 /* USER CODE BEGIN SPI3_Init 0 */

 /* USER CODE END SPI3_Init 0 */

 /* USER CODE BEGIN SPI3_Init 1 */

 /* USER CODE END SPI3_Init 1 */

 /* SPI3 parameter configuration*/

 hspi3.Instance = SPI3;

 hspi3.Init.Mode = SPI_MODE_MASTER;

 hspi3.Init.Direction = SPI_DIRECTION_1LINE;

 hspi3.Init.DataSize = SPI_DATASIZE_8BIT;

 hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;

 hspi3.Init.CLKPhase = SPI_PHASE_2EDGE;

 hspi3.Init.NSS = SPI_NSS_SOFT;

 hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;

 hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;

 hspi3.Init.TIMode = SPI_TIMODE_DISABLE;

 hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

 hspi3.Init.CRCPolynomial = 7;

 hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

 hspi3.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

 if (HAL_SPI_Init(&hspi3) != HAL_OK)

 {

   Error_Handler();

 }

 /* USER CODE BEGIN SPI3_Init 2 */

 /* USER CODE END SPI3_Init 2 */

}

with the function:

....

u8RxPointer = 0;

while (u8RxPointer < 8) {

      HAL_SPI_Receive (&hspi3, &u8RxBuffer[u8RxPointer++], 1, SPI_TIMEOUT);

      for (u16Counter=0; u16Counter < 100; u16Counter++);

 }

....

I solved it modifying the HAL Library in this way:

I anticipated these lines of code...

   /* Check the end of the transaction */

 if (SPI_EndRxTransaction(hspi, Timeout, tickstart) != HAL_OK)

 {

   hspi->ErrorCode = HAL_SPI_ERROR_FLAG;

 }

before this...

 /* Receive data in 8 Bit mode */

 if (hspi->Init.DataSize <= SPI_DATASIZE_8BIT)

 {

.............

}

respect to the original.

has anyone had the same problem? is it a library bug?

Thanks

1 REPLY 1
S.Ma
Principal

Many similar post are lost in this forum about it.

So, to avoid writing a book each time:

  • Use only bidirectional mode, and just don't activate the GPIO you don't use
  • SPI IP V2 with programmable length and FIFO has been too much focused on performance than application: If you write 16 bit on the DR, before the FIFO existed, only the LSB is taken. With the FIFO both MSB and LSB will be queued resulting in 16 bit outputs.