kernel/hil/
hw_debug.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Interfaces for interacting with debug hardware integrated in various SoCs.
6//! Currently allows reading the cycle counter.
7
8pub trait CycleCounter {
9    /// Enable and start the cycle counter.
10    /// Depending on the underlying hardware, it may be necessary to call reset
11    /// before starting the cycle counter for the first time to get accurate results.
12    fn start(&self);
13
14    /// Stop the cycle counter.
15    /// Does nothing if the cycle counter is not present.
16    fn stop(&self);
17
18    /// Return the current value of the cycle counter.
19    fn count(&self) -> u64;
20
21    /// Reset the counter to zero and stop the cycle counter.
22    fn reset(&self);
23
24    /// Benchmark the number of cycles to run a passed closure.
25    /// This function is intended for use debugging in-kernel routines.
26    fn profile_closure<F: FnOnce()>(&self, f: F) -> u64 {
27        self.reset();
28        self.start();
29        f();
30        self.stop();
31        self.count()
32    }
33}