Skip to content

Commit 065b66a

Browse files
committed
dwt: Added blinky example
1 parent c346718 commit 065b66a

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ required-features = ["rt", "stm32f401", "usb_fs"]
101101
name = "delay-blinky"
102102
required-features = ["rt", "stm32f411"]
103103

104+
[[example]]
105+
name = "dwt-blinky"
106+
required-features = ["rt", "stm32f429"]
107+
104108
[[example]]
105109
name = "ssd1306-image"
106110
required-features = ["rt", "stm32f411"]

examples/dwt-blinky.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#![deny(unsafe_code)]
2+
#![no_main]
3+
#![no_std]
4+
5+
// Halt on panic
6+
use crate::hal::{
7+
dwt::{ClockDuration, DwtExt},
8+
prelude::*,
9+
stm32,
10+
};
11+
use cortex_m;
12+
use cortex_m_rt::entry;
13+
use panic_halt as _;
14+
use stm32f4xx_hal as hal;
15+
16+
#[entry]
17+
fn main() -> ! {
18+
if let (Some(dp), Some(cp)) = (
19+
stm32::Peripherals::take(),
20+
cortex_m::peripheral::Peripherals::take(),
21+
) {
22+
// Set up the LEDs. On the STM32F429I-DISC[O1] they are connected to pin PG13/14.
23+
let gpiog = dp.GPIOG.split();
24+
let mut led1 = gpiog.pg13.into_push_pull_output();
25+
let mut led2 = gpiog.pg14.into_push_pull_output();
26+
27+
// Set up the system clock. We want to run at 48MHz for this one.
28+
let rcc = dp.RCC.constrain();
29+
let clocks = rcc.cfgr.sysclk(48.mhz()).freeze();
30+
31+
// Create a delay abstraction based on DWT cycle counter
32+
let dwt = cp.DWT.constrain(cp.DCB, clocks);
33+
let mut delay = dwt.delay();
34+
35+
// Create a stopwatch for maximum 9 laps
36+
// Note: it starts immediately
37+
let mut lap_times = [0u32; 10];
38+
let mut sw = dwt.stopwatch(&mut lap_times);
39+
loop {
40+
// On for 1s, off for 1s.
41+
led1.set_high().unwrap();
42+
led2.set_low().unwrap();
43+
delay.delay_ms(1000_u32);
44+
sw.lap();
45+
led1.set_low().unwrap();
46+
led2.set_high().unwrap();
47+
delay.delay_ms(900_u32);
48+
// Also you can measure with almost clock precision
49+
let cd: ClockDuration = dwt.measure(|| delay.delay_ms(100_u32));
50+
let _t: u32 = cd.as_ticks(); // Should return 48MHz * 0.1s as u32
51+
let _t: f32 = cd.as_secs_f32(); // Should return ~0.1s as a f32
52+
let _t: f64 = cd.as_secs_f64(); // Should return ~0.1s as a f64
53+
let _t: u64 = cd.as_nanos(); // Should return 100000000ns as a u64
54+
sw.lap();
55+
56+
// Get all the lap times
57+
{
58+
let mut lap = 1;
59+
while let Some(lap_time) = sw.lap_time(lap) {
60+
let _t = lap_time.as_secs_f64();
61+
lap += 1;
62+
}
63+
}
64+
65+
// Reset stopwatch
66+
sw.reset();
67+
}
68+
}
69+
70+
loop {}
71+
}

0 commit comments

Comments
 (0)