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

📄 sl811hst.c

📁 ARM44B0+sl811(包含所有固件程序和源码)调试通过
💻 C
📖 第 1 页 / 共 3 页
字号:
////////////////////////////////////////////////////////////////////////////////
// Cypress Semiconductor - Customer Design Center 
////////////////////////////////////////////////////////////////////////////////
// File:		host_811.c
// Purpose:	    8051 firmware to master SL811 Embedded Host. 
//              Contains SL811-related control firmware. 
//              Based on SL811HST code written by cxn.
// H/W Target:  SL811HST(Rev 1.5) + EZUSB DEV KIT
// IMPORTANT :  Turn ON SW4 of S6-DIP-SWITCH 
//              to enable upper 32K memory on EZUSB Dev Kit
//
// $Header: /USB/ez811/firmware/Emb_Host/host_811.c 8     5/14/02 8:30p Tpm $
// Copyright (c) 2002 Cypress Semiconductor. May not be reproduced without permission.
// See the license agreement for more details.
////////////////////////////////////////////////////////////////////////////////

#include "ezusb.h"
#include "ezregs.h"
#include "host_811.h"
#include "..\target\def.h"
#include "..\target\44blib.h"


//*****************************************************************************************
// SL811H Modules (major routines only): 
//*****************************************************************************************
// 1) usbXfer()		: handles usd data transfer, SETUP,IN, OUT
// 2) ep0Xfer()		: handles endpoint zero control pipe
// 3) DataRW()		: handles data endpoint transfer pipe
// 4) EnumUsbDev() 	: device enum(HID/HUB), excl. multi-interface
// 5) HubPortEnum()	: hub port device enum, incl. dynamic USB address
// 6) speed_detect(): SL811H slave device attach/speed detect
// 7) slave_detect(): Main loop control between SL811H & EZUSB & GUI
// 8) host_811.c	: Entire code size

//*****************************************************************************************
// xdata variables
//*****************************************************************************************
//xdata BYTE SL811H_ADDR	_at_	0x4000;		// A0 = '0' to external memory
//xdata BYTE SL811H_DATA  	_at_	0x4001;     // A0 = '1' (EZ-USB Dev Kit - SW4 ON)

#define	SL811H_ADDR	0x06000000		//USB board,A1=0;
#define	SL811H_DATA	0x06000002		//USB board,A1=1;
//--------------------------------------------------------------------
#define USBADDR (*(volatile unsigned char *)0x06000000) //USB board,A1=1;
#define USBDATA (*(volatile unsigned char *)0x06000002) //USB board,A1=0
//--------------------------------------------------------------------


BYTE 			DBUF[256];		// at 0x2000 for general descriptors data
BYTE 			STATUS[8];		// for status data buffer
BYTE			REGBUFF[16];    // Buffer for Register Data
BYTE 			HOSTCMD[8];		// EZUSB's OUT1 host command data
BYTE 			pHOSTCMD[8];	// previous data transfer info, during data transfer
BYTE 			HubChange[1];	// Hub port endpoint 1 data status
BYTE 			DataBufLen;		// EZUSB's IN #3 data transfer buffer length
BYTE 			pNumPort;		// Number of downstream ports on hub
BYTE			remainder;		// Remaining byte in a USB transfer
pUSBDEV  		uDev[MAX_DEV];	// Multiple USB devices attributes, Max 5 devices
pHUBDEV			uHub;			// Struct for downstream device on HUB
pDevDesc  		pDev;			// Device descriptor struct
pCfgDesc 		pCfg;			// Configuration descriptor struct
pIntfDesc 		pIfc;			// Interface descriptor struct
pEPDesc 		pEnp;			// Endpoint descriptor struct
pStrDesc 		pStr;			// String descriptor struct
pHidDesc 		pHid;			// HID class descriptor struct
pHubDesc 		pHub;			// HUD class descriptor struct
pPortStatus		pStat;			// HID ports status

//*****************************************************************************************
// Boolean Logic Defines
//************** ***************************************************************************
BOOL  	SLAVE_FOUND;				// Slave USB device found
BOOL  	SLAVE_ENUMERATED;			// slave USB device enumeration done
BOOL  	FULL_SPEED;					// Full-Speed = TRUE, Low-Speed = FALSE
BOOL	HUB_DEVICE;					// HUB device = TRUE
BOOL 	BULK_OUT_DONE;				// Set when EZUSB's OUT1 hostcmd xfer is done
BOOL 	DESC_XFER;					// Set when there is data for EZUSB's IN1 desc xfer
BOOL 	DATA_XFER;					// Set when there is data for EZUSB's IN3 data xfer
BOOL 	DATA_XFER_OUT;				// Set when there is data for EZUSB's OUT3 data xfer
BOOL 	CONFIG_DONE;				// Set when EZUSB completes its enumeration process.
BOOL 	TIMEOUT_ERR;				// timeout error during data endpoint transfer
BOOL	DATA_STOP;					// device unplugged during data transfer
BOOL	DATA_INPROCESS;				// set when we are in a data pipe transfer
BOOL	pLS_HUB;					// indicate previous command is a LS device on hub

BOOL	dsPoll;				        // poll downstream port conections

BOOL 	bData1;

void EZUSB_Delay(WORD ms)
{
	//unsigned long loop;
	int i;
	//while(ms--) 
	//{
	//	loop = 1200;
	//	while(loop--);
	//}
	for(i=0;i<10;i++)
		Delay((int)ms);		
}


//*****************************************************************************************
// Byte Read from SL811H
// a = register address
// return = data in register
//*****************************************************************************************
BYTE SL811Read(BYTE adr)
{  
	/***BYTE i=0;
	USBADDR = adr;
	i= USBDATA;
	return (i);***/
	outportb(adr, SL811H_ADDR);
	return (inportb(SL811H_DATA));
}

//*****************************************************************************************
// Byte Write to SL811H
// adr = register address
// dat = data to be written to this register address
//*****************************************************************************************
void SL811Write(BYTE adr, BYTE dat)
{  
	//USBADDR = adr;
	//USBDATA = dat;	
	outportb(adr, SL811H_ADDR);
	outportb(dat, SL811H_DATA);
}

//*****************************************************************************************
// Buffer Read from SL811H
// addr = buffer start address
// s    = return buffer address where data are to be save/read
// c	= buffer data length
//*****************************************************************************************
void SL811BufRead(BYTE addr, BYTE *s, BYTE c)
{	
	if(c<=0) return;
	Uart_Printf("BR(");
	USBADDR = addr;		
	//outportb(addr, SL811H_ADDR);	
   	while (c--) 
   	{   		
		*s = USBDATA;	
		//*s = (BYTE)inportb(SL811H_DATA);		
		Uart_Printf("%x,", *s);
		s++;
	}
		Uart_Printf(")\n");
}

//*****************************************************************************************
// Buffer Write  to SL811H
// addr = buffer start address
// s    = buffer address where data are to be written
// c	= buffer data length
//*****************************************************************************************
void SL811BufWrite(BYTE addr, BYTE *s, BYTE c)
{
	if(c<=0)
		return;
	Uart_Printf("BW[");		
	USBADDR = addr;
	//outportb(addr, SL811H_ADDR);
   	while (c--) 
   	{
   		Uart_Printf("%x,", *s);
   		USBDATA = *s++;	
		//outportb(*s, SL811H_DATA);
		
	}
		Uart_Printf("]\n");
}
//*****************************************************************************************
// Swap high and low byte 
//*****************************************************************************************
WORD WordSwap(WORD input)
{
	return(((input&0x00FF)<<8)|((input&0xFF00)>>8));
}

