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
fault.hpp
Go to the documentation of this file.
1#ifndef SBL_SYSTEM_FAULT_HPP_
2#define SBL_SYSTEM_FAULT_HPP_
3
4/**
5 * @file fault.hpp
6 * @brief HardFault handler with register dump
7 *
8 * Bridge between the MCU's HardFault vector (in sbl-hardware startup.cpp)
9 * and the library's diagnostic output. Including this header provides:
10 *
11 * 1. sbl::system::fault::on_hard_fault() - formats the Cortex-M exception frame
12 * 2. sbl_on_hard_fault() - the C function the HardFault stub calls, which
13 * forwards the frame to on_hard_fault(). It is defined inline, so it has
14 * weak (COMDAT) linkage.
15 *
16 * Include the header in one translation unit of the application:
17 * @code
18 * #include <sbl/system/fault.hpp>
19 * @endcode
20 *
21 * The output sink is shared with assert.hpp (SBL_FAULT_OUTPUT).
22 * See assert.hpp for sink configuration.
23 */
24
25#include <sbl/system/assert.hpp> // For SBL_FAULT_OUTPUT and sbl::log::format
26
27namespace sbl::system::fault {
28
29/**
30 * @brief Format and output the Cortex-M exception stack frame.
31 *
32 * The Cortex-M exception entry pushes these registers onto the
33 * active stack (MSP or PSP):
34 * @code
35 * [0] R0 [1] R1 [2] R2 [3] R3
36 * [4] R12 [5] LR [6] PC [7] xPSR
37 * @endcode
38 *
39 * The faulting instruction address is frame[6] (stacked PC).
40 * frame[5] (stacked LR) shows where the faulting function would
41 * have returned to.
42 *
43 * @param frame Pointer to the exception stack frame
44 */
45[[noreturn]] inline void on_hard_fault(const uint32_t* frame) {
46 __asm volatile("cpsid i");
47
48 char buf[256];
49 int pos = 0;
50
51 pos += sbl::log::format(buf + pos, sizeof(buf) - pos,
52 "\r\n!!! HARD FAULT\r\n"
53 " PC = 0x%08lX (faulting instruction)\r\n"
54 " LR = 0x%08lX (return address)\r\n"
55 " PSR = 0x%08lX\r\n",
56 frame[6], frame[5], frame[7]);
57
58 pos += sbl::log::format(buf + pos, sizeof(buf) - pos,
59 " R0 = 0x%08lX R1 = 0x%08lX\r\n"
60 " R2 = 0x%08lX R3 = 0x%08lX\r\n"
61 " R12 = 0x%08lX\r\n",
62 frame[0], frame[1], frame[2], frame[3], frame[4]);
63
64 SBL_FAULT_OUTPUT::write(buf);
65
66 while (true) {
67 __asm volatile("bkpt #1");
68 }
69}
70
71} // namespace sbl::system::fault
72
73// HardFault handler override
74// sbl_on_hard_fault connects the platform's HardFault stub to the
75// formatted output above. The function is inline, so it is emitted with
76// weak (COMDAT) linkage, not as a strong symbol. __attribute__((used))
77// makes the compiler emit it although no C++ code calls it; the call
78// comes from the HardFault stub through the vector table.
79
80extern "C" [[noreturn]] __attribute__((used)) inline void sbl_on_hard_fault(const uint32_t* frame) {
82}
83
84#endif // SBL_SYSTEM_FAULT_HPP_
Assert and panic for embedded firmware.
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 on_hard_fault(const uint32_t *frame)
Format and output the Cortex-M exception stack frame.
Definition fault.hpp:45