⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pe_main.c

📁 FIC8120方案的 StartCell_Driver
💻 C
字号:
///////////////////////////////////////////////////////////////////////////////
//
//	File name: main.c
//	Version: 1.0
//	Date: 2003/8/05
//
//	Author: Andrew
//	Email: yping@faraday.com.tw
//	Phone: (03) 578-7888
//	Company: Faraday Tech. Corp.
//
//	Description: Program Entry
//
///////////////////////////////////////////////////////////////////////////////
#define MAIN_GLOBALS

#include "fLib.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Pe_usb.h"
#include "Pe_my_usbtable.h"
#include "FOTG200_peripheral.h"
#include "FA510.h"
#include "serial.h"
#include "command.h"
#include "OTGController.h"
#include "GPIO.h"


UINT8 bForceRemoteWakeUp;

//============== extern function ========================
extern void vUsbCxLoopBackTest(void);
extern void vUsbPamLoopBackTest(INT32U u32StartFIFONum);
extern void flib_Host20_TimerEnable(UINT32 wTime_ms);
//============== local function =========================
void vOTGSysInit(void);	
UINT32 PamLoopBackTest(CommandTableStruct_T *CmdTable);
UINT32 DumpBulkBufData(CommandTableStruct_T *CmdTable);

//=============== Command table =======================
CommandTableStruct_T USBDevice_CmdTable[] =
{		
	{{"CxTest", "ct", NULL,}, "CxTest/ct (Cx loop back test)\n",
		0, 0, 0, 0, 0, 0, vUsbCxLoopBackTest, 0, 0, 
		{
			{IS_NUM | IS_USER_SYMBOL, NULL, 0, FALSE, "Cx loop back test\n"}, 
		},
	},
	{{"PamTest", "pam", NULL,}, "PamTest/pam (Pam loop back test) - 0: FIFO0; 1:FIFO1; 2:FIFO2\n",
		0, 0, 1, 1, 0, 0, PamLoopBackTest, 0, 0, 
		{
			{IS_NUM | IS_USER_SYMBOL, NULL, 0, FALSE, "Pam loop back test - 0: FIFO0; 1:FIFO1; 2:FIFO2\n"}, 
		},
	},
#if(OTG_AP_Satus == Bulk_AP)
	{{"DumpBlkBuf", "dbb", NULL,}, "DumpBlkBuf/dbb (dump bulk test buffer) - page number(decimal)\n",
		0, 0, 1, 1, 0, 0, DumpBulkBufData, 0, 0, 
		{
			{IS_NUM | IS_USER_SYMBOL, NULL, 0, FALSE, "dump bulk test buffer - page number(decimal,1 page=512 bytes)\n"}, 
		},
	},
#endif
		
	{NULL,
	},
};

#if(OTG_AP_Satus == Bulk_AP)
///////////////////////////////////////////////////////////////////////////////
//		DumpBulkBufData()
//		Description:
//		input: CommandTableStruct_T
//		output: OK_COMMAND
///////////////////////////////////////////////////////////////////////////////
UINT32 DumpBulkBufData(CommandTableStruct_T *CmdTable)
{
	INT32U u32PageNumber =  CmdTable->Argu[0].Value;
	INT32U u32BufIndex =  u32PageNumber*512,i,u32PtrLnCnt=16;
	INT32U u32PageBound =  u32BufIndex+512;
	printf("Dump Bulk buffer data - page %d, Start Mem addr 0x%x, Buffer Idx 0x%x\n", 
		u32PageNumber, (INT32U)&u8VirtOTGMemory[u32BufIndex], u32BufIndex);

	for(u32BufIndex=u32BufIndex;u32BufIndex<u32PageBound;u32BufIndex+=u32PtrLnCnt)
	{
		printf("0x%x-0x%05x:",(INT32U)&u8VirtOTGMemory[u32BufIndex],u32BufIndex);
		for(i=0;i<u32PtrLnCnt;i++)
		{
			printf("%02x ",u8VirtOTGMemory[u32BufIndex+i]);
		}
		printf("\n");
	}
	
	printf("Mass storage state = %d\n",eUsbOTGMassStorageState);		
	printf("0:IDLE ; 1:STATE_CBW ; 2:STATE_CB_DATA_IN \n");
	printf("3:STATE_CB_DATA_OUT ; 4:STATE_CSW\n");
	
	return OK_COMMAND;
}
#endif

