Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1/**
2 * @file timer.hpp
3 * @brief The system timer: the count, 64-bit time, busy waits and millis()
4 * @ingroup system
5 *
6 * The system layer's timebase over the target's tick port
7 * (<sbl/system/port/tick.hpp>, contract tick_requirements.hpp), modeled on
8 * CMSIS-RTOS2's kernel system timer: count() is the rolling 32-bit count
9 * (osKernelGetSysTimerCount), rate() its frequency (osKernelGetSysTimerFreq),
10 * a constexpr here. count64() extends it with Epoch; millis() is that in
11 * milliseconds, truncated to 32 bits so it wraps at 2^32 ms, which is what
12 * TinyUSB's millisecond hook expects. The busy waits spin on count deltas with no interrupt
13 * involved, so they work at any execution level.
14 *
15 * The driver's init() calls init() once the port counts, on every boot path;
16 * apps call neither.
17 */
18
19#ifndef SBL_SYSTEM_TIMER_HPP_
20#define SBL_SYSTEM_TIMER_HPP_
21
22#include <cstdint>
23#include <sbl/system/epoch.hpp>
24#include <sbl/system/port/tick.hpp>
26
28
29using Port = sbl::driver::Tick;
30
31static_assert(port::tick_port_valid<Port>, "SBL ERROR: the target's tick port does not meet its contract");
32
33/** Counts per second. */
34inline constexpr uint32_t rate() { return Port::rate(); }
35
36/** The rolling 32-bit count. */
37inline uint32_t count() { return Port::count(); }
38
39/** The count since the port started, 64 bits. */
40inline uint64_t count64() { return Epoch<Port>::count64(); }
41
42/** Register the overflow handler and enable the interrupt. Called by the driver's init(). */
43inline void init() {
45 Port::setup(&Epoch<Port>::on_overflow);
46 Port::enable();
47}
48
49/** Spin for `us` microseconds. Any execution level. */
50inline void busy_wait_us(uint32_t us) {
51 const uint32_t start = count();
52 const uint32_t counts = static_cast<uint32_t>(static_cast<uint64_t>(us) * rate() / 1'000'000u);
53 while (count() - start < counts) { // unsigned: a wrap is fine
54 }
55}
56
57/** Spin for `ms` milliseconds. Any execution level. */
58inline void busy_wait_ms(uint32_t ms) {
59 while (ms-- > 0) {
60 busy_wait_us(1000u);
61 }
62}
63
64/** Milliseconds since the port started, modulo 2^32. */
65inline uint32_t millis() { return static_cast<uint32_t>(detail::to_millis(count64(), rate())); }
66
67/** millis() as a logger's timestamp provider (the contract is in <sbl/common/log/sink.hpp>). */
69 static uint32_t now() { return millis(); }
70};
71
72} // namespace sbl::system::timer
73
74#endif // SBL_SYSTEM_TIMER_HPP_
static uint64_t count64()
Definition epoch.hpp:38
static void reset()
Definition epoch.hpp:56
A 32-bit counter extended to 64 bits by counting its overflows.
The system timer over the tick port: the count, the 64-bit count, busy waits and milliseconds.
Definition timer.hpp:27
void busy_wait_ms(uint32_t ms)
Definition timer.hpp:58
constexpr uint32_t rate()
Definition timer.hpp:34
uint32_t millis()
Definition timer.hpp:65
uint32_t count()
Definition timer.hpp:37
uint64_t count64()
Definition timer.hpp:40
void busy_wait_us(uint32_t us)
Definition timer.hpp:50
The tick port: the hardware timer under the system timer.