cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with SPI in External ISR

Mohammad Hossein
Associate II
Posted on December 31, 2016 at 07:35

Hello

I'm working with stm32f4 discovery board.I can make spi dma communication with a slave device in while(1) loop without any problem.

But I have problem to make spi communication with slave device in external interrupt service routine (I want to read data from slave device whenever a pin is set by slave and I detect this using external interrupt). The code is executed until it reaches this while loop:

while (HAL_SPI_GetState(&hspi2) == HAL_SPI_STATE_BUSY_TX);

 at this moment it stops and hangs. If I set a flag in ISR and read via SPI in while(1) loop when that flag is set by ISR, every thing is OK.

But how can I transfer data via spi in external ISR without mentioned problem?

Thanks

#external-interrupt #spi
1 REPLY 1
Seb
ST Employee
Posted on December 31, 2016 at 08:04

Each application has its specific ideal implementation.

Usually it depends on:

- How late the communication can be after the trigger event?

- How long the communication takes?

In one case, the source code would have a main while loop and regularly poll for an EXTI edge detection (PR bit) to run the SPI transaction. This solution would not require interrupts.

If the communication should occur as soon as possible (asap) after the trigger event, the EXTI interrupt handler may run the SPI transaction together. This forbid using SPI outside this interrupt routine as it would create conflict and side effects.

What if the SPI communication time is long and the interrupt must remain very short?

One possibility is to create an interrupt base state machine which looks like a peripheral made smarter and orchestrate between interrupt sources to get the job done 'under the hood'. This requires initial development investment and pays back by having job done in the background without the need of an RTOS.