[][src]Crate irq

Utilities for interrupt handling.

This crate provides:

  • An API to register scoped interrupts, inspired by crossbeam::scope.

    After registering interrupts using the scoped_interrupts! macro, the scope function can be used to enter an "interrupt scope", in which interrupt handlers can be registered for the duration of the scope. This allows them to make use of data defined in the calling function.

  • A PriorityLock that allows sharing mutable data between interrupts at different priorities.

Examples

Here is an example of how to use the Scoped Interrupts API with interrupts provided by an svd2rust-generated Peripheral Access Crate:

use irq::{scoped_interrupts, handler, scope};

// This macro is invoked by `scoped_interrupts!` to declare an interrupt handler. It needs to
// expand to code that makes `$f` act as an interrupt handler for interrupt `$name`.
macro_rules! hook {
    (
        interrupt = $name:ident;
        function = $f:item;
    ) => {
        #[interrupt]
        $f
    };
}

// Hook `INT0` and `INT1` using the `hook!` macro above.
scoped_interrupts! {
    enum Interrupt {
        INT0,
        INT1,
    }

    use mock_pac::interrupt;

    with hook!(...)
}

fn main() {
    // Define data to be used (via move or borrow) by the interrupt handlers.
    let mut i = 0;
    let shared = [0, 1, 2];

    // Define handlers using the `handler!` macro.
    handler!(int0 = || i += shared[1]);
    handler!(int1 = || println!("{}", shared[2]));

    // Create a scope and register the handlers.
    scope(|scope| {
        scope.register(Interrupt::INT0, int0);
        scope.register(Interrupt::INT1, int1);
    });
}

Macros

handler

Defines a closure-based interrupt handler that can use stack-local data.

scoped_interrupts

Hooks interrupts and makes them available to the scope API.

Structs

Deadlock

Error indicating that a lock could not be acquired in a high-priority context.

Handler

Wraps a closure used as an interrupt handler.

LockGuard

A guard keeping a lock acquired until it is dropped.

LockHalf

One half of a PriorityLock.

PriorityLock

A lock that allows sharing data between two interrupts at different priorities.

Scope

An interrupt scope created by the scope function.

Enums

PHigh

Type marker indicating the high-priority half of a PriorityLock.

PLow

Type marker indicating the low-priority half of a PriorityLock.

Traits

Interrupt

Trait for interrupt enums generated by scoped_interrupts!.

LockPriority

Trait implemented by the lock priority types PHigh and PLow.

Functions

scope

Creates a scope in which interrupt handlers using stack-local data can be registered.