From f1596493f935864987921e8076a65386be2f2f67 Mon Sep 17 00:00:00 2001 From: Henrik Stickann <4376396-Mithradir@users.noreply.gitlab.com> Date: Tue, 12 Apr 2022 15:46:56 +0200 Subject: [PATCH] Add debug serial --- include/sta/debug_serial.hpp | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 include/sta/debug_serial.hpp diff --git a/include/sta/debug_serial.hpp b/include/sta/debug_serial.hpp new file mode 100644 index 0000000..3865b0c --- /dev/null +++ b/include/sta/debug_serial.hpp @@ -0,0 +1,52 @@ +/** + * @brief Debug output via UART. + * + * Define `STA_DEBUG_SERIAL_ENABLE` in `` to enable. + * + * Defining `DEBUG` will automatically enable the module. + * Defining `NDEBUG` will always force module to be disabled. + * + * Application must provide the sta::DebugSerial instance. + * NOTE: Include this header before the definition because + * the default internal linkage of const namespace variables + * will cause undefined reference errors otherwise. + */ +#ifndef STA_DEBUG_SERIAL_HPP +#define STA_DEBUG_SERIAL_HPP + +#include + +#ifdef DEBUG +# ifndef STA_DEBUG_SERIAL_ENABLE +# define STA_DEBUG_SERIAL_ENABLE +# endif // !STA_DEBUG_SERIAL_ENABLE +#endif // DEBUG + +#ifdef NDEBUG +# ifdef STA_DEBUG_SERIAL_ENABLE +# undef STA_DEBUG_SERIAL_ENABLE +# endif // STA_DEBUG_SERIAL_ENABLE +#endif // NDEBUG + + +#ifdef STA_DEBUG_SERIAL_ENABLE + +#include + + +namespace sta +{ + extern UART * const DebugSerial; +} // namespace sta + + +# define STA_DEBUG_PRINT(...) sta::DebugSerial->print(__VA_ARGS__) +# define STA_DEBUG_PRINTLN(...) sta::DebugSerial->println(__VA_ARGS__) +# define STA_DEBUG_WRITE(b, s) sta::DebugSerial->write(b, s) +#else // !STA_DEBUG_SERIAL_ENABLE +# define STA_DEBUG_PRINT(...) ((void)0) +# define STA_DEBUG_PRINTLN(...) ((void)0) +# define STA_DEBUG_WRITE(b, s) ((void)0) +#endif // !STA_DEBUG_SERIAL_ENABLE + +#endif // STA_DEBUG_SERIAL_HPP