How to use multiple flags for intra-thread signaling?

EventFlags can also be very effectively used to handle interrupts:

// Define event flags.
const uint32_t INTERRUPT_FLAG = (1UL << 0);  // position 0

// Define global variables
EventFlags  evenFlags;
InterruptIn button(p22);

// The following function is called on interrupt. 
// It is as short as possible (to not blocking) .
void  on_iterrupt()
{
    eventFlags.set(INTERRUPT_FLAG);
}

// The next task is an Interrupt Service Routine and
// it is running in a high priority task.
// It is blocking/waiting for the INTERRUPT_FLAG to become set.
void interrupt_service_routine()
{
    while (1) {
        eventFlags.wait_all(INTERRUPT_FLAG);
        // serving the interrupt
        ...
    }
}

Thread thread_isr(osPriorityHigh);

int main()
{
    // Start the interrupt service routine task 
    // in a high priority task.
    thread_isr.start(callback(interrupt_service_routine);

    // Attach a callback to the interrupt.
    button.fall(on_iterrupt);
    ...
    // The main thread
    while(1) {
        ...
    }
}

Have a look also at API Can lose messages - #2 by hudakz .

1 Like