///////////////////////////////////////////////////////////////////////////////
//		vOTGSysInit()
//		Description:
//			1. Initial variables for USB test.
//			2. Initial USB Cx variables and FUSB220 registers.
//			3. Initial AHB and APB DMA.
//		input: none
//		output: none
///////////////////////////////////////////////////////////////////////////////
void vOTGSysInit(void)
{
	// Set message level.
	u8OTGMessageLevel = MESS_ERROR;
	//For OPT;;vOTG_APInit();
	vOTGInit();
}

///////////////////////////////////////////////////////////////////////////////
//		PamLoopBackTest()
//		Description:
//		input: CommandTableStruct_T
//		output: OK_COMMAND
///////////////////////////////////////////////////////////////////////////////
UINT32 PamLoopBackTest(CommandTableStruct_T *CmdTable)
{
	INT32U u32TestFIFO =  CmdTable->Argu[0].Value;
	vUsbPamLoopBackTest(u32TestFIFO);

	return OK_COMMAND;
}




//============================================================================= 
//		OTGP_main()
//		Description: Leave when the VBus is not valid 
//			
//		input: bDoNotInit   =0: Init
//                          =1: Do not init
//             bWaitForVBUS =0: Do not wait for VBUS
//                          =1: wait for VBUS 
//		output: 
//=============================================================================
void OTGP_main(UINT8 bDoNotInit,UINT8 bWaitForVBUS,UINT8 bExitMode)
{
	UINT8  bFlagLeave,bPrintDeviceMessage;
	char Buf[80],u8Tmp;
	int i;
	UINT32 status;
	//static UINT8 u8flibCmdInsert=FALSE;
	static UINT8 u8flibCmdInsert=TRUE;

#if DCacheEnable	
	fLib_EnableDCache();
#else
	// Disable Data Cache
	CPUCleanInvalidateDCacheAll();
	fLib_DisableDCache(); 		
#endif

	//printf(">>> Enter Peripheral Mode...\n");
	bForceRemoteWakeUp=0;
	
	//<1>.Waiting for the VBus high
	if(bWaitForVBUS>0)
	while(mdwOTGC_Control_A_VBUS_VLD_Rd()==0);     

	//<2>.Enable the Peripheral Interrupt
	if (bDoNotInit==1) {
		OTGP_init(1);//0:Do not init AP
	}
	else {
#if((OTG_AP_Satus == Bulk_AP)||(OTG_AP_Satus == Interrupt_AP))
		vOTG_Interrupt_Initial();
#elif((OTG_AP_Satus == IsochronousIN_AP)||(OTG_AP_Satus == IsochronousOUT_AP))
		vOTG_ISO_Initial();
#endif
		//vOTG_APInit();
		//OTGP_init(0);//0:Do not init AP
	}

	//<3>.Waiting for the VBUS is not valid
	//04192005;;Bruce;;fLib_EnableInt(FIQ_OTG);//(FIQ_MAC);
	fLib_UnmaskInt(FIQ_OTG);  //Enable Interrupt//04192005;;Bruce;;
	bFlagLeave=0;

	//<4>.Turn on D+
	mUsbUnPLGClr();//unplug issue;;
	
	// Initial FOTG200 device Reg
    //	mUsbIntSuspDis();
    //	mUsbTstHalfSpeedEn();		// Set for FPGA Testing, if AHB>30Mhz, doesn't need to set.
    //	mUsbChipEnSet();			// Set for PAM enable
    //	mUsbUnPLGClr();
    // 	vOTGSysInit();	

#if 0
	while(1)
	   mUsbDMA2FIFOSel((BIT0<<i));
#endif
	
	if(u8flibCmdInsert==FALSE) {
		// Initial command table
		fLib_InsertCommand(USBDevice_CmdTable);
		fLib_InitUserSymbol(NULL);
		u8flibCmdInsert=TRUE;
	}

    bPrintDeviceMessage=0;
	do
	{
	   if (bPrintDeviceMessage>0) {
#if(OTG_AP_Satus == Bulk_AP)
   	#if(Bulk_Satus == Bulk_FIFO_SingleDir)
     	printf("L%x: FOTG200 Device Bulk Single direction FIFO transfer test...\n", u8LineOTGCount ++);
   	#else
     	printf("L%x: FOTG200 Device Bulk Bi-direction FIFO transfer test...\n", u8LineOTGCount ++);
   	#endif
#elif(OTG_AP_Satus == Interrupt_AP)
   	    printf("L%x: FOTG200 Device Interrupt transfer test...\n", u8LineOTGCount ++);	
#elif(OTG_AP_Satus == IsochronousIN_AP)
     	printf("L%x: FOTG200 Device Isochronous IN transfer test...\n", u8LineOTGCount ++);
	   	printf("L%x: Packet content wrap = %d\n", u8LineOTGCount ++,ISO_Wrap);
#elif(OTG_AP_Satus == IsochronousOUT_AP)
     	printf("L%x: FOTG200 Device Isochronous OUT transfer test...\n", u8LineOTGCount ++);
     	printf("L%x: Packet content wrap = %d\n", u8LineOTGCount ++,ISO_Wrap);
#endif	     	
	   	printf("USB Device>>>\n");
	   }
	  
	   bPrintDeviceMessage=1;
  
	   memset(Buf, 0x00, 80);
	   for (i = 0; i<80; i++) {
	      do {
	         if (OTG_interrupt_level1 != 0) {
		        vOTGHandler();
			    //04192005;;Bruce;;fLib_EnableInt(FIQ_OTG);	
			    fLib_UnmaskInt(FIQ_OTG);  //Enable Interrupt//04192005;;Bruce;;				
			 }
			 if (bExitMode==0) {
		     //  if (mdwOTGC_Control_A_VBUS_VLD_Rd()==0) //Turn off the VBUS
    	        if (mdwOTGC_Control_B_SESS_VLD_Rd()==0) //Turn off theVBUS => Use B_SESS_VLD
				{
  		           // printf(">>> VBUS Turn OFF(mdwOTGC_Control_B_SESS_VLD_Rd==0)==>X1\n"); 
  		           flib_Host20_TimerEnable(1); 
  		           if (mdwOTGC_Control_B_SESS_VLD_Rd()==0)
  		              bFlagLeave=1; 
				}
			 }
			 if (OTG.CurrentRole==0) { //Current Role => Change to Host
			    bFlagLeave=2;
			 }
			 status=inw(DebugSerialPort+SERIAL_LSR);
		  }while ((!((status & SERIAL_LSR_DR)==SERIAL_LSR_DR))&&(bFlagLeave==0 ));	// wait until Rx ready

	      if (bFlagLeave==0 ) {
		     u8Tmp = inw(DebugSerialPort + SERIAL_RBR); 
		     fLib_DebugPrintChar(u8Tmp);
		     if (u8Tmp == 0xD)
			    break;
		     else
		 	    Buf[i] = u8Tmp;			
		  }
	   }
	   if (bFlagLeave==0 )
	      fLib_ExecuteCommand(Buf);
	} while(bFlagLeave==0 );

	//<4>.Close the Peripheral function
	OTGP_Close();

	//04192005;;Bruce;;fLib_EnableInt(FIQ_OTG);//bruce;;04182005;;
	fLib_UnmaskInt(FIQ_OTG);  //Enable Interrupt//04192005;;Bruce;;
	
	if (bFlagLeave==1)
		printf(">>> VBUS Turn OFF(mdwOTGC_Control_B_SESS_VLD_Rd==0)..\n"); 
	
	fLib_EnableDCache();
}

