-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtimer-syst.rs
62 lines (50 loc) · 1.7 KB
/
timer-syst.rs
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
//! Start and stop a periodic system timer.
//!
//! This example should run on all stm32f4xx boards but it was tested with
//! stm32f4-discovery board (model STM32F407G-DISC1).
//!
//! ```bash
//! cargo run --release --features stm32f407,rt --example timer-syst
//! ```
#![no_std]
#![no_main]
use panic_halt as _;
use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use hal::timer::Error;
use stm32f7xx_hal as hal;
use crate::hal::{pac, prelude::*};
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::peripheral::Peripherals::take().unwrap();
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.sysclk(24.MHz()).freeze();
// Create a timer based on SysTick
let mut timer = cp.SYST.counter_us(&clocks);
timer.start(42.millis()).unwrap();
hprintln!("hello!");
// wait until timer expires
nb::block!(timer.wait()).unwrap();
hprintln!("timer expired 1");
// the function syst() creates a periodic timer, so it is automatically
// restarted
nb::block!(timer.wait()).unwrap();
hprintln!("timer expired 2");
// cancel current timer
timer.cancel().unwrap();
// start it again
timer.start(42.millis()).unwrap();
nb::block!(timer.wait()).unwrap();
hprintln!("timer expired 3");
timer.cancel().unwrap();
let cancel_outcome = timer.cancel();
assert_eq!(cancel_outcome, Err(Error::Disabled));
hprintln!("ehy, you cannot cancel a timer two times!");
// this time the timer was not restarted, therefore this function should
// wait forever
nb::block!(timer.wait()).unwrap();
// you should never see this print
hprintln!("if you see this there is something wrong");
panic!();
}