📄 host_811.c
字号:
//------------------------------------------------
// Arm for next data transfer when requested data
// length have not reach zero, i.e. wLen!=0, and
// last xferlen of data was completed, i.e.
// remainder is equal to zero, not Addr short pkt
//------------------------------------------------
if(!remainder && wLen) // remainder==0 when last xferLen
{ // was all completed or wLen!=0
addr = (dataX & 1) ? data1:data0; // select next address for data
xferLen = (UINT8)(wLen>=wPayload) ? wPayload:wLen; // get data length required
// if (FULL_SPEED) // sync with SOF transfer
cmd |= 0x20; // always sync SOF when FS, regardless
SL811Write(EP0XferLen, xferLen); // select next xfer length
SL811Write(EP0Address, addr); // data buffer addr
SL811Write(IntStatus,INT_CLEAR); // is Addr LS is on Hub.
SL811Write(EP0Control,cmd); // Enable USB transfer and re-arm
}
//------------------------------------------------
// Copy last IN token data pkt from prev transfer
// Check if there was data available during the
// last data transfer
//------------------------------------------------
if(bufLen)
{
SL811BufRead(((dataX&1)?data0:data1), buffer, bufLen);
buffer += bufLen;
}
//------------------------------------------------
// Terminate on short packets, i.e. remainder!=0
// Addr short packet or empty data packet OR when
// requested data len have completed, i.e.wLen=0
// For Addr LOWSPEED device, the 1st device descp,
// wPayload is default to 64-byte, LS device will
// only send back Addr max of 8-byte device descp,
// and host detect this as Addr short packet, and
// terminate with OUT status stage
//------------------------------------------------
if(remainder || !wLen)
break;
}
}
//-------------------------NAK----------------------------
if (result & EP0_NAK) // NAK Detected
{
if(endpoint==0) // on ep0 during enumeration of LS device
{ // happen when slave is not fast enough,
SL811Write(IntStatus,INT_CLEAR); // clear interrupt status, need to
SL811Write(EP0Control,cmd); // re-arm and request for last cmd, IN token
result = 0; // respond to NAK status only
}
else // normal data endpoint, exit now !!! , non-zero ep
break; // main loop control the interval polling
}
//-----------------------TIMEOUT--------------------------
if (result & EP0_TIMEOUT) // TIMEOUT Detected
{
if(endpoint==0) // happens when hub enumeration
{
if(++timeout >= TIMEOUT_RETRY)
{
timeout--;
break; // exit on the timeout detected
}
SL811Write(IntStatus,INT_CLEAR); // clear interrupt status, need to
SL811Write(EP0Control,cmd); // re-arm and request for last cmd again
}
else
{ // all other data endpoint, data transfer
USB_Control.TIMEOUT_ERR = TRUE; // failed, set flag to terminate transfer
break; // happens when data transfer on Addr device
} // through the hub
}
//-----------------------STALL----------------------------
if (result & EP0_STALL) // STALL detected
return TRUE; // for unsupported request.
//----------------------OVEFLOW---------------------------
if (result & EP0_OVERFLOW) // OVERFLOW detected
break;
//-----------------------ERROR----------------------------
if (result & EP0_ERROR) // ERROR detected
break;
} // end of While(1)
if (result & EP0_ACK) // on ACK transmission
return TRUE; // return OK
return FALSE; // fail transmission
}
//*****************************************************************************************
// Control Endpoint 0'Buf USB Data Xfer
// ep0Xfer, endpoint 0 data transfer
//*****************************************************************************************
UINT8 ep0Xfer(UINT8 usbaddr, UINT16 payload, pSetupPKG setup, UINT8 *pData)
{
UINT8 pid = PID_IN;
#if 0 //gary modify here
UINT16 wLen = WordSwap(setup->wLength); // swap back for correct length
#endif
UINT16 wLen = setup->wLength;
UINT8 ep0 = 0; // always endpoint zero
////////////////////////////////////////////////////////////////////////
//----------------------------------------------------
// SETUP token with 8-byte request on endpoint 0
//----------------------------------------------------
if (!usbXfer(usbaddr, ep0, PID_SETUP,payload, 8, (UINT8*)setup))
return FALSE;
//----------------------------------------------------
// IN or OUT data stage on endpoint 0
//----------------------------------------------------
if (wLen) // if there are data for transfer
{
if (setup->bmRequest & 0x80) // host-to-device : IN token
{
pid = PID_OUT;
if(!usbXfer(usbaddr, ep0, PID_IN,payload, wLen, pData))
return FALSE;
payload = 0;
}
else // device-to-host : OUT token
{
if(!usbXfer(usbaddr, ep0, PID_OUT,payload, wLen, pData))
return FALSE;
}
}
//----------------------------------------------------
// Status stage IN or OUT zero-length data packet
//----------------------------------------------------
if(!usbXfer(usbaddr, ep0, pid,payload, 0, NULL))
return FALSE;
return TRUE;
}
//*****************************************************************************************
// Control endpoint
//*****************************************************************************************
UINT8 VendorCmd(UINT8 usbaddr,UINT8 bReq,UINT8 bCmd,UINT16 wValue,UINT16 wIndex,UINT16 wLen,UINT8 *pData)
{
UINT8 *x=0;
UINT8 y[30],i;
SetupPKG setup;
setup.bmRequest = bReq;
setup.bRequest = bCmd;
#if 0 //Gary change here
setup.wValue = wValue;
setup.wIndex = wIndex;
setup.wLength = WordSwap(wLen);
#endif
setup.wValue = (wValue);
setup.wIndex = (wIndex);
setup.wLength = wLen;
return ep0Xfer(usbaddr, uDev.wPayLoad[0], &setup, pData);
}
//*****************************************************************************************
// Set Device Address :
//*****************************************************************************************
UINT8 SetAddress(UINT16 addr)
{
return VendorCmd(0,0,SET_ADDRESS, addr, 0, 0, NULL);
}
//*****************************************************************************************
// Set Device interface
//*****************************************************************************************
UINT8 SetInterface(UINT8 usbaddr, UINT16 wVal)
{
return VendorCmd(usbaddr, 0, SET_INTERFACE, wVal, 0, 0, NULL);
}
//*****************************************************************************************
// Set Device Configuration :
//*****************************************************************************************
UINT8 SetConfiguration(UINT8 usbaddr, UINT16 wVal)
{
return VendorCmd(usbaddr, 0, SET_CONFIG, wVal, 0, 0, NULL);
}
//*****************************************************************************************
// Get Device Descriptor : Device, Configuration, String
//*****************************************************************************************
UINT8 GetDesc(UINT8 usbaddr, UINT16 wValue, UINT16 wIndex, UINT16 wLen, UINT8 *desc)
{
return VendorCmd(usbaddr, 0x80, GET_DESCRIPTOR, WordSwap(wValue)/*Gary add*/, wIndex, wLen, desc);
}
//*****************************************************************************************
// 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
//*****************************************************************************************
UINT8 EnumUsbDev(UINT8 usbaddr)
{
UINT8 i; // always reset USB transfer address
UINT8 uAddr = 0; // for enumeration to Address #0
UINT8 epLen;
pDevDesc pDev;
pCfgDesc pCfg;
pIntfDesc pIfc;
pEPDesc pEnp;
////////////////////////////////////////////////////////////////////////
uDev.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 Addr hub)
pDev =(pDevDesc)bBUF; // ask for 64 bytes on Addr #0
if (!GetDesc(uAddr,DEVICE,0,18,bBUF)) // and determine the wPayload size
return FALSE; // get correct wPayload of Endpoint 0
uDev.wPayLoad[0]=pDev->bMaxPacketSize0; // on current non-zero USB address
if (!SetAddress(usbaddr)) // set to specific USB address
return FALSE; //
uAddr = usbaddr; // transfer using this new address
if (!GetDesc(uAddr,DEVICE,0,(pDev->bLength),bBUF))
return FALSE; // For this current device:
uDev.wVID = pDev->idVendor; // save VID
uDev.wPID = pDev->idProduct; // save PID
uDev.iMfg = pDev->iManufacturer; // save Mfg Index
uDev.iPdt = pDev->iProduct; // save Product Index
pCfg = (pCfgDesc)bBUF;
if (!GetDesc(uAddr,CONFIGURATION,0,8,bBUF))
return FALSE;
if (!GetDesc(uAddr,CONFIGURATION,0,WordSwap(pCfg->wLength),bBUF))
return FALSE;
pIfc = (pIntfDesc)(bBUF + 9); // point to Interface Descp
uDev.bClass = pIfc->iClass; // update to class type
uDev.bNumOfEPs = (pIfc->bEndPoints<=MAX_EP) ? pIfc->bEndPoints : MAX_EP;
if (!SetConfiguration(uAddr,DEVICE)) // connected directly to SL811HS
return FALSE;
if(uDev.bClass==8)
{
USB_Control.bMassDevice=TRUE;
}
epLen = 0;
for (i=1; i<=uDev.bNumOfEPs; i++) // For each data endpoint
{
pEnp = (pEPDesc)(bBUF + 9 + 9 + epLen); // point to Endpoint Descp(non-HID)
uDev.bEPAddr[i] = pEnp->bEPAdd; // Ep address and direction
uDev.bAttr[i] = pEnp->bAttr; // Attribute of Endpoint
uDev.wPayLoad[i] = pEnp->wPayLoad; // Payload of Endpoint
uDev.bInterval[i] = pEnp->bInterval; // Polling interval
uDev.bData1[i] = 0; // init data toggleu
epLen += 7;
if(uDev.bAttr[i]==0x2)
{
if(uDev.bEPAddr[i]&0x80)
uDev.bEpin=uDev.bEPAddr[i];
else
uDev.bEpOut=uDev.bEPAddr[i];
}
}
return TRUE;
}
//--------------------------------------------------------------------------
//SL811H variables initialization
//--------------------------------------------------------------------------
void sl811h_init(void)
{
SL811Write(IntEna,0x20);
SL811Write(IntStatus,INT_CLEAR);
SL811Write(cSOFcnt,0xae);
DelayMs(10);
SL811Write(IntStatus,INT_CLEAR);
USB_Control.SLAVE_FOUND = FALSE;
USB_Control.SLAVE_ENUMERATED = FALSE;
USB_Control.SLAVE_ONLINE = FALSE;
USB_Control.SLAVE_REMOVED=FALSE;
USB_Control.DATA_STOP=FALSE;
USB_Control.TIMEOUT_ERR=FALSE;
USB_Control.bMassDevice = FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -