#ifdef BUP_DIAGS
#include <stdio.h>
#include <vxWorks.h>

extern int printDiags;
extern int bup_bme;

/*-------------------------------------------------- */
void ConfigUartMustang (int UartNo)
{
  UINT32 Addr;

  Addr = ((UartNo&0x1) << 13) | 0xe009800c;
  testPIOWrite (bup_bme, Addr,0x00000083); 	/* set DLAB bit */

  Addr = ((UartNo&0x1) << 13) | 0xe0098000;
  testPIOWrite (bup_bme, Addr,0x00000013); 	/* set divisor low bit */

  Addr = ((UartNo&0x1) << 13) | 0xe0098004;
  testPIOWrite (bup_bme, Addr,0x00000000); 	/* set divisor hi bit */

  Addr = ((UartNo&0x1) << 13) | 0xe009800c; 
  testPIOWrite (bup_bme, Addr,0x00000003); 	/* set clear DLAB bit */

  Addr = ((UartNo&0x1) << 13) | 0xe0098008;
  testPIOWrite (bup_bme, Addr,0x00000001); 	/* set fifo enable bit */

  Addr = ((UartNo&0x1) << 13) | 0xe0098004;
  testPIOWrite (bup_bme, Addr,0x00000001); 	/* set interrupt enable bit */

  if (printDiags) printf ("UART %d is now configured to 115200 baud .....\n", UartNo);
}
/*-------------------------------------------------- */

/*-------------------------------------------------- */
void SendMsgUart0Mustang ()
{ 
  char *pString = "\nUART0: Test Message\n";
  UINT32 uartdata;
  
  ConfigUartMustang(0);
  while(*pString != 0)
  {
    uartdata = *pString;
    testPIOWrite (bup_bme, 0xe0098000,uartdata);
    taskDelay(1);
/*    printf ("%x  ", uartdata); */
    pString++;
  }
}

/*-------------------------------------------------- */
void SendMsgUart1Mustang ()
{ 
  char *pString = "\nUART1: Test Message\n";
  UINT32 uartdata;
  
  ConfigUartMustang(1);
  while(*pString != 0)
  {
    uartdata = *pString;
    testPIOWrite (bup_bme, 0xe009a000,uartdata);
    taskDelay(1);
/*    printf ("%x  ", uartdata);  */
    pString++;
  }
}
//-----------------------------------------------------
UINT32 ReceiveCharMustang (int UartNo)
{
  UINT32 UartStatus;
  UINT32 RcvdChar;
  UINT32 Data;
  UINT32 StatusRegAddr;
  UINT32 DataRegAddr;
  int FailStatus = 0;

  int i = 0;

	StatusRegAddr = ((UartNo&0x1) << 13) | 0xe0098014;
	DataRegAddr =((UartNo&0x1) << 13) | 0xe0098000;
  	UartStatus = testPIORead1 (bup_bme,StatusRegAddr);
  	while (!(UartStatus & 0x01))
        {
	  if (i == 300) {
	    FailStatus = 1;
	    break;
	  }
          UartStatus = testPIORead1 (bup_bme,StatusRegAddr);
	  i++;
        }
	if (!FailStatus) {
  	  RcvdChar = testPIORead1 (bup_bme,DataRegAddr);
	  Data = RcvdChar & 0xff;
	  printf ("%c", Data);
	}
//	else
//	  printf ("***** Uart loopback test Timed out/Failed *****\n");
	
	return Data;
}
/*------- Top level uart diag function ------- */
int LoopbackUartMustang (int TxUart) /*input Tx uart number 0/1 */
{
  char *pString = "UART Loopback test\n";
  UINT32 UartData;
  UINT32 Lsr = 0x0;
  UINT32 DataAddr;
  UINT32 StatusAddr;
  UINT32 RxData;
  int RxUart;
  int TestFailStatus = 0;

  ConfigUartMustang (0);
  ConfigUartMustang (1);
  DataAddr = ((TxUart&0x1) << 13) | 0xe0098000;
  StatusAddr = ((TxUart&0x1) << 13) | 0xe0098014;
  RxUart = TxUart ? 0 : 1;
  if (printDiags)
	  printf ("UART %d Transmitting, UART %d receiving\n",TxUart,RxUart);
  while(*pString != 0)
  {
    UartData = *pString;
    testPIOWrite (bup_bme, DataAddr,UartData);
    Lsr = 0x0;
    while (!(Lsr & 0x40))
    {
        Lsr = testPIORead1 (bup_bme,StatusAddr);
    }
    RxData = ReceiveCharMustang (RxUart);
    if (RxData != UartData)
	{
	  TestFailStatus = 1;
	}
    pString++;
  }
  printf ("\n");
  if (TestFailStatus)
    {
	printf ("***************************************\n");
	printf ("      UART loopback test FAILED\n");
	printf ("***************************************\n");
    }
  else if (printDiags)
    {
	printf ("***************************************\n");
	printf ("      UART loopback test PASSED\n");
	printf ("***************************************\n");
    }

  return TestFailStatus;
}


#endif
