Sound Byte Libs 0.5.1-121-g3358a44
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
ticks.hpp
Go to the documentation of this file.
1/**
2 * @file ticks.hpp
3 * @brief Tick counter interface for profiling
4 *
5 * Includes the platform-specific timing backend (generated by sloth from
6 * the MCU manifest's timing capability) and provides the TickScope RAII
7 * helper. The generated backend supplies:
8 * - sbl::hw::timing::init() — call once at startup
9 * - sbl::hw::timing::read() — free-running counter (wraps at 2^32)
10 * - sbl::hw::timing::rate() — ticks per second
11 *
12 * Platform implementations (selected by MCU manifest):
13 * - dwt: ARM DWT CYCCNT (hardware cycles)
14 * - posix_monotonic: clock_gettime CLOCK_MONOTONIC (nanoseconds)
15 * - sdk_timer: Pico SDK hardware timer (microseconds)
16 */
17#ifndef SBL_COMMON_DIAGNOSTICS_TICKS_HPP_
18#define SBL_COMMON_DIAGNOSTICS_TICKS_HPP_
19
20#include <cstdint>
21#include <sbl/hw/config/timing.hpp>
22
23namespace sbl::diagnostics {
24
25/// Initialize the platform timing backend. Call once at startup.
26inline void ticks_init() { sbl::hw::timing::init(); }
27
28/// Read current tick count (free-running, wraps at 2^32).
29inline uint32_t ticks() { return sbl::hw::timing::read(); }
30
31/// Tick rate in Hz (ticks per second).
32inline constexpr uint32_t tick_rate() { return sbl::hw::timing::rate(); }
33
34/// RAII scope for measuring a block of code.
35/// Usage: volatile uint32_t elapsed; { TickScope s(elapsed); /* code */ }
36struct TickScope {
37 uint32_t start_;
38 volatile uint32_t& result_;
39
40 explicit TickScope(volatile uint32_t& out)
41 : start_(ticks()), result_(out) {}
43};
44
45} // namespace sbl::diagnostics
46
47#endif // SBL_COMMON_DIAGNOSTICS_TICKS_HPP_
Diagnostics and profiling.
uint32_t ticks()
Read current tick count (free-running, wraps at 2^32).
Definition ticks.hpp:29
constexpr uint32_t tick_rate()
Tick rate in Hz (ticks per second).
Definition ticks.hpp:32
void ticks_init()
Initialize the platform timing backend. Call once at startup.
Definition ticks.hpp:26
TickScope(volatile uint32_t &out)
Definition ticks.hpp:40
volatile uint32_t & result_
Definition ticks.hpp:38