From 304988fc6c0f6f72b76bca970d79493adbf85b59 Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Mon, 27 Sep 2021 11:02:17 +0200 Subject: [PATCH] Simple debug logging via USART1 Encapsulates the #define macros and AVR implementation of dumping strings (RAM and PROGMEM) onto USART1. --- src/CMakeLists.txt | 2 +- src/debug.cpp | 36 ++++++++++++++++++++++++++++ src/debug.h | 55 +++++++++++++++++++++++++++++++++++++++++++ src/hal/avr/usart.cpp | 8 +++++++ src/hal/usart.h | 4 ++++ 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/debug.cpp create mode 100644 src/debug.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e8bf73c..282331d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,4 @@ -target_sources(firmware PRIVATE main.cpp) +target_sources(firmware PRIVATE main.cpp debug.cpp) target_link_libraries(firmware LUFA) diff --git a/src/debug.cpp b/src/debug.cpp new file mode 100644 index 0000000..85a3de8 --- /dev/null +++ b/src/debug.cpp @@ -0,0 +1,36 @@ +#include "debug.h" + +#if defined(DEBUG_LOGIC) || defined(DEBUG_MODULES) || defined(DEBUG_HAL) + +#include "hal/usart.h" +#include +#include +#include + +namespace debug { + +#ifdef DEBUG_LOGIC +const char logic[] PROGMEM = "log:"; +#endif + +#ifdef DEBUG_MODULES +const char modules[] PROGMEM = "mod:"; +#endif + +#ifdef DEBUG_HAL +const char hal[] PROGMEM = "hal:"; +#endif + +void dbg_usart(const char *layer_P, const char *s) { + hu::usart1.puts_P(layer_P); + hu::usart1.puts(s); +} + +void dbg_usart_P(const char *layer_P, const char *s_P) { + hu::usart1.puts_P(layer_P); + hu::usart1.puts_P(s_P); +} + +} // namespace debug + +#endif diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..355178e --- /dev/null +++ b/src/debug.h @@ -0,0 +1,55 @@ +#pragma once +#include + +/// Enable DEBUG_LOGIC to compile debugging and error messages (beware of code base size ;) ) for the logic layer +#define DEBUG_LOGIC + +/// Enable DEBUG_LOGIC to compile debugging and error messages (beware of code base size ;) ) for the logic layer +#define DEBUG_MODULES + +/// Enable DEBUG_HAL to compile debugging and error messages (beware of code base size ;) ) for the logic layer +#define DEBUG_HAL + +/// Debugging macros and tools +namespace debug { + +#ifdef DEBUG_LOGIC +extern const char logic[]; +#define dbg_logic(x) debug::dbg_usart(debug::logic, x) +#define dbg_logic_P(x) debug::dbg_usart_P(debug::logic, x) +#else +#define dbg_logic(x) /* */ +#define dbg_logic_P(x) /* */ +#endif + +#ifdef DEBUG_MODULES +extern const char modules[]; +#define dbg_modules(x) debug::dbg_usart(debug::modules, x) +#define dbg_modules_P(x) debug::dbg_usart_P(debug::modules, x) +#else +#define dbg_modules(x) /* */ +#define dbg_modules_P(x) /* */ +#endif + +#ifdef DEBUG_HAL +extern const char hal[]; +#define dbg_hal(x) debug::dbg_usart(debug::hal, x) +#define dbg_hal_P(x) debug::dbg_usart_P(debug::hal, x) +#else +#define dbg_hal(x) /* */ +#define dbg_hal_P(x) /* */ +#endif + +#if defined(DEBUG_LOGIC) || defined(DEBUG_MODULES) || defined(DEBUG_HAL) +/// Dump an error message onto the USART +/// @param layer PROGMEM string +/// @param s RAM string to be printed +void dbg_usart(const char *layer_P, const char *s); + +/// Dump an error message onto the USART +/// @param layer PROGMEM string +/// @param s PROGMEM string to be printed +void dbg_usart_P(const char *layer_P, const char *s_P); +#endif + +} // namespace debug diff --git a/src/hal/avr/usart.cpp b/src/hal/avr/usart.cpp index 0497d56..42b2d2b 100644 --- a/src/hal/avr/usart.cpp +++ b/src/hal/avr/usart.cpp @@ -1,5 +1,6 @@ #include "../usart.h" #include +#include namespace hal { namespace usart { @@ -70,6 +71,13 @@ void USART::puts(const char *str) { } } +void USART::puts_P(const char *str) { + char c; + while ((c = pgm_read_byte(*str++)) != 0) { + Write(c); + } +} + } // namespace usart } // namespace hal diff --git a/src/hal/usart.h b/src/hal/usart.h index 010b8ec..fdbfc51 100644 --- a/src/hal/usart.h +++ b/src/hal/usart.h @@ -46,7 +46,11 @@ public: /// @param c character to be pushed into the TX buffer (to be sent) void Write(uint8_t c); /// @param str c string to be sent. NL is appended + /// Works on RAM strings void puts(const char *str); + /// @param str c string to be sent. NL is appended + /// Works on PROGMEM strings + void puts_P(const char *str); /// @returns true if there is at least one byte free in the TX buffer (i.e. some space to add a character to be sent) bool CanWrite() const { return !tx_buf.full();