📄 host_811.c
字号:
//------------------------------------------------
pDev =(pDevDesc)DBUF; // ask for 64 bytes on Addr #0
if (!GetDesc(uAddr,DEVICE,0,18,DBUF)) // and determine the wPayload size
return FALSE; // get correct wPayload of Endpoint 0
uDev[usbaddr].wPayLoad[0]=pDev->bMaxPacketSize0;// on current non-zero USB address
//------------------------------------------------
// Set Slave USB Device Address
//------------------------------------------------
if (!SetAddress(usbaddr)) // set to specific USB address
return FALSE; //
uAddr = usbaddr; // transfer 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
//------------------------------------------------
// 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,WordSwap(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;
//------------------------------------------------
// 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
epLen += 7;
}
//------------------------------------------------
// Get Hid Report Descriptors
//------------------------------------------------
if(pIfc->iClass == HIDCLASS)
{
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
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 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
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)
{
int i;
for(i=0;i<MAX_DEV;i++)
{
uHub.bPortPresent[i] = 0;
uHub.bPortNumber[i] = 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
//----------------------------
// SL811H + EZUSB I/Os setup
//----------------------------
PORTBCFG &= 0xAC; // Select i/o function for PB6, PB4, PB1, PB0
OEB |= 0x43; // Set PB6(Output), PB4(Input), PB1(Output), PB0(Output)
OUTB |= 0x43; // Default output high
PORTACFG &= 0x0F; // Select i/o function for PA7~PA4
OEA |= 0xF0; // Set PA7~PA4(Output)
OUTA |= 0xF0; // Default output high
//----------------------------
// Debug Monitor I/Os setup
//----------------------------
PORTBCFG |= 0x0C; // Select alternate function RxD1(PB2) & RxD1(PB3)
PORTCCFG |= 0xC0; // Select alternate function nWR(PC6) & nRD(PC7)
// Also needed for SL811H
//----------------------------
// SL811HST hardware reset
//----------------------------
OUTB &= ~nRESET; // reset SL811HST
EZUSB_Delay(5); // for 5ms
OUTB |= nRESET; // clear reset
OUTB &= ~nHOST_SLAVE_MODE; // set to Host mode
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -