Interrupt Handling#

IRQs And ISRs#

../../../../../../_images/sensor-irq.svg
  • Interrupt service routine (ISR): callback void(int irqnum)

  • Attached to an Interrupt request (IRQ) irqnum

  • Called asynchronously ⟶ runs at the most inopportune time

  • IRQ can safely be considered parallel (though varies per architecture)

  • Main loop: power saving, ideally

Serving Interrupts#

#include <why-irq.h>
#include <why-time.h>
#include <why-sensor.h>
#include <print>

int main()
{
    Why::init();

    Why::RandomSensor sensor(0, 100);
    auto isr = [&sensor](int irqnum){
        auto timestamp = Why::now_monotonic();
        auto value = sensor.get_value();
        std::println("irq #{}: timestamp={}, value={}", irqnum, timestamp, value);
    };

    Why::IRQ::connect(2, isr);                         // <-- activate ISR

    Why::pause();                                      // <-- main loop, power-saving
    return 0;
}
$ why-shell code/why-interrupt-sensor-irq
> irq 2
irq #2: timestamp=196188342592226, value=97
>