/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   (C) Copyright Intel Corp., 1996

   File: service.c

   Universal Serial Bus 930 Test firmware

   Revision History
   ---------------------------------------------
    0.1     05-27-96          Abdul Rahman Ismail
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#include <8x930ax.h>
#include "defs.h"

#include "service.h"

#define EPINDEX (*((volatile sfr char *)0xf1))

//	external functions.
extern	void FillEP1Fifo( void );
extern	void FillEP2Fifo( void );

// external variables
extern	BYTE	gbEP1Running;
extern	BYTE	gbEP2Running;

// function prototypes
reentrant void ServiceEndPoints( void );
reentrant void CheckEPRunning( void );
reentrant void CheckEPOverflow( void );

/*-------------------------------------------------------------------
Function name     : ServiceEndPoints
Brief Description : Called whenever a new SOF is recd. Updates the
				  : TX fifos on all endpoints running.    
-------------------------------------------------------------------*/

reentrant void ServiceEndPoints( void )
{
	if (gbEP1Running == TRUE)
		FillEP1Fifo();

	if (gbEP2Running == TRUE)
		FillEP2Fifo();
}

/*-------------------------------------------------------------------
Function name     : CheckEPRunning
Brief Description : Checks whether an endpoint has started sending data
                    to the host. If so then sets the gbEP1Running to
                    true.
--------------------------------------------------------------------*/

reentrant void CheckEPRunning( void )
{
	if (gbEP1Running == FALSE)
    {
      EPINDEX = 1;
    	if ((TXFLG & GET_FIFO_BITS) != FIFO_FULL)
       		gbEP1Running = TRUE;
    }

	if (gbEP2Running == FALSE)
    {
      EPINDEX = 2;
    	if ((TXFLG & GET_FIFO_BITS) != FIFO_FULL)
       		gbEP2Running = TRUE;
    }
}

/*-------------------------------------------------------------------
Function name     : CheckEPOverflow
Brief Description : Continuously called to check whether an overflow
					has occured on any endpoint. If it has then the 
                    endpoint is reset, and new data primed into the
                    fifos.
--------------------------------------------------------------------*/

reentrant void CheckEPOverflow( void )
{
  EPINDEX = 1;
	if (TXOVF == TRUE)
    {
    	gbEP1Running = FALSE;			// Assume that the ep has stopped running

		TXCLR = TRUE;					// Clear the fifo.

		/* Fill the fifo with a known pattern */
	    asm( "mov	R8, #00h" );
	    FillEP1Fifo();
	    asm( "mov	R8, #01h" );
	    FillEP1Fifo();
    }

  EPINDEX = 2;
	if (TXOVF == TRUE )
    {
    	gbEP2Running = FALSE;

	    TXCLR = TRUE;
	    asm( "mov	R8, #00h" ); 
	    FillEP2Fifo();

    	asm( "mov	R8, #01h" ); 
	    FillEP2Fifo();
    }
}
