/**************************************************************
$Workfile:   bup_iposapi.c  $
***************************************************************
**
** FIRMWARE, Copyright (C) 2000-2002
** Ikanos Communications, Fremont, CA
** All rights reserved. 
**
** The information and contents of this file are the
** proprietary information of Ikanos Communication and may not
** be disclosed or used without the formal written approval of
** Ikanos Communication
**
$Revision: 1.1 $
$Date: 2006/10/16 04:34:50 $
***************************************************************/
#ifdef BUP_DIAGS
#include <stdio.h>
#include <string.h>
#include "ipos.h"
#include "dapi.h"
#include "apilocal.h"
#include "host_ver.h"
#include "apposmacro.h"
#ifdef INI_SUPPORT
#include "HostPtmfApi.h"
#endif																
																
typedef	int	 (*iposDapiMap) (tBmeStruct	*pDABme, int portNum,	void	*par);																	

extern bup_bme;

extern ipos_api_bme	apiBme[IPOS_MAX_BME+1];  				/* BME objects for user API	   */

typedef	struct
{

	int	bmeNum;
	int	numPort;
	int	bmeHasCrashed;
	int	bmeErrorCode;

	int	bmeState;
	int	cmdState;

}	oamp_bme;




extern eApiRv   bup_testHICT2   ()
{

	int ApiRv;
	tBmeStruct		*pDABme; 			/* direct API BME structure	*/
	ipos_api_bme	*pApiBme;			/* user PPI BME structure	*/ 
	int i = 0;
	int tempVal;
	extern int gotAnRx;
	int data;
	extern char pTlStr [];


	pApiBme=&apiBme[bup_bme];
	pDABme=pApiBme->pBme;

	data = testPIORead1 (bup_bme,0x40000100);
	if (data != 0x12345678)
	{
		printf("\nBME Code not running. Wait to download Code to BME\n\n\n");

        sprintf(pTlStr,"bstart:0:%d;", bup_bme);  
        ReceiveTLCommand (pTlStr);

//#ifdef CPE_30
#if defined(CPE_30) || defined(CPE_40)
		iposTaskDelay (1000);
#endif


		do
		{
			data = testPIORead1 (bup_bme,0x40000100);
			iposTaskDelay (100);
			if (i++ > 100)
				return ;


		} while (data != 0x12345678);
	}
	

	/* Send Eoc Send Data Stream Control Message to BME */
	ApiRv=SendBmeMessage (pDABme,0,0xfe);
	do
		{
			int j = 0;
			tempVal = bmeRdStatus (pDABme);
			if (j++ > 100)
			{
				printf("w");
				j = 0;
			}
		} while (tempVal & DW_T_DMA_WRITE_DONE);

	bmeClrStatus(pDABme, 1);
	pDABme->fifoStatus = FIFO_EMPTY;
	
    return(ApiRv);

} /* PeriodicReportsEnable */


extern eApiRv   bup_testHICT1   (int arg)
{

	int ApiRv;
	tBmeStruct		*pDABme; 			/* direct API BME structure	*/
	ipos_api_bme	*pApiBme;			/* user PPI BME structure	*/ 
	int i=0;
	int tempVal;
	extern int gotAnRx;
	volatile int data;
	extern char pTlStr [];

	pApiBme=&apiBme[bup_bme];
	pDABme=pApiBme->pBme;

	data = testPIORead1 (bup_bme,0x40000100);
	if (data != 0x12345678)
	{
		printf("\nBME Code not running. Wait to download Code to BME\n\n\n");

        sprintf(pTlStr,"bstart:0:%d;", bup_bme);  
        ReceiveTLCommand (pTlStr);
//#ifdef CPE_30
#if defined(CPE_30) || defined(CPE_40)
		iposTaskDelay (1000);
#endif

		do
		{
			data = testPIORead1 (bup_bme,0x40000100);
			iposTaskDelay (100);
			if (i++ > 100)
			{
				printf("time out on bme running\n");
				return ;
			}

		} while (data != 0x12345678);
	}
	
	printf("\nBME Code running %x.\n", testPIORead1 (bup_bme,0x40000100));
	iposTaskDelay (100);

	for (i = 0;i < arg;i++)
	{
		gotAnRx = FALSE;
		/* Send Eoc Send Data Stream Control Message to BME */
		ApiRv=SendBmeMessage (pDABme,0,0xff);

		do
		{
			int j = 0;
			tempVal = bmeRdStatus (pDABme);
			if (j++ > 100)
			{
				printf("w");
				j = 0;
			}
		} while (tempVal & DW_T_DMA_WRITE_DONE);

		bmeClrStatus(pDABme, 1);
		pDABme->fifoStatus = FIFO_EMPTY;
		while (!gotAnRx) 
		{
			taskDelay (1);
		}
			
	}
	
    return(ApiRv);

} /* PeriodicReportsEnable */

#endif

/*
del *.bdx
copy //pc/vxWorks.bdx -pc 0x100000
load vxWorks.bdx!launch
*/
