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

📄 main.c

📁 usbio example. bulk transfer. platform bf533+usblan card.
💻 C
字号:
/*********************************************************************************

Copyright(c) 2005 Analog Devices, Inc. All Rights Reserved.

This software is proprietary and confidential.  By using this software you agree
to the terms of the associated Analog Devices License Agreement.

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


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

Include files

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

#include <services/services.h>		// system service includes
#include <drivers/adi_dev.h>		// device manager includes
#include <adi_net2272.h>			// NET2272 device driver includes

#include "ezkitutilities.h"			// EZ-Kit includes
#include "plx/NcCommon.H"			// Netchip common includes
#include "usbcmd.h"					// USB commands

// firmware version string info
#pragma align 4
char FwVersionInfo[NUM_VERSION_STRINGS][MAX_VERSION_STRING_LEN] = {
																	__DATE__,		// build date
																	__TIME__,		// build time
																	"01.00.01",		// version number
#ifdef __ADSPBF533__
																	"ADSP-BF533",	// target processor
#elif defined(__ADSPBF537__)
																	"ADSP-BF537",	// target processor
#elif defined(__ADSPBF561__)
																	"ADSP-BF561",	// target processor
#else
#error *** Processor not supported ***
#endif
																	"usbio"};		// application name

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

Prototypes

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

unsigned int QuerySupport( ADI_DEV_DEVICE_HANDLE devhandle, u32 u32Command );
unsigned int PerformIo( ADI_DEV_DEVICE_HANDLE dh );
unsigned int ReadMemory( ADI_DEV_DEVICE_HANDLE devhandle, u8 *p8Address, u32 u32Count );
unsigned int WriteMemory( ADI_DEV_DEVICE_HANDLE devhandle, u8 *p8Address, u32 u32Count );

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

Static data

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

static ADI_DEV_DEVICE_HANDLE DevHandle;										// device handle
static ADI_DEV_1D_BUFFER UsbcbBuffer;										// one-dimensional buffer for processing usbcb
static u8 IntMgrData[                      (ADI_INT_SECONDARY_MEMORY * 0)];	// storage for interrupt manager
static u8 DMAMgrData[ADI_DMA_BASE_MEMORY + (ADI_DMA_CHANNEL_MEMORY * 2)];	// storage for DMA manager
static u8 DevMgrData[ADI_DEV_BASE_MEMORY + (ADI_DEV_DEVICE_MEMORY * 1)];	// storage for device manager
static ADI_INT_CRITICAL_REGION_DATA	CriticalRegionData;						// storage for critical region


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

	Function:		failure

	Description:	In case of failure we toggle LEDs.

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

void failure()
{
	volatile unsigned int v = 0;	// counter

	while (1)
	{
		ezTurnOnAllLEDs();

		// delay...leave LEDs on
    	for ( v = 0; v < 0x01ffffff; v++)
			;

		ezTurnOffAllLEDs();

		// delay...leave LEDs off
    	for ( v = 0; v < 0x01ffffff; v++)
			;
	}
}


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

	Function:		ExceptionHandler
					HWErrorHandler

	Description:	We should never get an exception or hardware error,
					but just in case we'll catch them and simply turn
					on all the LEDS should one ever occur.

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

static ADI_INT_HANDLER(ExceptionHandler)	// exception handler
{
	failure();
	return(ADI_INT_RESULT_PROCESSED);
}

static ADI_INT_HANDLER(HWErrorHandler)		// hardware error handler
{
	failure();
	return(ADI_INT_RESULT_PROCESSED);
}

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

	Function:		ClientCallback

	Description:	Each type of callback event has it's own unique ID
					so we can use a single callback function for all
					callback events.  The switch statement tells us
					which event has occurred.

					In the example, we'll get a callback when the NET2272
					has completed processing of the buffer.  We don't
					really need the callback function as the driver
					uses the ProcessedFlag field of the buffer to
					determine when the buffer has completed processing,
					but it's included here for illustrative purposes.

					Note that in the device driver model, in order to
					generate a callback for buffer completion, the
					CallbackParameter of the buffer must be set to a non-NULL
					value.  That non-NULL value is then passed to the
					callback function as the pArg parameter.  In the example,
					the CallbackParameter is set to be the address of the
					buffer itself.  That allows	the callback function to
					determine which buffer was processed.  Often the
					CallbackParameter value is set to NULL for every buffer
					except the last buffer in a chain. That technique causes
					the callback function to get called only when the last
					buffer in the chain has been processed.

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

static void ClientCallback(
	void *AppHandle,
	u32  Event,
	void *pArg)
{

	static unsigned int Counter = 0;	// count the number of buffers processed
	ADI_DEV_BUFFER *pBuffer;			// pointer to the buffer that was processed

	switch (Event)
	{
		// CASE (buffer processed)
		case ADI_DEV_EVENT_BUFFER_PROCESSED:

			// point to the buffer
			pBuffer = (ADI_DEV_BUFFER *)pArg;

			// increment our counter
			Counter++;
			break;

		// CASE (an error)
		case ADI_DEV_EVENT_DMA_ERROR_INTERRUPT:

			// turn on all LEDs and wait for help
			ezTurnOnAllLEDs();
			while (1) ;
	}

	// return
}


/*********************************************************************
*
*	Function:	main
*
*********************************************************************/

void main(void)
{
	unsigned int Result;							// result
	unsigned int i;									// index
	u32	ResponseCount;								// response count
	u32 SecondaryCount;								// secondary count
	ADI_DEV_MANAGER_HANDLE DeviceManagerHandle;		// DevMgr handle
	ADI_DMA_MANAGER_HANDLE DMAHandle;				// DMAMgr handle
	bool bKeepRunning = TRUE;						// keep running flag
	USBCB usbcb;									// USB command block
	USBCB *pusbcb = &usbcb;							// pointer to USBCB

	// setup PLL, SDRAM, ASYNC memory, LEDs

	ezConfigurePLL();
	ezConfigureSDRAM();
	ezConfigureAsync();
	ezConfigureFlashA();
	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 = &usbcb;
	UsbcbBuffer.ElementCount = sizeof(usbcb);
	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
							NULL,							// 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 we should keep running
	while( bKeepRunning )
	{
		// wait for a USB command block from the host indicating what function we should perform
		Result = adi_dev_Read(DevHandle, ADI_DEV_1D, (ADI_DEV_BUFFER *)&UsbcbBuffer);

		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;

			    // perform IO over USB
			    case USBIO_START:
					Result = PerformIo( DevHandle );
			        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;
	    	}
		}
	}

	// 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();
}

⌨️ 快捷键说明

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