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
assert.hpp
Go to the documentation of this file.
1#ifndef SBL_SYSTEM_ASSERT_HPP_
2#define SBL_SYSTEM_ASSERT_HPP_
3
4/**
5 * @file assert.hpp
6 * @brief Assert and panic for embedded firmware
7 *
8 * Provides SBL_ASSERT(cond) and SBL_PANIC(msg) macros that output
9 * diagnostic info and halt the processor.
10 *
11 * The output sink is SBL_FAULT_OUTPUT, a type with `static void
12 * write(const char*)`. The default writes nothing: the fault still halts,
13 * silently, and needs no hardware. An app that wants the message defines
14 * SBL_FAULT_OUTPUT before including this header; the polling UART sink
15 * is sbl::uart::FaultOutput in <sbl/hw/hal/uart/log_output.hpp>. Fault
16 * handlers run with interrupts off, so the sink must write by polling.
17 * For HardFault register dumps, also include <sbl/system/fault.hpp>.
18 *
19 * Usage:
20 * @code
21 * #include <sbl/system/assert.hpp>
22 *
23 * void process(float* buf, uint16_t frames) {
24 * SBL_ASSERT(buf != nullptr);
25 * SBL_ASSERT(frames <= 48);
26 * // ...
27 * }
28 *
29 * if (something_unrecoverable) {
30 * SBL_PANIC("DMA config failed");
31 * }
32 * @endcode
33 *
34 * Configuration:
35 * @code
36 * SBL_ASSERT_ENABLED - 1 (default) to enable, 0 to compile out
37 * SBL_FAULT_OUTPUT - output sink class (default: a sink that writes nothing)
38 * @endcode
39 */
40
41#include <cstdint>
43
44// Output sink resolution
45
46// The default sink writes nothing: the fault halts silently and needs no
47// hardware. Define SBL_FAULT_OUTPUT before this header for a message.
48#ifndef SBL_FAULT_OUTPUT
49namespace sbl::system::fault::detail {
50struct NullFaultOutput {
51 static void write(const char*) {}
52};
53} // namespace sbl::system::fault::detail
54#define SBL_FAULT_OUTPUT sbl::system::fault::detail::NullFaultOutput
55#endif
56
57namespace sbl::system::fault {
58
59/**
60 * @brief Halt with a diagnostic message. Does not return.
61 *
62 * Disables interrupts, formats the message with file/line info,
63 * outputs it, then enters an infinite loop with a debug breakpoint.
64 *
65 * @context Any context: main loop, ISR, fault handler.
66 */
67[[noreturn]] inline void panic(const char* msg, const char* file, int line) {
68 __asm volatile("cpsid i"); // __disable_irq()
69
70 char buf[128];
71 sbl::log::format(buf, sizeof(buf), "\r\n!!! %s\r\n at %s:%d\r\n", msg, file, line);
72 SBL_FAULT_OUTPUT::write(buf);
73
74 while (true) {
75 __asm volatile("bkpt #0");
76 }
77}
78
79} // namespace sbl::system::fault
80
81// Macros
82
83#define SBL_PANIC(msg) sbl::system::fault::panic("PANIC: " msg, __FILE__, __LINE__)
84
85#ifndef SBL_ASSERT_ENABLED
86#define SBL_ASSERT_ENABLED 1
87#endif
88
89#if SBL_ASSERT_ENABLED
90#define SBL_ASSERT(cond) \
91 do { \
92 if (!(cond)) { \
93 sbl::system::fault::panic("ASSERT FAILED: " #cond, __FILE__, __LINE__); \
94 } \
95 } while (0)
96#else
97#define SBL_ASSERT(cond) ((void)0)
98#endif
99
100#endif // SBL_SYSTEM_ASSERT_HPP_
Minimal safe string formatting for embedded systems.
int format(char *buf, size_t size, const char *fmt,...)
Format a string into a buffer (snprintf-style)
Definition format.hpp:265
Assertions, panic and the HardFault handler.
Definition assert.hpp:49
void panic(const char *msg, const char *file, int line)
Halt with a diagnostic message. Does not return.
Definition assert.hpp:67