📄 sl811hst.c
字号:
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;
}
****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -