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_requirements.hpp
Go to the documentation of this file.
1/**
2 * @file timer_requirements.hpp
3 * @brief The periodic-callback timer contract
4 * @ingroup validation
5 *
6 * Every MCU provides `sbl::driver::TimerCallback`, one periodic interrupt
7 * with one callback (the control tick's base period):
8 * - `start(uint32_t period_us, void (*cb)())` - start; a second start while running is an error
9 * - `set_period(uint32_t period_us)` - change the period
10 * - `stop()` - stop
11 *
12 * The free-running counter is the tick port, whose contract is
13 * <sbl/system/port/tick_requirements.hpp>.
14 *
15 * Usage, at the bottom of a driver's timer_callback.hpp:
16 * @code
17 * #include <sbl/hw/validation/timer_requirements.hpp>
18 * static_assert(sbl::validation::timer_callback_valid<sbl::driver::TimerCallback>,
19 * "TimerCallback driver incomplete");
20 * @endcode
21 */
22
23#ifndef SBL_HW_VALIDATION_TIMER_REQUIREMENTS_HPP_
24#define SBL_HW_VALIDATION_TIMER_REQUIREMENTS_HPP_
25
26#include <cstdint>
27#include <type_traits>
28
29namespace sbl {
30namespace validation {
31
32template<typename T, typename = void>
33struct has_timer_callback_start : std::false_type {};
34template<typename T>
36 std::void_t<decltype(T::start(std::declval<uint32_t>(), std::declval<void (*)()>()))>>
37 : std::true_type {};
38
39template<typename T, typename = void>
40struct has_timer_callback_set_period : std::false_type {};
41template<typename T>
42struct has_timer_callback_set_period<T, std::void_t<decltype(T::set_period(std::declval<uint32_t>()))>>
43 : std::true_type {};
44
45template<typename T, typename = void>
46struct has_timer_callback_stop : std::false_type {};
47template<typename T>
48struct has_timer_callback_stop<T, std::void_t<decltype(T::stop())>> : std::true_type {};
49
50/** Validates a TimerCallback driver, with one message per missing method. */
51template<typename Driver>
53public:
54 static constexpr bool validate() {
56 "SBL ERROR: TimerCallback driver missing start().\n"
57 "Required: static void start(uint32_t period_us, void (*cb)())");
59 "SBL ERROR: TimerCallback driver missing set_period().\n"
60 "Required: static void set_period(uint32_t period_us)");
61 static_assert(has_timer_callback_stop<Driver>::value, "SBL ERROR: TimerCallback driver missing stop().\n"
62 "Required: static void stop()");
63 return true;
64 }
65};
66
67template<typename Driver>
68constexpr bool timer_callback_valid = TimerCallbackRequirements<Driver>::validate();
69
70} // namespace validation
71} // namespace sbl
72
73#endif // SBL_HW_VALIDATION_TIMER_REQUIREMENTS_HPP_
Root namespace for all Sound Byte Libs code.