//============================================================================= ok
//		OTGP_init()
//		Description:
//		input: Reserved
//		output: Reserved
//=============================================================================
void OTGP_init(UINT8 bInitAP)
{
	// Initial Interrupt and ISO transfer buffer
	if (bInitAP==1)
	{
		#if((OTG_AP_Satus == Bulk_AP)||(OTG_AP_Satus == Interrupt_AP))
		vOTG_Interrupt_Initial();
		#elif((OTG_AP_Satus == IsochronousIN_AP)||(OTG_AP_Satus == IsochronousOUT_AP))
		vOTG_ISO_Initial();
		#endif
		vOTG_APInit();
	}

	// Initial global variable
	bOTGDMARunning = FALSE;
	u8LineOTGCount = 1;

	// Initial FUSB220 Reg
	vOTGSysInit();	
}
//============================================================================= ok
//		OTGP_Close()
//		Description:
//		input: Reserved
//		output: Reserved
//=============================================================================
void OTGP_Close(void)
{
	UINT32 wTemp;

	//<1>.Clear All the Interrupt
	mUsbGlobIntDis();

	//<2>.Clear all the Interrupt Status
	wTemp=mUsbIntGroupRegRd();
	mUsbIntGroupRegSet(0);
	
	//Interrupt source group 0(0x144)
	wTemp=mUsbIntSrc0Rd();
	mUsbIntSrc0Set(0);          
	
	//Interrupt source group 1(0x148)          
	wTemp=mUsbIntSrc1Rd();
	mUsbIntSrc1Set(0);          
	
	//Interrupt source group 2(0x14C)          
	wTemp=mUsbIntSrc2Rd();
	mUsbIntSrc2Set(0);    

   //<3>.Turn off D+  
   if (mdwOTGC_Control_CROLE_Rd()>0)//For Current Role = Peripheral
         mUsbUnPLGSet();
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -