📄 sl811hst.c
字号:
DevInfo[DescBufLen++] = uDev[EpAddr].bAttr[i+1]; // transfer type
(WORD)DevInfo[DescBufLen++] = uDev[EpAddr].wPayLoad[i+1]; // max data payload
DescBufLen++; // 2 byte payload
DevInfo[DescBufLen++] = uDev[EpAddr].bInterval[i+1]; // polling interval
}
}
}
return(DescBufLen);
}
//*****************************************************************************************
// USB Data Endpoint Read/Write
// wLen is in low byte first format
//*****************************************************************************************
int DataRW(BYTE usbaddr, BYTE epaddr, WORD wPayload, WORD wLen, BYTE *pData)
{
BYTE pid = PID_OUT;
if(epaddr & 0x80) // get direction of transfer
pid = PID_IN;
if(usbXfer(usbaddr,epaddr&0x0F,pid,0,wPayload,wLen,pData))
return TRUE;
return FALSE;
}
//*****************************************************************************************
// USB Device Enumeration Process
// Support 1 confguration and interface #0 and alternate setting #0 only
// Support up to 1 control endpoint + 4 data endpoint only
//*****************************************************************************************
int EnumUsbDev(BYTE usbaddr)
{
int i; // always reset USB transfer address
BYTE uAddr = 0; // for enumeration to Address #0
BYTE epLen;
WORD strLang;
//------------------------------------------------
// Reset only Slave device attached directly
//------------------------------------------------
uDev[0].wPayLoad[0] = 64; // default 64-byte payload of Endpoint 0, address #0
if(usbaddr == 1) // bus reset for the device attached to SL811HS only
USBReset(); // that will always have the USB address = 0x01 (for a hub)
Delay(25);
//------------------------------------------------
// Get USB Device Descriptors on EP0 & Addr 0
// with default 64-byte payload
//------------------------------------------------
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; //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)
//------------------------------------------------
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;
}
//------------------------------------------------
// 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;
}
// 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
DATA_STOP = FALSE; //
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&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!! Register for status=0x%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
FULL_SPEED = FALSE; // low speed device flag
}
else
{
Uart_Printf("'Full' Speed is detected!! Register for status=0x%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
}
}
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
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;
for(i = 0x10; i< 0xff; i++) //addr = data
SL811Write((BYTE)i,(BYTE)i);
for(i = 0x10; i<0xff; i++) //verify data
{
if((BYTE)i != SL811Read((BYTE)i))
errors++;
SL811Write((BYTE)i,(BYTE)~i);
if((BYTE)~i != SL811Read((BYTE)i))
errors++;
}
if(errors != 0)
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
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -