📄 sl811.c
字号:
usbstack.wPayload=64;
usbstack.wLen=len;
usbstack.buffer=pBuffer;
if(usbstack.wLen)
{
if(!usbXfer())
return FALSE;
}
return TRUE;
}
//*****************************************************************************************
// Set Device Address :
//*****************************************************************************************
unsigned char SetAddress(unsigned char addr)
{
usbstack.usbaddr=0;
usbstack.setup.bmRequest=0;
usbstack.setup.bRequest=SET_ADDRESS;
usbstack.setup.wValue=addr;
usbstack.setup.wIndex=0;
usbstack.setup.wLength=0;
return ep0Xfer();
}
//*****************************************************************************************
// Set Device Configuration :
//*****************************************************************************************
unsigned char Set_Configuration(void)
{
usbstack.setup.bmRequest=0;
usbstack.setup.bRequest=SET_CONFIG;
usbstack.setup.wIndex=0;
usbstack.setup.wLength=0;
usbstack.buffer=NULL;
return ep0Xfer();
}
//*****************************************************************************************
// Get Device Descriptor : Device, Configuration, String
//*****************************************************************************************
unsigned char GetDesc(void)
{
usbstack.setup.bmRequest=0x80;
usbstack.setup.bRequest=GET_DESCRIPTOR;
usbstack.setup.wValue=WordSwap(usbstack.setup.wValue);
usbstack.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
//*****************************************************************************************
unsigned char EnumUsbDev(U8 usbaddr)
{
unsigned char i; // always reset USB transfer address
unsigned char uAddr = 0; // for enumeration to Address #0
unsigned char epLen;
//------------------------------------------------
// Reset only Slave device attached directly
//------------------------------------------------
uDev.wPayLoad[0] = 64; // default 64-U8 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)
USB_Delay(25);
//------------------------------------------------
// Get USB Device Descriptors on EP0 & Addr 0
// with default 64-U8 payload
//------------------------------------------------
usbstack.usbaddr=uAddr;
usbstack.setup.wValue=DEVICE;
usbstack.setup.wIndex=0;
usbstack.setup.wLength=18;
usbstack.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
//------------------------------------------------
usbstack.usbaddr=uAddr;
usbstack.setup.wLength=DBUF[0];
usbstack.setup.wValue=DEVICE;
usbstack.setup.wIndex=0;
usbstack.buffer=DBUF;
if (!GetDesc())
return FALSE; // For this current device:
uDev.iMfg = DBUF[14];
uDev.iPdt = DBUF[15];
//------------------------------------------------
// Get Slave USB Configuration Descriptors
//------------------------------------------------
usbstack.usbaddr=uAddr;
usbstack.setup.wValue=CONFIGURATION;
usbstack.setup.wIndex=0;
usbstack.setup.wLength=64;
usbstack.buffer=DBUF;
if (!GetDesc())
return FALSE;
uDev.bClass = DBUF[9+5] & 0xFF; // update to class type
uDev.bNumOfEPs = (DBUF[9+4] <= MAX_EP) ? DBUF[9+4] : MAX_EP;
if(uDev.bClass==8) //mass storage device
bFlags.bits.bMassDevice=TRUE;
//------------------------------------------------
// Set configuration (except for HUB device)
//------------------------------------------------
usbstack.usbaddr=uAddr;
usbstack.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] & 0xFF; // Ep address and direction
uDev.bAttr[i] = DBUF[9 + 9 + epLen+3] & 0xFF; // 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)
usbstack.epbulkin=uDev.bEPAddr[i];
else
usbstack.epbulkout=uDev.bEPAddr[i];
}
//////////////////////////////
}
return TRUE;
}
//*****************************************************************************************
// Full-speed and low-speed detect - Device atttached directly to SL811HS
//*****************************************************************************************
int speed_detect()
{
bFlags.bits.SLAVE_FOUND = FALSE;
bFlags.bits.FULL_SPEED = TRUE;
bFlags.bits.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
// printf("status=%x\n", SL811Read(IntStatus));
if(SL811Read(IntStatus)&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()
}
if((SL811Read(IntStatus)&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
bFlags.bits.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,0x05); // Setup 48MHz and SOF enable
}
bFlags.bits.SLAVE_FOUND = TRUE; // Set USB device found flag
bFlags.bits.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();
}
//*****************************************************************************************
// Slave_Detach
//*****************************************************************************************
int Slave_Detach(void)
{
if( (SL811Read(IntStatus)&INSERT_REMOVE) || (SL811Read(IntStatus)&USB_RESET) )
{ // Test USB detached?
bFlags.bits.SLAVE_ENUMERATED = FALSE; // return to un-enumeration
SL811Write(IntStatus,INT_CLEAR); // clear interrupt status
printf("Device Detached\n");
return TRUE; // exit now !!!
}
return FALSE;
}
//*****************************************************************************************
// Detect USB Device
//*****************************************************************************************
int slave_detect(void)
{
//-------------------------------------------------------------------------
// Wait for EZUSB enumeration
//-------------------------------------------------------------------------
//if(!CONFIG_DONE) // start SL811H after EZ-USB is configured
// return 0;
//-------------------------------------------------------------------------
// Wait for SL811HS enumeration
//-------------------------------------------------------------------------
if(!bFlags.bits.SLAVE_ENUMERATED) // only if slave is not configured
{
speed_detect(); // wait for an USB device to be inserted to
USB_Delay(1000);
if(bFlags.bits.SLAVE_FOUND) // the SL811HST host
{
// printf("slave found\n");
if(EnumUsbDev(2)) // enumerate USB device, assign USB address = #1
{
bFlags.bits.SLAVE_ENUMERATED = TRUE; // Set slave USB device enumerated flag
printf("%s Speed Device Attached\n",bFlags.bits.FULL_SPEED?"Full":"Low");
}
}
}
Slave_Detach();
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////
void SL811_Init(void)
{
bFlags.bits.SLAVE_ONLINE = FALSE;
bFlags.bits.SLAVE_FOUND = FALSE;
bFlags.bits.SLAVE_REMOVED=FALSE;
bFlags.bits.SLAVE_ENUMERATED = FALSE;
bFlags.bits.SLAVE_IS_ATTACHED = FALSE;
///////////////////////////////////////////////////////
SL811_BusInit();
SL811Write(CtrlReg,0x00); //0x04
USB_Delay(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(bFlags.bits.SLAVE_ONLINE ==TRUE)
{
bFlags.bits.SLAVE_REMOVED=TRUE;
bFlags.bits.SLAVE_ONLINE =FALSE;
}
}
else{
if(bFlags.bits.SLAVE_ONLINE == FALSE)
{
bFlags.bits.SLAVE_FOUND=TRUE;
bFlags.bits.SLAVE_ONLINE =TRUE;
}
}
SL811Write(IntStatus,INT_CLEAR);
SL811Write(IntStatus,INSERT_REMOVE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -