24 #if defined(ARDUINO) && ARDUINO >=100
30 static uint8_t usb_error = 0;
31 static uint8_t usb_task_state;
49 return( usb_task_state );
54 usb_task_state = state;
66 for (uint8_t i=0; i<p->
epcount; i++)
68 if ((pep)->epAddr == ep)
95 uint8_t USB::SetAddress(uint8_t addr, uint8_t ep,
EpInfo **ppep, uint16_t &nak_limit)
135 uint8_t
USB::ctrlReq( uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi,
136 uint16_t wInd, uint16_t total, uint16_t nbytes, uint8_t* dataptr,
USBReadParser *p)
138 boolean direction =
false;
145 rcode = SetAddress(addr, ep, &pep, nak_limit);
150 direction = (( bmReqType & 0x80 ) > 0);
167 if( dataptr != NULL )
171 uint16_t left = total;
178 uint16_t read = nbytes;
181 rcode = InTransfer( pep, nak_limit, &read, dataptr );
199 rcode = OutTransfer( pep, nak_limit, nbytes, dataptr );
212 uint8_t
USB::inTransfer( uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t* data)
215 uint16_t nak_limit = 0;
217 uint8_t rcode = SetAddress(addr, ep, &pep, nak_limit);
222 return InTransfer(pep, nak_limit, nbytesptr, data);
225 uint8_t USB::InTransfer(
EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, uint8_t* data)
230 uint16_t nbytes = *nbytesptr;
250 assert(pktsize <= nbytes);
252 int16_t mem_left = (int16_t)nbytes - *((int16_t*)nbytesptr);
257 data =
bytesRd(
rRCVFIFO, ((pktsize > mem_left) ? mem_left : pktsize), data );
260 *nbytesptr += pktsize;
265 if (( pktsize < maxpktsize ) || (*nbytesptr >= nbytes ))
283 uint8_t rcode = SetAddress(addr, ep, &pep, nak_limit);
288 return OutTransfer(pep, nak_limit, nbytes, data);
291 uint8_t USB::OutTransfer(
EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data)
293 uint8_t rcode, retry_count;
294 uint8_t *data_p = data;
295 uint16_t bytes_tosend, nak_count;
296 uint16_t bytes_left = nbytes;
300 if (maxpktsize < 1 || maxpktsize > 64)
311 bytes_tosend = ( bytes_left >= maxpktsize ) ? maxpktsize : bytes_left;
319 while( rcode && ( timeout > millis()))
325 if( nak_limit && ( nak_count == nak_limit ))
346 bytes_left -= bytes_tosend;
347 data_p += bytes_tosend;
362 uint8_t retry_count = 0;
363 uint16_t nak_count = 0;
365 while( timeout > millis() )
370 while( millis() < timeout )
374 if( tmpdata & bmHXFRDNIRQ )
392 if( nak_limit && ( nak_count == nak_limit ))
414 static unsigned long delay = 0;
416 bool lowspeed =
false;
447 rcode = devConfig[i]->
Poll();
449 switch( usb_task_state ) {
455 rcode = devConfig[i]->
Release();
464 if( delay < millis() )
477 delay = millis() + 20;
483 if( delay < millis() )
523 p0->
lowspeed = (lowspeed) ?
true :
false;
526 uint8_t bAddress = addrPool.
AllocAddress(parent,
false, port);
536 p->lowspeed = lowspeed;
539 rcode =
setAddr( 0, 0, bAddress );
552 static uint8_t dev_index = 0;
557 if (!devConfig[devConfigIndex])
560 rcode = devConfig[devConfigIndex]->
Init(parent, port, lowspeed);
592 if (devConfig[i]->GetAddress() == addr)
593 return devConfig[i]->
Release();
597 //get device descriptor
600 return(
ctrlReq( addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, 0x00,
USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, nbytes, dataptr, NULL ));
603 uint8_t
USB::getConfDescr( uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr )
605 return(
ctrlReq( addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, conf,
USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, nbytes, dataptr, NULL ));
610 const uint8_t bufSize = 64;
611 uint8_t buf[bufSize];
622 return(
ctrlReq( addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, conf,
USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p ));
626 uint8_t
USB::getStrDescr( uint8_t addr, uint8_t ep, uint16_t ns, uint8_t index, uint16_t langid, uint8_t* dataptr )
628 return(
ctrlReq( addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, index,
USB_DESCRIPTOR_STRING, langid, ns, ns, dataptr, NULL ));
633 return(
ctrlReq( oldaddr, ep,
bmREQ_SET,
USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL ));
638 return(
ctrlReq( addr, ep,
bmREQ_SET,
USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL ));
641 #endif // defined(USB_METHODS_INLINE)