/** @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-------------------------------------------------------------

