void _USBNTransmitEvent(void) { unsigned char event; void (*ptr)(); event = USBNRead(TXEV); USBNDebug("tx event\r\n"); if(event & TX_FIFO0) _USBNTransmitFIFO0(); // dynamic function call else if(event & TX_FIFO1) { USBNRead(TXS1); if(txfifos.tx1==1){ ptr = txfifos.func1; (*ptr)(); } return; } else { #if DEBUG USBNDebug("tx event\r\n"); #endif //USBNRead(TXS1); // get transmitter status USBNRead(TXS2); // get transmitter status USBNRead(TXS3); // get transmitter status } }
void _USBNGetDescriptor(DeviceRequest *req) { unsigned char index = req->wValue; unsigned char type = req->wValue >> 8; EP0tx.Index = 0; EP0tx.DataPid = 1; switch (type) { case DEVICE: #if DEBUG USBNDebug("DEVICE DESCRIPTOR\n\r"); #endif EP0tx.Size = DeviceDescriptor[0]; EP0tx.Buf = DeviceDescriptor; // first get descriptor request is // always be answered with first 8 unsigned chars of dev descriptor if(req->wLength==0x40) EP0tx.Size = 8; break; case CONFIGURATION: #if DEBUG USBNDebug("CONFIGURATION DESCRIPTOR "); #endif // send complete tree EP0tx.Size =req->wLength; EP0tx.Buf = ConfigurationDescriptor; break; #if 0 case STRING: #if DEBUG USBNDebug("STRING DESCRIPTOR "); SendHex(index); USBNDebug("\r\n"); #endif if(index ==1) { EP0tx.Buf = &FinalStringArray[index][0]; EP0tx.Size = EP0tx.Buf[0]; } else { char lang[]={0x04,0x03,0x09,0x04}; EP0tx.Size=4; EP0tx.Buf=lang; } break; #endif } //if (EP0rx.Buf[7]==0) //if less than 256 req'd // if (EP0tx.Size > EP0rx.Buf[6]) EP0tx.Size = EP0rx.Buf[6]; _USBNTransmit(&EP0tx); }
void _USBNTransmitFIFO0(void) { unsigned char txstat; txstat = USBNRead(TXS0); // get transmitter status #if DEBUG USBNDebug("t"); #endif if(txstat & TX_DONE) // if transmit completed { USBNWrite(TXC0,FLUSH); // flush TX0 and disable //USBNDebug("tx done "); if(txstat & ACK_STAT) // ACK received { //USBNDebug("tx ack "); if(EP0tx.Index < EP0tx.Size) { //USBNDebug("next packets "); _USBNTransmit(&EP0tx); } else // not in multi-packet mode { //USBNDebug("no multi packet "); //USBNWrite(RXC0,FLUSH); // re-enable the receiver //USBNWrite(TXC0,TX_EN); // re-enable the receiver USBNWrite(RXC0,RX_EN); // re-enable the receiver } } else // this probably means we issued a stall handshake { #if DEBUG USBNDebug("stall handshake "); #endif //USBNFunctionInfo.GetDesc=0; // exit multi-packet mode USBNWrite(RXC0,RX_EN); // re-enable the receiver } } // otherwise something must have gone wrong with the previous // transmission, or we got here somehow we shouldn't have else { #if DEBUG USBNDebug("tx0 error\n"); #endif } // we do this stuff for all tx_0 events }
void _USBNAlternateEvent(void) { unsigned char event; event = USBNRead(ALTEV); #if DEBUG USBNDebug("alt event\r\n"); #endif if(event & ALT_RESET) { USBNWrite(NFSR,RST_ST); // NFS = NodeReset USBNWrite(FAR,AD_EN+0); USBNWrite(EPC0,0x00); USBNWrite(TXC0,FLUSH); USBNWrite(RXC0,RX_EN); // allow reception USBNWrite(NFSR,OPR_ST); // NFS = NodeOperational #if DEBUG USBNDebug("reset\r\n"); #endif } if(event & ALT_SD3) { USBNWrite(ALTMSK,ALT_RESUME+ALT_RESET); // adjust interrupts /* USBNWrite(NFSR,SUS_ST); // enter suspend state USBNDebug("sd3\r\n"); */ } if(event & ALT_RESUME) { USBNWrite(ALTMSK,ALT_SD3+ALT_RESET+ALT_RESUME); /* USBNWrite(EPC0,0x00); USBNWrite(RXC0,RX_EN); // allow reception USBNWrite(TXC0,FLUSH); USBNWrite(NFSR,OPR_ST); USBNDebug("resume\r\n"); */ } if(event & ALT_EOP) { #if DEBUG USBNDebug("eop\r\n"); #endif } }
void _USBNTransmitEvent(void) { unsigned char event; event = USBNRead(TXEV); //USBNDebug("tx event\r\n"); if(event & TX_FIFO0) _USBNTransmitFIFO0(); else { #if DEBUG USBNDebug("tx event\r\n"); #endif USBNRead(TXS1); // get transmitter status USBNRead(TXS2); // get transmitter status USBNRead(TXS3); // get transmitter status } }
void _USBNNackEvent(void) { unsigned char event; event = USBNRead(NAKEV); //USBNWrite(RXC1,FLUSH); //re-enable the receiver //USBNWrite(RXC1,RX_EN); //re-enable the receiver USBNDebug("nack event\r\n"); /* if (EP0tx.Size > EP0tx.usbnfifo) //multi-pkt status stage? { //USBNDebug("flush"); USBNWrite(TXC0,FLUSH); //flush TX0 and disable //USBNWrite(RXC0,RX_EN); //re-enable the receiver EP0tx.DataPid = 1; } */ }
//this is only my debug tool void Terminal(char cmd) { char h,l; unsigned char tmp; int i; struct list_entry *ptr; char *values; switch(cmd) { case 'i': USBNStart(); break; // write to usb register case 'w': //UARTWrite("write to USB reg:"); //USBNDEBUGPRINT("write to USB reg:"); h = UARTGetChar(); l = UARTGetChar(); SendHex(AsciiToHex(h,l)); tmp = AsciiToHex(h,l); UARTWrite("value:"); h = UARTGetChar(); l = UARTGetChar(); SendHex(AsciiToHex(h,l)); //USBNWrite(tmp,AsciiToHex(h,l)); UARTWrite("result:"); SendHex(USBNRead(tmp)); UARTWrite("\r\n"); break; // read from usb register case 'r': UARTWrite("read USB reg:"); h = UARTGetChar(); l = UARTGetChar(); SendHex(AsciiToHex(h,l)); UARTWrite("->"); SendHex(USBNRead(AsciiToHex(h,l))); UARTWrite("\r\n"); break; case 'h': UARTWrite("i usbn init procedure\r\n"); UARTWrite("w write USBN Register <h,l>(address) <h,l> (value) e.g 05 00\r\n"); UARTWrite("r read USBN Register <h,l> e.g. 02 ( RID)\r\n"); UARTWrite("s show all USBN Registers\r\n"); UARTWrite("b send test data from func to host\r\n"); UARTWrite("d show descriptors\r\n"); break; // show all registers case 's': for(i=0;i<=63;i++) { SendHex(i); UARTWrite("->"); SendHex(USBNRead(i)); UARTWrite("\r\n"); } break; case 'd': USBNDebug("\r\nDescriptor List\r\n"); ptr = DescriptorList; while(ptr != NULL) { values = (char*)ptr->data; SendHex(ptr->type); SendHex(ptr->len); SendHex(ptr->conf); SendHex(ptr->interf); USBNDebug(" "); for(i=0;i<ptr->len;i++) SendHex(values[i]); USBNDebug("\r\n"); ptr=ptr->next; } break; case 'b': UARTWrite("send test data from fifo1\r\n"); int j,i; char stat; USBNWrite(TXC1,FLUSH); USBNWrite(TXD1,0x01); for(j=0;j<63;j++) USBNBurstWrite((unsigned char)j); USBNWrite(TXC1,TX_LAST+TX_EN); //USBNWrite(TXC1,TX_LAST+TX_EN+TX_TOGL); break; case 'p': USBNWrite(TXC1,TX_LAST+TX_EN); break; default: UARTWrite("unknown command\r\n"); } }
void _USBNReceiveEvent(void) { unsigned char event; void (*ptr)(); char buf[64]; char *bufp=&buf[0]; event = USBNRead(RXEV); char tmp; int i=0; USBNDebug("rx event\r\n"); if(event & RX_FIFO0) _USBNReceiveFIFO0(); // dynamic function call else if(event & RX_FIFO1) { USBNRead(RXS1); *bufp = USBNRead(RXD1); for(i=0;i<63;i++) *(++bufp)=USBNBurstRead(); if(rxfifos.rx1==1){ ptr = rxfifos.func1; (*ptr)(&buf); } USBNWrite(RXC1,FLUSH); USBNWrite(RXC1,RX_EN); return; } // dynamic function call else if(event & RX_FIFO2) { USBNRead(RXS2); *bufp = USBNRead(RXD2); for(i=0;i<63;i++) *(++bufp)=USBNBurstRead(); if(rxfifos.rx2==1){ ptr = rxfifos.func2; (*ptr)(&buf); } USBNWrite(RXC2,FLUSH); USBNWrite(RXC2,RX_EN); return; } // dynamic function call else if(event & RX_FIFO3) { USBNRead(RXS3); for(i=0;i<64;i++) buf[i]=USBNRead(RXD3); if(rxfifos.rx3==1){ ptr = rxfifos.func1; (*ptr)(&buf); } } else {} }
// ******************************************************************** // Receive and Transmit functions for EPs // ******************************************************************** void _USBNReceiveFIFO0(void) { unsigned char rxstatus; char Buf[8]; DeviceRequest *req; int i; #if DEBUG USBNDebug("rx\r\n"); #endif rxstatus = USBNRead(RXS0); if(rxstatus & SETUP_R) { for(i=0;i<8;i++){ Buf[i] = USBNRead(EP0rx.usbnData); } #if DEBUG for(i=0;i<8;i++) SendHex(Buf[i]); // type - get descr or set address USBNDebug("\r\n"); #endif req = (DeviceRequest*)(Buf); USBNWrite(RXC0,FLUSH); // make sure the RX is off USBNWrite(TXC0,FLUSH); // make sure the TX is off USBNWrite(EPC0,USBNRead(EPC0)&0x7F); // turn of stall switch (req->bmRequestType & 0x60) // decode request type { case DO_STANDARD: // standard request USBNDebug("HALLO STANDARD "); SendHex(req->bRequest); USBNDebug("\n\r"); switch (req->bRequest) // decode request code { case CLR_FEATURE: #if DEBUG USBNDebug("CLR FEATURE\n\r"); #endif //_USBNClearFeature(req); break; case GET_CONFIGURATION: #if DEBUG //USBNDebug("GET CONFIG\n\r"); #endif _USBNGetConfiguration(req); break; case GET_DESCRIPTOR: #if DEBUG USBNDebug("GET DESCRIPTOR\n\r"); #endif _USBNGetDescriptor(req); break; case GET_INTERFACE: #if DEBUG USBNDebug("GET INTERFACE\n\r"); #endif break; case GET_STATUS: #if DEBUG #endif USBNDebug("GET STATUS\n\r"); //_USBNGetStatus(req); USBNWrite(TXC0,FLUSH); USBNWrite(TXD0,1); USBNWrite(TXD0,0); USBNWrite(TXC0,TX_TOGL+TX_EN); //enable the TX (DATA1) break; case SET_ADDRESS: #if DEBUG USBNDebug("SET ADDRESS "); #endif _USBNSetAddress(req); break; case SET_CONFIGURATION: #if DEBUG USBNDebug("SET CONFIGURATION\n\r"); #endif _USBNSetConfiguration(req); break; case SET_FEATURE: #if DEBUG USBNDebug("SET FEATURE\n\r"); #endif USBNWrite(TXC0,TX_TOGL+TX_EN); //enable the TX (DATA1) break; case SET_INTERFACE: #if DEBUG USBNDebug("SET INTERFACE\n\r"); #endif USBNWrite(TXC0,TX_TOGL+TX_EN); //enable the TX (DATA1) break; default: // unsupported standard req #if DEBUG USBNDebug("unsupported standard req\n\r"); #endif USBNWrite(EPC0,USBNRead(EPC0)+STALL); // stall the endpoint break; } break; case DO_CLASS: // class request #if DEBUG USBNDebug("Class request\n\r"); #endif //USBNDecodeClassRequest(req); USBNWrite(TXC0,TX_TOGL+TX_EN); //enable the TX (DATA1) break; case DO_VENDOR: // vendor request #if DEBUG USBNDebug("Vendor request\n\r"); #endif USBNDecodeVendorRequest(req); USBNWrite(TXC0,TX_TOGL+TX_EN); //enable the TX (DATA1) break; default: // unsupported req type #if DEBUG USBNDebug("unsupported req type\r\n"); #endif USBNWrite(EPC0,USBNRead(EPC0)+STALL); // stall the endpoint break; } //the following is done for all setup packets. Note that if //no data was stuffed into the FIFO, the result of the fol- //lowing will be a zero-length response. // only for stage 2 transfers if(req->bmRequestType == 0x00) USBNWrite(TXC0,TX_TOGL+TX_EN); //enable the TX (DATA1) } else // if not a setuppacket { USBNDebug("error transmit\r\n"); if (EP0tx.Size > EP0tx.usbnfifo) // multi-pkt status stage? { if ((rxstatus& 0x5F)!=0x10) // length error?? { #if DEBUG USBNDebug("length error\r\n"); #endif } EP0tx.Size=0; // exit multi-packet mode USBNWrite(TXC0,FLUSH); // flush TX0 and disable USBNWrite(RXC0,RX_EN); // re-enable the receiver } } }
void _USBNGetDescriptor(DeviceRequest *req) { unsigned char index = req->wValue; unsigned char type = req->wValue >> 8; // unsigned char length = req->wLength; EP0tx.Index = 0; EP0tx.DataPid = 1; switch (type) { case DEVICE: #if DEBUG USBNDebug("DEVICE DESCRIPTOR\n\r"); #endif EP0tx.Size = DeviceDescriptor.bLength; EP0tx.Buf = (unsigned char*)(&DeviceDescriptor); // first get descriptor request is // always be answered with first 8 unsigned chars of dev descriptor //SendHex(req->wLength); if(req->wLength==0x08) req->wLength=0x40; if(req->wLength==0x40) EP0tx.Size = 8; break; case CONFIGURATION: #if DEBUG USBNDebug("CONFIGURATION DESCRIPTOR "); #endif // send complete tree EP0tx.Size =(unsigned short)req->wLength; EP0tx.Buf = (unsigned char*)&(FinalConfigurationArray[index][0]); break; case STRING: #if DEBUG USBNDebug("STRING DESCRIPTOR "); SendHex(index); USBNDebug("\r\n"); #endif if(index >0) { EP0tx.Buf = (unsigned char*)&(FinalStringArray[index][0]); EP0tx.Size = EP0tx.Buf[0]; } else { #if DEBUG USBNDebug("language descriptor"); #endif char lang[]= {0x04,0x03,0x09,0x04}; //EP0tx.Buf = &FinalStringArray[0][0]; //EP0tx.Size = EP0tx.Buf[0]; if(req->wLength==1) EP0tx.Size=1; else EP0tx.Size=4; EP0tx.Buf = (unsigned char*)lang; } break; } //if (EP0rx.Buf[7]==0) //if less than 256 req'd // if (EP0tx.Size > EP0rx.Buf[6]) EP0tx.Size = EP0rx.Buf[6]; _USBNTransmit(&EP0tx); }
void _USBNGetDescriptor(DeviceRequest *req) { unsigned char index = req->wValue; unsigned char type = req->wValue >> 8; EP0tx.Index = 0; EP0tx.DataPid = 1; switch (type) { case DEVICE: #if DEBUG USBNDebug("DEVICE DESCRIPTOR\n\r"); #endif EP0tx.Size = DeviceDescriptor[0]; EP0tx.Buf = DeviceDescriptor; // first get descriptor request is // always be answered with first 8 unsigned chars of dev descriptor if(req->wLength==0x40) EP0tx.Size = 8; break; case CONFIGURATION: #if DEBUG USBNDebug("CONFIGURATION DESCRIPTOR "); #endif // send complete tree EP0tx.Size =req->wLength; EP0tx.Buf = ConfigurationDescriptor; break; case STRING: if(index >0) { EP0tx.Buf = &FinalStringArray[index][0]; EP0tx.Size = EP0tx.Buf[0]; UARTWrite("a"); _USBNTransmit(&EP0tx); UARTWrite("b"); return; } else { char lang[]={0x04,0x03,0x09,0x04,0,0,0,0,0}; //EP0tx.Buf = &FinalStringArray[0][0]; EP0tx.Size=4; EP0tx.Buf=lang; UARTWrite("x"); _USBNTransmit(&EP0tx); UARTWrite("y"); //USBNWrite(TXC0,FLUSH); //USBNWrite(RXC0,FLUSH); //USBNWrite(RXC0,RX_EN); //USBNWrite(TXD0,0); //USBNWrite(TXC0,TX_EN+TX_TOGL); return; } break; } //if (EP0rx.Buf[7]==0) //if less than 256 req'd // if (EP0tx.Size > EP0rx.Buf[6]) EP0tx.Size = EP0rx.Buf[6]; _USBNTransmit(&EP0tx); //_USBNTransmit(&EP0tx); //USBNWrite(TXD0,0); //USBNWrite(TXC0,FLUSH); //USBNWrite(TXC0,TX_EN+TX_TOGL); }