Simple debug logging via USART1

Encapsulates the #define macros and AVR implementation of dumping strings
(RAM and PROGMEM) onto USART1.
pull/122/head
D.R.racer 2021-09-27 11:02:17 +02:00 committed by DRracer
parent 011b8cae33
commit 304988fc6c
5 changed files with 104 additions and 1 deletions

View File

@ -1,4 +1,4 @@
target_sources(firmware PRIVATE main.cpp)
target_sources(firmware PRIVATE main.cpp debug.cpp)
target_link_libraries(firmware LUFA)

36
src/debug.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "debug.h"
#if defined(DEBUG_LOGIC) || defined(DEBUG_MODULES) || defined(DEBUG_HAL)
#include "hal/usart.h"
#include <avr/pgmspace.h>
#include <stdio.h>
#include <string.h>
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

55
src/debug.h Normal file
View File

@ -0,0 +1,55 @@
#pragma once
#include <stdint.h>
/// 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

View File

@ -1,5 +1,6 @@
#include "../usart.h"
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
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

View File

@ -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();