📄 my_sl811.c.bak
字号:
}
//*****************************************************************************************
// 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(BYTE 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-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
//------------------------------------------------
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]; // 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]; // 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)
usbstack.epbulkin=uDev.bEPAddr[i];
else
usbstack.epbulkout=uDev.bEPAddr[i];
}
//////////////////////////////
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////////
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;
///////////////////////////////////////////////////////
//允许硬件产生SOF(Start of Frame)
SL811Write(CtrlReg,0x04);
DelayMs(100);
SL811Write(CtrlReg,0x01);
//设置SOF,1 ms中断
SL811Write(cDATASet,0xe0);
//设置为主机工作模式
SL811Write(cSOFcnt,0xae);
SL811Write(EP0Status,0x50);
//地址设为0
SL811Write(EP0Counter,0);
//允许数据发送
SL811Write(EP0Control,0x01);
SL811Write(IntEna,0x20); // USB-A, Insert/Remove, USB_Resume.
//清中断
SL811Write(IntStatus,INT_CLEAR); // Clear Interrupt enable status
}
//*****************************************************************************************
// Byte Read from SL811H
// a = register address
// return = data in register
//*****************************************************************************************
unsigned char SL811Read(unsigned char a)
{
//unsigned char nVal;
//unsigned char xdata *exAddress;
//exAddress = SL811_ADDR_PORT;
////SL811_CS=1;
//*exAddress=a;
//exAddress=SL811_DATA_PORT;
//nVal = *exAddress;
////SL811_CS=0;
//return nVal;
SL811H_ADDR = a;
return (SL811H_DATA&0xFF);
}
void SL811Write(unsigned char a, unsigned char d)
{
//unsigned char xdata *exAddress;
//exAddress = SL811_ADDR_PORT;
////SL811_CS=1;
//*exAddress=a;
//exAddress=SL811_DATA_PORT;
//*exAddress = d;
////SL811_CS=0;
SL811H_ADDR = a;
SL811H_DATA = d&0xFF;
}
void SL811BufRead(BYTE addr, BYTE *s, BYTE c)//(unsigned char addr, unsigned char *s, unsigned char c)
{
//unsigned char i;
//unsigned char xdata *exAddress;
//exAddress=SL811_ADDR_PORT;
////SL811_CS=1;
//*exAddress = addr;
//exAddress=SL811_DATA_PORT;
//for(i=0;i<c;i++)
// {
// *s++ = *exAddress;
// }
////SL811_CS=0;
char tmp_char;
SL811H_ADDR = addr;
while (c--)
{
tmp_char = SL811H_DATA;
*s++ = (BYTE)tmp_char&0xFF;
}
}
void SL811BufWrite(BYTE addr, BYTE *s, BYTE c)//(unsigned char addr, unsigned char *s, unsigned char c)
{
//unsigned char xdata *exAddress;
//exAddress = SL811_ADDR_PORT;
////SL811_CS=1;
//*exAddress = addr;
//exAddress=SL811_DATA_PORT;
//while (c--)
// {
// *exAddress = *s++;
// }
////SL811_CS=0;
SL811H_ADDR = addr;
while (c--)
SL811H_DATA = *s++;
}
unsigned short WordSwap(unsigned short input)
{
return(((input&0x00FF)<<8)|((input&0xFF00)>>8));
//return(((input&0x00FF)<<16)|((input&0x00FF0000)>>16));
}
void DelayMs(unsigned char nFactor)
{
unsigned char i;
unsigned int j;
for(i=0; i<nFactor; i++)
{
for(j=0;j<1000;j++)
j=j;
}
}
unsigned int LSwapINT16(unsigned short dData1,unsigned short dData2)
{
unsigned int xdata dData;
dData = ((dData2<<8)&0xff00)|(dData1&0x00ff);
return dData;
}
unsigned long LSwapINT32(unsigned long dData1,unsigned long dData2,unsigned long dData3,unsigned long dData4)
{
unsigned long xdata dData;
dData = ((dData4<<24)&0xff000000)|((dData3<<16)&0xff0000)|((dData2<<8)&0xff00)|(dData1&0xff);
return dData;
}
//*****************************************************************************************
// Byte Read from SL811H
// a = register address
// return = data in register
//*****************************************************************************************
//BYTE SL811Read(BYTE a)
//{
// SL811H_ADDR = a;
// return (SL811H_DATA&0xFF);
//}
//*****************************************************************************************
// Byte Write to SL811H
// a = register address
// d = data to be written to this register address
//*****************************************************************************************
//void SL811Write(BYTE a, BYTE d)
//{
// SL811H_ADDR = a;
// SL811H_DATA = d&0xFF;
//}
//*****************************************************************************************
// Buffer Read from SL811H
// addr = buffer start address
// s = return buffer address where data are to be save/read
// c = buffer data length
//*****************************************************************************************
//void SL811BufRead(BYTE addr, BYTE *s, BYTE c)
//{
// char tmp_char;
//
// SL811H_ADDR = addr;
// while (c--)
// {
// tmp_char = SL811H_DATA;
// *s++ = (BYTE)tmp_char&0xFF;
// }
//}
//*****************************************************************************************
// Buffer Write to SL811H
// addr = buffer start address
// s = buffer address where data are to be written
// c = buffer data length
//*****************************************************************************************
//void SL811BufWrite(BYTE addr, BYTE *s, BYTE c)
//{
// SL811H_ADDR = addr;
// while (c--)
// SL811H_DATA = *s++;
//}
//*****************************************************************************************
// Swap high and low byte
//*****************************************************************************************
//WORD WordSwap(WORD input)
//{
// return(((input&0x00FF)<<16)|((input&0x00FF0000)>>16));
//}
//*****************************************************************************************
// UsbReset during enumeration of device attached directly to SL811HS
//*****************************************************************************************
//void USBReset()
//{
// BYTE tmp;
// tmp = SL811Read(CtrlReg);
// SL811Write(CtrlReg,tmp|0x08);
// EZUSB_Delay(25);
// SL811Write(CtrlReg,tmp);
//}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -