aboutsummaryrefslogtreecommitdiffstats
path: root/sw/demo1/src/main.rs
blob: 80e38b98ee2a3f6be5fd280d4e28d861fe626e96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#![no_main]
#![no_std]

use cortex_m_rt::ExceptionFrame;
use cortex_m_semihosting::hio;
use panic_semihosting as _;

use stm32f1xx_hal::{
    prelude::*,
    pac,
    timer::Timer,
};

use embedded_hal::digital::v2::{OutputPin, ToggleableOutputPin};

use core::fmt::Write;
use nb::block;

fn print(step: usize) -> Result<(), core::fmt::Error> {
    let mut stdout = match hio::hstdout() {
        Ok(fd) => fd,
        Err(()) => return Err(core::fmt::Error),
    };

    let language = "Rust";
    let ranking = 1;

    write!(stdout, "{}: {} on embedded is #{}!\n", step, language, ranking)?;

    Ok(())
}

#[cortex_m_rt::entry]
fn main() -> ! {
    let cp = cortex_m::Peripherals::take().unwrap();
    let dp = pac::Peripherals::take().unwrap();

    let mut flash = dp.FLASH.constrain();
    let mut rcc = dp.RCC.constrain();
    let clocks = rcc.cfgr.freeze(&mut flash.acr);

    let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);

    // Configure PB14 as output. (LED)
    let mut led = gpiob.pb14.into_push_pull_output(&mut gpiob.crh);
    led.set_low().unwrap();

    let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.hz());

    let mut step = 0;
    print(step).unwrap();

    loop {
        for _ in 0..5 {
            led.toggle().unwrap();
            block!(timer.wait()).ok();
        }

        print(step).unwrap();
        step += 1;
    }
}

#[cortex_m_rt::exception]
fn HardFault(ef: &ExceptionFrame) -> ! {
    panic!("HardFault at {:#?}", ef);
}

#[cortex_m_rt::exception]
fn DefaultHandler(irqn: i16) {
    panic!("Unhandled exception (IRQn = {})", irqn);
}