📄 sl811.c
字号:
return TRUE; // return OK
}
return FALSE; // fail transmission
}
//*****************************************************************************************
// Control Endpoint 0's USB Data Xfer
// ep0Xfer, endpoint 0 data transfer
//*****************************************************************************************
bit ep0Xfer(uchar usbaddr,uint payload,pSetupPKG setup,uchar *pData)
{
idata uchar pid=PID_IN;
//uchar ep0=0; // always endpoint zero
uint wLen=SwapINT16(setup->wLength); // swap back for correct length
//----------------------------------------------------
// SETUP token with 8-byte request on endpoint 0
//----------------------------------------------------
if (!usbXfer(usbaddr,0,PID_SETUP,payload,8,(uchar *)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,0,PID_IN,payload,wLen,pData))
return FALSE;
payload=0;
}
else // device-to-host : OUT token
{
if(!usbXfer(usbaddr,0,PID_OUT,payload,wLen,pData))
return FALSE;
}
}
//----------------------------------------------------
// Status stage IN or OUT zero-length data packet
//----------------------------------------------------
if(!usbXfer(usbaddr,0,pid,payload,0,NULL))
return FALSE;
return TRUE;
}
bit epBulkSend(unsigned char *pBuffer,unsigned int len)
{
// usbstack.usbaddr=0x1;
// usbstack.endpoint=usbstack.epbulkout;
// usbstack.pid=PID_OUT;
// usbstack.wPayload=64;
// usbstack.wLen=len;
// usbstack.buffer=pBuffer;
// if(usbstack.wLen)
// while(!usbXfer());
// return TRUE;
if(usbXfer(1,uDev.epbulkout&0x0F,PID_OUT,uDev.wPayLoad[uDev.epbulkout&0x0F],len,pBuffer)) //64
// if(usbXfer(1,2,PID_OUT,64,len,pBuffer)) //64
return TRUE;
return FALSE;
}
bit epBulkRcv(unsigned char *pBuffer,unsigned int len)
{
// usbstack.usbaddr=0x1;
// usbstack.endpoint=usbstack.epbulkin;
// usbstack.pid=PID_IN;
// usbstack.wPayload=64;
// usbstack.wLen=len;
// usbstack.buffer=pBuffer;
// while(usbstack.wLen)
// while(!usbXfer());
// return TRUE;
if(usbXfer(1,uDev.epbulkin&0x0F,PID_IN,uDev.wPayLoad[uDev.epbulkin&0x0F],len,pBuffer)) //64
// if(usbXfer(1,1,PID_IN,64,len,pBuffer)) //64
return TRUE;
return FALSE;
}
//*****************************************************************************************
// Control endpoint
//*****************************************************************************************
bit VendorCmd(uchar usbaddr,uchar bReq,uchar bCmd,uint wValue,uint wIndex,uint wLen,uchar *pData)
{
xdata SetupPKG setup;
setup.bmRequest=bReq;
setup.bRequest=bCmd;
setup.wValue=wValue;
setup.wIndex=wIndex;
setup.wLength=SwapINT16(wLen);
return ep0Xfer(usbaddr,uDev.wPayLoad[0], &setup,pData);
}
//*****************************************************************************************
// Set Device Address :
//*****************************************************************************************
//bit SetAddress(uint addr)
// {
// return VendorCmd(0,0,SET_ADDRESS,SwapINT16(addr),0,0,NULL);
// }
//*****************************************************************************************
// Set Device Configuration :
//*****************************************************************************************
//bit Set_Configuration(uchar usbaddr,uint wVal)
// {
// return VendorCmd(usbaddr,0,SET_CONFIG,SwapINT16(wVal),0,0,NULL);
// }
//*****************************************************************************************
// Get Device Descriptor : Device, Configuration, String
//*****************************************************************************************
//bit GetDesc(uchar usbaddr,uint wValue,uint wIndex,uint wLen,uchar *desc)
// {
// return VendorCmd(usbaddr,0x80,GET_DESCRIPTOR,wValue,wIndex,wLen,desc);
// }
//*****************************************************************************************
// USB Data Endpoint Read/Write
// wLen is in low byte first format
//*****************************************************************************************
//bit DataRW(uchar usbaddr,uchar epaddr,uint wPayload,uint wLen,uchar *pData)
// {
// xdata uchar pid=PID_OUT;
// if(epaddr&0x80) // get direction of transfer
// pid=PID_IN;
// if(usbXfer(usbaddr,epaddr&0x0F,pid,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
//*****************************************************************************************
bit EnumUsbDev(uchar usbaddr)
{
int i; // always reset USB transfer address
idata uchar uAddr=0; // for enumeration to Address #0
idata uchar epLen;
// idata uint strLang;
//------------------------------------------------
// Reset only Slave device attached directly
//------------------------------------------------
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)
// DelayMs(50);
pDev =(pDevDesc)DBUF; // ask for 64 bytes on Addr #0
// if (!GetDesc(uAddr,DEVICE,0,18,DBUF)) // and determine the wPayload size
if(!VendorCmd(uAddr,0x80,GET_DESCRIPTOR,DEVICE,0,18,DBUF))
return FALSE; // get correct wPayload of Endpoint 0
uDev.wPayLoad[0]=pDev->bMaxPacketSize0;// on current non-zero USB address
//------------------------------------------------
// Set Slave USB Device Address
//------------------------------------------------
if(!VendorCmd(0,0,SET_ADDRESS,SwapINT16(usbaddr),0,0,NULL))
// 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))
if (!VendorCmd(uAddr,0x80,GET_DESCRIPTOR,DEVICE,0,(pDev->bLength),DBUF))
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
//------------------------------------------------
// Get Slave USB Configuration Descriptors
//------------------------------------------------
pCfg=(pCfgDesc)DBUF;
// if(!GetDesc(uAddr,CONFIGURATION,0,8,DBUF))
if(!VendorCmd(uAddr,0x80,GET_DESCRIPTOR,CONFIGURATION,0,8,DBUF))
return FALSE;
// if(!GetDesc(uAddr,CONFIGURATION,0,SwapINT16(pCfg->wLength),DBUF))
if(!VendorCmd(uAddr,0x80,GET_DESCRIPTOR,CONFIGURATION,0,SwapINT16(pCfg->wLength),DBUF))
return FALSE;
pIfc=(pIntfDesc)(DBUF+9); // point to Interface Descp
uDev.bClass=pIfc->iClass; // update to class type
uDev.bNumOfEPs=(pIfc->bEndPoints<=MAX_EP)?pIfc->bEndPoints:MAX_EP;
if(uDev.bClass==8) //mass storage device
bMassDevice=TRUE;
//------------------------------------------------
// Set configuration (except for HUB device)
//------------------------------------------------
if (uDev.bClass!=HUBCLASS) // enumerating a FS/LS non-hub device
{
if(!VendorCmd(uAddr,0,SET_CONFIG,DEVICE,0,0,NULL))
// 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.bNumOfEPs;i++) // For each data endpoint
{
pEnp=(pEPDesc)(DBUF+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]=SwapINT16(pEnp->wPayLoad); // Payload of Endpoint
uDev.bInterval[i]=pEnp->bInterval; // Polling interval
uDev.bData1[i]=0; // init data toggle
epLen+=7;
if(uDev.bAttr[i]==0x2)
{
if(uDev.bEPAddr[i]&0x80)
uDev.epbulkin=uDev.bEPAddr[i];
else
uDev.epbulkout=uDev.bEPAddr[i];
}
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////////
void SL811_Init(void)
{
hardware_reset_811();
MASTER_SLAVER=0;
SLAVE_IS_ATTACHED=FALSE;
SEL_OTHERS=1;
DelayMs(50);
SL811Write(cSOFcnt,0xAE);
SL811Write(CtrlReg,0x08);
DelayMs(50);
SL811Write(CtrlReg,0x00);
SL811Write(IntEna,0x61);
SL811Write(IntStatus,INT_CLEAR);
// DelayMs(50);
/* SL811Write(cDATASet,0xE0);
SL811Write(CtrlReg,0x5);
SL811Write(EP0Status,0xD0);
SL811Write(EP0Counter,0x00);
SL811Write(EP0Control,0x01);
SL811Write(IntEna,0x20); // USB-A, Insert/Remove, USB_Resume.
SL811Write(IntStatus,INT_CLEAR); // Clear Interrupt enable status*/
}
bit check_u_insert(void)
{
uchar intr;
intr=SL811Read(IntStatus);
if(intr==0x80)
{
SL811Write(IntStatus,INT_CLEAR);
SL811Write(cSOFcnt,0xAE);
SL811Write(cDATASet,0xE0);
SL811Write(CtrlReg,0x05);
SL811Write(EP0Status,0xD0);
SL811Write(EP0Counter,0x00);
SL811Write(EP0Control,0x01);
DelayMs(50);
SL811Write(IntStatus,INT_CLEAR);
return 1;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -