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

📄 usb_bf535.c

📁 基于ADSP-BF535 USB驱动代码
💻 C
字号:
#include <ccblkfn.h>
#include "usb.h"
#include "usb100.h"
#include "register.h"
#include "usb_BF535cmd.h"

#define TRUE 1
#define FALSE 0

void ProcessHostCommand(void);

static __inline void flush(volatile int * c) { asm ("flushinv [%0];" :: "p" (c)); }

extern int UsbConfiguredByDsp;
extern int UsbConfiguredByHost;

section("usb_dma") char UsbDataBuffer[1024*4];
section("usb_io") volatile USB_COMMAND_BLOCK UsbCmdBlock[1];


///////////////////////////////////////////////////////////////////////////////
// main - main function for the UsbBF535 application.
//
///////////////////////////////////////////////////////////////////////////////

main()
{
	bool bRunning = TRUE;			// flag which indicates to keep running
	static USHORT ep2count = 0;		// EP2 counter
	static USHORT ep6count = 0;		// EP6 counter
	
//	// enable just the IVG15 and USB interrupts
//	sti(0x404);
	
	// init the USB interface
	InitUsbInterface();
	
	// set the flag, the DSP has configured USB
	UsbConfiguredByDsp = TRUE;
	
	// wait here for the host to initialize the USB
	while ( UsbConfiguredByHost != TRUE );
		
	// arm the endpoints we use first
	
	//////////////////////////////////////////////
	// setup ENDPOINT 6 (DSP -> HOST)
	//////////////////////////////////////////////
	USBD_EPCFG6_REG =	USBD_MAX(USBD_MAX_64)	|						// maximum packet size = 64 bytes
						USBD_TYP(USBD_TYP_BULK)	|						// endpoint type = BULK
						USBD_EPCFG_DIR;									// transfer direction = IN (1)
	USBD_EPADR6_REG = (USHORT)((ULONG)UsbCmdBlock & USBD_OFFSET_SGMSK);	// buffer address
	USBD_EPLEN6_REG = (USHORT)sizeof(UsbCmdBlock);						// byte count					
	USBD_EPCFG6_REG |= USBD_EPCFG_ARM;									// arm the endpoint

	//////////////////////////////////////////////
	// setup ENDPOINT 2 (HOST -> DSP)
	//////////////////////////////////////////////
	USBD_EPCFG2_REG =	USBD_MAX(USBD_MAX_64)	|						// maximum packet size = 64 bytes
						USBD_TYP(USBD_TYP_BULK);						// endpoint type = BULK
																		// transfer direction = OUT (0)
	USBD_EPADR2_REG = (USHORT)((ULONG)UsbCmdBlock & USBD_OFFSET_SGMSK);	// buffer address
	USBD_EPLEN2_REG = (USHORT)sizeof(UsbCmdBlock);						// byte count
	USBD_EPCFG2_REG |= USBD_EPCFG_ARM;									// arm the endpoint

	
	// loop forever, but put this flag in here so we can jump out when we want to
	while( bRunning )
	{
		asm("ssync;");
		
		// poll the IN and OUT endpoint interrupt registers to see if the host is
		// sending or requesting data
	
		// EP2: see if we have received data (HOST->DSP)
		if ( (USHORT)(USBD_INTR2_REG & USBD_INTR_BCSTAT) )
		{
			// clear pending interrupt
			USBD_INTR2_REG = USBD_INTR_BCSTAT;
			ProcessHostCommand();
			ep2count++;
		}
		
		// EP6: see if we are sending data (DSP->HOST)
		if ( (USHORT)(USBD_INTR6_REG & USBD_INTR_BCSTAT) )
		{
			// clear pending interrupt
			USBD_INTR6_REG = USBD_INTR_BCSTAT;
			ProcessHostCommand();
			ep6count++;
		}
	}
	
	exit(0);
}


///////////////////////////////////////////////////////////////////////////////
// ProcessHostCommand - Called when we have received a USB command and we
// need to setup the endpoints.
//
///////////////////////////////////////////////////////////////////////////////

