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

すべてをまとめる

#![no_main]
#![no_std]

use aux9::{entry, switch_hal::OutputSwitch, tim6};

#[inline(never)]
fn delay(tim6: &tim6::RegisterBlock, ms: u16) {
    // タイマーが `ms` ティック後に発火するように設定する
    // 1 ティック = 1 ms
    tim6.arr.write(|w| w.arr().bits(ms));

    // CEN: カウンタを有効にする
    tim6.cr1.modify(|_, w| w.cen().set_bit());

    // アラームが発生するまで待機する(更新イベントが発生するまで)
    while !tim6.sr.read().uif().bit_is_set() {}

    // 更新イベントフラグをクリアする
    tim6.sr.modify(|_, w| w.uif().clear_bit());
}

#[entry]
fn main() -> ! {
    let (leds, rcc, tim6) = aux9::init();
    let mut leds = leds.into_array();

    // TIM6 タイマーの電源をオンにする
    rcc.apb1enr.modify(|_, w| w.tim6en().set_bit());

    // OPM ワンパルスモードを選択する
    // CEN 今のところカウンタは無効のままにする
    tim6.cr1.write(|w| w.opm().set_bit().cen().clear_bit());

    // カウンタが 1 KHz で動作するようにプリスケーラを設定する
    // APB1_CLOCK = 8 MHz
    // PSC = 7999
    // 8 MHz / (7999 + 1) = 1 KHz
    // カウンタ(CNT)は 1 ミリ秒ごとに増加する
    tim6.psc.write(|w| w.psc().bits(7_999));

    let ms = 50;
    loop {
        for curr in 0..8 {
            let next = (curr + 1) % 8;

            leds[next].on().unwrap();
            delay(tim6, ms);
            leds[curr].off().unwrap();
            delay(tim6, ms);
        }
    }
}