#define NUM-ENDPOINTS 4 #define CONFIG-DESCRIPTOR-LENGTH sizeof (USB-CONFIGURATION-DESCRIPTOR) +sizeof (USB-INTERFACE-DESCRIPTOR) +(NUM-ENDPOINTS*sizeof (USB-ENDPOINT-DESCRIPTOR)) void fn-usb-isr() { unsigned int i-st; bEPPflags.bits.in-isr=1; i-st=D12-ReadInterruptRegister(); if (i-st!=0) { if (i-st&D12-INT-BUSRESET) { bEPPflags.bits.bus-reset=1; } if(i-st & D12-INT-EOT) dma-eot(); if (i-st&D12-INT-SUSPENDCHANGE) bEPPflags.bits.suspend=1; if(i- st & D12-INT-ENDPOIN) ep0-txdone(); if(i-st & D12-INT-ENDPOOUT) ep0-rxdone(); if(i-st&D12-INT-ENDPI IN) epl txdone(); if(i-st & D12-INT-ENDPIOUT) epl-rxdone(); if (i-st&D12-INT-ENDP2IN) main-txdone(); if(i-st&D12-INT-ENDP20UT) main-rxdone(); } bEPPflags. bits. in-isr=0; } void outportb(unsigned int Addr,unsigned char Data) { *((unsigned char xdata*)Addr)=Data; } unsigned char inportb(unsigned int Addr) { return *((unsigned char xdata*)Addr); } void D12 SetAddressEnable(unsigned char bAddress,unsigned char bEnable) { outportb(D12-COMMAND,0xD0; if(bEnable) bAddress|=0x80; outportb(D12-DATA,bAddress); } void D12-SetEndpointEnable(unsigned char bEnable) { outportb(D12-COMMAND,0xD8); if (bEnable) outportb(D12-DATA,1); else outportb(D12-DATA,0); } void D12-SetMode(unsigned char bConfig,unsigned char bCIkDiv) { outportb(D12-COMMAND,0xF3); outportb(D12-DATA,bConf ig); outportb(D12-DATA,bClkDiv); } void D12-SetDMA(unsigned char bMode) { outportb(D12-COMMAND,0xFB); outportb(D12-DATA,bMode); } unsigned short D12-ReadInterruptRegister(void) { unsigned char b1; unsigned int j; outportb(D12-COMMAND,0xF4); b1=inportb(D12-DATA); j=inportb(D12-DATA); j<<= 8; i+=b1; return; } unsigned char Dl 2-SelectEndpoint(unsigned char bEndp) { unsigned char c; outportb(D12-COMMAND,bEndp); c=inportb(D12-DATA); return c; } unsigned char D12-ReadLastTransactionStatus(unsigned char bEndp) { outportb(D12-COMMAND,0x40+bEndp); return inportb(D12-DATA); } void D12-SetEndpointStatus(unsigned char bEndp,unsigned char bStalled) { outportb(D12-COMMAND,0x40+bEndp); outportb(D12-DATA,bStalled); } void D12-SendResume(void) { outportb(D12-COMMAND,0xF6); } unsigned short D12-ReadCurrentFrameNumber(VOid) { unsigned short i,j; outportb(D12-COMMAND,0xF5); i= inportb(D12-DATA); j=inportb(D12-DATA); i+=(j<<8); return i; } unsigned char Dl2-ReadEndpoint(unsigned char endp,unsigned char len,unsigned char buf) { unsigned char i,j; outportb(D12-COMMAND,endp); if((inportb(D12-DATA)&D12-FULLEMPTY)==0) { return 0; } outportb(D12-COMMAND,0x80+endp); i=inportb(D12-DATA); i=i&0x60; if(endp==2) { if(i==0x60) bEPPflags.bits.eplbuf-full=1; else bEPPflags.bits.eplbuf-full=0; } if(endp==4) { if(i== 0x60 bEPPflags.bits.ep2buf-full = 1; else bEPPflags.bits.ep2buf-full=0; } outportb(D12-COMMAND,0xF0) j=inportb(D12-DATA); J=inportb(D12-DATA); if(j>len) j=len; for(i=0;i<j;i++) *(buf+i)=inportb(D12-DATA); outportb(D12-COMMAND,0xF2); return; } unsigned char Dl 2-WriteEndpoint(unsigned char endp,unsigned char len,unsigned char buf) { unsigned char i; outportb(D12-COMMAND,endp); inportb(D12-DATA); outportb(D12-COMMAND,OxFO); outportb(D12-DATA,0); outportb(D12-DATA,len); for(i=0;i<len;i++) outportb(D12-DATA,*(buf+i)); outportb(D12-COMMAND,0xFA); return len; } void D12-AcknowledgeEndpoint(unsigned char endp) { outportb(D12-COMMAND,endp); outportb(D12-COMMAND,0xFl); if(endp==0) outportb(D12-COMMAND,0xF2); type=ControlData.DeviceRequest.bmRequestType&USB-REQUEST-TYPE-MASK, / req = ControlData.DeviceRequest.bRequest&USB-REQUEST-MASK; if(type==USB-STANDARDLREQUEST) (*StandardDeviceRequest[req])(); else if(type==USB-VENDOR-REQUEST) (*VendorDeviceRequest[req])(); else stall-ep0(); } code void(*StandardDeviceRequest[])(void)={get-status,clear-feature,reserved,set feature, reserved,set address,get descriptor,reserved,get configuration,set configuration,get interface, set interface,reserved,reserved,reserved,reserved}; void reserved(void) { stall-ep0(); } void init unconfig(void) { D12-SetEndpointEnable(0); } void init config(void) { D12-SetEndpointEnable(1); } void single-transmit(unsigned char buf, unsigned char len) { if(len<=EPO-PACKETeeSIZE){ D12-WriteEndpoint(1,len,buf); } } void code-transmit(unsigned char code * pRomData,unsigned short len) { ControlData. wCount=0; if (ControlData.wLength>len) ControlData. wLength=len; ControlData. pData=pRomData; if( ControlData. wLength)=EPO-PACKET-SIZE) { D12-WriteEndpoint(1,EPO-PACKET-SIZE,ControlData.pData); ControlData. wCount+=EPO-PACKET-SIZE; DISABLE; bEPPflags. bits. control-state=USB-TRANSMIT; ENABLE; } else { D12-WriteEndpoint(1,ControlData. wLength,pRomData); ControlData. wCount+=ControlData.wLength; DISABLE; bEPPflags.bits.control-state=USB-IDLE; ENABLE; } } void get status(void) { unsigned char endp,txdat[2]; unsigned char bRecipient=ControlData. DeviceRequest. bmRequestType&USB-RECIPIENT; unsigned char c; if(bRecipient==USB-RECIPIENT-DEVICE) { if(bEPPflags.bits.remote-wakeup==1) txdat[0]=3; else txdat[0j=1; txdat[1]=0; single-transmit(txdat,2); } else if(bRecipient==USB-RECIPIENT-INTERFACE) { txdat[0j=0; txdat[1」=0; single-transmit(txdat,2); } else if(bRecipient==USB-RECIPIENT-ENDPOINT) { endp=(unsigned char)(ControlData. DeviceRequest. windex&MAX-ENDPOINTS); if(ControlData.DeviceRequest.WIndex&(unsigned char) USB-ENDPOINT-DIRECTION-MASK) c = D12-SelectEndpoint(2*endp+1); else c=D12-SelectEndpoint(endp,2); if (c&D12 STALL) txdatI0]=1; else txdat[0]=0; txdatEl〕=0; single-transmit(txdat,2); } else stall-ep0(); } void clear-feature (void) { unsigned char endp; unsigned char bRecipient=ControlData. DeviceRequest. bmRequestType & USB-RECIPIENT; if(bRecipient==USB-RECIPIENT-DEVICE && ControlData. DeviceRequest. wValue==USB-FEATURE-REMOTE-WAKEUP) { DISABLE; bEPPflags. bits. remote-wakeup=0; ENABLE; single-transmit(0,0); } else if(bRecipient== USB-RECIPIENT-ENDPOINT && ControlData. DeviceRequest. wValue==USB-FEATURE-ENDPOINT-STALL){ endp=(unsigned char)(ControlData. DeviceRequest. wIndex&MAX-ENDPOINTS); if(ControlData,DeviceRequest,wIndex&(unsigned char) USB-ENDPOINT-DIRECTION-MASK) D12-SetEndpointStatus(2*endp+1,0); else D12-SetEndpointStatus(2* endp,0); single-transmit(0,0); }else stall-ep0(); } void set-feature( void) { unsigned char endp; unsigned char bRecipient=ControlData. DeviceRequest. bmRequestType&USB-RECIPIENT; if(bRecipient ==USB-RECIPIENT-DEVICE&ControlData.DeviceRequest.wValue==USB-FEATURE-REMOTE-WAKEUP){ DISABLE; bEPPflags. bits. remote-wakeup = 1; ENABLE; single-transmit(0,0); } else if(bRecipient=一USB-RECIPIENT ENDPOINT &&ControlData. DeviceRequest. wValue== USB-FEATURE-ENDPOINT-STALL){ endp=(unsigned char)(ControlData. DeviceRequest. wIndex&MAX-ENDPOINTS); if(ControlData.DeviceRequest.wIndex.&.(unsigned char) SB-ENDPOINT-DIRECTION-MASK) D12 SetEndpointStatus(2*endp+1,1); else D12-SetEndpointStatus(2*endp,1); single-transmit(0,0); } else stall-ep0(); } void set address(void) { D12-SetAddressEnable((unsigned char)(ControlData.DeviceRequest.wValue& DEVICE-ADDRESS-MASK),1); single-transmit(0,0); } void get-descriptor(void) { unsigned char bDescriptor = MSB(ControlData. DeviceRequest. wValue); if(bDescriptor==USB-DEVICE-DESCRIPTOR-TYPE){ code-transmit((unsigned char code*)&DeviceDescr,sizeof (USB-DEVICE-DESCRIPTOR)); }else if(bDescriptor ==USB-CONFIGURATION-DESCRIPTOR-TYPE){ if(ControlData.DeviceRequest.wLength>CONFIG-DESCRIPTOR-LENGTH) {ControlData. DeviceRequest. wLength=CONFIG-DESCRIPTOR-LENGTH;} code transmit((unsigned char code*)&ConfigDescr,ControlData.DeviceRequest.wLength); }else stall-epo(); } void get configuration(void) { unsigned char c = bEPPflags. bits. configuration; single-transmit(&c,1); } void set configuration(void) { if(ControlData. DeviceRequest. wValue==0){ single-transmit(0,0); DISABLE; bEPPflags. bits. Configuration=0; ENABLE; init unconfig(); }else if(ControlData. DeviceRequest. wValue==1){ single-transmit(0,0); init-unconfig(); init config(); DISABLE; bEPPflags. bits. configuration=1; ENABLE; }else stall-ep0(); } void get interface(void) { unsigned char txdat=0; single-transmit(&txdat,1); } void set interface(void) { if(ControlData. DeviceRequest. wValue==0 &&ControlData. DeviceRequest. wIndex ==0) single-transmit(0,0); else stall-ep0(); }