void ProcessHostCommand(void)
{  
	static unsigned char *srcptr, *destptr;	// pointers
	static int	nBytesRead;					// total bytes read counter
	static int	nTotalBytesToRead;			// total bytes to read
	static int	nBytesWritten;				// total bytes written counter
	static int	nTotalBytesToWrite;			// total bytes to write

	flush((volatile int *)&UsbCmdBlock->bCommand); 
	
	switch (UsbCmdBlock->bCommand)
	{
		case HOST_REQUEST_RX_DATA:
			
			// host wants to receive some data
			
			UsbCmdBlock->bCommand = HOST_RX_DATA;
			srcptr = UsbCmdBlock->buffer;
			nBytesWritten = 0;
			
			// get the total bytes to send
			flush((volatile int *)&UsbCmdBlock->count); 		
			nTotalBytesToWrite = UsbCmdBlock->count;
			
			// get the number of bytes to send in this transmission
			UsbCmdBlock->count = (nTotalBytesToWrite - nBytesWritten > MAX_USB_EP_DATA ) ? MAX_USB_EP_DATA : (nTotalBytesToWrite - nBytesWritten); 
			
			// add this to the number we have written so far																
			nBytesWritten += UsbCmdBlock->count;
			
			// setup IN for data
			USBD_DMABL_REG = (USHORT)((ULONG)srcptr & USBD_DMABL_SGMSK);
			USBD_DMABH_REG = (USHORT)((ULONG)srcptr >> 16);
			USBD_EPCFG6_REG = USBD_MAX(USBD_MAX_64) | USBD_TYP(USBD_TYP_BULK) | USBD_EPCFG_DIR;
			USBD_EPADR6_REG = (USHORT)((ULONG)srcptr & USBD_OFFSET_SGMSK);
			USBD_EPLEN6_REG = (USHORT)UsbCmdBlock->count;
			USBD_EPCFG6_REG |= USBD_EPCFG_ARM;
			
			// bump the pointer for next time through
			srcptr += UsbCmdBlock->count;
			
			// setup OUT for command
			USBD_EPCFG2_REG = USBD_MAX(USBD_MAX_64) | USBD_TYP(USBD_TYP_BULK);
			USBD_EPADR2_REG = (USHORT)((ULONG)UsbCmdBlock & USBD_OFFSET_SGMSK);
			USBD_EPLEN2_REG = (USHORT)sizeof(UsbCmdBlock);
			USBD_EPCFG2_REG |= USBD_EPCFG_ARM;
								
			break;
			
		case HOST_RX_DATA:
		
			// host wants to receive some data
				
			// if there is more data to send
			if (nBytesWritten < nTotalBytesToWrite)
			{
				// get the number of bytes to send in this transmission
				UsbCmdBlock->count = (nTotalBytesToWrite - nBytesWritten > MAX_USB_EP_DATA ) ? MAX_USB_EP_DATA : (nTotalBytesToWrite - nBytesWritten);
				
				// add this to the number we have written so far
				nBytesWritten += UsbCmdBlock->count;

				// setup IN for data
				USBD_DMABL_REG = (USHORT)((ULONG)srcptr & USBD_DMABL_SGMSK);
				USBD_DMABH_REG = (USHORT)((ULONG)srcptr >> 16);
				USBD_EPCFG6_REG = USBD_MAX(USBD_MAX_64) | USBD_TYP(USBD_TYP_BULK) | USBD_EPCFG_DIR;
				USBD_EPADR6_REG = (USHORT)((ULONG)srcptr & USBD_OFFSET_SGMSK);
				USBD_EPLEN6_REG = (USHORT)UsbCmdBlock->count;
				USBD_EPCFG6_REG |= USBD_EPCFG_ARM;
				
				// bump the pointer for next time through
				srcptr += UsbCmdBlock->count;
			}
			
			// else there is no more data to send
			else
			{								
				// setup OUT for command, we should only have to setup the
				// DMABx registers here since OUT should already be setup
				// but it won't hurt to play it safe
				USBD_DMABL_REG = (USHORT)((ULONG)UsbCmdBlock & USBD_DMABL_SGMSK);
				USBD_DMABH_REG = (USHORT)((ULONG)UsbCmdBlock >> 16);
				USBD_EPCFG2_REG = USBD_MAX(USBD_MAX_64) | USBD_TYP(USBD_TYP_BULK);
				USBD_EPADR2_REG = (USHORT)((ULONG)UsbCmdBlock & USBD_OFFSET_SGMSK);
				USBD_EPLEN2_REG = (USHORT)sizeof(UsbCmdBlock);
				USBD_EPCFG2_REG |= USBD_EPCFG_ARM;
			}
			
			break;
	
		case HOST_REQUEST_TX_DATA:
		
			// host wants to send some data
			
			UsbCmdBlock->bCommand = HOST_TX_DATA;			
			destptr=UsbCmdBlock->buffer; 
			nBytesRead = 0;
			
			// get the total bytes to receive
			flush((volatile int *)&UsbCmdBlock->count); 
			nTotalBytesToRead=UsbCmdBlock->count;

			// get the number of bytes to receive in this transmission
			UsbCmdBlock->count = (nTotalBytesToRead - nBytesRead > MAX_USB_EP_DATA ) ? MAX_USB_EP_DATA : (nTotalBytesToRead - nBytesRead); 

			// add this to the number we have read so far												
			nBytesRead += UsbCmdBlock->count;
				
			// setup OUT for data
			USBD_DMABL_REG = (USHORT)((ULONG)destptr & USBD_DMABL_SGMSK);
			USBD_DMABH_REG = (USHORT)((ULONG)destptr >> 16);
			USBD_EPCFG2_REG = USBD_MAX(USBD_MAX_64) | USBD_TYP(USBD_TYP_BULK);
			USBD_EPADR2_REG = (USHORT)((ULONG)destptr & USBD_OFFSET_SGMSK);
			USBD_EPLEN2_REG = (USHORT)UsbCmdBlock->count;
			USBD_EPCFG2_REG |= USBD_EPCFG_ARM;
			
			// bump the pointer for next time through
			destptr += UsbCmdBlock->count;		
			
			break;
	
		case HOST_TX_DATA:
		
			// host wants to send some data

			// if there is more data to receive
			if (nBytesRead < nTotalBytesToRead)
			{
				// get the number of bytes to receive in this transmission
				UsbCmdBlock->count = (nTotalBytesToRead - nBytesRead > MAX_USB_EP_DATA ) ? MAX_USB_EP_DATA : (nTotalBytesToRead - nBytesRead);
				
				// add this to the number we have read so far
				nBytesRead += UsbCmdBlock->count;

				// setup OUT for data
				USBD_DMABL_REG = (USHORT)((ULONG)destptr & USBD_DMABL_SGMSK);
				USBD_DMABH_REG = (USHORT)((ULONG)destptr >> 16);	
				USBD_EPCFG2_REG = USBD_MAX(USBD_MAX_64) | USBD_TYP(USBD_TYP_BULK);
				USBD_EPADR2_REG = (USHORT)((ULONG)destptr & USBD_OFFSET_SGMSK);
				USBD_EPLEN2_REG = (USHORT)UsbCmdBlock->count;
				USBD_EPCFG2_REG |= USBD_EPCFG_ARM;
				
				// bump the pointer for next time through
				destptr += UsbCmdBlock->count;	
			}
			
			// else there is no more data to receive
			else
			{
				// setup OUT for command
				USBD_DMABL_REG = (USHORT)((ULONG)UsbCmdBlock & USBD_DMABL_SGMSK);
				USBD_DMABH_REG = (USHORT)((ULONG)UsbCmdBlock >> 16);
				USBD_EPCFG2_REG = USBD_MAX(USBD_MAX_64) | USBD_TYP(USBD_TYP_BULK);
				USBD_EPADR2_REG = (USHORT)((ULONG)UsbCmdBlock & USBD_OFFSET_SGMSK);
				USBD_EPLEN2_REG = (USHORT)sizeof(UsbCmdBlock);
				USBD_EPCFG2_REG |= USBD_EPCFG_ARM;		
			}
				
			break;
							
		default:
			break;
	}

	return;
}

⌨️ 快捷键说明

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