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

📄 main.c

📁 基于usb 芯片net2272的一个loopback的程序
💻 C
📖 第 1 页 / 共 2 页
字号:


	// setup PLL, SDRAM, ASYNC memory, LEDs
	ezConfigurePLL();
	ezConfigureSDRAM();
	ezConfigureAsync();
	ezInitLEDFlags();	// NET2272 also uses flags, so make sure we init
						// here before opening up NET2272, otherwise USB
						// might not work if we call this after the NET2272
						// has already setup flags

	// initialize the buffer
	UsbcbBuffer.Data = &refCmd;
	UsbcbBuffer.ElementCount = sizeof(refCmd);
	UsbcbBuffer.ElementWidth = 1;
	UsbcbBuffer.CallbackParameter = NULL;
	UsbcbBuffer.ProcessedFlag = FALSE;
	UsbcbBuffer.ProcessedElementCount = 0;
	UsbcbBuffer.pNext = NULL;

	// initialize interrupt manager
	Result = adi_int_Init(	IntMgrData,			// ptr to memory for use by IntMgr
							sizeof(IntMgrData),	// size of memory for use by IntMgr
							&ResponseCount,		// response count
							NULL);				// NULL
	if (Result != ADI_INT_RESULT_SUCCESS)
		failure();

	// initialize DMA
	Result = adi_dma_Init(	DMAMgrData,				// ptr to memory for use by DmaMgr
							sizeof(DMAMgrData),		// size of memory for use by DmaMgr
							&ResponseCount,			// response count
							&DMAHandle,				// ptr to DMA handle
							NULL);					// NULL
	if (Result != ADI_DMA_RESULT_SUCCESS)
		failure();

	// hook the exception interrupt
	if(adi_int_CECHook(3, ExceptionHandler, NULL, FALSE) != ADI_INT_RESULT_SUCCESS)
		failure();

	// hook the hardware error
	if(adi_int_CECHook(5, HWErrorHandler, NULL, FALSE) != ADI_INT_RESULT_SUCCESS)
		failure();

	// initialize device manager
	Result = adi_dev_Init(	DevMgrData,				// ptr to memory for use by DevMgr
							sizeof(DevMgrData),		// size of memory for use by DevMgr
							&ResponseCount,			// returns number of devices DevMgr can support
							&DeviceManagerHandle,	// ptr to DevMgr handle
							&CriticalRegionData);	// ptr to critical region info

	if (Result != ADI_DEV_RESULT_SUCCESS)
		failure();

	// open the NET2272
	Result = adi_dev_Open(	DeviceManagerHandle,			// DevMgr handle
							&ADINET2272EntryPoint,			// pdd entry point
							0,								// device instance
							(void*)0x2272,					// client handle callback identifier
							&DevHandle,						// DevMgr handle for this device
							ADI_DEV_DIRECTION_BIDIRECTIONAL,// data direction for this device
							DMAHandle,						// handle to DmaMgr for this device
							NULL,							// handle to deferred callback service
							ClientCallback);				// client's callback function
	if (Result != ADI_DEV_RESULT_SUCCESS)
		failure();

	// configure the NET2272 mode
	Result = adi_dev_Control(DevHandle, ADI_DEV_CMD_SET_DATAFLOW_METHOD, (void*)ADI_DEV_MODE_CHAINED);
	if (Result != ADI_DEV_RESULT_SUCCESS)
		failure();

	// enable data flow
	Result = adi_dev_Control(DevHandle, ADI_DEV_CMD_SET_DATAFLOW, (void*)TRUE);
	if (Result != ADI_DEV_RESULT_SUCCESS)
		failure();
		
	while(1)
	{ 
	    if(adi_dev_Read(DevHandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&refCmd) != ADI_DEV_RESULT_SUCCESS)		// receive command
			continue;

		cmd.type = TYPE_HANDSHAKE_COMMAND;

		cmd.cmdParam.hdshk = COM_ACKNOWLEDGE;
		
		UsbcbBuffer.Data = &cmd;

		while(adi_dev_Write(DevHandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&cmd) != ADI_DEV_RESULT_SUCCESS);			// send acknowledgemen
		
		UsbcbBuffer.Data = my_buf;
		
		UsbcbBuffer.ElementCount = refCmd.cmdParam.data_len;

		while(adi_dev_Read(DevHandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)my_buf));							// receive data
	}

#if 0
	// while we want to keep running (for debugging only, no one clears this flag)
	while( bKeepRunning )
	{
		// wait until the USB is configured by the host before continuing
		while( !g_bUsbConfigured )
			;

		// wait for a USB command block from the host indicating what function we should perform
		Result = usb_Read(DevHandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer, TRUE);

		if ( ADI_DEV_RESULT_SUCCESS == Result )
		{
			// switch on the command we just received
			switch( pusbcb->ulCommand )
		    {
				// host is asking if we support a given command
				case QUERY_SUPPORT:
					Result = QuerySupport( DevHandle, pusbcb->ulData );
		    		break;

		    	// get the firmware version
			    case GET_FW_VERSION:
			    	Result = ReadMemory( DevHandle, (u8*)FwVersionInfo, pusbcb->ulCount );
			        break;

				// run loopback
			    case LOOPBACK:
					Result = Loopback( DevHandle, pusbcb->ulCount );
			        break;

				// read memory
			    case MEMORY_READ:
					Result = ReadMemory( DevHandle, (u8*)pusbcb->ulData, pusbcb->ulCount );
			        break;

				// write memory
			    case MEMORY_WRITE:
					Result = WriteMemory( DevHandle, (u8*)pusbcb->ulData, pusbcb->ulCount );
			        break;

				// unsupported command
			    default:
			   		failure();
			        break;
	    	}
		}
	}

#endif
	// disable data flow
	Result = adi_dev_Control(DevHandle, ADI_DEV_CMD_SET_DATAFLOW, (void*)FALSE);

	// close the device
	Result = adi_dev_Close(DevHandle);
	if (Result != ADI_DEV_RESULT_SUCCESS)
		failure();
}


/*********************************************************************

	Function:		usb_Read

	Description:	Wrapper for adi_dev_Read() which lets the user specify if
					they want to wait for completion or not.

	Arguments:		ADI_DEV_DEVICE_HANDLE devhandle -	device handle
					ADI_DEV_BUFFER_TYPE BufferType -	buffer type
					ADI_DEV_BUFFER *pBuffer -			pointer to buffer
					bool bWaitForCompletion -			completion flag

	Return value:	u32 - return status

*********************************************************************/

u32 usb_Read(	ADI_DEV_DEVICE_HANDLE 	DeviceHandle,			// device handle
				ADI_DEV_BUFFER_TYPE		BufferType,				// buffer type
				ADI_DEV_BUFFER 			*pBuffer,				// pointer to buffer
				bool					bWaitForCompletion)		// completion flag
{
    u32 Result = ADI_DEV_RESULT_SUCCESS;

 	// if the user wants to wait until the operation is complete
    if (bWaitForCompletion)
    {
        // clear the rx flag and call read
   		g_bRxFlag = FALSE;
		Result = adi_dev_Read(DeviceHandle, BufferType, pBuffer);

		// wait for the rx flag to be set
		while (!g_bRxFlag)
		{
		    // make sure we are still configured, if not we should fail
			if ( !g_bUsbConfigured )
				return ADI_DEV_RESULT_FAILED;
		}

		return Result;
    }

    // else they do not want to wait for completion
    else
    {
        return (adi_dev_Read(DeviceHandle, BufferType, pBuffer));
    }
}


/*********************************************************************

	Function:		usb_Write

	Description:	Wrapper for adi_dev_Write() which lets the user specify if
					they want to wait for completion or not.

	Arguments:		ADI_DEV_DEVICE_HANDLE devhandle -	device handle
					ADI_DEV_BUFFER_TYPE BufferType -	buffer type
					ADI_DEV_BUFFER *pBuffer -			pointer to buffer
					bool bWaitForCompletion -			completion flag

	Return value:	u32 - return status

*********************************************************************/

u32 usb_Write(	ADI_DEV_DEVICE_HANDLE 	DeviceHandle,			// DM handle
				ADI_DEV_BUFFER_TYPE		BufferType,				// buffer type
				ADI_DEV_BUFFER			*pBuffer,				// pointer to buffer
				bool					bWaitForCompletion)		// completion flag
{
    u32 Result = ADI_DEV_RESULT_SUCCESS;

 	// if the user wants to wait until the operation is complete
    if (bWaitForCompletion)
    {
        // clear the tx flag and call read
   		g_bTxFlag = FALSE;
		Result = adi_dev_Write(DeviceHandle, BufferType, pBuffer);

		// wait for the tx flag to be set
		while (!g_bTxFlag)
		{
		   // make sure we are still configured, if not we should fail
			if ( !g_bUsbConfigured )
				return ADI_DEV_RESULT_FAILED;
		}

		return Result;
    }

    // else they do not want to wait for completion
    else
    {
        return (adi_dev_Write(DeviceHandle, BufferType, pBuffer));
    }
}

⌨️ 快捷键说明

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