cancel
Showing results for 
Search instead for 
Did you mean: 

Relationship between GPIO Interrupt and SPI Communication

pass3master
Senior

We are trying to do SPI communication in the GPIO interrupt handler.
We are not sure about the following two points. Can someone please give me an answer?

1, If another GPIO interrupt occurs while the GPIO interrupt handler is running (with the same priority), does it wait for the GPIO interrupt handler running first to complete before running the later GPIO interrupt handler?
Or is the later GPIO interrupt ignored?

2,We would like to disable GPIO interrupts during SPI communication in order to perform SPI communication in the GPIO interrupt handler.
How should we write the program?

1 ACCEPTED SOLUTION

Accepted Solutions

@pass3master wrote:

Is there a way to prevent GPIO interrupts from occurring within an interrupt handler?


The interrupt does not occur within the interrupt handler. If it's the same IRQ handler it waits for the interrupt routine to finish and get back to it again. If another EXTI with higher preemption priority it will stop the execution of the lowest priority interrupt and execute the highest one.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

10 REPLIES 10

Generally, doing any operation which takes a significant amount of time in an interrupt handler is a Bad Idea.

Why do you feel the need to do this?

Oh, I see.

We are doing SPI communication (100μs) in the GPIO interrupt handler, but there is a possibility that the next GPIO interrupt comes in during SPI communication.

We are doing SPI communication in the GPIO interrupt handler.”
↑Is this idea wrong?

SofLit
ST Employee

The EXTI pending bit is cleared as soon as the interrupt handler is executed:

void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
{
  /* EXTI line interrupt detected */
  if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
  {
    __HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
    HAL_GPIO_EXTI_Callback(GPIO_Pin);
  }
}

Then the callback is called. So if in meantime (execution of the callback) an EXTI interrupt is fired, as soon as you exit the handler you will enter it again.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

@pass3master wrote:

Is this idea wrong?


As I said, it's generally not a good approach.

Not enough detail to say whether you might just get away with it in your particular case.

Again, why do you feel the need to do the SPI comms within the interrupt handler?

Thank you for your answer.
I see. So when a GPIO interrupt occurs within an interrupt handler, it is put on hold and the handler is executed again immediately after it finishes.
Thank you. That's very helpful.
Is there a way to prevent GPIO interrupts from occurring within an interrupt handler?

Five switches are connected to a CPU (microcontroller), and FPGA.
Our customer requests that SPI communication be performed between the CPU (microcontroller) and FPGA when a switch is pressed.
Each of the five switches performs different SPI communication, and the moment a switch is pressed, there is a possibility that the other switches will also be pressed.
For this reason, we are planning to detect switch changes via GPIO interrupts and perform SPI communication.


@pass3master wrote:

Is there a way to prevent GPIO interrupts from occurring within an interrupt handler?


The interrupt does not occur within the interrupt handler. If it's the same IRQ handler it waits for the interrupt routine to finish and get back to it again. If another EXTI with higher preemption priority it will stop the execution of the lowest priority interrupt and execute the highest one.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

So a common approach would be to just set a flag (or flags) in the switch interrupt handler(s), and then your mainline code checks the flag(s) for when to send the SPI message(s).

 

You have debounced these switches - haven't you ... ?

I see, that's how it works.
Thanks to you I understand it now.
Thank you.