cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L476RG Full-Duplex SPI Slave Mode Configuration Issue

kmkoo
Associate III

Hi STM32 Community,

I'm working with an STM32L476RG board and attempting to set it up as a Full-Duplex SPI slave. My configuration in STM32CubeIDE seems correct, but I'm encountering an issue with receiving data. Attached are my STM32CubeIDE configuration screenshots and oscilloscope readings from the SPI master. (Master is sending out 6 bytes, "1, 2, 3, 4, 5, 6)The oscilloscope's D0, D1, and D2 channels are connected to PC3, PB10, and PB12, respectively. I believe my clock parameter settings are accurate.

pinout_view1.png

pinout_view2.png

Screenshot 2024-02-09 at 9.35.44 PM.png

The problem is that data reception is not working as expected. The HAL_SPI_RxCpltCallback function is never triggered, and consequently, an LED that should toggle within this callback remains static. Below are snippets of my STM32 code related to this issue:

1. Buffer and functions:

uint8_t rxData[6];
 
void LED_Toggle(void){
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
 
void HAL_SPI_RxCpltCallback (SPI_HandleTypeDef *hspi){
  LED_Toggle();
}
 
2. Main function: 


MX_GPIO_Init();
MX_USART2_UART_Init();
MX_SPI2_Init();

HAL_SPI_Receive(&hspi2, rxData, 6, HAL_MAX_DELAY);

while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */
}

3. SPI initialization: 

static void MX_SPI2_Init(void)
{
hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_SLAVE;
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi2.Init.NSS = SPI_NSS_HARD_INPUT;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.CRCPolynomial = 7;
hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi2.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI2_Init 2 */

/* USER CODE END SPI2_Init 2 */

}

I've also attached the full code for reference. Any guidance or insights would be greatly appreciated. Thank you in advance for your help!

 
 
3 REPLIES 3
TDK
Guru

> HAL_SPI_Receive(&hspi2, rxData, 6, HAL_MAX_DELAY);

This is a blocking function. It will block (wait) until the SPI operation completes, and then return. It will not call callbacks like HAL_SPI_RxCpltCallback.

If you want to do something at completion, put it after the call to HAL_SPI_Receive.

 

If you use non-blocking calls like HAL_SPI_Receive_IT/DMA, then HAL_SPI_RxCpltCallback is called at their (asynchronous) completion.

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

Thank you for your reply!

But shouldn't callback function be called after the receival is complete? I believe that master device is sending 6 bytes continuously(verified by oscilloscope image), so callback function should be called, but it is not being called at all. 

> But shouldn't callback function be called after the receival is complete?

No. As I explained in the previous post, HAL_SPI_Receive will not call HAL_SPI_RxCpltCallback.

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