Electrical – method to handle concurrent GPIO interrupt sources

freertosgpiogpio-external-interruptinterruptsmicrocontroller

I have two GPIO interrupt sources running in two separate threads:

  1. GPIO pulses following line frequency(60 Hz)
  2. button press

How can I use both these interrupts concurrently, considering my controller(ESP32) multiplexes all GPI0 peripheral interrupts into one CPU interrupt?

Currently, the program allows only one of them to work at a time.

Thanks

Best Answer

I'm writing this without ever using an ESP32 (or any other dual core) or FreeRTOS. From my understanding of FreeRTOS (searching very quickly), you implement interrupt handlers on your own, but have to call some OS-functions from within.

So regardless of the GPIO the interrupt happens on, you end up in the same interrupt function, because there is only a single interrupt vector for GPIO.

But the ESP32 has some registers to tell you which interrupts are pending. And you have registers which tell you which interrupt is enabled. The combination of those registers allows you to decide which pins are causing an interrupt.

So based on this you can do different things depending on which GPIO triggered the interrupt.

I'm going to give you the idea, not an implementation, so following is pseudocode:

if ((GPIO_InterruptPendingRegister & GPIO_InterruptEnabledRegister) == GPIO_0_Interrupt)
{
    executeGPIO_0_Function();
    // or
    volatile GPIO_0_Flag = true;
    // depends on how the peripheral handles this, usually needed
    Reset_GPIO_0_PendingFlag();
}
if ((GPIO_InterruptPendingRegister & GPIO_InterruptEnabledRegister) == GPIO_1_Interrupt)
{
    executeGPIO_1_Function();
    // or
    volatile GPIO_1_Flag = true;
    // depends on how the peripheral handles this, usually needed
    Reset_GPIO_1_PendingFlag();
}
etc.

This way you can split the interrupt into multiple execution functions. I prefer the way using functions, but I'm programming in C++ and try to avoid global variables.