📄 host_811.c
字号:
// last xferlen of data was completed, i.e.
// remainder is equal to zero, not a 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 = (BYTE)(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 a 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
// a short packet or empty data packet OR when
// requested data len have completed, i.e.wLen=0
// For a LOWSPEED device, the 1st device descp,
// wPayload is default to 64-byte, LS device will
// only send back a max of 8-byte device descp,
// and host detect this as a 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 a 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's USB Data Xfer
// ep0Xfer, endpoint 0 data transfer
//*****************************************************************************************
unsigned char ep0Xfer(BYTE usbaddr, WORD payload, pSetupPKG setup, BYTE *pData)
{
BYTE pid = PID_IN;
WORD wLen = WordSwap(setup->wLength); // swap back for correct length
BYTE ep0 = 0; // always endpoint zero
////////////////////////////////////////////////////////////////////////
//----------------------------------------------------
// SETUP token with 8-byte request on endpoint 0
//----------------------------------------------------
if (!usbXfer(usbaddr, ep0, PID_SETUP,payload, 8, (BYTE*)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
//*****************************************************************************************
unsigned char VendorCmd(BYTE usbaddr,BYTE bReq,BYTE bCmd,WORD wValue,WORD wIndex,WORD wLen,BYTE *pData)
{
SetupPKG setup;
setup.bmRequest = bReq;
setup.bRequest = bCmd;
setup.wValue = wValue;
setup.wIndex = wIndex;
setup.wLength = WordSwap(wLen);
return ep0Xfer(usbaddr, uDev.wPayLoad[0], &setup, pData);
}
//*****************************************************************************************
// Set Device Address :
//*****************************************************************************************
unsigned char SetAddress(WORD addr)
{
return VendorCmd(0,0,SET_ADDRESS, WordSwap(addr), 0, 0, NULL);
}
//*****************************************************************************************
// Set Device Configuration :
//*****************************************************************************************
unsigned char SetConfiguration(BYTE usbaddr, WORD wVal)
{
return VendorCmd(usbaddr, 0, SET_CONFIG, WordSwap(wVal), 0, 0, NULL);
}
//*****************************************************************************************
// Get Device Descriptor : Device, Configuration, String
//*****************************************************************************************
unsigned char GetDesc(BYTE usbaddr, WORD wValue, WORD wIndex, WORD wLen, BYTE *desc)
{
return VendorCmd(usbaddr, 0x80, GET_DESCRIPTOR, wValue, 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
//*****************************************************************************************
unsigned char EnumUsbDev(BYTE usbaddr)//地址总是1;
{
BYTE i; // always reset USB transfer address
BYTE uAddr = 0; // for enumeration to Address #0
BYTE epLen;
pDevDesc pDev; //四个描述符;
pCfgDesc pCfg;
pIntfDesc pIfc;
pEPDesc pEnp;
////////////////////////////////////////////////////////////////////////枚举在端点0,地址0发生;
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 a hub)
pDev =(pDevDesc)bBUF; // bBUF是单片机上的缓冲去;
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)
{ 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;
SL811Write(IntEna,0x20); //只允许设备移入和移除;
SL811Write(IntStatus,INT_CLEAR);
SL811Write(cSOFcnt,0xae);
DelayMs(10);
SL811Write(IntStatus,INT_CLEAR);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -