00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "Usb.h"
00033
00034 static byte usb_error = 0;
00035 static byte usb_task_state;
00036 DEV_RECORD devtable[ USB_NUMDEVICES + 1 ];
00037 EP_RECORD dev0ep;
00038
00039
00040
00041
00042 USB::USB () {
00043 usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
00044 init();
00045 }
00046
00047 void USB::init()
00048 {
00049 byte i;
00050 for( i = 0; i < ( USB_NUMDEVICES + 1 ); i++ ) {
00051 devtable[ i ].epinfo = NULL;
00052 devtable[ i ].devclass = 0;
00053 }
00054 devtable[ 0 ].epinfo = &dev0ep;
00055
00056 dev0ep.sndToggle = bmSNDTOG0;
00057 dev0ep.rcvToggle = bmRCVTOG0;
00058 }
00059 byte USB::getUsbTaskState( void )
00060 {
00061 return( usb_task_state );
00062 }
00063 void USB::setUsbTaskState( byte state )
00064 {
00065 usb_task_state = state;
00066 }
00067 EP_RECORD* USB::getDevTableEntry( byte addr, byte ep )
00068 {
00069 EP_RECORD* ptr;
00070 ptr = devtable[ addr ].epinfo;
00071 ptr += ep;
00072 return( ptr );
00073 }
00074
00075
00076 void USB::setDevTableEntry( byte addr, EP_RECORD* eprecord_ptr )
00077 {
00078 devtable[ addr ].epinfo = eprecord_ptr;
00079
00080 }
00081
00082
00083
00084
00085
00086 byte USB::ctrlReq( byte addr, byte ep, byte bmReqType, byte bRequest, byte wValLo, byte wValHi, unsigned int wInd, unsigned int nbytes, char* dataptr, unsigned int nak_limit )
00087 {
00088 boolean direction = false;
00089 byte rcode;
00090 SETUP_PKT setup_pkt;
00091
00092 regWr( rPERADDR, addr );
00093 if( bmReqType & 0x80 ) {
00094 direction = true;
00095 }
00096
00097 setup_pkt.ReqType_u.bmRequestType = bmReqType;
00098 setup_pkt.bRequest = bRequest;
00099 setup_pkt.wVal_u.wValueLo = wValLo;
00100 setup_pkt.wVal_u.wValueHi = wValHi;
00101 setup_pkt.wIndex = wInd;
00102 setup_pkt.wLength = nbytes;
00103 bytesWr( rSUDFIFO, 8, ( char *)&setup_pkt );
00104 rcode = dispatchPkt( tokSETUP, ep, nak_limit );
00105
00106 if( rcode ) {
00107 Serial.print("Setup packet error: ");
00108 Serial.print( rcode, HEX );
00109 return( rcode );
00110 }
00111
00112 if( dataptr != NULL ) {
00113 rcode = ctrlData( addr, ep, nbytes, dataptr, direction );
00114 }
00115 if( rcode ) {
00116 Serial.print("Data packet error: ");
00117 Serial.print( rcode, HEX );
00118 return( rcode );
00119 }
00120 rcode = ctrlStatus( ep, direction );
00121 return( rcode );
00122 }
00123
00124
00125 byte USB::ctrlStatus( byte ep, boolean direction, unsigned int nak_limit )
00126 {
00127 byte rcode;
00128 if( direction ) {
00129 rcode = dispatchPkt( tokOUTHS, ep, nak_limit );
00130 }
00131 else {
00132 rcode = dispatchPkt( tokINHS, ep, nak_limit );
00133 }
00134 return( rcode );
00135 }
00136
00137 byte USB::ctrlData( byte addr, byte ep, unsigned int nbytes, char* dataptr, boolean direction, unsigned int nak_limit )
00138 {
00139 byte rcode;
00140 if( direction ) {
00141 devtable[ addr ].epinfo[ ep ].rcvToggle = bmRCVTOG1;
00142 rcode = inTransfer( addr, ep, nbytes, dataptr, nak_limit );
00143 return( rcode );
00144 }
00145 else {
00146 devtable[ addr ].epinfo[ ep ].sndToggle = bmSNDTOG1;
00147 rcode = outTransfer( addr, ep, nbytes, dataptr, nak_limit );
00148 return( rcode );
00149 }
00150 }
00151
00152
00153
00154
00155 byte USB::inTransfer( byte addr, byte ep, unsigned int nbytes, char* data, unsigned int nak_limit )
00156 {
00157 byte rcode;
00158 byte pktsize;
00159 byte maxpktsize = devtable[ addr ].epinfo[ ep ].MaxPktSize;
00160 unsigned int xfrlen = 0;
00161 regWr( rHCTL, devtable[ addr ].epinfo[ ep ].rcvToggle );
00162 while( 1 ) {
00163 rcode = dispatchPkt( tokIN, ep, nak_limit );
00164 if( rcode ) {
00165 return( rcode );
00166 }
00167
00168
00169 if(( regRd( rHIRQ ) & bmRCVDAVIRQ ) == 0 ) {
00170 return ( 0xf0 );
00171 }
00172 pktsize = regRd( rRCVBC );
00173 data = bytesRd( rRCVFIFO, pktsize, data );
00174 regWr( rHIRQ, bmRCVDAVIRQ );
00175 xfrlen += pktsize;
00176
00177
00178
00179 if (( pktsize < maxpktsize ) || (xfrlen >= nbytes )) {
00180 if( regRd( rHRSL ) & bmRCVTOGRD ) {
00181 devtable[ addr ].epinfo[ ep ].rcvToggle = bmRCVTOG1;
00182 }
00183 else {
00184 devtable[ addr ].epinfo[ ep ].rcvToggle = bmRCVTOG0;
00185 }
00186 return( 0 );
00187 }
00188 }
00189 }
00190
00191 int USB::newInTransfer( byte addr, byte ep, unsigned int nbytes, char* data, unsigned int nak_limit )
00192 {
00193 byte rcode;
00194 byte pktsize;
00195 byte maxpktsize = devtable[ addr ].epinfo[ ep ].MaxPktSize;
00196 unsigned int xfrlen = 0;
00197 regWr( rHCTL, devtable[ addr ].epinfo[ ep ].rcvToggle );
00198 while( 1 ) {
00199 rcode = dispatchPkt( tokIN, ep, nak_limit );
00200 if( rcode ) {
00201 return -1;
00202 }
00203
00204
00205 if(( regRd( rHIRQ ) & bmRCVDAVIRQ ) == 0 ) {
00206 return -1;
00207 }
00208 pktsize = regRd( rRCVBC );
00209 data = bytesRd( rRCVFIFO, pktsize, data );
00210 regWr( rHIRQ, bmRCVDAVIRQ );
00211 xfrlen += pktsize;
00212
00213
00214
00215 if (( pktsize < maxpktsize ) || (xfrlen >= nbytes )) {
00216 if( regRd( rHRSL ) & bmRCVTOGRD ) {
00217 devtable[ addr ].epinfo[ ep ].rcvToggle = bmRCVTOG1;
00218 }
00219 else {
00220 devtable[ addr ].epinfo[ ep ].rcvToggle = bmRCVTOG0;
00221 }
00222 return xfrlen;
00223 }
00224 }
00225 }
00226
00227
00228
00229
00230
00231 byte USB::outTransfer( byte addr, byte ep, unsigned int nbytes, char* data, unsigned int nak_limit )
00232 {
00233 byte rcode, retry_count;
00234 char* data_p = data;
00235 unsigned int bytes_tosend, nak_count;
00236 unsigned int bytes_left = nbytes;
00237 byte maxpktsize = devtable[ addr ].epinfo[ ep ].MaxPktSize;
00238 unsigned long timeout = millis() + USB_XFER_TIMEOUT;
00239
00240 if (!maxpktsize) {
00241 return 0xFE;
00242 }
00243
00244 regWr( rHCTL, devtable[ addr ].epinfo[ ep ].sndToggle );
00245 while( bytes_left ) {
00246 retry_count = 0;
00247 nak_count = 0;
00248 bytes_tosend = ( bytes_left >= maxpktsize ) ? maxpktsize : bytes_left;
00249 bytesWr( rSNDFIFO, bytes_tosend, data_p );
00250 regWr( rSNDBC, bytes_tosend );
00251 regWr( rHXFR, ( tokOUT | ep ));
00252 while(!(regRd( rHIRQ ) & bmHXFRDNIRQ ));
00253 regWr( rHIRQ, bmHXFRDNIRQ );
00254 rcode = ( regRd( rHRSL ) & 0x0f );
00255 while( rcode && ( timeout > millis())) {
00256 switch( rcode ) {
00257 case hrNAK:
00258 nak_count++;
00259 if( nak_limit && ( nak_count == USB_NAK_LIMIT )) {
00260 return( rcode);
00261 }
00262 break;
00263 case hrTIMEOUT:
00264 retry_count++;
00265 if( retry_count == USB_RETRY_LIMIT ) {
00266 return( rcode );
00267 }
00268 break;
00269 default:
00270 return( rcode );
00271 }
00272
00273 regWr( rSNDBC, 0 );
00274 regWr( rSNDFIFO, *data_p );
00275 regWr( rSNDBC, bytes_tosend );
00276 regWr( rHXFR, ( tokOUT | ep ));
00277 while(!(regRd( rHIRQ ) & bmHXFRDNIRQ ));
00278 regWr( rHIRQ, bmHXFRDNIRQ );
00279 rcode = ( regRd( rHRSL ) & 0x0f );
00280 }
00281 bytes_left -= bytes_tosend;
00282 data_p += bytes_tosend;
00283 }
00284 devtable[ addr ].epinfo[ ep ].sndToggle = ( regRd( rHRSL ) & bmSNDTOGRD ) ? bmSNDTOG1 : bmSNDTOG0;
00285 return( rcode );
00286 }
00287
00288
00289
00290
00291
00292 byte USB::dispatchPkt( byte token, byte ep, unsigned int nak_limit )
00293 {
00294 unsigned long timeout = millis() + USB_XFER_TIMEOUT;
00295 byte tmpdata;
00296 byte rcode;
00297 unsigned int nak_count = 0;
00298 char retry_count = 0;
00299
00300 while( timeout > millis() ) {
00301 regWr( rHXFR, ( token|ep ));
00302 rcode = 0xff;
00303 while( millis() < timeout ) {
00304 tmpdata = regRd( rHIRQ );
00305 if( tmpdata & bmHXFRDNIRQ ) {
00306 regWr( rHIRQ, bmHXFRDNIRQ );
00307 rcode = 0x00;
00308 break;
00309 }
00310 }
00311 if( rcode != 0x00 ) {
00312 return( rcode );
00313 }
00314 rcode = ( regRd( rHRSL ) & 0x0f );
00315 switch( rcode ) {
00316 case hrNAK:
00317 nak_count ++;
00318 if( nak_limit && ( nak_count == nak_limit )) {
00319 return( rcode );
00320 }
00321 break;
00322 case hrTIMEOUT:
00323 retry_count ++;
00324 if( retry_count == USB_RETRY_LIMIT ) {
00325 return( rcode );
00326 }
00327 break;
00328 default:
00329 return( rcode );
00330 }
00331 }
00332 return( rcode );
00333 }
00334
00335 void USB::Task( void )
00336 {
00337 byte i;
00338 byte rcode;
00339 static byte tmpaddr;
00340 byte tmpdata;
00341 static unsigned long delay = 0;
00342 USB_DEVICE_DESCRIPTOR buf;
00343 tmpdata = getVbusState();
00344
00345
00346 switch( tmpdata ) {
00347 case SE1:
00348 usb_task_state = USB_DETACHED_SUBSTATE_ILLEGAL;
00349 break;
00350 case SE0:
00351 if(( usb_task_state & USB_STATE_MASK ) != USB_STATE_DETACHED ) {
00352 usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
00353 }
00354 break;
00355 case FSHOST:
00356 case LSHOST:
00357 if(( usb_task_state & USB_STATE_MASK ) == USB_STATE_DETACHED ) {
00358 delay = millis() + USB_SETTLE_DELAY;
00359 usb_task_state = USB_ATTACHED_SUBSTATE_SETTLE;
00360 }
00361 break;
00362 }
00363
00364
00365 switch( usb_task_state ) {
00366 case USB_DETACHED_SUBSTATE_INITIALIZE:
00367 init();
00368 usb_task_state = USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE;
00369 break;
00370 case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE:
00371 break;
00372 case USB_DETACHED_SUBSTATE_ILLEGAL:
00373 break;
00374 case USB_ATTACHED_SUBSTATE_SETTLE:
00375 if( delay < millis() ) {
00376 usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE;
00377 }
00378 break;
00379 case USB_ATTACHED_SUBSTATE_RESET_DEVICE:
00380 regWr( rHCTL, bmBUSRST );
00381 usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE;
00382 break;
00383 case USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE:
00384 if(( regRd( rHCTL ) & bmBUSRST ) == 0 ) {
00385 tmpdata = regRd( rMODE ) | bmSOFKAENAB;
00386 regWr( rMODE, tmpdata );
00387
00388 usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_SOF;
00389 delay = millis() + 20;
00390 }
00391 break;
00392 case USB_ATTACHED_SUBSTATE_WAIT_SOF:
00393 if( regRd( rHIRQ ) & bmFRAMEIRQ ) {
00394 if( delay < millis() ) {
00395 usb_task_state = USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE;
00396 }
00397 }
00398 break;
00399 case USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE:
00400
00401 devtable[ 0 ].epinfo->MaxPktSize = 8;
00402 rcode = getDevDescr( 0, 0, 8, ( char* )&buf );
00403 if( rcode == 0 ) {
00404 devtable[ 0 ].epinfo->MaxPktSize = buf.bMaxPacketSize0;
00405 usb_task_state = USB_STATE_ADDRESSING;
00406 }
00407 else {
00408 usb_error = USB_ATTACHED_SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE;
00409 usb_task_state = USB_STATE_ERROR;
00410 }
00411 break;
00412 case USB_STATE_ADDRESSING:
00413 for( i = 1; i < USB_NUMDEVICES; i++ ) {
00414 if( devtable[ i ].epinfo == NULL ) {
00415 devtable[ i ].epinfo = devtable[ 0 ].epinfo;
00416
00417
00418 rcode = setAddr( 0, 0, i );
00419 if( rcode == 0 ) {
00420 tmpaddr = i;
00421 usb_task_state = USB_STATE_CONFIGURING;
00422 }
00423 else {
00424 usb_error = USB_STATE_ADDRESSING;
00425 usb_task_state = USB_STATE_ERROR;
00426 }
00427 break;
00428 }
00429 }
00430 if( usb_task_state == USB_STATE_ADDRESSING ) {
00431 usb_error = 0xfe;
00432 usb_task_state = USB_STATE_ERROR;
00433 }
00434 break;
00435 case USB_STATE_CONFIGURING:
00436 break;
00437 case USB_STATE_RUNNING:
00438 break;
00439 case USB_STATE_ERROR:
00440 break;
00441 }
00442 }
00443