xref: /aosp_15_r20/external/pigweed/pw_assert_basic/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_assert_basic:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker===============
4*61c4878aSAndroid Build Coastguard Workerpw_assert_basic
5*61c4878aSAndroid Build Coastguard Worker===============
6*61c4878aSAndroid Build Coastguard Worker
7*61c4878aSAndroid Build Coastguard Worker--------
8*61c4878aSAndroid Build Coastguard WorkerOverview
9*61c4878aSAndroid Build Coastguard Worker--------
10*61c4878aSAndroid Build Coastguard WorkerThis is a simple assert backend to implement the ``pw_assert`` facade which
11*61c4878aSAndroid Build Coastguard Workerrelies on a single function ``pw_assert_basic_HandleFailure`` handler facade
12*61c4878aSAndroid Build Coastguard Workerwhich defaults to the ``basic_handler`` backend. Users may be interested in
13*61c4878aSAndroid Build Coastguard Workeroverriding this default in case they need to do things like transition to
14*61c4878aSAndroid Build Coastguard Workercrash time logging or implementing application specific reset and/or hang
15*61c4878aSAndroid Build Coastguard Workerbehavior.
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker.. attention::
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker  This log backend comes with a very large ROM and potentially RAM cost. It is
20*61c4878aSAndroid Build Coastguard Worker  intended mostly for ease of initial bringup. We encourage teams to use
21*61c4878aSAndroid Build Coastguard Worker  tokenized asserts since they are much smaller both in terms of ROM and RAM.
22*61c4878aSAndroid Build Coastguard Worker
23*61c4878aSAndroid Build Coastguard Worker----------------------------
24*61c4878aSAndroid Build Coastguard WorkerModule Configuration Options
25*61c4878aSAndroid Build Coastguard Worker----------------------------
26*61c4878aSAndroid Build Coastguard WorkerThe following configurations can be adjusted via compile-time configuration of
27*61c4878aSAndroid Build Coastguard Workerthis module, see the
28*61c4878aSAndroid Build Coastguard Worker:ref:`module documentation <module-structure-compile-time-configuration>` for
29*61c4878aSAndroid Build Coastguard Workermore details.
30*61c4878aSAndroid Build Coastguard Worker
31*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_ASSERT_BASIC_ACTION
32*61c4878aSAndroid Build Coastguard Worker
33*61c4878aSAndroid Build Coastguard Worker  Controls what happens after an assert failure. Should be set to one of the
34*61c4878aSAndroid Build Coastguard Worker  following options:
35*61c4878aSAndroid Build Coastguard Worker
36*61c4878aSAndroid Build Coastguard Worker  - PW_ASSERT_BASIC_ACTION_ABORT: Call std::abort()
37*61c4878aSAndroid Build Coastguard Worker  - PW_ASSERT_BASIC_ACTION_EXIT: Call std::_Exit(-1)
38*61c4878aSAndroid Build Coastguard Worker  - PW_ASSERT_BASIC_ACTION_LOOP: Loop forever
39*61c4878aSAndroid Build Coastguard Worker
40*61c4878aSAndroid Build Coastguard Worker  Defaults to abort.
41*61c4878aSAndroid Build Coastguard Worker
42*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_ASSERT_BASIC_SHOW_BANNER
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard Worker  Controls whether ASCII art banner is printed on assert failure.
45*61c4878aSAndroid Build Coastguard Worker
46*61c4878aSAndroid Build Coastguard Worker  This defaults to enabled.
47*61c4878aSAndroid Build Coastguard Worker
48*61c4878aSAndroid Build Coastguard Worker.. c:macro:: PW_ASSERT_BASIC_USE_COLORS
49*61c4878aSAndroid Build Coastguard Worker
50*61c4878aSAndroid Build Coastguard Worker  Controls whether colors are used in assert message printed to console.
51*61c4878aSAndroid Build Coastguard Worker
52*61c4878aSAndroid Build Coastguard Worker  This defaults to enabled.
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard Worker.. _module-pw_assert_basic-custom_handler:
55*61c4878aSAndroid Build Coastguard Worker
56*61c4878aSAndroid Build Coastguard WorkerCustom handler backend example
57*61c4878aSAndroid Build Coastguard Worker------------------------------
58*61c4878aSAndroid Build Coastguard WorkerHere is a typical usage example implementing a simple handler backend which uses
59*61c4878aSAndroid Build Coastguard Workera UART backed sys_io implementation to print during crash time and then reboots.
60*61c4878aSAndroid Build Coastguard WorkerNote that this example uses CMSIS and a psuedo STM HAL, as a backend implementer
61*61c4878aSAndroid Build Coastguard Workeryou are responsible for using whatever APIs make sense for your use case(s).
62*61c4878aSAndroid Build Coastguard Worker
63*61c4878aSAndroid Build Coastguard Worker.. code-block:: cpp
64*61c4878aSAndroid Build Coastguard Worker
65*61c4878aSAndroid Build Coastguard Worker   #include "cmsis.h"
66*61c4878aSAndroid Build Coastguard Worker   #include "hal.h"
67*61c4878aSAndroid Build Coastguard Worker   #include "pw_string/string_builder.h"
68*61c4878aSAndroid Build Coastguard Worker
69*61c4878aSAndroid Build Coastguard Worker   using pw::sys_io::WriteLine;
70*61c4878aSAndroid Build Coastguard Worker
71*61c4878aSAndroid Build Coastguard Worker   extern "C" void pw_assert_basic_HandleFailure(
72*61c4878aSAndroid Build Coastguard Worker       [[maybe_unused]] const char* file_name,
73*61c4878aSAndroid Build Coastguard Worker       [[maybe_unused]] int line_number,
74*61c4878aSAndroid Build Coastguard Worker       [[maybe_unused]] const char* function_name,
75*61c4878aSAndroid Build Coastguard Worker       const char* message,
76*61c4878aSAndroid Build Coastguard Worker       ...) {
77*61c4878aSAndroid Build Coastguard Worker     // Global interrupt disable for a single core microcontroller.
78*61c4878aSAndroid Build Coastguard Worker     __disable_irq();
79*61c4878aSAndroid Build Coastguard Worker
80*61c4878aSAndroid Build Coastguard Worker     // Re-initialize the UART to ensure it's safe to use at crash time.
81*61c4878aSAndroid Build Coastguard Worker     HAL_UART_DeInit(sys_io_uart);
82*61c4878aSAndroid Build Coastguard Worker     HAL_UART_Init(sys_io_uart);
83*61c4878aSAndroid Build Coastguard Worker
84*61c4878aSAndroid Build Coastguard Worker     WriteLine(
85*61c4878aSAndroid Build Coastguard Worker         "  Welp, that didn't go as planned. "
86*61c4878aSAndroid Build Coastguard Worker         "It seems we crashed. Terribly sorry! Assert reason:");
87*61c4878aSAndroid Build Coastguard Worker     {
88*61c4878aSAndroid Build Coastguard Worker       pw::StringBuffer<150> buffer;
89*61c4878aSAndroid Build Coastguard Worker       buffer << "     ";
90*61c4878aSAndroid Build Coastguard Worker       va_list args;
91*61c4878aSAndroid Build Coastguard Worker       va_start(args, format);
92*61c4878aSAndroid Build Coastguard Worker       buffer.FormatVaList(format, args);
93*61c4878aSAndroid Build Coastguard Worker       va_end(args);
94*61c4878aSAndroid Build Coastguard Worker       WriteLine(buffer.view());
95*61c4878aSAndroid Build Coastguard Worker     }
96*61c4878aSAndroid Build Coastguard Worker
97*61c4878aSAndroid Build Coastguard Worker     // Reboot the microcontroller.
98*61c4878aSAndroid Build Coastguard Worker     HAL_NVIC_SystemReset();
99*61c4878aSAndroid Build Coastguard Worker   }
100