diff --git a/srv/debug.c b/srv/debug.c new file mode 100644 index 0000000..44711ac --- /dev/null +++ b/srv/debug.c @@ -0,0 +1,60 @@ +/** @file debug.c + * Module handling various debug functions + * + * The module provides a range of functions and tools to make debug more + * convenient + */ + +//--includes-------------------------------------------------------------------- + +#include "debug.h" + + +//--local definitions----------------------------------------------------------- + +#define BUFFER_SIZE 83 //80 char line + \n + 2 bytes for index +#define BUFFER_NB 3 //1 buffer in write, one in transfer and one waiting + +//--local variables------------------------------------------------------------- + +static enum UsartPeriph usart_periph; +static uint8_t tx_buffer1[BUFFER_SIZE]; +static uint8_t tx_buffer2[BUFFER_SIZE]; +static uint8_t tx_buffer3[BUFFER_SIZE]; +static uint8_t* tx_buffers[BUFFER_NB] = {tx_buffer1, tx_buffer2, tx_buffer3}; + + +//--public functions------------------------------------------------------------ + +#if DEBUG_TRACE || DEBUG_WARN || DEBUG_ERROR + +void _debug_init(enum UsartPeriph usart, enum GpioPort tx_port, + enum GpioPin tx_pin) +{ + usart_periph = usart; + gpio_configure(tx_port, tx_pin, GPIO_MODE_OUTPUT_FAST, + GPIO_CONFIG_OUT_ALT_PUSH_PULL); + usart_configure(usart_periph, USART_CONFIG_8N1, 1152000); + + usart_set_tx_buffer(USART_PERIPH_3, tx_buffers, BUFFER_SIZE, BUFFER_NB, + DMA_CONFIG_PRIO_LOW); + + debug_trace("\n"); + debug_trace("--------------------------------------------------------------------------------\n"); + debug_trace("starting debug software\n"); + debug_trace("compiled on " __DATE__ " at " __TIME__ "\n"); + debug_trace("--------------------------------------------------------------------------------\n"); +} + +void _debug_trace(char* format, ...) +{ + while(*format != '\0') { + while(usart_write_byte(usart_periph, *format)) {} + ++format; + } +} + +#endif + +//--local functions------------------------------------------------------------- + diff --git a/srv/debug.h b/srv/debug.h new file mode 100644 index 0000000..cd511d6 --- /dev/null +++ b/srv/debug.h @@ -0,0 +1,59 @@ +/** @file debug.h + * Module handling various debug functions + * + * The module provides a range of functions and tools to make debug more + * convenient + */ + +#ifndef _DEBUG_H_ +#define _DEBUG_H_ + +//--includes-------------------------------------------------------------------- + +#include "../drv/usart.h" +#include "../drv/gpio.h" + +#include "stdint.h" + +#define DEBUG_TRACE 1 +#define DEBUG_WARN 0 +#define DEBUG_ERROR 0 + +//--type definitions------------------------------------------------------------ + +#if DEBUG_TRACE || DEBUG_WARN || DEBUG_ERROR +#define debug_init(usart, tx_port, tx_pin) _debug_init(usart, tx_port, tx_pin) +#else +#define debug_init(usart) do {} while (0) +#endif + +#if DEBUG_TRACE +#define debug_trace(format, ...) _debug_trace(format __VA_OPT__(,) __VA_ARGS__) +#else +#define debug_trace(format, ...) do {} while (0) +#endif + +#if DEBUG_WARN +#define debug_warn(format, ...) _debug_warn(format __VA_OPT__(,) __VA_ARGS__) +#else +#define debug_warn(format, ...) do {} while (0) +#endif + +#if DEBUG_ERROR +#define debug_error(format, ...) _debug_error(format __VA_OPT__(,) __VA_ARGS__) +#else +#define debug_error(format, ...) do {} while (0) +#endif + + +//--functions------------------------------------------------------------------- + +void _debug_init(enum UsartPeriph usart, enum GpioPort tx_port, + enum GpioPin tx_pin); +void _debug_trace(char* format, ...); +void _debug_warn(char* format, ...); +void _debug_error(char* format, ...); + + +#endif //_DEBUG_H_ +