cancel
Showing results for 
Search instead for 
Did you mean: 

SPI always calling callback

IRodr.2
Associate II

Hello to everyone.

I'm creating a project where i need to communicate via SPI. My main board, is configured as full duplex Master, and i have other STM working as full duplex slave.

But, i have a problem. If i configure the boards to just have one function, for example, the master to send messages, and the slave to only receive, everything works ok. But, when i configure the Master to receive messages and the Slave to send messages, it's allways calling the CallBackFunction on both boards.

Can anyone help me? I already check and Pull is at Pulldown.

static void MX_SPI4_Init(void)
{
 
  /* USER CODE BEGIN SPI4_Init 0 */
 
  /* USER CODE END SPI4_Init 0 */
 
  /* USER CODE BEGIN SPI4_Init 1 */
 
  /* USER CODE END SPI4_Init 1 */
  /* SPI4 parameter configuration*/
  hspi4.Instance = SPI4;
  hspi4.Init.Mode = SPI_MODE_MASTER;
  hspi4.Init.Direction = SPI_DIRECTION_2LINES;
  hspi4.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi4.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi4.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi4.Init.NSS = SPI_NSS_SOFT;
  hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  hspi4.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi4.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi4.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi4.Init.CRCPolynomial = 7;
  hspi4.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  hspi4.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;
  if (HAL_SPI_Init(&hspi4) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI4_Init 2 */
 
  /* USER CODE END SPI4_Init 2 */
 
}
HAL_SPI_Receive_IT(&hspi4,(uint8_t*)RxData,1);
 
while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	  if(send == 1)
	  {
		  ++ubKeyNumber;
		  TxData[0] = ubKeyNumber;
		  if (HAL_SPI_Transmit(&hspi4, (uint8_t*)TxData, sizeof(TxData),1000) != HAL_OK)
		  {
			  /* Transmission request Error */
			  Error_Handler();
		  }
		  if(ubKeyNumber == 0x03)
		  {
			  ubKeyNumber = 0x00;
		  }
 
 
		  send=0;
	  }
}
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
{
	LED_Display(RxData[0]);
	HAL_SPI_Receive_IT(&hspi4,(uint8_t*)RxData,1); //Reactivate the flag
}

4 REPLIES 4
TDK
Guru

Seems like your callback starts reception again. Maybe the master is continuing to clock. Hard to tell what's going on with limited code.

Generally you should avoid HAL_SPI_Receive on master due to the wonky way the clocking works. Use HAL_SPI_TransmitReceive instead with dummy data.

Plenty of CubeMX examples to go off of.

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

Hi, thanks for your answer. The data here is dummy just for test porpuses, but it will be important in the future.

What part of the code do you think it would be usefull?

The part with the bug in it.
At a minimum, the logic used for sending and receiving SPI messages and for the synchronization between the two.
If you feel a post has answered your question, please click "Accept as Solution".

hmmmm.... But i already send to you all the code that i wrote.....

The goal is, when i click the blue button on the board, it starts to transmit the data..... and with the interrupt it would receive the message