Skip to main content
Associate III
July 5, 2023
Question

HAL_SPI_TransmitReceive() on SPI1 of Discovery board giving HAL_BUSY

  • July 5, 2023
  • 2 replies
  • 2718 views

Hello STM32 community,

I'm currently working with the Discovery board and trying to implement SPI communication using the HAL library. Specifically, I am attempting to write data from the MOSI line and simultaneously read data from the MISO line using the HAL_SPI_TransmitReceive() function on SPI1.

However, I'm facing an issue where the HAL_SPI_TransmitReceive() function returns HAL_BUSY. I have verified the MOSI and MISO connections, and they are correctly wired. The SPI peripheral (SPI1) on the Discovery board is properly initialized and configured.

Here's a snippet of the code I'm using:


uint8_t txData = 0xAB; // Data to be transmitted
uint8_t rxData; // Data to be received

// Perform simultaneous transmit and receive
if (HAL_SPI_TransmitReceive(&hspi1, &txData, &rxData, 1, HAL_MAX_DELAY) == HAL_OK)
{
// Transmission and reception successful
// Process the received data as needed
}
else
{
// Transmission and reception failed
// Handle the error condition
}
I have tried increasing the delay using HAL_Delay() before calling HAL_SPI_TransmitReceive(), but it did not resolve the issue.

I would appreciate any insights or suggestions to help me understand why HAL_SPI_TransmitReceive() is returning HAL_BUSY. Is it possible to perform simultaneous transmit and receive on SPI1 of the Discovery board using this function?

Thank you in advance for your assistance.

This topic has been closed for replies.

2 replies

Graduate II
July 5, 2023

Hello. How you have setup your SPI1, can you show the initialization code ?

 

Associate III
July 6, 2023

 

 

Hello! Thank you for reaching out. Below is SPI1 configuration

 

static void MX_SPI1_Init(void)
{

/* USER CODE BEGIN SPI1_Init 0 */

/* USER CODE END SPI1_Init 0 */

/* USER CODE BEGIN SPI1_Init 1 */

/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_4BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */

/* USER CODE END SPI1_Init 2 */

}

Graduate II
July 6, 2023

Have you test with 8 bit data instead of 4 bits ? 

 

ST Technical Moderator
July 5, 2023

Hello @Sudharshan,

 

Would you please also specify the STM32 product?  Would you screenshot the debug just before the fail?

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL
Associate III
July 6, 2023

Hello,

Thank you for reaching out. we are working on Discovery Board part number STM32F072B-DISCO

Regarding the sentence "screenshot the debug just before the fail," . Could you please provide more information or context about what you meant by "screenshot the debug just before the fail"? This will help me better understand

 

ST Technical Moderator
July 6, 2023

Thank you for your feedback.

Regarding the screenshot, I meant, if possible, to share the state of the peripheral before returning HAL_BUSY.
I would recommend starting with cube example STM32CubeF0/Projects/STM32F072B-Discovery/Examples/SPI at master · STMicroelectronics/STM32CubeF0 · GitHub

/* Set the SPI parameters */
SpiHandle.Instance = SPIx;

SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SpiHandle.Init.CRCPolynomial = 7;
SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
SpiHandle.Init.NSS = SPI_NSS_SOFT;
SpiHandle.Init.TIMode = SPI_TIMODE_DISABLE;
SpiHandle.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
SpiHandle.Init.CRCLength = SPI_CRC_LENGTH_8BIT;

If still having issues, please keep me updated.

 

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL