www.pudn.com > ARM+SL811.rar > sl811hst.c
////////////////////////////////////////////////////////////////////////////////
// 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= 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
//------------------------------------------------
SL811Write(EP0Status,((endpoint&0x0F)|pid)); // PID + EP address
SL811Write(EP0Counter,usbaddr); // USB address
SL811Write(EP0Address,data0); // buffer address, start with "data0"
SL811Write(EP0XferLen,xferLen); // data transfer length
SL811Write(IntStatus,INT_CLEAR); // clear interrupt status
SL811Write(EP0Control,cmd); // Enable ARM and USB transfer start here
// printf("[%x,%x,%x,%x,%x]\n", ((endpoint&0x0F)|pid), usbaddr, data0, xferLen, cmd);
//------------------------------------------------
// Main loop for completing a wLen data trasnfer
//------------------------------------------------
while(TRUE)
{
//---------------Wait for done interrupt------------------
while(TRUE) // always ensure requested device is
{ // inserted at all time, then you will
intr = SL811Read(IntStatus); // wait for interrupt to be done, and
//Uart_Printf("IntStatus Status %x\n", intr);
// proceed to parse result from slave
if((intr & USB_RESET) || (intr & INSERT_REMOVE))
{ // device.
DATA_STOP = TRUE; // if device is removed, set DATA_STOP
return FALSE; // flag true, so that main loop will
} // know this condition and exit gracefully
if(intr & USB_A_DONE)
break; // interrupt done !!!
}
SL811Write(IntStatus,INT_CLEAR); // clear interrupt status
result = SL811Read(EP0Status); // read EP0status register
remainder = SL811Read(EP0Counter); // remainder value in last pkt xfer
Uart_Printf("EP0 Status %x\n", result);
//-------------------------ACK----------------------------
if (result & EP0_ACK) // Transmission ACK
{
// SETUP TOKEN
if(pid == PID_SETUP) // do nothing for SETUP/OUT token
break; // exit while(1) immediately
// OUT TOKEN
else if(pid == PID_OUT)
break;
// IN TOKEN
else if(pid == PID_IN)
{ // for IN token only
wLen -= (WORD)xferLen; // update remainding wLen value
cmd ^= 0x40; // toggle DATA0/DATA1
dataX++; // point to next dataX
//------------------------------------------------
// If host requested for more data than the slave
// have, and if the slave's data len is a multiple
// of its endpoint payload size/last xferLen. Do
// not overwrite data in previous buffer.
//------------------------------------------------
if(remainder==xferLen) // empty data detected
bufLen = 0; // do not overwriten previous data
else // reset bufLen to zero
bufLen = xferLen; // update previous buffer length
//------------------------------------------------
// Arm for next data transfer when requested data
// length have not reach zero, i.e. wLen!=0, and
// last xferlen of data was completed, i.e.
// remainder is equal to zero, not a short pkt
//------------------------------------------------
if(!remainder && wLen) // remainder==0 when last xferLen
{ // was all completed or wLen!=0
addr = (dataX & 1) ? data1:data0; // select next address for data
xferLen = (BYTE)(wLen>=wPayload) ? wPayload:wLen; // get data length required
if (FULL_SPEED) // sync with SOF transfer
cmd |= 0x20; // always sync SOF when FS, regardless
//Uart_Printf("endpoint=%d,usbaddr=%d,data0=%d,xferLen=%d,cmd=%d",endpoint,usbaddr,data0,xferLen,cmd);
SL811Write(EP0XferLen, xferLen); // select next xfer length
SL811Write(EP0Address, addr); // data buffer addr
SL811Write(IntStatus,INT_CLEAR); // is a LS is on Hub.
SL811Write(EP0Control,cmd); // Enable USB transfer and re-arm
}
//------------------------------------------------
// Copy last IN token data pkt from prev transfer
// Check if there was data available during the
// last data transfer
//------------------------------------------------
if(bufLen)
{
SL811BufRead(((dataX&1)?data0:data1), buffer, bufLen);
buffer += bufLen;
}
//------------------------------------------------
// Terminate on short packets, i.e. remainder!=0
// a short packet or empty data packet OR when
// requested data len have completed, i.e.wLen=0
// For a LOWSPEED device, the 1st device descp,
// wPayload is default to 64-byte, LS device will
// only send back a max of 8-byte device descp,
// and host detect this as a short packet, and
// terminate with OUT status stage
//------------------------------------------------
if(remainder || !wLen)
break;
}
}
//-------------------------NAK----------------------------
if (result & EP0_NAK) // NAK Detected
{
if(endpoint==0) // on ep0 during enumeration of LS device
{ // happen when slave is not fast enough,
SL811Write(IntStatus,INT_CLEAR); // clear interrupt status, need to
SL811Write(EP0Control,cmd); // re-arm and request for last cmd, IN token
result = 0; // respond to NAK status only
}
else // normal data endpoint, exit now !!! , non-zero ep
break; // main loop control the interval polling
}
//-----------------------TIMEOUT--------------------------
if (result & EP0_TIMEOUT) // TIMEOUT Detected
{
if(endpoint==0) // happens when hub enumeration
{
if(++timeout >= TIMEOUT_RETRY)
{
timeout--;
break; // exit on the timeout detected
}
SL811Write(IntStatus,INT_CLEAR); // clear interrupt status, need to
SL811Write(EP0Control,cmd); // re-arm and request for last cmd again
}
else
{ // all other data endpoint, data transfer
TIMEOUT_ERR = TRUE; // failed, set flag to terminate transfer
break; // happens when data transfer on a device
} // through the hub
}
//-----------------------STALL----------------------------
if (result & EP0_STALL) // STALL detected
return TRUE; // for unsupported request.
//----------------------OVEFLOW---------------------------
if (result & EP0_OVERFLOW) // OVERFLOW detected
break;
//-----------------------ERROR----------------------------
if (result & EP0_ERROR) // ERROR detected
break;
} // end of While(1)
if (result & EP0_ACK) // on ACK transmission
return TRUE; // return OK
return FALSE; // fail transmission
}
//*****************************************************************************************
// Control Endpoint 0's USB Data Xfer
// Dev contains the CRC of EP | Address
// ep0Xfer, endpoint 0 data transfer
// Return: -Fail -1 -Sucess 0 -non-zero remainder(0wlength)
//**********************************************************************************
int ep0Xfer(BYTE usbaddr, WORD payload, pSetupPKG setup, BYTE *pData)
{
BYTE pid = PID_IN;
WORD wLen = setup->wLength; // swap back for correct length
BYTE ep0 = 0; // always endpoint zero
//----------------------------------------------------
// SETUP token with 8-byte request on endpoint 0
//----------------------------------------------------
if (!usbXfer(usbaddr, ep0, PID_SETUP, 0, payload, 8, (BYTE*)setup))
{
Uart_Printf("SETUP token error\n");
return FALSE;
}
//----------------------------------------------------
// IN or OUT data stage on endpoint 0
//----------------------------------------------------
if (wLen) // if there are data for transfer
{
if (setup->bmRequest & 0x80) // host-to-device : IN token
{
pid = PID_OUT;
if(!usbXfer(usbaddr, ep0, PID_IN, 0, payload, wLen, pData))
return FALSE;
payload = 0;
}
else // device-to-host : OUT token
{
if(!usbXfer(usbaddr, ep0, PID_OUT, 0, payload, wLen, pData))
return FALSE;
}
}
//----------------------------------------------------
// Status stage IN or OUT zero-length data packet
//----------------------------------------------------
if(!usbXfer(usbaddr, ep0, pid, 0, payload, 0, NULL))
return FALSE;
return TRUE;
}
//*****************************************************************************************
// Control endpoint
// return:
// -1 on failure, 0 on success > 0 if remainder
//*****************************************************************************************
int VendorCmd(BYTE usbaddr,BYTE bReq,BYTE bCmd,WORD wValue,WORD wIndex,WORD wLen,BYTE *pData)
{
SetupPKG setup;
setup.bmRequest = bReq;
setup.bRequest = bCmd;
setup.wValue = WordSwap(wValue);
setup.wIndex = wIndex;
setup.wLength = wLen;
return ep0Xfer(usbaddr, uDev[usbaddr].wPayLoad[0], &setup, pData);
}
//*****************************************************************************************
// Set Device Address :
//*****************************************************************************************
int SetAddress(WORD addr)
{
return VendorCmd(0,0,SET_ADDRESS, WordSwap(addr), 0, 0, NULL);
}
//*****************************************************************************************
// Set Device Configuration :
//*****************************************************************************************
int Set_Configuration(BYTE usbaddr, WORD wVal)
{
return VendorCmd(usbaddr, 0, SET_CONFIG, WordSwap(wVal), 0, 0, NULL);
}
//*****************************************************************************************
// Get Device Descriptor : Device, Configuration, String
//*****************************************************************************************
int GetDesc(BYTE usbaddr, WORD wValue, WORD wIndex, WORD wLen, BYTE *desc)
{
return VendorCmd(usbaddr, 0x80, GET_DESCRIPTOR, wValue, wIndex, wLen, desc);
}
//*****************************************************************************************
// HID Get_Desc :
//*****************************************************************************************
int GetHid_Desc(BYTE usbaddr, WORD wValue, WORD wLen, BYTE *desc)
{
return VendorCmd(usbaddr, 0x81, GET_DESCRIPTOR, wValue, 0, wLen, desc);
}
//*****************************************************************************************
// GetHUBDesc :
//*****************************************************************************************
int GetHubDesc(BYTE usbaddr, WORD wValue, WORD wLen, BYTE *desc)
{
return VendorCmd(usbaddr, 0xA0, GET_DESCRIPTOR, wValue, 0, wLen, desc);
}
//*****************************************************************************************
// Get Status : (HUB)
//*****************************************************************************************
int GetStatus(BYTE usbaddr, BYTE *desc)
{
return VendorCmd(usbaddr, 0x80, GET_STATUS, 0, 0, 2, desc);
}
//*****************************************************************************************
// PortFeature : (SET_FEATURE, CLEAR_FEATURE)
//*****************************************************************************************
int PortFeature(BYTE usbaddr, BYTE bReq, WORD wValue, BYTE cPort)
{
return VendorCmd(usbaddr, 0x23, bReq, WordSwap(wValue), WordSwap((WORD)cPort), 0, NULL);
}
//*****************************************************************************************
// GetPortStatus :
//*****************************************************************************************
int GetPortStatus(BYTE usbaddr, BYTE cPort, BYTE *desc)
{
return VendorCmd(usbaddr, 0xA3, GET_STATUS, 0, WordSwap((WORD)cPort), 0x04, desc);
}
//*****************************************************************************************
// GetDevInfo :
//*****************************************************************************************
/*WORD GetDevInfo(BYTE *DevInfo)
{
BYTE EpAddr;
int i;
BYTE DescBufLen = 0;
for(EpAddr = 1; (EpAddr < MAX_DEV); EpAddr++)
{
if(uHub.bPortPresent[EpAddr])
{
DevInfo[DescBufLen++] = EpAddr; // USB Address
DevInfo[DescBufLen++] = uHub.bPortNumber[EpAddr]; // Port Number
DevInfo[DescBufLen++] = uHub.bPortSpeed[EpAddr]; // Device Speed (from enum)
DevInfo[DescBufLen++] = uDev[EpAddr].bClass; // Class Type (from enum)
(WORD)DevInfo[DescBufLen++] = uDev[EpAddr].wVID; // VID
DescBufLen++;
(WORD)DevInfo[DescBufLen++] = uDev[EpAddr].wPID; // PID
DescBufLen++;
DevInfo[DescBufLen++] = (BYTE)uDev[EpAddr].wPayLoad[0]; // Ep0 MaxPktSize (max 64 bytes)
DevInfo[DescBufLen++] = uDev[EpAddr].bNumOfEPs; // Number of data endpoints
for(i=0;ibMaxPacketSize0;// on current non-zero USB address
//------------------------------------------------
// Set Slave USB Device Address
//------------------------------------------------
if (!SetAddress(usbaddr)) // set to specific USB address
return FALSE; //
uAddr = usbaddr; //ransfer using this new address
//------------------------------------------------
// Get USB Device Descriptors on EP0 & Addr X
//------------------------------------------------
if (!GetDesc(uAddr,DEVICE,0,(pDev->bLength),DBUF))
return FALSE; // For this current device:
uDev[usbaddr].wVID = pDev->idVendor; // save VID
uDev[usbaddr].wPID = pDev->idProduct; // save PID
uDev[usbaddr].iMfg = pDev->iManufacturer; // save Mfg Index
uDev[usbaddr].iPdt = pDev->iProduct; // save Product Index
Uart_Printf("VID 0x%x, PID 0x%x\n", uDev[usbaddr].wVID, uDev[usbaddr].wPID);
//------------------------------------------------
// Get String Descriptors
//------------------------------------------------
pStr = (pStrDesc)DBUF;
if (!GetDesc(uAddr,STRING,0,4,DBUF)) // Get string language
return FALSE;
strLang = pStr->wLang; // get iManufacturer String length
if (!GetDesc(uAddr,(WORD)(uDev[usbaddr].iMfg<<8)|STRING,strLang,4,DBUF))
return FALSE; // get iManufacturer String descriptors
if (!GetDesc(uAddr,(WORD)(uDev[usbaddr].iMfg<<8)|STRING,strLang,pStr->bLength,DBUF))
return FALSE;
//------------------------------------------------
// Get Slave USB Configuration Descriptors
//------------------------------------------------
pCfg = (pCfgDesc)DBUF;
if (!GetDesc(uAddr,CONFIGURATION,0,8,DBUF))
return FALSE;
if (!GetDesc(uAddr,CONFIGURATION,0,pCfg->wLength,DBUF))
return FALSE;
pIfc = (pIntfDesc)(DBUF + 9); // point to Interface Descp
uDev[usbaddr].bClass = pIfc->iClass; // update to class type
uDev[usbaddr].bNumOfEPs = (pIfc->bEndPoints <= MAX_EP) ? pIfc->bEndPoints : MAX_EP;
//------------------------------------------------
// Set configuration (except for HUB device)
//------------------------------------------------
//if (uDev[usbaddr].bClass!=HUBCLASS) // enumerating a FS/LS non-hub device
// if (!Set_Configuration(uAddr,DEVICE)) // connected directly to SL811HS
// return FALSE;
Uart_Printf("Set_Configuration end\n");
//------------------------------------------------
// For each slave endpoints, get its attributes
// Excluding endpoint0, only data endpoints
//------------------------------------------------
epLen = 0;
for (i=1; i<=uDev[usbaddr].bNumOfEPs; i++) // For each data endpoint
{
pEnp = (pEPDesc)(DBUF + 9 + 9 + epLen); // point to Endpoint Descp(non-HID)
if(pIfc->iClass == HIDCLASS)
pEnp = (pEPDesc)(DBUF + 9 + 9 + 9 + epLen); // update pointer to Endpoint(HID)
uDev[usbaddr].bEPAddr[i] = pEnp->bEPAdd; // Ep address and direction
uDev[usbaddr].bAttr[i] = pEnp->bAttr; // Attribute of Endpoint
uDev[usbaddr].wPayLoad[i] = pEnp->wPayLoad; // Payload of Endpoint
uDev[usbaddr].bInterval[i] = pEnp->bInterval; // Polling interval
uDev[usbaddr].bData1[i] = 0; // init data toggle
Uart_Printf("EndPoint 0x%x, attr = 0x%x, pkt_size = 0x%x, interval = 0x%x\n", uDev[usbaddr].bEPAddr[i],uDev[usbaddr].bAttr[i],uDev[usbaddr].wPayLoad[i],uDev[usbaddr].bInterval[i]);
epLen += 7;
if(uDev[usbaddr].bAttr[i]==0x2)
{
if(uDev[usbaddr].bEPAddr[i]&0x80)
epbulkin=uDev[usbaddr].bEPAddr[i];
else
epbulkout=uDev[usbaddr].bEPAddr[i];
}
}
//------------------------------------------------
// Get Hid Report Descriptors
//------------------------------------------------
if(pIfc->iClass == HIDCLASS)
{
Uart_Printf("HID Class\n");
pHid = (pHidDesc)(DBUF + 9 + 9); // point to HID-CLASS descp
if (!GetHid_Desc(uAddr,HID_REPORT,pHid->wItemLength,DBUF))
return FALSE;
}
//------------------------------------------------
// Get HUB Class Specific Descriptor (per port switching)
//------------------------------------------------
/******
if(uDev[usbaddr].bClass==HUBCLASS)
{ // enumerating a HUB device
pHub =(pHubDesc)DBUF; // Ask for 71 bytes ???
if (!GetHubDesc(uAddr,0x00,0x47,DBUF))
return FALSE; // Get Hub Desriptor
if (!GetStatus(uAddr,STATUS))
return FALSE; // Get Status
if (!Set_Configuration(uAddr,DEVICE))
return FALSE; // Set configuration
pNumPort = pHub->bNbrPort; // save no. of ports available
for(i=1; i<=pNumPort; i++) // ClearPortFeature: C_PORT_CONNECTION off
if(!PortFeature(uAddr,CLEAR_FEATURE, C_PORT_CONNECTION,i))
return FALSE;
for(i=1; i<=pNumPort; i++) // SetPortFeature: PORT_POWER on
{
if(!PortFeature(uAddr,SET_FEATURE, PORT_POWER, i))
return FALSE;
}
for(i=1; i<=pHub->bNbrPort; i++) // GetPortStatus(wHubStatus,wHubChange)
if( !GetPortStatus(uAddr,i,STATUS) ) //
return FALSE; //
for(i=2;i<=pNumPort+1;i++) // clear port present status
{ // address #2..#5 only
uHub.bPortPresent[i] = 0; // clear status
uHub.bPortNumber[i] = 0; //
}
HUB_DEVICE = TRUE; // Set Hub flag, as long as a hub is attached
}
*****/ // directly to the HUB_DEVICE will be set
Uart_Printf("EnumUsbDev Return\n");
return TRUE;
}
//*****************************************************************************************
// Full-speed and low-speed detect - Device atttached directly to SL811HS
//*****************************************************************************************
int speed_detect()
{
volatile int i =0;
pNumPort = 0; // zero no. of downstream ports
SLAVE_FOUND = FALSE; // Clear USB device found flag
FULL_SPEED = TRUE; // Assume full speed device
//HUB_DEVICE = FALSE; // not HUB device
DATA_STOP = FALSE; //
//SL811MemTest(); // Memory test
/////SL811Write(IntEna,0x63); // USBA/B, Insert/Remove,USBRest/Resume
////////SL811Write(cSOFcnt,0xAE); // Set SOF high counter, no change D+/D-, host mode
//////SL811Write(CtrlReg,0x48); //Clear Sl811H mode and setup normal operation
//////Delay(100);
//////SL811Write(CtrlReg,0x00); //Disable USB transfer operation and SOF
//////Delay(100);
//////SL811Write(IntEna,0x61); // USB-A, Insert/Remove, USB_Resume.
//////SL811Write(IntStatus,INT_CLEAR); // Clear Interrupt enable status
//////Delay(100); // Delay for HW stablize
i = SL811Read(IntStatus); //Read Interrupt Status
/*
if(i & 0x40)
{ // test for USB reset
SL811Write(IntStatus,0x40); // Clear Interrupt enable status
Uart_Printf("NO device or No Power\n");
return 0; // exit speed_detect()
}
i = SL811Read(IntStatus);*/
/////if(!(i & 0x40))
if(i&0x20)//device is inserted or removed
{
if((i & USB_DPLUS)==0) // Checking full or low speed
{ // ** Low Speed is detected ** //
Uart_Printf("Low Speed is detected, status=%x\n",i);
//Set up Master and low Speed direct and SOF cnt high = 0x2e
SL811Write(cSOFcnt,0xee); // Set up host and low speed direct and SOF cnt
//SOF Coonuter Low = 0xe0; 1ms interval
SL811Write(cDATASet,0xe0); // SOF Counter Low = 0xE0; 1ms interval
//Setup 6Mhz and EOP enable
SL811Write(CtrlReg,0x21); // Setup 6MHz and EOP enable
//uHub.bPortSpeed[1] = 1; // low speed for Device #1
FULL_SPEED = FALSE; // low speed device flag
}
else
{
Uart_Printf("Full Speed is detected, status=%x\n",i); // ** Full Speed is detected ** //
SL811Write(cSOFcnt,0xae); // Set up Master & low speed direct and SOF cnt high=0x2e
SL811Write(cDATASet,0xe0); // SOF Counter Low = 0xE0; 1ms interval
SL811Write(CtrlReg,0x05); // Setup 48MHz and SOF enable
//uHub.bPortSpeed[1] = 0; // full speed for Device #1
//// SL811Write(CtrlReg,0x05);
}
}
else
{
SL811Write(IntStatus,INT_CLEAR);
Uart_Printf("NO device detected or No Power.\n");
return 0;
}
SLAVE_FOUND = TRUE; // Set USB device found flag
SLAVE_ENUMERATED = FALSE; // no slave device enumeration
SL811Write(EP0Status,0x50); // Setup SOF Token, EP0
SL811Write(EP0Counter,0x00); // reset to zero count
SL811Write(EP0Control,0x01); // start generate SOF or EOP
Delay(250); // Hub required approx. 24.1mS
SL811Write(IntStatus,INT_CLEAR); // Clear Interrupt status
return 0; // exit speed_detect();
}
//*****************************************************************************************
// Detect USB Device
//*****************************************************************************************
int slave_detect(void)
{
int retDataRW = FALSE;
//-------------------------------------------------------------------------
// Wait for SL811HS enumeration
//-------------------------------------------------------------------------
if(!SLAVE_ENUMERATED) // only if slave is not configured
{
speed_detect(); // wait for an USB device to be inserted to
Delay(100);
if(SLAVE_FOUND) // the SL811HST host
{
// printf("slave found\n");
if(EnumUsbDev(1)) // enumerate USB device, assign USB address = #1
{
SLAVE_ENUMERATED = TRUE; // Set slave USB device enumerated flag
Uart_Printf("%s Speed Device Attached\n", FULL_SPEED?"Full":"Low");
//Uart_Printf("Press ESC key to Exit...\n");
}
}
}
//-------------------------------------------------------------------------
// SL811HS enumerated, proceed accordingly
//-------------------------------------------------------------------------
else
{
//
if(Slave_Detach()) // test for slave device detach
return 0; // exit now.
} // end of else
return 0;
}
//*****************************************************************************************
// Slave_Detach
//*****************************************************************************************
int Slave_Detach(void)
{
if( (SL811Read(IntStatus)&INSERT_REMOVE) || (SL811Read(IntStatus)&USB_RESET) )
{ // Test USB detached?
SLAVE_ENUMERATED = FALSE; // return to un-enumeration
//uHub.bPortPresent[1] = 0; // Device #1 not present
SL811Write(IntStatus,INT_CLEAR); // clear interrupt status
Uart_Printf("Device Detached\n");
return TRUE; // exit now !!!
}
return FALSE;
}
int SL811MemTest(void)
{
int errors =0, i;
BYTE temp;
////outportb(0x0d, SL811H_ADDR);
////temp=inportb(SL811H_DATA);
////Uart_Printf("Status Register: %x,\n",temp);
for(i = 0x10/*EP0Buf*/; i< 0xff; i++) //addr = data
SL811Write((BYTE)i,(BYTE)i);
for(i = 0x10/*EP0Buf*/; i<0xff; i++) //verify data
{
if((BYTE)i != SL811Read((BYTE)i))
errors++;
SL811Write((BYTE)i,(BYTE)~i);
if((BYTE)~i != SL811Read((BYTE)i))
errors++;
}
Uart_Printf("Memory test done errors: 0x%x\n",errors);
/*// auto increment: addr = data
for(i = EP0Buf, outportb(EP0Buf,SL811H_ADDR); i < cMemEnd; i++)
outportb(i,SL811H_DATA);
// auto: addr = data
for(i = EP0Buf, outportb(EP0Buf,SL811H_ADDR); i < cMemEnd; i++)
{
if((BYTE)i != (BYTE)inportb(SL811H_DATA))
errors++;
}
// clear all sl811h Memory
for(i = EP0Buf, outportb(EP0Buf,SL811H_ADDR); i < cMemEnd; i++)
outportb(0,SL811H_DATA);
Uart_Printf("Memory test done: 0x%x\n",errors);*/
return errors; //Return number of error
}
//-------------------------------------------------------------------------
//Find Full Speed USB Device
//------------------------------------------------------------------------
/***
int FindUsbDev(short DevNum)
{
pDevDesc pDev;
pCfgDesc pConf;
pIntfDesc pINtf;
int i,j;
BYTE *p,*ep;
sEPDesc sEP;
if (DevNum==1)
USBReset(); //root hub
uDev.wEPxCRC[0] = crc5(0); //address=0, endpoint =0
uDev.wPayLoad[0] = 8;
Uart_Printf("Get Device Descriptor: .. \n");
pDev = (pDevDesc)SCMD;
if( !GetDesc(DEVICE<<8,0x40,(BYTE*)SCMD))
return FALSE;
Uart_Printf("len=%02x ep0Len=%d\n", pDev->bLength, pDev->bMaxPacketSize0);
uDev.wPayLoad[0] = (pDev->bMaxPacketSize0 & 0xff)? pDev->bMaxPacketSize 0:8;
Uart_Printf("Get Device Descriptor: .. ");
uDev.wEPxCRC[0] = crc5((WORD)(DevNum+(0<<7))); //Endpoint 0 | assign new addr
if(!GetDesc(DEVICE<<8,0x09,(BYTE*)SCMD))
return FALSE;
Uart_Printf("Vendor:%04x Product: %04x\n",pDev->idVendor,pDev->idProduct);
uDev.wVID = pDev->idVendor;
uDev.wPID = pDev->idProduct;
Uart_Printf("Get Configuration\n");
if(!GetDesc(CONFIGURATION<<8,0xff,(BYTE*)SCMD)) return false;
if(!GetDesc(CONFIGURATION<<8,0x12,(BYTE*)SCMD)) return false;
if(!GetDesc(CONFIGURATION<<8,0x09,(BYTE*)SCMD)) return false;
if(!GetDesc(CONFIGURATION<<8,0x20,(BYTE*)SCMD)) return false;
pConf = (pConfDesc)SCMD;
//printf("Len=%d,type:%02x\n",pConf->wLength, pConf->bType);
//printf("Set Configuration\n");
if(!Set_Configuration(DEVICE)) return FALSE;
p = (BYTE*)SCMD;
i = *p;
pIntf = (pINtfDesc)& p[i]; //point to Interface Desc
p = (BYTE*)& p[i+pIntf->bLength]; //point to EP Desc
Uart_Printf("Number of Endpoint = %d\n", pIntf->bEndPoints);
uDev.bNumofEPs = pIntf -> bEndPoints;
if(uDev.bNumofEPS > cMaxEP) return FALSE;
ep = (BYTE*) & sEP;
for(i=1; i<= pIntf->bEndPoints; i++)
{
uDev.wEPxCRC[i] = crc5((WORD)(DevNum + (i<<7))); //Endpoint CRC
for(j=0; j<7; j++)
ep[j] = *p++;
if(sEP.bEPAdd & 0x80)
Rd_EP = sEP.bEPAdd & 0xf; //index of Read EP
else
Wr_EP = sEP.bEPAdd;
uDev.bEPAddr[i] = sEP.bAttr;
uDev.wPayLoad[i] = sEP.wPayLoad;
Uart_Printf("Length %x Type %x EndPoint Addr %02x Attr %02x wLength = %04x\n",
sEP.bLength, sEP.bType, sEP.bEPAdd, sEP.bAttr, sEP.wPayLoad);
DToggle[i] = 0;
}
Uart_Printf("Wrinte Endpoint %d, Read Endpoint %d\n", Wr_EP, Rd_EP);
return TRUE;
}
****/