[][src]Attribute Macro msp430_rt_macros::interrupt

#[interrupt]

Attribute to declare an interrupt handler

When the device feature is disabled this attribute can only be used to override the DefaultHandler.

When the device feature is enabled this attribute can be used to override other interrupt handlers but only when imported from a PAC (Peripheral Access Crate) crate which re-exports it. Importing this attribute from the msp430-rt crate and using it on a function will result in a compiler error.

Syntax

This example is not tested
extern crate device;

// the attribute comes from the device crate not from msp430-rt
use device::interrupt;

#[interrupt]
// Pass in optional CriticalSection
fn USART1(cs: CriticalSection) {
    // ..
}

where the name of the function must be DefaultHandler or one of the device interrupts.

Usage

#[interrupt] fn Name(.. overrides the default handler for the interrupt with the given Name. These handlers must have signature [unsafe] fn([<name>: CriticalSection]) [-> !]. It's possible to add state to these handlers by declaring static mut variables at the beginning of the body of the function. These variables will be safe to access from the function body.

If the interrupt handler has not been overridden it will be dispatched by the default interrupt handler (DefaultHandler).

#[interrupt] fn DefaultHandler(.. can be used to override the default interrupt handler. When not overridden DefaultHandler defaults to an infinite loop.

Properties

Interrupts handlers can only be called by the hardware. Other parts of the program can't refer to the interrupt handlers, much less invoke them as if they were functions.

static mut variables declared within an interrupt handler are safe to access and can be used to preserve state across invocations of the handler. The compiler can't prove this is safe so the attribute will help by making a transformation to the source code: for this reason a variable like static mut FOO: u32 will become let FOO: &mut u32;.

Examples

  • Using state within an interrupt handler
This example is not tested
extern crate device;

use device::interrupt;

#[interrupt]
fn TIM2() {
    static mut COUNT: i32 = 0;

    // `COUNT` is safe to access and has type `&mut i32`
    *COUNT += 1;

    println!("{}", COUNT);
}