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
20
namespace
sbl
{
21
namespace
system {
22
namespace
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
*/
38
enum 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. */
55
inline
constexpr
Level
CONTROL_CEILING
=
Level::ControlTick
;
56
57
/** True if a handler at `level` is masked while the mask sits at the ceiling. */
58
constexpr
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
*/
77
template
<
typename
Interrupts>
78
class
ScopedControlCritical
{
79
public
:
80
ScopedControlCritical
() : saved_(Interrupts::save_ceiling()) { Interrupts::raise_ceiling(
CONTROL_CEILING
); }
81
~ScopedControlCritical
() { Interrupts::restore_ceiling(saved_); }
82
ScopedControlCritical
(
const
ScopedControlCritical
&) =
delete
;
83
ScopedControlCritical
& operator=(
const
ScopedControlCritical
&) =
delete
;
84
85
private
:
86
uint32_t saved_;
87
};
88
89
}
// namespace interrupts
90
}
// namespace system
91
}
// namespace sbl
92
93
#endif
// SBL_SYSTEM_INTERRUPTS_HPP_
sbl::system::interrupts::ScopedControlCritical
RAII critical section against the control tick and everything below.
Definition
interrupts.hpp:78
sbl::system::interrupts::CONTROL_CEILING
constexpr Level CONTROL_CEILING
Definition
interrupts.hpp:55
sbl::system::interrupts::masked_by_ceiling
constexpr bool masked_by_ceiling(Level level)
Definition
interrupts.hpp:58
sbl::system::interrupts::Level
Level
Execution levels, most urgent first.
Definition
interrupts.hpp:38
sbl::system::interrupts::Level::Dma
@ Dma
sbl::system::interrupts::Level::SysTick
@ SysTick
sbl::system::interrupts::Level::ShortDeadline
@ ShortDeadline
sbl::system::interrupts::Level::PendSv
@ PendSv
sbl::system::interrupts::Level::Bus
@ Bus
sbl::system::interrupts::Level::ControlTick
@ ControlTick
sbl::system::interrupts::Level::Reserved
@ Reserved
sbl::system::interrupts::Level::Debug
@ Debug
sbl::system::interrupts::Level::Audio
@ Audio
sbl::system::interrupts::Level::Exti
@ Exti
sbl::system::interrupts::Level::SoftPwm
@ SoftPwm
sbl::system::interrupts::Level::SoftPend
@ SoftPend
sbl::system::interrupts::Level::UsbMidi
@ UsbMidi
sbl
Root namespace for all Sound Byte Libs code.
Definition
audio_capture.hpp:16
src
sbl
system
interrupts.hpp
Generated by
1.9.8