📄 usb.c
字号:
unsigned char epBulkSend(unsigned char *pBuffer,unsigned int len)
{
// usbstack.endpoint=usbstack.epbulkout;
usbstack.pid=PID_OUT;
TxEpnum = uDev.bEPAddr[usbstack.epbulkout];
usbstack.wPayload=64;
usbstack.wLen=len;
usbstack.buffer=pBuffer;
bXXGFlags.bToggle=uDev.bData1[usbstack.epbulkout];
while(len>0)
{
if (len > usbstack.wPayload)
usbstack.wLen = usbstack.wPayload;
else
usbstack.wLen = len;
if(!usbXfer(TxEpnum))
return FALSE;
bXXGFlags.bToggle=~bXXGFlags.bToggle;
len-=usbstack.wLen;
usbstack.buffer=usbstack.buffer+usbstack.wLen;
}
uDev.bData1[usbstack.epbulkout]=bXXGFlags.bToggle; // save toggle status back to associated EP
return TRUE;
}
unsigned char epBulkRcv(unsigned char *pBuffer,unsigned int len)
{
// usbstack.endpoint=usbstack.epbulkin;
usbstack.pid=PID_IN;
TxEpnum = uDev.bEPAddr[usbstack.epbulkin];
usbstack.wPayload=64;
usbstack.wLen=len;
usbstack.buffer=pBuffer;
if(usbstack.wLen)
{
bXXGFlags.bToggle = uDev.bData1[usbstack.epbulkin]; // restore toggle state
if(!usbXfer(TxEpnum))
return FALSE;
uDev.bData1[usbstack.epbulkin] = bXXGFlags.bToggle; // save toggle state
}
return TRUE;
}
//*****************************************************************************************
// Set Device Address :
//*****************************************************************************************
unsigned char setAddress(unsigned char addr)
{
usbstack.setup.bmRequest=0;
usbstack.setup.bRequest=SET_ADDRESS;
usbstack.setup.wValue=addr;
usbstack.setup.wIndex=0;
usbstack.setup.wLength=0;
usbstack.wLen=0x8;
return ep0Xfer();
}
//*****************************************************************************************
// Set Device Configuration :
//*****************************************************************************************
unsigned char setConfiguration(void)
{
usbstack.setup.bmRequest=0;
usbstack.setup.bRequest=SET_CONFIG;
usbstack.setup.wIndex=0;
usbstack.setup.wLength=0;
usbstack.wLen=0x8;
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];
usbstack.wLen=8; // set for 8-byte descriptor length
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 epLen;
// pointers to descriptor data structures
pDevDesc pDev; // device descriptor
pCfgDesc pCfg; // configuration descriptor
pIntfDesc pIfc; // interface descriptor
pEPDesc pEnp; // end-point descriptor
TxAddr = 0x0; // double make sure Address is zero
uDev.wPayLoad[0] = 8; // default 8-byte payload for EP0
usbstack.wPayload = uDev.wPayLoad[0];
usbstack.buffer=DBUF; // setup source and destination buffer
usbstack.usbaddr = 0; // default address
//------------------------------------------------
// Get USB Device Descriptors on EP0 & Addr 0
// with default 64-byte payload
//------------------------------------------------
pDev =(pDevDesc)DBUF; // device descriptor data structure overlay
// part of the 8-byte setup command
usbstack.setup.wValue=DEVICE;
usbstack.setup.wIndex=0;
usbstack.setup.wLength=8; // we only try to get 8 bytes for the
// correct wPayload of Endpoint 0
if (!getDesc())
return FALSE;
uDev.wPayLoad[0]=pDev->bMaxPacketSize0;
usbstack.wPayload = uDev.wPayLoad[0];
//------------------------------------------------
// Set Slave USB Device Address
//------------------------------------------------
if (!setAddress(usbaddr)) // set to specific USB address
return FALSE; //
TxAddr = usbaddr;
//------------------------------------------------
// Get USB Device Descriptors on EP0 & Addr X
//------------------------------------------------
usbstack.usbaddr=usbaddr;
usbstack.setup.wLength=pDev->bLength;
usbstack.setup.wValue=DEVICE;
usbstack.setup.wIndex=0;
usbstack.buffer=DBUF;
if (!getDesc())
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;
usbstack.setup.wValue=CONFIGURATION;
usbstack.setup.wIndex=0;
usbstack.setup.wLength=9;
usbstack.buffer=DBUF;
if (!getDesc())
return FALSE;
usbstack.setup.wLength=WordSwap(pCfg->wLength);
usbstack.buffer=DBUF;
// usbstack.setup.wLength=32;
if (!getDesc())
return FALSE;
// the DBUF now should contain 32 bytes of configuration data:
// 0-8: configuration descriptor
// 9-17: interface descriptor
// 18-24: EP0 descriptor
// 25-31: EP1 descriptor
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
{
bXXGFlags.bMassDevice=TRUE;
}
//------------------------------------------------
// Set configuration (except for HUB device)
//------------------------------------------------
usbstack.setup.wValue=DEVICE;
//------------------------------------------------
// For each slave endpoints, get its attributes
// Excluding endpoint0, only data endpoints
//------------------------------------------------
if (!setConfiguration())
return FALSE;
epLen = 0;
for (i=1; i<=uDev.bNumOfEPs; i++) // For each data endpoint
{
pEnp = (pEPDesc)(DBUF + 18 + epLen); // point to Endpoint Descp(non-HID)
//if(pIfc->iClass == HIDCLASS)
// pEnp = (pEPDesc)(DBUF + 9 + 9 + 9 + epLen); // update pointer to Endpoint(HID)
uDev.bEPAddr[i] = pEnp->bEPAdd; // Ep address and direction
uDev.bAttr[i] = pEnp->bAttr; // Attribute of Endpoint
uDev.wPayLoad[i] = WordSwap(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) // bulk endpoint
{
if(uDev.bEPAddr[i]&0x80)
usbstack.epbulkin=i; // bulk in
else
usbstack.epbulkout=i; // bulk out
}
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////////
void usbInit(void)
{
SieCtrl = 0x03; // apply bus power & reset SIE
DelayUs(100);
SieCtrl = 0x09; // enable SOF, otherwise device will sleep
TxEpnum = 0x00;
TxAddr = 0x00;
SieStat0 = INT_CLEAR;
SieStat1 = INT_CLEAR;
Alarm = INT_CLEAR;
Mp3Stat = INT_CLEAR;
AlarmEn = 0x02;
Ualarm = INT_CLEAR;
while(TRUE) // FIXME: do nothing if device not attached, should go power down
{
if(Ualarm & 0x04) // device attachment detected
{
DelayMs(100); // wait power stable
Ualarm = INT_CLEAR;
SieCtrl = 0x19; // set attached status
DelayMs(100); // wait more....
break;
}
}
//IntStatus:0x0D,Interrupt status, Clear Interrupt enable status
}
//////////////////////////////////////////////////////////////////////////////////////////////
#define DATA0 0x0
#define DATA1 0x1
#define BYTES_COUNT_COMPARED 0x8
#define COMPARE_RESULT_DIFF 0x0
#define ThisFrameLength XferCnt
//***************************************************************************************
//information of DATA0/DATA1
//***************************************************************************************
static unsigned char
DATA0_Byte00 = 0x00, DATA0_Byte01 = 0x01, DATA0_Byte02 = 0x02, DATA0_Byte03 = 0x03,
DATA0_Byte04 = 0x04, DATA0_Byte05 = 0x05, DATA0_Byte06 = 0x06, DATA0_Byte07 = 0x07;
// DATA0_Byte08 = 0x08, DATA0_Byte09 = 0x09, DATA0_Byte10 = 0x0a, DATA0_Byte11 = 0x0b,
// DATA0_Byte12 = 0x0c, DATA0_Byte13 = 0x0d, DATA0_Byte14 = 0x0e, DATA0_Byte15 = 0x0f;
//static unsigned char DATA0_Length = 64;
static unsigned char
DATA1_Byte00 = 0x0f, DATA1_Byte01 = 0x0e, DATA1_Byte02 = 0x0d, DATA1_Byte03 = 0x0c,
DATA1_Byte04 = 0x0b, DATA1_Byte05 = 0x0a, DATA1_Byte06 = 0x09, DATA1_Byte07 = 0x08;
// DATA1_Byte08 = 0x07, DATA1_Byte09 = 0x06, DATA1_Byte10 = 0x05, DATA1_Byte11 = 0x04,
// DATA1_Byte12 = 0x03, DATA1_Byte13 = 0x02, DATA1_Byte14 = 0x01, DATA1_Byte15 = 0x00;
//static unsigned char DATA1_Length = 64;
//***************************************************************************************
//get head 16 bytes from present frame
//***************************************************************************************
static unsigned char FrameTypeExpected = DATA0;
void GetThisFrameData()
{
if(FrameTypeExpected == DATA0)
usbBufRead(&DATA0_Byte00, BYTES_COUNT_COMPARED);
else//FrameTypeExpected == DATA1
usbBufRead(&DATA1_Byte00, BYTES_COUNT_COMPARED);
// reset read pointer
BufRst = 0x02;
BufRst = 0x00;
}
//***************************************************************************************
//give a conculsion by comparing this frame's contents with last frame's.
//pay attention to macro BYTES_COUNT_COMPARED!!
//***************************************************************************************
static unsigned char Consecutive0x00Count = 0;
static unsigned char Consecutive0xffCount = 0;
static unsigned char ConclusionByCompareContents()
{
// unsigned char ThisFrameAll0x00;
// unsigned char ThisFrameAll0xff;
// unsigned char ThisFrameSameLastFrame;
if(ThisFrameLength < 64)
{
Consecutive0x00Count = 0;
Consecutive0xffCount = 0;
return TRUE;
}
/* if( (FrameTypeExpected == DATA0)?(
(DATA0_Byte00 == 0x00) && (DATA0_Byte01 == 0x00) &&
(DATA0_Byte02 == 0x00) && (DATA0_Byte03 == 0x00) &&
(DATA0_Byte04 == 0x00) && (DATA0_Byte05 == 0x00) &&
(DATA0_Byte06 == 0x00) && (DATA0_Byte07 == 0x00)
)
:
(
(DATA1_Byte00 == 0x00) && (DATA1_Byte01 == 0x00) &&
(DATA1_Byte02 == 0x00) && (DATA1_Byte03 == 0x00) &&
(DATA1_Byte04 == 0x00) && (DATA1_Byte05 == 0x00) &&
(DATA1_Byte06 == 0x00) && (DATA1_Byte07 == 0x00)
) );*/
// if(ThisFrameAll0x00)
if( (DATA1_Byte00 == 0x00) && (DATA1_Byte01 == 0x00) &&
(DATA1_Byte02 == 0x00) && (DATA1_Byte03 == 0x00) &&
(DATA1_Byte04 == 0x00) && (DATA1_Byte05 == 0x00) &&
(DATA1_Byte06 == 0x00) && (DATA1_Byte07 == 0x00) )
{
Consecutive0xffCount = 0;
if(Consecutive0x00Count != 8)
{
Consecutive0x00Count = Consecutive0x00Count + 1;
FrameTypeExpected = (FrameTypeExpected == DATA0)?DATA1:DATA0;
return TRUE;
}
else
return FALSE;
}
/* if( (FrameTypeExpected == DATA0)?(
(DATA0_Byte00 == 0xff) && (DATA0_Byte01 == 0xff) &&
(DATA0_Byte02 == 0xff) && (DATA0_Byte03 == 0xff) &&
(DATA0_Byte04 == 0xff) && (DATA0_Byte05 == 0xff) &&
(DATA0_Byte06 == 0xff) && (DATA0_Byte07 == 0xff)
)
:
(
(DATA1_Byte00 == 0xff) && (DATA1_Byte01 == 0xff) &&
(DATA1_Byte02 == 0xff) && (DATA1_Byte03 == 0xff) &&
(DATA1_Byte04 == 0xff) && (DATA1_Byte05 == 0xff) &&
(DATA1_Byte06 == 0xff) && (DATA1_Byte07 == 0xff)
) );*/
// if(ThisFrameAll0xff != 0)
if( (DATA1_Byte00 == 0xff) && (DATA1_Byte01 == 0xff) &&
(DATA1_Byte02 == 0xff) && (DATA1_Byte03 == 0xff) &&
(DATA1_Byte04 == 0xff) && (DATA1_Byte05 == 0xff) &&
(DATA1_Byte06 == 0xff) && (DATA1_Byte07 == 0xff) )
{
Consecutive0x00Count = 0;
if(Consecutive0xffCount != 8)
{
Consecutive0xffCount = Consecutive0xffCount + 1;
FrameTypeExpected = (FrameTypeExpected == DATA0)?DATA1:DATA0;
return TRUE;
}
else
return FALSE;
}
Consecutive0x00Count = 0;
Consecutive0xffCount = 0;
if( (DATA0_Byte00 == DATA1_Byte00) && (DATA0_Byte01 == DATA1_Byte01) &&
(DATA0_Byte02 == DATA1_Byte02) && (DATA0_Byte03 == DATA1_Byte03) &&
(DATA0_Byte04 == DATA1_Byte04) && (DATA0_Byte05 == DATA1_Byte05) &&
(DATA0_Byte06 == DATA1_Byte06) && (DATA0_Byte07 == DATA1_Byte07) )
return FALSE;
FrameTypeExpected = (FrameTypeExpected == DATA0)?DATA1:DATA0;
return TRUE;
}
//***************************************************************************************
//
//***************************************************************************************
unsigned char PassToggleCheckSoftware()
{
GetThisFrameData();
return ConclusionByCompareContents();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -