/****************************************************************************/
/* Copyright 2013 MBARI.                                                    */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/

#include <xc.h>
#include "port.h"
#include "debug.h"
#include "../O5ExpanderMain/serial.h"
#include "../O5ExpanderMain/dig_io.h"
#include "../O5ExpanderMain/sys_timer.h"

#ifdef DEBUG_PORT

void	debugInit(void)
{
    serSetConfig(DEBUG_PORT, D8_NONE, S_1);
    serSetBaud(DEBUG_PORT, 9600);
    digPwrSet(DEBUG_PORT, 1);
    digSerEnable(DEBUG_PORT);
    tmrDelayMs(20);
    serPutString(DEBUG_PORT, "Debug on\r\n");
}

static void debugPutNibble(UCHAR c)
{
    BYTE nib = c & 0xf;

    serPutByte(DEBUG_PORT, (nib > 9) ? nib + 'A' - 10 : nib + '0');
}

void debugFrame(volatile UCHAR *bufp, USHORT buflen)
{
    int	i;

    digSerEnable(DEBUG_PORT);
    tmrDelayMs(20);

    for (i = 0; i < buflen; i++)
    {
	debugPutNibble((bufp[i] >> 4) & 0xf);
	debugPutNibble(bufp[i] & 0xf);
	serPutByte(DEBUG_PORT, ' ');
    }
}

#endif
