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
interrupts.hpp
Go to the documentation of this file.
1/**
2 * @file interrupts.hpp
3 * @brief Interrupt execution levels and the control critical section
4 * @ingroup system
5 *
6 * One execution-level scheme for the whole library. Every platform's vector
7 * table maps onto these names (see `<mcu>/driver/priorities.hpp` in
8 * sbl-hardware); application code never writes a numeric priority. How a
9 * level becomes a hardware priority, and how masking is done, is each MCU's
10 * interrupts driver (contract: <sbl/hw/validation/interrupts_requirements.hpp>).
11 *
12 * Levels are ordered most urgent first: a lower number preempts a higher one.
13 */
14
15#ifndef SBL_SYSTEM_INTERRUPTS_HPP_
16#define SBL_SYSTEM_INTERRUPTS_HPP_
17
18#include <cstdint>
19
20namespace sbl {
21namespace system {
22namespace interrupts {
23
24/**
25 * @brief Execution levels, most urgent first.
26 *
27 * The invariants the scheme guarantees:
28 * - Level 0 stays empty, so nothing can ever tie with audio and nothing
29 * escapes the ceiling.
30 * - Audio has its own level. Any handler that shares it would delay audio
31 * by its full length (equal priority cannot preempt).
32 * - ControlTick is the control ceiling: a ScopedControlCritical masks this
33 * level and everything below it, and nothing above it may touch state
34 * that main or the tick protect. Audio's blocking term is therefore zero.
35 * - SysTick, PendSV and SVCall are assigned explicitly; unassigned system
36 * handlers reset to level 0, above audio.
37 */
38enum class Level : uint8_t {
39 Reserved = 0, /**< Empty by design (never masked by the ceiling) */
40 ShortDeadline = 1, /**< Reserved for microsecond handlers that touch only SPSC queues */
41 Audio = 2, /**< Audio DMA half/complete - the audio streams only */
42 UsbMidi = 4, /**< USB interrupt, MIDI UART RX (both FIFO-elastic) */
43 Dma = 5, /**< Every other DMA stream (ADC, SD card) */
44 Bus = 6, /**< I2C / SPI event handlers, if interrupt-driven */
45 ControlTick = 8, /**< Control-rate timer tick - the control ceiling */
46 SoftPwm = 9, /**< Software PWM / BCM timer (tolerates tick preemption) */
47 Exti = 10, /**< External pin interrupts (encoders, buttons) */
48 SoftPend = 11, /**< Software-pended work: tud_task(), USB-MIDI parse */
49 Debug = 12, /**< Debug UART, diagnostics drain */
50 SysTick = 14, /**< Unused; assigned so it never sits at level 0 */
51 PendSv = 15, /**< PendSV and SVCall (unused) */
52};
53
54/** The level a ScopedControlCritical raises the mask to. */
56
57/** True if a handler at `level` is masked while the mask sits at the ceiling. */
58constexpr bool masked_by_ceiling(Level level) {
59 return static_cast<uint8_t>(level) >= static_cast<uint8_t>(CONTROL_CEILING);
60}
61
62/**
63 * @brief RAII critical section against the control tick and everything below.
64 *
65 * Raises the interrupt mask to CONTROL_CEILING for its scope and restores the
66 * previous mask on exit. Safe to nest: the driver's raise never lowers the
67 * mask. Handlers above the ceiling - audio, USB/MIDI, other DMA - are
68 * unaffected by construction.
69 *
70 * Use it around any multi-word state shared between main and the control
71 * tick. Do not use it from a handler above the ceiling (that handler must
72 * not touch such state at all), and do not reach for disable_all() instead:
73 * that is for death paths only.
74 *
75 * @tparam Interrupts The MCU's interrupts driver (e.g. sbl::driver::Interrupts)
76 */
77template<typename Interrupts>
79public:
80 ScopedControlCritical() : saved_(Interrupts::save_ceiling()) { Interrupts::raise_ceiling(CONTROL_CEILING); }
81 ~ScopedControlCritical() { Interrupts::restore_ceiling(saved_); }
83 ScopedControlCritical& operator=(const ScopedControlCritical&) = delete;
84
85private:
86 uint32_t saved_;
87};
88
89} // namespace interrupts
90} // namespace system
91} // namespace sbl
92
93#endif // SBL_SYSTEM_INTERRUPTS_HPP_
RAII critical section against the control tick and everything below.
constexpr Level CONTROL_CEILING
constexpr bool masked_by_ceiling(Level level)
Level
Execution levels, most urgent first.
Root namespace for all Sound Byte Libs code.