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

📄 usb_bf535cfg.c

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

#define TRUE 1
#define FALSE 0

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

extern ProcessHostCommand(void);
extern volatile USB_COMMAND_BLOCK UsbCmdBlock[1];

// init configured states to FALSE
int UsbConfiguredByHost = FALSE;
int UsbConfiguredByDsp = FALSE;

EX_INTERRUPT_HANDLER (UsbIsr);

USHORT UsbdGintr;
USHORT UsbdStatus;

///////////////////////////////////////////////////////////////////////////////
// InitUsbInterface - Initializes the USBD registers.
//
///////////////////////////////////////////////////////////////////////////////

InitUsbInterface()
{
	int i, j;
	
	if (USBD_CTRL_REG & USBD_CTRL_ENA) 
	{
		// the USB has been configured on a previous execution
		UsbConfiguredByHost = TRUE;			
	}
		
	else
	{
		// perform hardware reset of interface. 
		USBD_CTRL_REG |=USBD_CTRL_UDCRST;
		asm("ssync;");
		USBD_CTRL_REG &=~USBD_CTRL_UDCRST;

		USBD_GMASK_REG =0xFFFF;		
	
		// enable interface. 
		USBD_CTRL_REG |=USBD_CTRL_ENA;		
		asm("ssync;");
	
		// initialize all 64 EP through the USBD_EPBUF
		for(i=0; i<MAX_EP_BUF; i++)
		{	
			for (j=EP_BUF_SZ-1;j>=0;j--)
			{
				USBD_EPBUF_REG=(UCHAR)EpBuf[i].a_ep_buf[j];	
				while (!(USBD_EPBUF_REG) & USBD_EPBUF_RDY)  asm("ssync;");
			}					
		}	
	}
		
	// install the handler 
	register_handler(ik_ivg7,UsbIsr);

	// unmask SIC MASK 
	SIC_IMASK_REG |= SIC_MASK2;
	
	asm("csync;");

	//=========================================================================
	// setup the USBD_GINTR, USBD_INTRx and DMA USBD_DMACFG
	// interrupt mask !
	//=========================================================================
	// clear all pending interrupts
	USBD_GINTR_REG = (USHORT)0xFFFF;

	// unmask EP0
	USBD_GMASK_REG = 0xFFFF & (USHORT)~(USBD_GMASK_EP0MSK|USBD_GMASK_CFGM);

	// clear interrupt for all the EPs
	USBD_INTR0_REG |=(USHORT)(USBD_INTR_TC|USBD_INTR_PC|USBD_INTR_BCSTAT|
						   USBD_INTR_SETUP|USBD_INTR_MSETUP|USBD_INTR_MERR);
	USBD_INTR1_REG |=(USHORT)(USBD_INTR_TC|USBD_INTR_PC|USBD_INTR_BCSTAT|USBD_INTR_MERR);
	USBD_INTR2_REG |=(USHORT)(USBD_INTR_TC|USBD_INTR_PC|USBD_INTR_BCSTAT|USBD_INTR_MERR);
	USBD_INTR3_REG |=(USHORT)(USBD_INTR_TC|USBD_INTR_PC|USBD_INTR_BCSTAT|USBD_INTR_MERR);
	USBD_INTR4_REG |=(USHORT)(USBD_INTR_TC|USBD_INTR_PC|USBD_INTR_BCSTAT|USBD_INTR_MERR);
	USBD_INTR5_REG |=(USHORT)(USBD_INTR_TC|USBD_INTR_PC|USBD_INTR_BCSTAT|USBD_INTR_MERR);
	USBD_INTR6_REG |=(USHORT)(USBD_INTR_TC|USBD_INTR_PC|USBD_INTR_BCSTAT|USBD_INTR_MERR);
	USBD_INTR7_REG |=(USHORT)(USBD_INTR_TC|USBD_INTR_PC|USBD_INTR_BCSTAT|USBD_INTR_MERR);

	// unmask the USBD interrupts
	USBD_MASK0_REG = 0x3FF & (USHORT)~(USBD_MASK_TC|USBD_MASK_PC|USBD_MASK_SETUP|
					USBD_MASK_MSETUP|USBD_MASK_MERR); 
											
	USBD_MASK1_REG =0x3FF;
	USBD_MASK2_REG =0x3FF;
	USBD_MASK3_REG =0x3FF;
	USBD_MASK4_REG =0x3FF;
	USBD_MASK5_REG =0x3FF;
	USBD_MASK6_REG =0x3FF;
	USBD_MASK7_REG =0x3FF;

	// clear the DMA FIFO first. before we use it
	USBD_DMACFG_REG |= (USHORT)USBD_DMACFG_DMABC;	
	USBD_DMACFG_REG = (USHORT)USBD_DMACFG_DMAEN;

	//=========================================================================
	// program the DMA master interface with the USB module's base address
	// USBD_DMABL, USBD_DMABH
	//=========================================================================
	// need to do the lower 16 bit first to avoid a compiler bug
	USBD_DMABL_REG = (USHORT)((ULONG)&DeviceRequestDesc & USBD_DMABL_SGMSK);
	USBD_DMABH_REG = (USHORT)((ULONG)&DeviceRequestDesc >> 16);

	//=========================================================================
	// program the USBD_EPCFGx, USBD_EPADRx and USBD_EPLENx
	//=========================================================================

	// OUT EP2
	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);

	// IN EP6
	USBD_EPCFG6_REG = USBD_MAX(USBD_MAX_64) | USBD_TYP(USBD_TYP_BULK) | USBD_EPCFG_DIR;
	USBD_EPADR6_REG = (USHORT)((ULONG)UsbCmdBlock&USBD_OFFSET_SGMSK);
	USBD_EPLEN6_REG = (USHORT)sizeof(UsbCmdBlock);

	// we need to ARM the EP0 right here
	USBD_EPCFG0_REG = USBD_MAX(USBD_MAX_8) | (USHORT)(USBD_EPCFG_ARM);
	USBD_EPADR0_REG = (USHORT)((ULONG)&DeviceRequestDesc & USBD_OFFSET_SGMSK);
	USBD_EPLEN0_REG = (USHORT)sizeof(DeviceRequestDesc);

	// enable the USBD first
	USBD_CTRL_REG |= (USHORT)USBD_CTRL_ENA;

	asm("ssync;");
}


