cancel
Showing results for 
Search instead for 
Did you mean: 

SPI communication with STM32H7(Master) to STM32F1(slave) with interrupt mode

Kanna
Associate III

Hi,

i am trying to implement a SPI communication with STM32H7(Master) to STM32F1(slave) with interrupt mode. In Slave Side SPI3(with NSS Hardware Input) i have enabled, In Master Side SPI6 (GPIO Pin Toggling for SS) I have Enabled

My Issue is at Slave Side I have this Function to receive from master HAL_SPI_Receive_IT(&hspi3, RX_Buffer, 4); and Its not Entering into -> void SPI3_IRQHandler(void) function. How we can resolve this issue? and Only one time below condition gets satisfied in HAL_SPI_Receive_IT function

"if (hspi->State != HAL_SPI_STATE_READY)"

10 REPLIES 10
CPINA
Associate III

Hello,

Does RXNE flag is set even if you don't have the interrupt ? Are you sure your SPI slave is ready before the master. Because, for instance, if you sent one data from the master, the SPI clock starts and the data is transferred to the slave. But if this one is enabled somewhere after the clock has already started from the master, the slave is waiting for missing clock to set RXNE flag at 1.

So Slave needs always to be ready before the master.

BR

Kanna
Associate III

Dear CPINA,

    Yes, After SPI3_Init() function i am enabling RSNE flag  "__HAL_SPI_ENABLE_IT(&hspi3, SPI_IT_RXNE);" and also setting DFF bit and SPE bit

    "hspi3.Instance->CR1 = 0x0840;"

But Once  "HAL_SPI_Receive_IT(&hspi3,(uint8_t*)&read,4);" this function get completed in CR2 Register ERRIE Bit also has been set

Can you please help me of this configuration sequence!

Thanks

Kanna
Associate III

Dear CPINA,

Could you please help (Please Find the Below Comment)

TDK
Guru

Did you enable the NVIC interrupt?

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

as said by @TDK​ , Can you check that you enabled the NVIC Interrupt. The ERROR you get are probably linked to overrun (you can check the flag OVR) because your data is accumulated without being read by your software from the interrupt which is not generated.

Kanna
Associate III

Hi

Yes i have enabled both side(master and slave) interrupt, i did a mistake in clock settings that i have resolved now now i am able to receive the data from master side but only one byte alone receiving

Master Side in While(1) loop :

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);

    HAL_Delay(10);

    HAL_SPI_Transmit_IT(&hspi6, TX_Buffer, 3);

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);

Slave side:

 /* USER CODE BEGIN 2 */

    hspi3.Instance->CR1 = 0x0840;

    HAL_SPI_Receive_IT(&hspi3,(uint8_t*)&read,3);

and in Interrupt Handler

    RX_Resp_Buffer[i++] = hspi3.Instance->DR;

Did i miss Something? or anything i need to change

TDK
Guru

HAL_SPI_Transmit_IT is asynchronous. You need to wait for the transfer to complete before raising the CS line.

HAL_SPI_Receive_IT will put data into the buffer, you shouldn't read it from DR, you should read it from the buffer. ("read" in this case)

> hspi3.Instance->CR1 = 0x0840;

Not sure what this does offhand but you generally shouldn't be messing with registers if you're using regular HAL functions.

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

Dear TDK,

I have tested the way that you have mentioned

Master side :

uint8_t TX_Buffer[2] = {9,7};

while(1)

{

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);

    HAL_Delay(10);

    HAL_SPI_Transmit_IT(&hspi6, TX_Buffer, 2);

   HAL_Delay(500);

    if(SPI_FLAG_EOT)

{

     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);

    }

}

Slave side:

while(1)

{

     HAL_SPI_Receive_IT(&hspi3,(uint8_t*)&read,2);

}

As you said in this case read buffer is not receiving anything in by reading from DR register in spi3_irqHandler function only i can read only one data Can you please help me?

TDK
Guru

There are a lot of issues with this code.

I suggest following an example from CubeMX:

https://github.com/STMicroelectronics/STM32CubeF1/blob/f5aaa9b45492d70585ade1dac4d1e33d5531c171/Projects/STM32F103RB-Nucleo/Examples_MIX/SPI/SPI_HalfDuplex_ComPollingIT/Src/main.c

Additionally, I would monitor the return value from HAL_SPI_* calls and output debug information if it's not HAL_OK so that you can fix your program.

Don't call HAL_SPI_Transmit_IT/HAL_SPI_Receive_IT repeatedly. Wait for it to complete the transaction before calling it again.

Note that if(SPI_FLAG_EOT) always returns true since SPI_FLAG_EOT is nonzero. If you're trying to test for completion, check the state of the peripheral within the hspi6 handle instead.

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