Skip to main content
Senior
November 8, 2024
Solved

SPI Slave Receive Interrupt

  • November 8, 2024
  • 4 replies
  • 1822 views

I am trying to make a very simple SPI ISR to read the RXDR and store the value to a buffer. However, I cannot get the ISR to even trigger without using HAL_SPI_Receive_IT(). I do not want to use the callbacks or the default IRQ generated by cube. I currently just have the following in my ISR:

 

void spiISR(SPI_HandleTypeDef *hspi){
	if (__HAL_SPI_GET_FLAG(&hspi1, SPI_FLAG_RXNE)) {

		rxBuffer[rxIndex] = SPI1->RXDR;
		// Increment index and wrap around if necessary
		rxIndex = (rxIndex + 1) % RX_BUFFER_SIZE;

		__HAL_SPI_CLEAR_OVRFLAG(&hspi1);

	}
}

 

Since I don't want to use the generated IRQ I do this:

 

void SPI1_IRQHandler(void)
{
 /* USER CODE BEGIN SPI1_IRQn 0 */
	spiISR(&hspi1);
	return;
 /* USER CODE END SPI1_IRQn 0 */
 HAL_SPI_IRQHandler(&hspi1);
 /* USER CODE BEGIN SPI1_IRQn 1 */

 /* USER CODE END SPI1_IRQn 1 */
}

 

 

I have turned on the following:

 

 MX_SPI1_Init();
 /* USER CODE BEGIN 2 */
 HAL_NVIC_EnableIRQ(SPI1_IRQn);
 __HAL_SPI_ENABLE_IT(&hspi1, SPI_IT_RXNE);

 

 

This would leave me to believe that the ISR should trigger when the fifo is not empty; however that is not the case.
I have ensured data is being transferred to the board on an oscilloscope, but the board shows no indication of receiving data

This topic has been closed for replies.
Best answer by EthanMankins

Thank you for the responses @PGump.1 and @gbm
I have found that I needed to call __HAL_SPI_ENABLE(&hspi1); to get the interrupt to trigger however the data coming in seems random. I try sending 1-8 to this slave, and the data shows correctly on oscilloscope; however, I just get repeating sequences of:

64 192 224 1 1 1 129 1 

 
20 12 14 8 16 16 24 16 

 


Sometimes other randoms sequences appear too, I'm not seeing any correlation between these numbers. However, I will likely create a new post since this no longer pertains to the ISR not triggering.

4 replies

gbm
Super User
November 9, 2024

Have you configured the SPI? Show the code.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Senior
November 11, 2024

Hi @gbm ,
Here is my Initialization for the spi slave

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_SLAVE;
 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
 hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
 hspi1.Init.NSS = SPI_NSS_SOFT;
 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 hspi1.Init.CRCPolynomial = 0x0;
 hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
 hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
 hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
 hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
 hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
 hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
 hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
 hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
 hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
 hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
 if (HAL_SPI_Init(&hspi1) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN SPI1_Init 2 */

 /* USER CODE END SPI1_Init 2 */

}
PGump.1
Senior II
November 11, 2024

Hi,

Part of the problem is - you are not clearing the IRQ in your handler.

Oops - cancel the above - I was thinking of another brand device (not ST)...

Kind regards
Pedro

AI = Artificial Intelligence, NI = No Intelligence, RI = Real Intelligence.