//*****************************************************************************************
// UsbReset during enumeration of device attached directly to SL811HS
//*****************************************************************************************
void USBReset(void)
{
/*	BYTE tmp;
    tmp = SL811Read(CtrlReg);   	//Read Control Status
 	SL811Write(CtrlReg,tmp|0x08);	//Setup USB Reset
	Delay(2500);					//Delay 250 ms
	SL811Write(CtrlReg,tmp|0x18);	//suspend/resume, reset
	Delay(1500);					//Delay 150 ms
	SL811Write(CtrlReg,tmp|0x08);	//Setup USB Reset
	Delay(100);						//Delay 10 ms
    SL811Write(CtrlReg,tmp);    	//enable USB*/
    BYTE tmp;
    tmp =  SL811Read(CtrlReg);   
 	SL811Write(CtrlReg,tmp|0x08);
	Delay(25);			
    SL811Write(CtrlReg,tmp);
}

//*****************************************************************************************
// SL811H variables initialization
//*****************************************************************************************
void sl811h_init(void)
{	
	int i;
/****
	for(i=0;i<MAX_DEV;i++)
	{
		uHub.bPortPresent[i] = 0;
		uHub.bPortNumber[i] = 0;
	}
	*****/
	
	USBADDR = 0;
	USBDATA = 0;
	////// USBReset();
	SL811Write(cSOFcnt,0xae);
	//outportb(0x0f, SL811H_ADDR);
	//outportb(0xae, SL811H_DATA);
	pNumPort = 0x00;	
	//Delay(250);
	
	
	//FULL_SPEED = TRUE;
	FULL_SPEED = FALSE;	
	HUB_DEVICE = FALSE;
	SLAVE_FOUND = FALSE;
	SLAVE_ENUMERATED = FALSE;
	BULK_OUT_DONE = FALSE;
	DESC_XFER = FALSE;
	DATA_XFER = FALSE;
	DATA_XFER_OUT = FALSE;
	DATA_INPROCESS = FALSE;
	//pLS_HUB = FALSE;
//	IN2BUF[0] = 0;
    dsPoll = 1;				    // poll downstream port conections
	SL811MemTest();	
	
	//////////////////////
	SL811Write(cSOFcnt,0xae);
	SL811Write(CtrlReg,0x08);      		// Reset USB engine, full-speed setup, suspend disable
	Delay(100);					// Delay for HW stablize
	SL811Write(CtrlReg,0x00);      		// Set to normal operation
	SL811Write(IntEna,0x61);      		// USB-A, Insert/Remove, USB_Resume.
	SL811Write(IntStatus,INT_CLEAR);	// Clear Interrupt enable status
	Delay(100);					// Delay for HW stablize
	///////////////////////
	////SL811Write(IntEna,0x61);
	//SL811Write(cSOFcnt,0xae);
		
	
}
//*****************************************************************************************
// usbXfer:
// NOTE: The CRC contains Device 4-bit of EndPoint Number | 7-bit of Device Addr
// iso = 1 transfer with ISO mode, else Bulk /Interrupt/Control
// return 0 on Success
// successful transfer = return TRUE
// fail transfer = return FALSE
// USB Status 
// 0x01  ACK
// 0x02  Device Error Bit 
// 0x04  Device Time out 
// 0x08  Toggle bit 
// 0x10  SET_UP packet bit (Not used in SL811H)
// 0x20  Overflow bit 
// 0x40  Device return NAKs, forever
// 0x80  Device return STALL
// need to keep track DATA0 and DATA1 for write endpoint 
//*****************************************************************************************
int usbXfer(BYTE usbaddr, BYTE endpoint, BYTE pid, BYTE iso, WORD wPayload, WORD wLen, BYTE *buffer)
{  
	U8 	cmd, result, timeout, intr=0x0,intr1=0xff;
	U8	xferLen, bufLen, data0, data1, dataX, addr;

	//------------------------------------------------
	// Default setting for usb trasnfer
	//------------------------------------------------
	bufLen = dataX = timeout = 0;					//reset all
	data0 = EP0_Buf;								// DATA0 buffer address
	data1 = data0 + (BYTE)wPayload;					// DATA1 buffer address
	DATA_STOP =	TIMEOUT_ERR = FALSE;				// set default conditions

	//------------------------------------------------
	// Define data transfer payload
	//------------------------------------------------
	if (wLen >= wPayload)  							// select proper data payload
		xferLen = wPayload;							// limit to wPayload size 
	else											// else take < payload len
		xferLen = wLen;								//	

	//------------------------------------------------
	// For IN token
	//------------------------------------------------
	if (pid==PID_IN)								// for current IN tokens
	{												//
		if(FULL_SPEED)								//
			cmd = sDATA0_RD;						// FS/FS on Hub, sync to sof
		else										// LS, no sync to sof for IN
			cmd = DATA0_RD;							//
/****		-------------------------------------------- response to OUT can propagate to SL811HS
		// handling LS device on HUB
		//--------------------------------------------
		if(HUB_DEVICE && usbaddr!=HUB_ADDR)			// Transfer on Hub, USB addr #0, #2..#5 only
		{
			if(uHub.bPortSpeed[usbaddr])			// If transfer of LS on Hub & previous is			
				cmd = psDATA0_RD;					// SETUP & current is IN, no sync to sof,
		}***/
	}
	//------------------------------------------------
	// For OUT token
	//------------------------------------------------
	else if(pid==PID_OUT)							// for OUT tokens
	{  	
		if(xferLen)									// only when there are	
			SL811BufWrite(data0,buffer,xferLen); 	// data to transfer on USB

		if(FULL_SPEED)								
			cmd = sDATA0_WR;						// FS/FS on Hub, sync to sof
		else										// LS, no sync to sof for OUT
			cmd = DATA0_WR;							
		//--------------------------------------------
		// handling LS device on HUB
		//--------------------------------------------
/***		if(HUB_DEVICE && usbaddr!=HUB_ADDR)			// Transfer on Hub, USB addr #0, #2..#5 only
		{
			if(uHub.bPortSpeed[usbaddr])			// If transfer of LS on Hub, previous
				cmd = psDATA0_WR;
		} ****/
		// implement data toggle
		bData1 = uDev[usbaddr].bData1[endpoint];
        uDev[usbaddr].bData1[endpoint] = (uDev[usbaddr].bData1[endpoint] ? 0 : 1); // DataToggle
		if(bData1)
          cmd |= 0x40;                              // Set Data1 bit in command
	}
	//------------------------------------------------
	// For SETUP/OUT token
	//------------------------------------------------
	else											// for current SETUP/OUT tokens
	{  	
		if(xferLen)									// only when there are	
			SL811BufWrite(data0,buffer,xferLen); 	// data to transfer on USB

		if(FULL_SPEED)								
			cmd = sDATA0_WR;						// FS/FS on Hub, sync to sof
		else										// LS, no sync to sof for OUT
			cmd = DATA0_WR;							
		//--------------------------------------------
		// handling LS device on HUB
		//--------------------------------------------
/***		if(HUB_DEVICE && usbaddr!=HUB_ADDR)			// Transfer on Hub, USB addr #0, #2..#5 only
		{
			if(uHub.bPortSpeed[usbaddr])			// If transfer of LS on Hub, previous
				cmd = psDATA0_WR;
		}***/
	}

	//------------------------------------------------
	// Isochronous data transfer setting
	//------------------------------------------------
	if (iso) 
		cmd |= ISO_BIT;                     		// if iso setup ISO mode

	//------------------------------------------------
	// For EP0's IN/OUT token data, start with DATA1
	// Control Endpoint0's status stage.
	// For data endpoint, IN/OUT data, start ????
	//------------------------------------------------
	if (endpoint == 0 && pid != PID_SETUP) 			// for Ep0's IN/OUT token
		cmd |= 0x40; 								// always set DATA1

	//------------------------------------------------
	// Arming of USB data transfer for the first pkt

⌨️ 快捷键说明

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