📄 811.c
字号:
if(!usbXfer())
return FALSE;
return TRUE;
}
Uint16 epBulkSend(Uint16 *pBuffer,Uint16 len)
{
usb.usbaddr=0x1;
usb.endpoint=usb.epbulkout;
usb.pid=PID_OUT;
usb.wPayLoad=64;
usb.wLen=len;
usb.buffer=pBuffer;
while(len>0)
{
if (len > usb.wPayLoad)
usb.wLen = usb.wPayLoad;
else
usb.wLen = len;
if(!usbXfer())
return FALSE;
len-=usb.wLen;
usb.buffer=usb.buffer+usb.wLen;
}
return TRUE;
}
Uint16 epBulkRcv(Uint16 *pBuffer,Uint16 len)
{
usb.usbaddr=0x1;
usb.endpoint=usb.epbulkin;
usb.pid=PID_IN;
usb.wPayLoad=64;
usb.wLen=len;
usb.buffer=pBuffer;
if(usb.wLen)
{
if(!usbXfer())
return FALSE;
}
return TRUE;
}
//*****************************************************************************************
// Set Device Address :
//*****************************************************************************************
Uint16 SetAddress(Uint16 addr)
{
usb.usbaddr=0;
usb.setup.bmRequest=0;
usb.setup.bRequest=SET_ADDRESS;
usb.setup.wValue=addr;
usb.setup.wIndex=0;
usb.setup.wLength=0;
return ep0Xfer();
}
//*****************************************************************************************
// Set Device Configuration :
//*****************************************************************************************
Uint16 Set_Configuration(void)
{
usb.setup.bmRequest=0;
usb.setup.bRequest=SET_CONFIG;
usb.setup.wIndex=0;
usb.setup.wLength=0;
usb.buffer=NULL;
return ep0Xfer();
}
//*****************************************************************************************
// Get Device Descriptor : Device, Configuration, String
//*****************************************************************************************
Uint16 GetDesc(void)
{
usb.setup.bmRequest=0x80;
usb.setup.bRequest=GET_DESCRIPTOR;
usb.setup.wValue=WordSwap(usb.setup.wValue);
usb.wPayLoad=uDev.wPayLoad[0];
return ep0Xfer();
}
//*****************************************************************************************
// 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
//*****************************************************************************************
Uint16 EnumUsbDev(Uint16 usbaddr)
{
Uint16 i; // always reset USB transfer address
Uint16 uAddr = 0; // for enumeration to Address #0
Uint16 epLen;
//------------------------------------------------
// 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(25);
//------------------------------------------------
// Get USB Device Descriptors on EP0 & Addr 0
// with default 64-byte payload
//------------------------------------------------
usb.usbaddr = uAddr;
usb.setup.wValue = DEVICE;
usb.setup.wIndex = 0;
usb.setup.wLength = 18;
usb.buffer = DBUF;
if (!GetDesc()) // and determine the wPayload size
return FALSE; // get correct wPayload of Endpoint 0
uDev.wPayLoad[0]=DBUF[7]; // on current non-zero USB address
//------------------------------------------------
// Set Slave USB Device Address
//------------------------------------------------
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
//------------------------------------------------
usb.usbaddr=uAddr;
usb.setup.wLength=DBUF[0];
usb.setup.wValue=DEVICE;
usb.setup.wIndex=0;
usb.buffer=DBUF;
if (!GetDesc())
return FALSE; // For this current device:
uDev.iMfg = DBUF[14];
uDev.iPdt = DBUF[15];
//------------------------------------------------
// Get Slave USB Configuration Descriptors
//------------------------------------------------
usb.usbaddr=uAddr;
usb.setup.wValue=CONFIGURATION;
usb.setup.wIndex=0;
usb.setup.wLength=64;
usb.buffer=DBUF;
if (!GetDesc())
return FALSE;
uDev.bClass = DBUF[9+5]; // update to class type
uDev.bNumOfEPs = (DBUF[9+4] <= MAX_EP) ? DBUF[9+4] : MAX_EP;
if(uDev.bClass==8) //mass storage device
Flags.bits.bMassDevice=TRUE;
//------------------------------------------------
// Set configuration (except for HUB device)
//------------------------------------------------
usb.usbaddr=uAddr;
usb.setup.wValue=DEVICE;
// enumerating a FS/LS non-hub device
if (!Set_Configuration()) // 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
{
uDev.bEPAddr[i] = DBUF[9 + 9 + epLen+2]; // Ep address and direction
uDev.bAttr[i] = DBUF[9 + 9 + epLen+3]; // Attribute of Endpoint
uDev.wPayLoad[i] = LSwapINT16(DBUF[9 + 9 + epLen+4],DBUF[9 + 9 + epLen+5]); // Payload of Endpoint
uDev.bInterval[i] = DBUF[9 + 9 + epLen+6]; // Polling interval
uDev.bData1[i] = 0; // init data toggle
epLen += 7;
//////////////////////////////
if(uDev.bAttr[i]==0x2) //bulk transfer
{
if(uDev.bEPAddr[i]&0x80)
usb.epbulkin=uDev.bEPAddr[i];
else
usb.epbulkout=uDev.bEPAddr[i];
}
//////////////////////////////
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////////
void SL811_Init(void)
{
Flags.bits.SLAVE_ONLINE = FALSE;
Flags.bits.SLAVE_FOUND = FALSE;
Flags.bits.SLAVE_REMOVED = FALSE;
Flags.bits.SLAVE_ENUMERATED = FALSE;
Flags.bits.SLAVE_IS_ATTACHED = FALSE;
///////////////////////////////////////////////////////
SL811Write(CtrlReg,0x00); //0x04
DelayMs(100);
SL811Write(CtrlReg,0x01);
SL811Write(cDATASet,0xe0);
SL811Write(cSOFcnt,0xae);
SL811Write(EP0Status,0x50);
SL811Write(EP0Counter,0);
SL811Write(EP0Control,0x01);
SL811Write(IntEna,0x20); // USB-A, Insert/Remove, USB_Resume.
SL811Write(IntStatus,INT_CLEAR); // Clear Interrupt enable status
}
void check_usb(void)
{
unsigned char intr;
intr=SL811Read(IntStatus);
if (intr & 0x40)
{
if(Flags.bits.SLAVE_ONLINE ==TRUE)
{
Flags.bits.SLAVE_REMOVED=TRUE;
Flags.bits.SLAVE_ONLINE =FALSE;
}
}
else{
if(Flags.bits.SLAVE_ONLINE == FALSE)
{
Flags.bits.SLAVE_FOUND=TRUE;
Flags.bits.SLAVE_ONLINE =TRUE;
}
}
SL811Write(IntStatus,INT_CLEAR);
SL811Write(IntStatus,INSERT_REMOVE);
}
void play_Udisc()
{
Uint32 time_count;
SL811_Init();
while(TRUE)
{
time_count++;
if (time_count>10000)
{
time_count = 0;
check_usb();
}
if(Flags.bits.SLAVE_FOUND)
{
Flags.bits.SLAVE_FOUND=FALSE;
DelayMs(250);
if(EnumUsbDev(1)) // enumerate USB device, assign USB address = #1
Flags.bits.SLAVE_ENUMERATED = TRUE; // Set slave USB device enumerated flag
}
if(Flags.bits.SLAVE_REMOVED)
{
Flags.bits.SLAVE_REMOVED=FALSE;
Flags.bits.SLAVE_ENUMERATED = FALSE;
Flags.bits.SLAVE_IS_ATTACHED = FALSE;
}
}
}
//=================================== end ===========================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -