📄 usb.i
字号:
else cmd = 0x42; } else { if(xferLen) { intr=usbstack.setup.wLength; usbstack.setup.wValue=WordSwap(usbstack.setup.wValue); usbstack.setup.wIndex=WordSwap(usbstack.setup.wIndex); usbstack.setup.wLength=WordSwap(usbstack.setup.wLength); ((unsigned char volatile xdata *) 0)[0xF80C] = 0x03; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; ((unsigned char volatile xdata *) 0)[0xF80B] = 0x01; usbBufWrite((unsigned char *)&usbstack.setup,xferLen); if (xferLen < 64) { ((unsigned char volatile xdata *) 0)[0xF80C] = 0x03; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; } usbstack.setup.wLength=intr; } cmd = 0x41; } ((unsigned char volatile xdata *) 0)[0xF801] = 0xff; ((unsigned char volatile xdata *) 0)[0xF802] = 0xff; ((unsigned char volatile xdata *) 0)[0xF803] = cmd; DelayUs(20); ConsecutiveNAKCount = 0; while(1) { DelayUs(10); intr = ((unsigned char volatile xdata *) 0)[0xF801]; if(usbstack.pid==0x90) { if (intr & 0x04) { ConsecutiveNAKCount = 0; ((unsigned char volatile xdata *) 0)[0xF801] = 0x04; if(PassToggleCheckSoftware()) #line 177 "usb.c" /1 #line 179 "usb.c" /0 { bXXGFlags.bToggle = ~bXXGFlags.bToggle; if(xferLen<64) { ((unsigned char volatile xdata *) 0)[0xF80C] = 0x03; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; } usbstack.wLen -= (unsigned int)xferLen; if(usbstack.wLen==0) { if(!bXXGFlags.bIN_ISR) usbBufRead(usbstack.buffer, xferLen); else ((unsigned char volatile xdata *) 0)[0xF80B] = 0x13; ((unsigned char volatile xdata *) 0)[0xF80F] = 0x0; ((unsigned char volatile xdata *) 0)[0xF80F] = 0x0; ((unsigned char volatile xdata *) 0)[0xF80F] = 0x0; ((unsigned char volatile xdata *) 0)[0xF80F] = 0x0; if(xferLen<64) { ((unsigned char volatile xdata *) 0)[0xF80C] = 0x03; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; } else { ((unsigned char volatile xdata *) 0)[0xF80C] = 0x01; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; } ((unsigned char volatile xdata *) 0)[0xF80B] = 0x10; result = 0x20; break; } else { if(!bXXGFlags.bIN_ISR) usbBufRead(usbstack.buffer, xferLen); else ((unsigned char volatile xdata *) 0)[0xF80B] = 0x13; ((unsigned char volatile xdata *) 0)[0xF80F] = 0x0; ((unsigned char volatile xdata *) 0)[0xF80F] = 0x0; ((unsigned char volatile xdata *) 0)[0xF80F] = 0x0; ((unsigned char volatile xdata *) 0)[0xF80F] = 0x0; if(xferLen<64) { ((unsigned char volatile xdata *) 0)[0xF80C] = 0x03; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; } else { ((unsigned char volatile xdata *) 0)[0xF80C] = 0x01; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; } usbstack.buffer += xferLen; if (usbstack.wLen >= usbstack.wPayload) xferLen = usbstack.wPayload; else xferLen = usbstack.wLen; cmd = 0x04; ((unsigned char volatile xdata *) 0)[0xF80B] = 0x10; ((unsigned char volatile xdata *) 0)[0xF806] = xferLen; ((unsigned char volatile xdata *) 0)[0xF803] = cmd; } } else { if(xferLen<64) { ((unsigned char volatile xdata *) 0)[0xF80C] = 0x03; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; } ((unsigned char volatile xdata *) 0)[0xF80B] = 0x14; ((unsigned char volatile xdata *) 0)[0xF80F] = 0x0; ((unsigned char volatile xdata *) 0)[0xF80F] = 0x0; ((unsigned char volatile xdata *) 0)[0xF80F] = 0x0; ((unsigned char volatile xdata *) 0)[0xF80F] = 0x0; if(xferLen<64) { ((unsigned char volatile xdata *) 0)[0xF80C] = 0x03; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; } else { ((unsigned char volatile xdata *) 0)[0xF80C] = 0x01; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; } ((unsigned char volatile xdata *) 0)[0xF80B] = 0x10; cmd = 0x04; ((unsigned char volatile xdata *) 0)[0xF803] = cmd; } } else if(intr & 0x10) { ((unsigned char volatile xdata *) 0)[0xF801] = 0x10; if(ConsecutiveNAKCount < 200) ConsecutiveNAKCount = ConsecutiveNAKCount + 1; else return 0; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x01; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; DelayMs(1); ((unsigned char volatile xdata *) 0)[0xF803] = cmd; } else if(intr & 0x80) { if (((unsigned char volatile xdata *) 0)[0xF801] & 0x80) { ((unsigned char volatile xdata *) 0)[0xF80C] = 0x01; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; ((unsigned char volatile xdata *) 0)[0xF803] = cmd; } } else if (intr & 0x08) { ((unsigned char volatile xdata *) 0)[0xF801] = 0x08; bXXGFlags.SLAVE_STALLED = 1; break; } } else { if(intr & 0x20) { result = 0x20; ((unsigned char volatile xdata *) 0)[0xF801] = 0x20; break; } else if(intr & 0x80) { if (((unsigned char volatile xdata *) 0)[0xF801] & 0x80) { ((unsigned char volatile xdata *) 0)[0xF80C] = 0x02; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; ((unsigned char volatile xdata *) 0)[0xF803] = cmd; } } } } if (result == 0x20) { return 1; } return 0; } unsigned char ep0Xfer(void) { ((unsigned char volatile xdata *) 0)[0xF804] = 0x0; usbstack.pid=0xD0; if (!usbXfer(0)) return 0; usbstack.pid = 0x90; usbstack.wLen=usbstack.setup.wLength; if (usbstack.wLen) { if (usbstack.setup.bmRequest & 0x80) { usbstack.pid = 0x90; bXXGFlags.bToggle=1; if(!usbXfer(0)) return 0; usbstack.pid = 0x10; } else { usbstack.pid = 0x10; if(!usbXfer(0)) return 0; usbstack.pid = 0x90; } } DelayMs(2); usbstack.wLen=0; bXXGFlags.bToggle=1; if(!usbXfer(0)) return 0; return 1; } unsigned char epBulkSend(unsigned char *pBuffer,unsigned int len) { usbstack.pid=0x10; ((unsigned char volatile xdata *) 0)[0xF804] = 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(((unsigned char volatile xdata *) 0)[0xF804])) return 0; bXXGFlags.bToggle=~bXXGFlags.bToggle; len-=usbstack.wLen; usbstack.buffer=usbstack.buffer+usbstack.wLen; } uDev.bData1[usbstack.epbulkout]=bXXGFlags.bToggle; return 1; } unsigned char epBulkRcv(unsigned char *pBuffer,unsigned int len) { usbstack.pid=0x90; ((unsigned char volatile xdata *) 0)[0xF804] = uDev.bEPAddr[usbstack.epbulkin]; usbstack.wPayload=64; usbstack.wLen=len; usbstack.buffer=pBuffer; if(usbstack.wLen) { bXXGFlags.bToggle = uDev.bData1[usbstack.epbulkin]; if(!usbXfer(((unsigned char volatile xdata *) 0)[0xF804])) return 0; uDev.bData1[usbstack.epbulkin] = bXXGFlags.bToggle; } return 1; } unsigned char setAddress(unsigned char addr) { usbstack.setup.bmRequest=0; usbstack.setup.bRequest=0x05; usbstack.setup.wValue=addr; usbstack.setup.wIndex=0; usbstack.setup.wLength=0; usbstack.wLen=0x8; return ep0Xfer(); } unsigned char setConfiguration(void) { usbstack.setup.bmRequest=0; usbstack.setup.bRequest=0x09; usbstack.setup.wIndex=0; usbstack.setup.wLength=0; usbstack.wLen=0x8; return ep0Xfer(); } unsigned char getDesc(void) { usbstack.setup.bmRequest=0x80; usbstack.setup.bRequest=0x06; usbstack.setup.wValue=WordSwap(usbstack.setup.wValue); usbstack.wPayload=uDev.wPayLoad[0]; usbstack.wLen=8; return ep0Xfer(); } unsigned char enumUsbDev(unsigned char usbaddr) { unsigned char i; unsigned char epLen; pDevDesc pDev; pCfgDesc pCfg; pIntfDesc pIfc; pEPDesc pEnp; ((unsigned char volatile xdata *) 0)[0xF805] = 0x0; uDev.wPayLoad[0] = 8; usbstack.wPayload = uDev.wPayLoad[0]; usbstack.buffer=DBUF; usbstack.usbaddr = 0; pDev =(pDevDesc)DBUF; usbstack.setup.wValue=0x01; usbstack.setup.wIndex=0; usbstack.setup.wLength=8; if (!getDesc()) return 0; uDev.wPayLoad[0]=pDev->bMaxPacketSize0; usbstack.wPayload = uDev.wPayLoad[0]; if (!setAddress(usbaddr)) return 0; ((unsigned char volatile xdata *) 0)[0xF805] = usbaddr; usbstack.usbaddr=usbaddr; usbstack.setup.wLength=pDev->bLength; usbstack.setup.wValue=0x01; usbstack.setup.wIndex=0; usbstack.buffer=DBUF; if (!getDesc()) return 0; uDev.wVID = pDev->idVendor; uDev.wPID = pDev->idProduct; uDev.iMfg = pDev->iManufacturer; uDev.iPdt = pDev->iProduct; pCfg = (pCfgDesc)DBUF; usbstack.setup.wValue=0x02; usbstack.setup.wIndex=0; usbstack.setup.wLength=9; usbstack.buffer=DBUF; if (!getDesc()) return 0; usbstack.setup.wLength=WordSwap(pCfg->wLength); usbstack.buffer=DBUF; if (!getDesc()) return 0; pIfc = (pIntfDesc)(DBUF + 9); uDev.bClass = pIfc->iClass; uDev.bNumOfEPs = (pIfc->bEndPoints <= 0x05) ? pIfc->bEndPoints : 0x05; if(uDev.bClass==8) { bXXGFlags.bMassDevice=1; } usbstack.setup.wValue=0x01; if (!setConfiguration()) return 0; epLen = 0; for (i=1; i<=uDev.bNumOfEPs; i++) { pEnp = (pEPDesc)(DBUF + 18 + epLen); uDev.bEPAddr[i] = pEnp->bEPAdd; uDev.bAttr[i] = pEnp->bAttr; uDev.wPayLoad[i] = WordSwap(pEnp->wPayLoad); uDev.bInterval[i] = pEnp->bInterval; uDev.bData1[i] = 0; epLen += 7; if(uDev.bAttr[i]==0x2) { if(uDev.bEPAddr[i]&0x80) usbstack.epbulkin=i; else usbstack.epbulkout=i; } } return 1; } void usbInit(void) { ((unsigned char volatile xdata *) 0)[0xF800] = 0x03; DelayUs(100); ((unsigned char volatile xdata *) 0)[0xF800] = 0x09; ((unsigned char volatile xdata *) 0)[0xF804] = 0x00; ((unsigned char volatile xdata *) 0)[0xF805] = 0x00; ((unsigned char volatile xdata *) 0)[0xF801] = 0xff; ((unsigned char volatile xdata *) 0)[0xF802] = 0xff; ((unsigned char volatile xdata *) 0)[0xF407] = 0xff; ((unsigned char volatile xdata *) 0)[0xF405] = 0xff; ((unsigned char volatile xdata *) 0)[0xF408] = 0x02; ((unsigned char volatile xdata *) 0)[0xF808] = 0xff; while(1) { if(((unsigned char volatile xdata *) 0)[0xF808] & 0x04) { DelayMs(100); ((unsigned char volatile xdata *) 0)[0xF808] = 0xff; ((unsigned char volatile xdata *) 0)[0xF800] = 0x19; DelayMs(100); break; } } } 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; 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; static unsigned char FrameTypeExpected = 0x0; void GetThisFrameData() { if(FrameTypeExpected == 0x0) usbBufRead(&DATA0_Byte00, 0x8); else usbBufRead(&DATA1_Byte00, 0x8); ((unsigned char volatile xdata *) 0)[0xF80C] = 0x02; ((unsigned char volatile xdata *) 0)[0xF80C] = 0x00; } static unsigned char Consecutive0x00Count = 0; static unsigned char Consecutive0xffCount = 0; static unsigned char ConclusionByCompareContents() { if(((unsigned char volatile xdata *) 0)[0xF807] < 64) { Consecutive0x00Count = 0; Consecutive0xffCount = 0; return 1; } 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 == 0x0)?0x1:0x0; return 1; } else return 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 == 0x0)?0x1:0x0; return 1; } else return 0; } 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 0; FrameTypeExpected = (FrameTypeExpected == 0x0)?0x1:0x0; return 1; } unsigned char PassToggleCheckSoftware() { GetThisFrameData(); return ConclusionByCompareContents(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -