2023-07-05 03:25 AM - edited 2023-07-05 03:25 AM
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.
2023-07-05 04:25 AM
Hello. How you have setup your SPI1, can you show the initialization code ?
2023-07-05 06:35 AM
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 Accept as Solution on the reply which solved your issue or answered your question.
2023-07-05 09:49 PM
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 */
}
2023-07-05 10:18 PM - edited 2023-07-05 10:18 PM
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
2023-07-05 10:20 PM
Have you test with 8 bit data instead of 4 bits ?
2023-07-06 02:33 AM
"Yes, I have tested with 8-bit data instead of 4 bits"
2023-07-06 02:36 AM
I have been working on a project that involves SPI communication, and I would like to confirm if my GPIO configuration is correct. I have MOSI (Master Out Slave In) connected to MISO (Master In Slave Out) for SPI communication.
Here is my GPIO initialization code:
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|EXT_RESET_Pin|LD3_Pin|LD6_Pin
|LD4_Pin|LD5_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : NCS_MEMS_SPI_Pin EXT_RESET_Pin LD3_Pin LD6_Pin
LD4_Pin LD5_Pin */
GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|EXT_RESET_Pin|LD3_Pin|LD6_Pin
|LD4_Pin|LD5_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : MEMS_INT1_Pin MEMS_INT2_Pin */
GPIO_InitStruct.Pin = MEMS_INT1_Pin|MEMS_INT2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : B1_Pin */
GPIO_InitStruct.Pin = B1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
/* SPI SCK, MOSI, and MISO pin configuration */
GPIO_InitStruct.Pin = GPIO_PIN_7 ; // SCK and MOSI pins
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_6; // MISO pin
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* SPI NSS (Chip Select) pin configuration */
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
2023-07-06 02:55 AM
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 Accept as Solution on the reply which solved your issue or answered your question.