Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ハードウェアタスク

RTIC はその中核で、タスクのスケジュールと実行開始にハードウェア割り込みコントローラー(cortex-m 上の ARM NVIC)を使用します。pre-init(隠れた「タスク」)、#[init]#[idle] を除くすべてのタスクは、割り込みハンドラとして実行されます。

タスクを割り込みにバインドするには、#[task] 属性引数 binds = InterruptName を使用します。すると、このタスクはこのハードウェア割り込みベクターの割り込みハンドラになります。

明示的な割り込みにバインドされたタスクはすべて、ハードウェアイベントに反応して実行を開始するため、ハードウェアタスク と呼ばれます。

存在しない割り込み名を指定すると、コンパイルエラーになります。割り込み名は通常、PAC または HAL クレートで定義されています。

利用可能な割り込みベクターであれば、どれでも動作するはずです。特定のデバイスでは、ユーザーコードからは制御できない形で、特定の割り込み優先度が特定の割り込みベクターに結び付けられている場合があります。例として nRF “softdevice” を参照してください。

ハードウェア機能によって内部的に使用されている割り込みベクターを使用する際は注意してください。RTIC は、そのようなハードウェア固有の詳細を認識しません。

以下の例は、割り込みハンドラにバインドされたハードウェアタスクを宣言するための #[task(binds = InterruptName)] 属性の使い方を示しています。

//! examples/hardware.rs

#![no_main]
#![no_std]
#![deny(warnings)]
#![deny(unsafe_code)]
#![deny(missing_docs)]

use panic_semihosting as _;

#[rtic::app(device = lm3s6965)]
mod app {
    use cortex_m_semihosting::{debug, hprintln};
    use lm3s6965::Interrupt;

    #[shared]
    struct Shared {}

    #[local]
    struct Local {}

    #[init]
    fn init(_: init::Context) -> (Shared, Local) {
        // Pends the UART0 interrupt but its handler won't run until *after*
        // `init` returns because interrupts are disabled
        rtic::pend(Interrupt::UART0); // equivalent to NVIC::pend

        hprintln!("init");

        (Shared {}, Local {})
    }

    #[idle]
    fn idle(_: idle::Context) -> ! {
        // interrupts are enabled again; the `UART0` handler runs at this point

        hprintln!("idle");

        // Some backends provide a manual way of pending an
        // interrupt.
        rtic::pend(Interrupt::UART0);

        loop {
            cortex_m::asm::nop();
            debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
        }
    }

    #[task(binds = UART0, local = [times: u32 = 0])]
    fn uart0(cx: uart0::Context) {
        // Safe access to local `static mut` variable
        *cx.local.times += 1;

        hprintln!(
            "UART0 called {} time{}",
            *cx.local.times,
            if *cx.local.times > 1 { "s" } else { "" }
        );
    }
}
$ cargo xtask qemu --verbose --example hardware
init
UART0 called 1 time
idle
UART0 called 2 times