• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. _module-pw_assert_basic:
2
3===============
4pw_assert_basic
5===============
6
7--------
8Overview
9--------
10This is a simple assert backend to implement the ``pw_assert`` facade which
11relies on a single function ``pw_assert_basic_HandleFailure`` handler facade
12which defaults to the ``basic_handler`` backend. Users may be interested in
13overriding this default in case they need to do things like transition to
14crash time logging or implementing application specific reset and/or hang
15behavior.
16
17.. attention::
18
19  This log backend comes with a very large ROM and potentially RAM cost. It is
20  intended mostly for ease of initial bringup. We encourage teams to use
21  tokenized asserts since they are much smaller both in terms of ROM and RAM.
22
23.. _module-pw_assert_basic-custom_handler:
24
25Custom handler backend example
26------------------------------
27Here is a typical usage example implementing a simple handler backend which uses
28a UART backed sys_io implementation to print during crash time and then reboots.
29Note that this example uses CMSIS and a psuedo STM HAL, as a backend implementer
30you are responsible for using whatever APIs make sense for your use case(s).
31
32.. code-block:: cpp
33
34  #include "cmsis.h"
35  #include "hal.h"
36  #include "pw_string/string_builder.h"
37
38  using pw::sys_io::WriteLine;
39
40  extern "C" void pw_assert_basic_HandleFailure(
41      [[maybe_unused]] const char* file_name,
42      [[maybe_unused]] int line_number,
43      [[maybe_unused]] const char* function_name,
44      const char* message,
45      ...) {
46    // Global interrupt disable for a single core microcontroller.
47    __disable_irq();
48
49    // Re-initialize the UART to ensure it's safe to use at crash time.
50    HAL_UART_DeInit(sys_io_uart);
51    HAL_UART_Init(sys_io_uart);
52
53    WriteLine(
54        "  Welp, that didn't go as planned. "
55        "It seems we crashed. Terribly sorry! Assert reason:");
56    {
57      pw::StringBuffer<150> buffer;
58      buffer << "     ";
59      va_list args;
60      va_start(args, format);
61      buffer.FormatVaList(format, args);
62      va_end(args);
63      WriteLine(buffer.view());
64    }
65
66    // Reboot the microcontroller.
67    HAL_NVIC_SystemReset();
68  }
69