#ifdef BUP_DIAGS
#ifndef CPE_40
#include <stdio.h>
#include <vxWorks.h>
#include "apposmacro.h"

extern int printDiags;
extern int bup_bme;

//#define DEBUG
//---------------------------------------------------------------
void bup_Enable_Vbus (int vbusnum) {
	testPIOWrite (bup_bme,0xe0018000+(vbusnum*0x2000),0x80000000);
//	printf (" Vbus-%d Enabled !!!\n", vbusnum);
}
//---------------------------------------------------------------
UINT32 bup_rdAfeReg(int afenum, UINT32 addr) {
	UINT32 ad, dat;
	int i = 0;

	ad  = 0xe0018008 + (afenum/2 * 0x2000);
	dat = 0x80000000 + ((afenum&0x1)<<8) + addr;

	testPIOWrite( bup_bme, ad, dat);
	ad  = 0xe0018014 + (afenum/2 * 0x2000);
	dat = testPIORead1 (bup_bme, ad);
	i   = 0;
	while ((dat & 0x80000000) ==0 )
	{
		dat = testPIORead1(bup_bme, ad);
		i++;
		if (i > 200 )
		{
			printf(" ERROR : TimedOUT for ReadDone bit\n");
			break;
		}
	}
	ad  = 0xe001800c + (afenum/2 * 0x2000);
	dat = testPIORead1(bup_bme, ad);
	return dat;
}
//---------------------------------------------------------------
int bup_rdnchkAfeReg(int afenum, UINT32 addr, UINT32 exp_data) {
	UINT32 dat;

	dat = bup_rdAfeReg (afenum, addr);
	if (dat == exp_data)
		return 0;
	else
	{
		printf ("AFE register 0x%x mismatch- expected: 0x%x, actual: 0x%x\n", addr, exp_data, dat);
		return 1;
	}
}

//---------------------------------------------------------------
void bup_wtAfeReg(int afenum, UINT32 addr, UINT32 data) {
	UINT32 ad, dat;
	int i = 0;

	ad = 0xe0018014 + (afenum/2 * 0x2000);
	dat = testPIORead1 (bup_bme, ad);
	while ((dat & 0x40000000) !=0 )
	{
		dat = testPIORead1(bup_bme, ad);
		i++;
		if (i > 200 )
		{
			printf(" ERROR : Command Queue is Full \n");
			break;
		}
	}

	ad = 0xe0018010 + (afenum/2 * 0x2000);
	testPIOWrite(bup_bme, ad, data);
	ad = 0xe0018008 + (afenum/2 * 0x2000);
	dat = 0x40000000 + ((afenum&0x1)<<8) + addr;
	testPIOWrite(bup_bme, ad, dat);
}
//---------------------------------------------------------------
int bup_chk_afeid (afenum) {
	UINT32 AfeId;

	AfeId = bup_rdAfeReg (afenum, 0x3f);
	AfeId = AfeId & 0xfff;
	if (printDiags)
		printf ("\n");
	if ((AfeId == 0x810) || (AfeId == 0x820))
	{
		printf ("\tAFE on board is AFE-LS\n");
		return 1;
	}
	else if (AfeId == 0x910)
	{
		printf ("\tAFE %d is AFE-BRONCO\n", afenum);
		return 2;
	}
	else
	{
		printf ("   >>>> No/Bad AFE #%d on board <<<< \n",afenum);
		return 0;
	}
}
//---------------------------------------------------------------
int bup_afe_reg_rw ()
{
	int testfail=0;
	int i=0;
	int loop_cnt=1;
	int afe_present=0;
	int afe=0;
	UINT32 dat;


#ifdef CO_20
	for (afe=0; afe<=7; afe++)
#endif
	{
		bup_Enable_Vbus (afe/2);
		afe_present = bup_chk_afeid (afe);
		i = 0;
		if (afe_present)
			while (i<loop_cnt)
			{
				dat = rand ();
				bup_wtAfeReg (afe, 0xf0, dat);
				testfail = bup_rdnchkAfeReg (afe, 0xf0, dat);
				i++;
				if (testfail)
					break;
			}
		else
			testfail=1;

		if (testfail)
		{
			printf ("\t****** AFE-%d register write/read test FAILED\n\n", afe);
		}
		else
		{
			printf ("\tAFE-%d register write/read test PASSED\n\n", afe);
		}
	}
	return testfail;
}
//-----------------------------------------------------------
int bup_afe_all_reg_rw (int afenum) {
	int i,j = 0;
	int testfail = 0;
	int error;
	UINT32 ReadData;
//  int afenum;

	UINT32 RegAddr [32] = { 0x24, 0x20, 0x21, 0x25, 0x29, 0x2d, 0x30, 0x31,
		0x32, 0x33, 0x34, 0x36, 0x3f, 0x70, 0x71, 0x72,
		0x73, 0x78, 0x7a, 0x7c, 0x86, 0x88, 0x8a, 0x90,
		0x91, 0x92, 0x93 ,0x94, 0xa0, 0xa2, 0xa8, 0xf0};

	UINT32 Regwdat [32] = { 0x1,  0x1,  0x1,  0x1,  0x1,  0x200, 0x8000, 0,
		0,    0,    0x1,  0x1,  0,    0x1,   0x1,    0,
		0,    0x1,  0x1,  0x1,  0x100, 0x1,  0x1,    0x1,
		0x1,  0x1,  0x1,  0x1,  0,    0,     0x1,    0x1};

	UINT32 RegDef [32] = {  0x00000000, 0x00000000, 0x00000000, 0x00000000, 
		0x00000000, 0x00000200, 0x0000c000, 0x00000000, 
		0x00000000, 0x00000000, 0x00000000, 0x00000000, 
		0x00000910, 0x00000000, 0x00000000, 0x00000010, 
		0x00000010, 0x00000000, 0x00000000, 0x00007fff, 
		0x0000700c, 0x0000300c, 0x00000001, 0x00000000, 
		0x00000000, 0x00000000, 0x00000000, 0x00000000, 
		0x00000000, 0x00000000, 0x00000000, 0x003001d0};

//  printf("Enter the AFE number:");
//  scanf ("%d", &afenum);
//  printf("%d\n", afenum);
//  printf ("\n");

	bup_Enable_Vbus (afenum/2);

	//checking defaule value
	//printf ("\t***** Reading default (reset) values of all registers\n");
	for (i=0; i<32; i++)
	{
		if ((RegAddr[i] == 0x32) || (RegAddr[i] == 0x73))
			i++;
		else
		{
			ReadData = bup_rdAfeReg (afenum, RegAddr[i]);
#ifdef DEBUG
			printf ("Addr : 0x%x  reset value : 0x%x\n", RegAddr[i], ReadData);
#endif
			if (ReadData != RegDef[i])
			{
				printf ("AFE register 0x%x reset value WRONG: Expected: 0x%x, Actual: 0x%x\n", 
						RegAddr[i], RegDef[i], ReadData);
				testfail = 1;
				j++;
			}
		}
	}

	//writing registers
	//printf ("\t***** Writing all registers\n");
	for (i=0; i<32; i++)
	{
		if ((RegAddr[i] != 0x31) && (RegAddr[i] != 0x32) && (RegAddr[i] != 0x33) &&
			(RegAddr[i] != 0x3f) && (RegAddr[i] != 0x72) && (RegAddr[i] != 0x73) &&
			(RegAddr[i] != 0xa0) && (RegAddr[i] != 0xa2))	// read only regs
			bup_wtAfeReg (afenum, RegAddr[i], Regwdat[i]);
	}

	//reading registers
	//printf ("\t***** Reading and comparing all registers\n");
	for (i=0; i<32; i++)
	{
		if ((RegAddr[i] != 0x31) && (RegAddr[i] != 0x32) && (RegAddr[i] != 0x33) &&
			(RegAddr[i] != 0x3f) && (RegAddr[i] != 0x72) && (RegAddr[i] != 0x73) &&
			(RegAddr[i] != 0xa0) && (RegAddr[i] != 0xa2) && (RegAddr[i] != 0x25) &&
			(RegAddr[i] != 0x91) && (RegAddr[i] != 0x24))
		{	// read is invalid on these
			ReadData = bup_rdAfeReg (afenum, RegAddr[i]);
			if (ReadData != Regwdat[i])
			{
				printf ("Register 0x%x write/read FAILED, ReadData:0x%x ExpData:0x%x\n", RegAddr[i], ReadData, Regwdat[i]);
				testfail = 1;
				j++;
			}
		}
	}

	if (testfail)
	{
		printf ("***********************************************************\n");
		printf ("\t***** AFE-%d All registers r/w test FAILED for %d registers\n",afenum,j);
		printf ("***********************************************************\n");
	}
	else if (printDiags)
	{
		printf ("\tAFE-%d All registers r/w test PASSED !\n",afenum);
	}
	return testfail;
}
//----------------------------------------------------------------------------------
void bup_afe_all_reg_rw_loop (int loopcnt) {
	int i, j, testfail;

	for (i=0; i<loopcnt; i++)
	{
		printf ("%d  ", i);
		for (j=0; j<8; j++)
		{
			testfail = bup_afe_all_reg_rw(j);
			if (testfail)
				break;
			else
				bmeHWReset (1);
			iposTaskDelay (50);
		}
	}
}


int bup_read_ife_id () 
{
	int data;
	int i;

	bup_Enable_Vbus (0);
	bup_Enable_Vbus (1);
	bup_Enable_Vbus (2);
	bup_Enable_Vbus (3);

	for (i = 0;i < 4;i++)
	{
		data = bup_rdAfeReg (i,0x21);
		data = bup_rdAfeReg (i,0x21);
		data &= 0xffff;
		if (data == 0x2400)
		{

			printf("IFE Recognized. IFE %d = %x\n",i, data);
		}
		else
			printf("IFE Not Recognized. IFE %d = %x\n",i, data);

	}
	return 0;
}



int bup_testIfe (int j)
{
	int data, stat;
	int i = 0;

	bup_Enable_Vbus (j/2);

	bup_wtAfeReg(j, 0x20, 0x09040000 + (j*0x10000));
	data = bup_rdAfeReg (j,0x20);
	while (i++<100)
	{
		stat = bup_rdAfeReg (j,0x24);
		if (stat & 2) break;
	}
	data = bup_rdAfeReg (j,0x20);
	if ((data&0xffff0000) == 0x09040000 + (j*0x10000))
	{
		printf("IFE Read/Write passed. IFE %d = %x\n",j, data);
		stat = 0;
	}
	else
	{
		printf("******* IFE Read/Write Failed. IFE %d = %x\n",j, data);
		stat = ERROR;
	}
	return stat;

}

#endif
#endif




