Forked from
Per Lindgren / e7020e_2020
5 commits ahead of the upstream repository.
-
Gustav Hansson authoredGustav Hansson authored
bare2.rs 2.62 KiB
//! bare2.rs
//!
//! Measuring execution time
//!
//! What it covers
//! - Generating documentation
//! - Using core peripherals
//! - Measuring time using the DWT
//! - ITM tracing using `iprintln`
//! - Panic halt
//!
#![no_main]
#![no_std]
use panic_halt as _;
use cortex_m::{iprintln, peripheral::DWT, Peripherals};
use cortex_m_rt::entry;
// burns CPU cycles by just 5looping `i` times
#[inline(never)]
fn wait(i: u32) {
for _ in 0..i {
// no operation (ensured not optimized out)
cortex_m::asm::nop();
}
}
#[entry]
fn main() -> ! {
let mut p = Peripherals::take().unwrap();
let stim = &mut p.ITM.stim[0];
let mut dwt = p.DWT;
iprintln!(stim, "bare2");
dwt.enable_cycle_counter();
// Reading the cycle counter can be done without `owning` access
// the DWT (since it has no side effect).
//
// Look in the docs:
// pub fn enable_cycle_counter(&mut self)
// pub fn get_cycle_count() -> u32
//
// Notice the difference in the function signature!
let start = DWT::get_cycle_count();
wait(1_000_000);
let end = DWT::get_cycle_count();
// notice all printing outside of the section to measure!
iprintln!(stim, "Start {:?}", start);
iprintln!(stim, "End {:?}", end);
iprintln!(stim, "Diff {:?}", end - start);
loop {}
}
// 0. Setup
// > cargo doc --open
//
// This will document your crate, and open the docs in your browser.
// If it does not auto-open, then copy paste the path in your browser.
// (Notice, it will try to document all dependencies, you may have only one
// one panic handler, so comment out all but one in `Cargo.toml`.)
//
// In the docs, search (`S`) for DWT, and click `cortex_m::peripheral::DWT`.
// Read the API docs.
//
// 1. Build and run the application (debug build).
// Setup ITM tracing (see `bare1.rs`) and `openocd` (if not using vscode).
//
// > cargo run --example bare2
// (or use the vscode build task)
//
// What is the output in the ITM console?
//
// bare2
// Start 2012359631
// End 2139359709
// Diff 127000078
//
// Rebuild and run in release mode
//
// > cargo build --example bare2 --release
//
// bare2
// Start 3019839635
// End 3026839650
// Diff 7000015
//
// Compute the ratio between debug/release optimized code
// (the speedup).
//
// 18,1428 times faster
//
// commit your answers (bare2_1)
//
// 3. *Optional
// Inspect the generated binaries, and try stepping through the code
// for both debug and release binaries. How do they differ?
//
// ** your answer here **
//
// commit your answers (bare2_2)