///////////////////////////////////////////////////////////////////////////////
// UsbIsr - Interrupt handler for the USBD global interrupt.
//
///////////////////////////////////////////////////////////////////////////////

EX_INTERRUPT_HANDLER (UsbIsr)
{
	// clear the interrupt first
	UsbdGintr = (USHORT)USBD_GINTR_REG;
	UsbdStatus = (USHORT)USBD_STAT_REG;
	
	asm("ssync;");

	if (UsbdGintr & USBD_GINTR_EP0INT)
	{
		USBD_GINTR_REG = USBD_GINTR_EP0INT;

		if (USBD_INTR0_REG & USBD_INTR_SETUP)
		{
			flush((volatile int *)&DeviceRequestDesc.bmRequestType);
			flush((volatile int *)&DeviceRequestDesc.bRequest);
			flush((volatile int *)&DeviceRequestDesc.wValue);
			flush((volatile int *)&DeviceRequestDesc.wLenght);

			// clear pending interrupts
			USBD_INTR0_REG = USBD_INTR_SETUP;
			
			// is this for device descriptor
			if (DeviceRequestDesc.bmRequestType == 0x80 && DeviceRequestDesc.bRequest == USB_REQUEST_GET_DESCRIPTOR
				&& DeviceRequestDesc.wValue == USB_DEVICE_DESCRIPTOR_TYPE << 8)
			{
				// program the USBD_EPCFG0, USBD_EPADR0 and USBD_EPLEN0
				// to DMA the device descriptor to the host.
				USBD_DMABL_REG = (USHORT)((ULONG)&DeviceDesc & USBD_DMABL_SGMSK);
				USBD_DMABH_REG = (USHORT)((ULONG)&DeviceDesc >> 16);
				USBD_EPCFG0_REG = (USHORT)(USBD_EPCFG_DIR | USBD_EPCFG_ARM);
				USBD_EPADR0_REG = (USHORT)((ULONG)&DeviceDesc & USBD_OFFSET_SGMSK);
				USBD_EPLEN0_REG = sizeof(USB_DEVICE_DESCRIPTOR);
			}
			
			// is this for configuration descriptor
			else if (DeviceRequestDesc.bmRequestType == 0x80 && DeviceRequestDesc.bRequest == USB_REQUEST_GET_DESCRIPTOR
				&& DeviceRequestDesc.wValue == USB_CONFIGURATION_DESCRIPTOR_TYPE << 8)
			{
				USBD_DMABL_REG = (USHORT)((ULONG)&ConfigDesc & USBD_DMABL_SGMSK);
				USBD_DMABH_REG = (USHORT)((ULONG)&ConfigDesc >> 16);
				USBD_EPCFG0_REG = (USHORT)(USBD_EPCFG_DIR | USBD_EPCFG_ARM);
				USBD_EPADR0_REG = (USHORT)((ULONG)&ConfigDesc & USBD_OFFSET_SGMSK);
				if (DeviceRequestDesc.wLenght < sizeof(ConfigDesc))
					USBD_EPLEN0_REG = (USHORT)DeviceRequestDesc.wLenght;
				else
				{
					USBD_EPLEN0_REG = (USHORT)sizeof(ConfigDesc);
				}
			}
			
			// is this for string descriptor
			else if (DeviceRequestDesc.bmRequestType == 0x80 && DeviceRequestDesc.bRequest == USB_REQUEST_GET_DESCRIPTOR
				&& DeviceRequestDesc.wValue == USB_STRING_DESCRIPTOR_TYPE << 8)
			{
				// strings currently not supported
			}
		}

		if (USBD_INTR0_REG & USBD_INTR_TC)
		{
			// clear pending interrupts
			USBD_INTR0_REG = USBD_INTR_TC;

			// if DMA is completed, get ready for another setup command
			USBD_DMABL_REG = (USHORT)((ULONG)&DeviceRequestDesc & USBD_DMABL_SGMSK);
			USBD_DMABH_REG = (USHORT)((ULONG)&DeviceRequestDesc >> 16);
			USBD_EPCFG0_REG = USBD_MAX(USBD_MAX_8) | (USHORT)(USBD_EPCFG_ARM);
			USBD_EPADR0_REG = (USHORT)((ULONG)&DeviceRequestDesc & USBD_OFFSET_SGMSK);
			USBD_EPLEN0_REG = (USHORT)sizeof(DeviceRequestDesc);
		}

		// clear pending interrupts
		USBD_INTR0_REG = USBD_INTR0_REG ;
	}

	if (UsbdGintr & USBD_GINTR_CFG)
	{
		// clear pending interrupts
		USBD_GINTR_REG = USBD_GINTR_CFG;

		UsbdStatus = USBD_STAT_REG;
		
		USBD_DMABL_REG = (USHORT)((ULONG)EpDmaBuf&USBD_DMABL_SGMSK);
		USBD_DMABH_REG = (USHORT)((ULONG)EpDmaBuf >> 16);

		// USB has been configured by host so set the flag
		UsbConfiguredByHost = TRUE;
	}
}

⌨️ 快捷键说明

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