📄 host_811.c
字号:
//------------------------------------------------
// Get USB Device Descriptors on EP0 & Addr X
//------------------------------------------------
if (!GetDesc(uAddr,DEVICE,0,(pDev->bLength),TRBUF))
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
//------------------------------------------------
// Get String Descriptors
//------------------------------------------------
pStr = (pStrDesc)TRBUF;
if (!GetDesc(uAddr,STRING,0,4,TRBUF)) // Get string language
return FALSE;
strLang = pStr->wLang; // get iManufacturer String length
if (!GetDesc(uAddr,(WORD)(uDev[usbaddr].iMfg<<8)|STRING,strLang,4,TRBUF))
return FALSE; // get iManufacturer String descriptors
if (!GetDesc(uAddr,(WORD)(uDev[usbaddr].iMfg<<8)|STRING,strLang,pStr->bLength,TRBUF))
return FALSE;
//------------------------------------------------
// Get Slave USB Configuration Descriptors
//------------------------------------------------
pCfg = (pCfgDesc)TRBUF;
if (!GetDesc(uAddr,CONFIGURATION,0,8,TRBUF))
return FALSE;
if (!GetDesc(uAddr,CONFIGURATION,0,WordSwap(pCfg->wLength),TRBUF))
return FALSE;
pIfc = (pIntfDesc)(TRBUF + 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;
if((TRBUF[20]&0x80)==0) { disk_out=1;disk_in=2;}
else { disk_out=2;disk_in=1;}
/*
if (( TRBUF[20] & 0x80 ))
{
disk_in=TRBUF[20] & 0x0F;
}
else
{
disk_out=TRBUF[20] & 0x0F;
}
if (( TRBUF[27] & 0x80 ))
{
disk_in=TRBUF[27] & 0x0F;
}
else
{
disk_out=TRBUF[27] & 0x0F;
}
//Also Cannot
*/
//------------------------------------------------
// 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;
//------------------------------------------------
// 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)(TRBUF + 9 + 9 + epLen); // point to Endpoint Descp(non-HID)
if(pIfc->iClass == HIDCLASS)
pEnp = (pEPDesc)(TRBUF + 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
epLen += 7;
}
//------------------------------------------------
// Get Hid Report Descriptors
//------------------------------------------------
if(pIfc->iClass == HIDCLASS)
{
pHid = (pHidDesc)(TRBUF + 9 + 9); // point to HID-CLASS descp
if (!GetHid_Desc(uAddr,HID_REPORT,pHid->wItemLength,TRBUF))
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,TRBUF))
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
return TRUE;
}
//*****************************************************************************************
// Full-speed and low-speed detect - Device atttached directly to SL811HS
//*****************************************************************************************
int speed_detect()
{
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; //
SL811Write(cSOFcnt,0xAE); // Set SOF high counter, no change D+/D-, host mode
SL811Write(CtrlReg,0x08); // Reset USB engine, full-speed setup, suspend disable
EZUSB_Delay(10); // 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
EZUSB_Delay(10); // Delay for HW stablize
if(SL811Read(IntStatus)&USB_RESET)
{ // test for USB reset
SL811Write(IntStatus,INT_CLEAR);// Clear Interrupt enable status
EZUSB_Delay(30); // Blink LED - waiting for slave USB plug-in
// OUTB ^= ACTIVE_BLINK; // Blink Active LED
// OUTA |= PORTX_LED; // clear debug LEDs
return 0; // exit speed_detect()
}
if((SL811Read(IntStatus)&USB_DPLUS)==0) // Checking full or low speed
{ // ** Low Speed is detected ** //
SL811Write(cSOFcnt,0xEE); // Set up host and low speed direct and SOF cnt
SL811Write(cDATASet,0xE0); // SOF Counter Low = 0xE0; 1ms interval
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
{ // ** Full Speed is detected ** //
SL811Write(cSOFcnt,0xAE); // Set up host & full speed direct and SOF cnt
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
}
// OUTB |= ACTIVE_BLINK; // clear Active LED
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
EZUSB_Delay(25); // 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 EZUSB enumeration
//-------------------------------------------------------------------------
if(!CONFIG_DONE) // start SL811H after EZ-USB is configured
return 0;
//-------------------------------------------------------------------------
// Wait for SL811HS enumeration
//-------------------------------------------------------------------------
if(!SLAVE_ENUMERATED) // only if slave is not configured
{
speed_detect(); // wait for an USB device to be inserted to
if(SLAVE_FOUND) // the SL811HST host
{
if(EnumUsbDev(1)) // enumerate USB device, assign USB address = #1
{
SLAVE_ENUMERATED = TRUE; // Set slave USB device enumerated flag
uHub.bPortPresent[1] = 1; // set device addr #1 present
//Set_ezDEV(1); // inform master of new attach/detach
}
}
}
//-------------------------------------------------------------------------
// SL811HS enumerated, proceed accordingly
//-------------------------------------------------------------------------
else
{
// OUTB &= ~ACTIVE_BLINK; // Turn on Active LED, indicate successful slave enum
if(Slave_Detach()) // test for slave device detach ???
return 0; // exit now.
//----------------------------------------------
// HUB DEVICE Polling (Addr #1, EndPt #1)
//----------------------------------------------
// Polling of Hub deivce Endpoint #1 for any Port Attachement
// for onboard HUB device, after enumeration, start to
// transfer IN token to check for ports attachment
// wLen = wPayload = 1 byte, always use USB address #1
// if return is TRUE, a data pkt was ACK, data in HubChange
// else is a NAK, no data was received
if(HUB_DEVICE && dsPoll)
{
retDataRW = DataRW(HUB_ADDR,uDev[1].bEPAddr[1],uDev[1].wPayLoad[1],1,HubChange);
if(retDataRW)
{
// HubPortEnum();
// Set_ezDEV(1); // inform master of new attach/detach
}
EZUSB_Delay(10); // maintain a polling interval
}
} // end of else
return 1;
}
//*****************************************************************************************
// 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
// Set_ezDEV(1); // inform master of slave detach
SL811Write(IntStatus,INT_CLEAR); // clear interrupt status
return TRUE; // exit now !!!
}
return FALSE;
}
//*****************************************************************************************
// Indicate to EZUSB's endpoint #2 IN of a new device attach/detach
//*****************************************************************************************
/*void Set_ezDEV(BYTE chg)
{
if( (dsPoll) && (!(IN2CS & bmEPBUSY)) )
{
IN2BUF[0] = chg; // Arm endpoint #2, inform EZUSB host of attach/detatch
IN2BC = 1;
}
}*/
//*****************************************************************************************
// SL811H variables initialization
//*****************************************************************************************
void sl811h_init(void) //SL811H初始化
{
unsigned int j;
// P1M1=0x10;
// P1M2=0x24;
// P3M1=0x00;
// P3M2=0x00;
for(j=0;j<MAX_DEV;j++)
{
uHub.bPortPresent[j] = 0;
uHub.bPortNumber[j] = 0;
}
SL811H_DATA = 0x00;
SL811H_ADDR = 0x00;
pNumPort = 0x00;
FULL_SPEED = TRUE;
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
DOSave = DOSave & 0xBF;
DO = DOSave;
// SL811_RST=0;
for(j=0;j<400;j++);
// kick_watchdog(); //踢看门狗
// EZUSB_Delay(5);
DOSave = DOSave | 0x40;
DO = DOSave;
// SL811_RST=1;
DOSave = DOSave & 0x7F;
DO = DOSave;//M_S=0;
for(j=0;j<400;j++);
// kick_watchdog(); //踢看门狗
//OUTB &= ~nRESET; // reset SL811HST
//EZUSB_Delay(5); // for 5ms
//OUTB |= nRESET; // clear reset
//OUTB &= ~nHOST_SLAVE_MODE; // set to Host mode
//SL811Write(14,0xE0);
//SL811Write(15,0xAE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -