//----------------------------------------------------------------------------------
// <copyright file="spidebug.c" company="LiquidRobotics">
//	Copyright (c) Liquid Robotics Corporation.  All rights reserved.
// </copyright>
//
// <summary>
// 	Functions to report debug information thru the SPI port
// </summary>
//
// <owner>Mike Cookson</owner>
//---------------------------------------------------------------------------------

#include "includes.h"
#include <avr/io.h>
#include "spidebug.h"

/*PAGE*/
/*
 *******************************************************************************
 *                      CONFIGURES SPI TO REPORT DEBUG INFO
 *
 * Description:			This routine should be called at program startup to set
 *						the SPI port for debugging
 *
 * Arguments:			n/a
 *
 * Returns:				n/a	
 *
 * Notes:
 *******************************************************************************
 */
#if SPIDEBUG_ENABLED != 0
void OSspiDebugInit(void)
{
	PRR0 &= ~_BV(PRSPI);				// enable the SPI port
	DDRB = (DDRB & ~_BV(PB3)) | _BV(PB2) | _BV(PB1); // configure in and out pins
	SPCR = _BV(SPE) | _BV(MSTR);		// configure as MASTER with /2 clock (fastest clock)
	SPSR = _BV(SPI2X);					// no interrupts
	SPDR = 0;							// send a 0 initially
}
#endif

/*PAGE*/
/*
 *******************************************************************************
 *             REPORT PRIORITY OR 8-BIT VALUE TO SPI PORT WITHOUT WAITING
 *
 * Description:			Reports the task prority being activated or another 8-bit
 *						value without waiting,
 *
 * Arguments:			prio is the priority number (or other 8-bit value) to be
 *						reported
 *
 * Returns:				n/a	
 *
 * Notes:				The transmitter is configured to be very fast, but if
 *						transmission is incomplete, no action is taken
 *******************************************************************************
 */
#if (SPIDEBUG_ENABLED != 0) && (SPIDEBUG_REPORT_PRIO_ENABLE != 0)
void OSspiReportPrio(uint8_t prio)
{
	if (SPSR & _BV(SPIF))
		SPDR = prio;
}
#endif
