#if 0 //def CO_30_DIAGS /* not tested for CO3 */


/************** INCLUDE FILES ****************************/
#include <stdio.h>
#include <vxWorks.h>
#include "apposmacro.h"
#include "diags_defines.h"

/************** LOCAL MACROS *****************************/
#define UART_BASE0		0xe0098000
#define UART_BASE1		0xe009a000

/************** GLOBAL VARIABLES *************************/

/************** EXTERN FUNCTIONS AND VARIABLES ***********/


/********************************************************/
/*              LOCAL FUNCTION DEFINITIONS              */
/********************************************************/
void bup_ConfigUartCO (int bmeNum, int UartNo)
{
  UINT32 Addr;

  Addr = ((UartNo&0x1) << 13) | UART_BASE0 + 0xc;
  testPIOWrite (bmeNum, Addr,0x00000083); 	/* set DLAB bit */

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

  Addr = ((UartNo&0x1) << 13) | UART_BASE0 + 4;
  testPIOWrite (bmeNum, Addr,0x00000000); 	/* set divisor hi bit */

  Addr = ((UartNo&0x1) << 13) | UART_BASE0 + 0xc; 
  testPIOWrite (bmeNum, Addr,0x00000003); 	/* set clear DLAB bit */

  Addr = ((UartNo&0x1) << 13) | UART_BASE0 + 8;
  testPIOWrite (bmeNum, Addr,0x00000001); 	/* set fifo enable bit */

  Addr = ((UartNo&0x1) << 13) | UART_BASE0 + 4;
  testPIOWrite (bmeNum, Addr,0x00000001); 	/* set interrupt enable bit */

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

/********************************************************/
/*            EXPORTED FUNCTION DEFINITIONS             */
/********************************************************/

void bup_SendMsgUartCO (int bmeNum, int tstUart)
{ 
  char *pString = "\nUART0: Test Message\n";
  UINT32 uartdata, uartBase;
  
  bup_ConfigUartCO(bmeNum, tstUart);

  uartBase = tstUart == 0? UART_BASE0 : UART_BASE1;
  while(*pString != 0)
  {
    uartdata = *pString;
    testPIOWrite (bmeNum, uartBase, uartdata);
    iposTaskDelay(1);
/*    DBG1(("%x  ", uartdata)) */
    pString++;
  }
}

/*-------------------------------------------------- */
UINT32 bup_ReceiveCharCO (int bmeNum, int UartNo)
{
  UINT32 UartStatus;
  UINT32 RcvdChar;
  UINT32 Data;
  UINT32 StatusRegAddr;
  UINT32 DataRegAddr;
  int FailStatus = 0;

  int i = 0;

	StatusRegAddr = ((UartNo&0x1) << 13) | UART_BASE0 + 0x14;
	DataRegAddr =((UartNo&0x1) << 13) | UART_BASE0;
  	UartStatus = testPIORead1 (bmeNum,StatusRegAddr);
  	while (!(UartStatus & 0x01))
        {
	  if (i == 300) {
	    FailStatus = 1;
	    break;
	  }
          UartStatus = testPIORead1 (bmeNum,StatusRegAddr);
	  i++;
        }
	if (!FailStatus) {
  	  RcvdChar = testPIORead1 (bmeNum,DataRegAddr);
	  Data = RcvdChar & 0xff;
	  DBG1(("%c", Data))
	}
//	else
//	  DBG1(("***** Uart loopback test Timed out/Failed *****\n"))
	
	return Data;
}
/*------- Top level uart diag function ------- */
int bup_LoopbackUartCO (int bmeNum, 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;

  bup_ConfigUartCO (bmeNum, 0);
  bup_ConfigUartCO (bmeNum, 1);
  DataAddr = ((TxUart&0x1) << 13) | UART_BASE0  ;
  StatusAddr = ((TxUart&0x1) << 13) | UART_BASE0 + 0x14;
  RxUart = TxUart ? 0 : 1;
  DBG1(("UART %d Transmitting, UART %d receiving\n",TxUart,RxUart))
  while(*pString != 0)
  {
    UartData = *pString;
    testPIOWrite (bmeNum, DataAddr,UartData);
    Lsr = 0x0;
    while (!(Lsr & 0x40))
    {
        Lsr = testPIORead1 (bmeNum,StatusAddr);
    }
    RxData = bup_ReceiveCharCO (bmeNum, RxUart);
    if (RxData != UartData)
	{
	  TestFailStatus = 1;
	}
    pString++;
  }
  DBG1(("\n"))
  if (TestFailStatus)
    {
	DBG0(("***************************************\n"))
	DBG0(("      UART loopback test FAILED\n"))
	DBG0(("***************************************\n"))
    }
  else
    {
	DBG1(("***************************************\n"))
	DBG1(("      UART loopback test PASSED\n"))
	DBG1(("***************************************\n"))
    }

  return TestFailStatus;
}


#endif //CO_30_DIAGS
