📄 host_811.c
字号:
}
}
else
{
TIMEOUT_ERR=15;
return FALSE;
}
//------------------------------------------------
// For each slave endpoints, get its attributes
// Excluding endpoint0, only data endpoints
//------------------------------------------------
epLen = 0;
for (i=1; i<=uDev[uAddr].bNumOfEPs; i++) // For each data endpoint
{
pEnp = (pEPDesc)(DBUF + 9 + 9 + epLen); // point to Endpoint Descp(non-HID)
uDev[uAddr].bEPAddr[i] = pEnp->bEPAdd; // Ep address and direction
uDev[uAddr].bAttr[i] = pEnp->bAttr; // Attribute of Endpoint
uDev[uAddr].wPayLoad[i] = (WORD)pEnp->wPayLoad; // Payload of Endpoint
uDev[uAddr].bInterval[i] = pEnp->bInterval; // Polling interval
uDev[uAddr].bData1[i] = 0; // init data toggle
epLen += 7;
}
return TRUE;
}
//*****************************************************************************************
// Full-speed and low-speed detect - Device atttached directly to SL811HS
//*****************************************************************************************
int speed_detect()
{
BYTE kk; //
BYTE temp; //edit by ricky
//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
usb_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
usb_Delay(10); // Delay for HW stablize
SL811Read(IntStatus,&temp);
if(temp&USB_RESET)
{ // test for USB reset
SL811Write(IntStatus,INT_CLEAR);// Clear Interrupt enable status
usb_Delay(30); // Blink LED - waiting for slave USB plug-in
return 0; // exit speed_detect()
}
SL811Write(IntStatus,INT_CLEAR); // Clear Interrupt status
SL811Read(IntStatus,&kk);
SL811Read(0x33,&kk);
SL811Read(IntStatus,&temp);
if(( temp &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,0x01); // Setup 48MHz and SOF enable//edit by ricky,orignal is ,0x05)to start SOF
uHub.bPortSpeed[1] = 0; // full speed for Device #1
}
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
usb_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 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(2)) // 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
return TRUE;
}
}
}
//-------------------------------------------------------------------------
// SL811HS enumerated, proceed accordingly
//-------------------------------------------------------------------------
else
{
if(Slave_Detach()) // test for slave device detach ???
return FALSE; // exit now.
} // end of else
}
//*****************************************************************************************
// Slave_Detach
//*****************************************************************************************
int Slave_Detach(void)
{
BYTE temp;
SL811Read(IntStatus,&temp);
if( (temp &INSERT_REMOVE) || (temp &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;
}
//*****************************************************************************************
// SL811H variables initialization
//*****************************************************************************************
void sl811h_init(void)
{
int i;
BYTE ricky;
for(i=0;i<MAX_DEV;i++)
{
uHub.bPortPresent[i] = 0;
uHub.bPortNumber[i] = 0;
}
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;
dsPoll = 1; // poll downstream port conections
//----------------------------
// SL811HST hardware reset
//added by csl
//----------------------------
//rPDATC &=0xffef;
usb_Delay(50); // for 5ms
//rPDATC |=0x00000010; //disable the reset and the light on
//USB memory test,read 0x0e first
usb_com=0x0e;/*rPDATC &=0xfffe; //low A0
rPDATC &=0xfff9; //low CS and WR
rPDATD =0x0e; //output address*/
ricky=usb_data;
/*rPDATC |=0x0004; //high WR
rPDATC |=0x01; //high A0
rPDATD=0xff;
rPCOND=0x0000; //PORT D input PORT
rPUPD=0x00;
rPDATC &=0xfff7; //LOW OE
ricky=rPDATD;
rPDATC |=0x08; //high OE
rPDATC |=0x02; //high CS*/
}
//*************************************************************************
//memory test verifies read/write operation of the sl811hs internal memory
//*************************************************************************
int SL811HMemTest()
{
int i;
BYTE j;
for(i=EP0Buf;i<=cMemEnd;i++)
{
SL811Write((BYTE)i,(BYTE)(i+2));
DBUF[i]=i+2;
}
for(i=0;i<0x40;i++)
DBUF[i]=i;
for (i = EP0Buf; i <=cMemEnd; i++) // verify data
{
SL811Read((BYTE)i,&j);
if ((BYTE)(i +2)!= j)
return 0;
SL811Write ((BYTE)i, (BYTE)~i);
SL811Read((BYTE)i,&j);
if ((BYTE)~i != j)
return 0;
}
SL811BufWrite(0x10,&DBUF[2],0xef);
SL811BufRead(0x10,&DBUF[0],0xef);
// auto increment: addr = data
//add late
// clear all SL811H/SL11H Memory
for(i=0;i<=cMemEnd;i++)
DBUF[i]=0;
i=0x10;
SL811BufWrite((BYTE)i,&DBUF[i],0xf0);
return 1;
}
//***********************************************************************************
//mass storage bulk only transfer,one mission
//return 1 success;
//return 0 FALSE( other condition,the host shall reset recovery
//return 2 command error.
//return 3 phase error.
//***********************************************************************************
int BulkOnlyXfer(BYTE usbaddr,pCBW CBW,BYTE *pData,int in_delay,int out_delay)
{
BYTE b1,buf[16] ;
pCSW pCSW1;
DWORD tmp,tmp2;
//--------------------------------------
//CBW transport
//--------------------------------------
if (!DataRW(usbaddr,1,uDev[usbaddr].wPayLoad[1],0x1f,(BYTE*)CBW))
{
//should add error control
TIMEOUT_ERR=0x20;
return FALSE;
}
//-------------------------------------
//IN or OUT data stage on bulk transfor
//-------------------------------------
tmp=((DWORD)(CBW->dCBWDataTransferLength[0]))|((DWORD)(CBW->dCBWDataTransferLength[1])<<8)|((DWORD)(CBW->dCBWDataTransferLength[2])<<16)|((DWORD)(CBW->dCBWDataTransferLength[3])<<24);
if (tmp) //if there are data for transfer
{
if(CBW->bmCBWFlags&0x80) //data in from the device to the host
{
usb_Delay(in_delay);// 0,1500,2500
if (!DataRW(usbaddr,0x82,uDev[usbaddr].wPayLoad[2],tmp,pData))
{
TIMEOUT_ERR=0x21;
//return FALSE;
if(GetEndpStatus(2, &buf[0]))
{
if(buf[0]==1)
ClearFeature(2);
}
}
}
else
{
usb_Delay(out_delay); // 0,50,850
if(!DataRW(usbaddr,0x01,uDev[usbaddr].wPayLoad[1],tmp,pData))
{
TIMEOUT_ERR=0x22;
//return FALSE;
if(GetEndpStatus(1, &buf[0]))
{
if(buf[0]==1)
ClearFeature(1);
}
}
}
}
//------------------------------------
//CSW transport
//------------------------------------
//here shall add STALL Bulk_In and Bulk_Error check $$$
if(CBW->bmCBWFlags&0x80)
usb_Delay(20);
else
usb_Delay(20);
if (!DataRW(usbaddr,0x82,uDev[usbaddr].wPayLoad[2],13,&buf[0]))
{
TIMEOUT_ERR=0x23;
if(GetEndpStatus(2, &b1))
{
if(b1==1) //if endpoint 1 halt
ClearFeature(2); //clear bulk in pipe
usb_Delay(10);
if (!DataRW(usbaddr,0x82,uDev[usbaddr].wPayLoad[2],13,&buf[0]))
return FALSE;
}
}
pCSW1=(pCSW)&buf[0];
tmp2=((DWORD)(pCSW1->dCSWDataResidue[0]))|((DWORD)(pCSW1->dCSWDataResidue[1])<<8)
|((DWORD)(pCSW1->dCSWDataResidue[2])<<16)|((DWORD)(pCSW1->dCSWDataResidue[3])<<24);
//if the contents of the CSW is valid
if((pCSW1->dCSWSignature[3] == 0x53)&& (pCSW1->dCSWSignature[2] == 0x42)
&&(pCSW1->dCSWSignature[1] == 0x53)&&(pCSW1->dCSWSignature[0] == 0x55)
&&(pCSW1->dCSWTag[0] == (CBW->dCBWTag[0]))&&(pCSW1->dCSWTag[1] == (CBW->dCBWTag[1]))
&&(pCSW1->dCSWTag[2] == (CBW->dCBWTag[2]))&&(pCSW1->dCSWTag[3] == (CBW->dCBWTag[3])))
{
//if the content of the CSW is meaningful
if((pCSW1->bCSWStatus==0)&&(tmp2<=tmp))
{
return TRUE; //command passed
}
else if((pCSW1->bCSWStatus==1)&&(tmp2<=tmp))
{
return 2; //command failed
}
else if(pCSW1->bCSWStatus==2)
{
TIMEOUT_ERR=0x24;
return 3; //phase error
}
// CSW not meaningful (the device may STALL the bulk in pipe)
else
{
TIMEOUT_ERR=0x25;
return FALSE;
}
}
//CSW not valid
else
{
TIMEOUT_ERR=0x26;
return FALSE;
}
}
//******************************************************
//
//******************************************************
int ClearFeature(BYTE endpoint)
{
//bData1=0;
//uDev[2].bData1[1]=0;
//uDev[2].bData1[2]=0;
return VendorCmd(2, 2,1,0,endpoint,0,NULL);
}
//*****************************************************
//CBW input function
//****************************************************
void CBW(pCBW sCBW1,DWORD dcbwTag,DWORD transLength,BYTE flag,BYTE CBLength,
BYTE CB0,BYTE CB1,BYTE CB2,BYTE CB3,BYTE CB4,BYTE CB5,BYTE CB6,
BYTE CB7,BYTE CB8,BYTE CB9,BYTE CBa,BYTE CBb,BYTE CBc,BYTE CBd,BYTE CBe,BYTE CBf)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -