アプリケーションの初期化と #[init] タスク
RTIC アプリケーションでは、システムをセットアップする init タスクが必要です。対応する init 関数は
シグネチャ fn(init::Context) -> (Shared, Local) を持つ必要があります。ここで、Shared と Local はユーザー定義のリソース構造体です。
init タスクは、システムリセット後、[任意で定義された pre-init コードセクション]1 と、常に行われる RTIC の内部初期化の後に実行されます。
init タスクとオプションの pre-init タスクは、割り込みを無効化した状態で 実行され、Cortex-M への排他的アクセス権を持ちます(critical_section::CriticalSection トークンは cs として利用できます)。
デバイス固有のペリフェラルは、init::Context の core フィールドと device フィールドを通じて利用できます。
例
以下の例は、core、device、cs フィールドの型を示すとともに、'static ライフタイムを持つ local 変数の使用例を示しています。このような変数は、RTIC アプリケーションの init タスクから他のタスクへ委譲できます。
device フィールドは、peripherals 引数がデフォルト値 true に設定されている場合にのみ利用できます。
ごくまれに超軽量なアプリケーションを実装したい場合は、peripherals を明示的に false に設定できます。
//! examples/init.rs
#![no_main]
#![no_std]
#![deny(warnings)]
#![deny(unsafe_code)]
#![deny(missing_docs)]
use panic_semihosting as _;
#[rtic::app(device = lm3s6965, peripherals = true)]
mod app {
use cortex_m_semihosting::{debug, hprintln};
#[shared]
struct Shared {}
#[local]
struct Local {}
#[init(local = [x: u32 = 0])]
fn init(cx: init::Context) -> (Shared, Local) {
// Cortex-M peripherals
let _core: cortex_m::Peripherals = cx.core;
// Device specific peripherals
let _device: lm3s6965::Peripherals = cx.device;
// Locals in `init` have 'static lifetime
let _x: &'static mut u32 = cx.local.x;
// Access to the critical section token,
// to indicate that this is a critical section
let _cs_token: rtic::export::CriticalSection = cx.cs;
hprintln!("init");
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
(Shared {}, Local {})
}
}
この例を実行すると、コンソールに init が出力され、その後 QEMU プロセスが終了します。
$ cargo xtask qemu --verbose --example init
init