diff --git a/include/sta/debug_serial.hpp b/include/sta/debug_serial.hpp index 68cb77d..470bfed 100644 --- a/include/sta/debug_serial.hpp +++ b/include/sta/debug_serial.hpp @@ -3,15 +3,10 @@ * @brief Debug output via UART. * * Configuration: - * STA_DEBUG_SERIAL_ENABLE: Enable module - * STA_DEBUG_SERIAL_FORCE: Ignore debug defines and always enable output - * DEBUG: Enables output when defined - * NDEBUG: Disables output when defined (overrides DEBUG) - * - * The `sta::DebugSerial` instance must be provided. - * NOTE: Include this header before the definition because - * the default internal linkage of const namespace variables - * will cause undefined reference errors otherwise. + * STA_DEBUG_SERIAL_UART: UART interface for output + * STA_DEBUG_SERIAL_FORCE: Ignore debug defines and always enable output + * DEBUG: Enables output when defined + * NDEBUG: Disables output when defined (overrides DEBUG) */ #ifndef STA_CORE_DEBUG_SERIAL_HPP #define STA_CORE_DEBUG_SERIAL_HPP @@ -24,50 +19,51 @@ #ifdef DOXYGEN /** - * @def STA_DEBUG_SERIAL_ENABLE - * @brief Enable module. - * - * Automatically defined if DEBUG is defined. - * Automatically disabled if NDEBUG is defined. + * @def STA_DEBUG_SERIAL_UART + * @brief UART interface for debug output. * * @ingroup staCoreBuildConfig */ -# define STA_DEBUG_SERIAL_ENABLE +# define STA_DEBUG_SERIAL_UART /** - * @def STA_DEBUG_SERIAL_DISABLE - * @brief Force disable module. + * @def STA_DEBUG_SERIAL_FORCE + * @brief Force enable module. * - * Overrides STA_DEBUG_SERIAL_ENABLE option. + * Enables debug output even if NDEBUG is defined. * * @ingroup staCoreBuildConfig */ -# define STA_DEBUG_SERIAL_DISABLE +# define STA_DEBUG_SERIAL_FORCE #endif // DOXYGEN #include -#ifdef DEBUG -# ifndef STA_DEBUG_SERIAL_ENABLE -# define STA_DEBUG_SERIAL_ENABLE -# endif // !STA_DEBUG_SERIAL_ENABLE -#endif // DEBUG - -#if defined(NDEBUG) || defined(STA_DEBUG_SERIAL_DISABLE) -# ifdef STA_DEBUG_SERIAL_ENABLE -# undef STA_DEBUG_SERIAL_ENABLE -# endif // STA_DEBUG_SERIAL_ENABLE -#endif // NDEBUG || STA_DEBUG_SERIAL_DISABLE +// Determine if module should be enabled +// Condition 1: STA_DEBUG_SERIAL_UART is defined +// Condition 2: +// STA_DEBUG_SERIAL_FORCE is defined +// or +// DEBUG is defined but not NDEBUG +#ifdef STA_DEBUG_SERIAL_UART +# ifdef STA_DEBUG_SERIAL_FORCE +# define STA_DEBUG_SERIAL_ENABLED +# else // !STA_DEBUG_SERIAL_FORCE +# if defined(DEBUG) && !defined(NDEBUG) +# define STA_DEBUG_SERIAL_ENABLED +# endif // DEBUG && !NDEBUG +# endif // !STA_DEBUG_SERIAL_FORCE +#endif // STA_DEBUG_SERIAL_UART // Show enabled module in doxygen output #ifdef DOXYGEN -# define STA_DEBUG_SERIAL_ENABLE +# define STA_DEBUG_SERIAL_ENABLED #endif // DOXYGEN -#ifdef STA_DEBUG_SERIAL_ENABLE +#ifdef STA_DEBUG_SERIAL_ENABLED #include @@ -99,10 +95,10 @@ namespace sta * @ingroup staCoreDebug */ # define STA_DEBUG_PRINTLN(...) sta::DebugSerial.println(__VA_ARGS__) -#else // !STA_DEBUG_SERIAL_ENABLE +#else // !STA_DEBUG_SERIAL_ENABLED # define STA_DEBUG_PRINT(...) ((void)0) # define STA_DEBUG_PRINTLN(...) ((void)0) -#endif // !STA_DEBUG_SERIAL_ENABLE +#endif // !STA_DEBUG_SERIAL_ENABLED #endif // STA_CORE_DEBUG_SERIAL_HPP diff --git a/include/sta/stm32/uart.hpp b/include/sta/stm32/uart.hpp index 45ab782..7669968 100644 --- a/include/sta/stm32/uart.hpp +++ b/include/sta/stm32/uart.hpp @@ -11,16 +11,6 @@ * @brief STM32 UART module. */ -#ifdef DOXYGEN -/** - * @def STA_STM32_UART_DEBUG_SERIAL - * @brief Create global sta::DebugSerial object using this HAL UART instance. - * - * @ingroup stm32BuildConfig - */ -# define STA_STM32_UART_DEBUG_SERIAL -#endif // DOXYGEN - // Only enable module on STM32 platform w/ HAL UART module enabled #include diff --git a/src/debug_serial.cpp b/src/debug_serial.cpp new file mode 100644 index 0000000..68bb98f --- /dev/null +++ b/src/debug_serial.cpp @@ -0,0 +1,30 @@ +#include +#ifdef STA_DEBUG_SERIAL_ENABLED + + +#ifdef STA_PLATFORM_STM32 + +#include + +#include + +// Set platform UART alias +using PlatformUART = sta::STM32UART; + +#endif // STA_PLATFORM_STM32 + + +namespace +{ + // Create platform specific serial interface + PlatformUART platformDebugSerial(&STA_DEBUG_SERIAL_UART); +} + +namespace sta +{ + // Create debug serial object using platform specific serial interface + PrintableUART DebugSerial(&platformDebugSerial); +} // namespace sta + + +#endif // STA_DEBUG_SERIAL_ENABLED diff --git a/src/stm32/uart.cpp b/src/stm32/uart.cpp index d530c7c..54d1eea 100644 --- a/src/stm32/uart.cpp +++ b/src/stm32/uart.cpp @@ -22,22 +22,4 @@ namespace sta } // namespace sta -#ifdef STA_STM32_UART_DEBUG_SERIAL - -// Get extern declaration for DebugSerial because const namespace level variables have internal linkage by default -#include - -#include - -namespace sta -{ - STM32UART gStm32DebugSerial(&STA_STM32_UART_DEBUG_SERIAL); - - // Used by - PrintableUART DebugSerial(&gStm32DebugSerial); -} // namespace sta - -#endif // STA_STM32_UART_DEBUG_SERIAL - - #endif // STA_STM32_UART_ENABLED