24 #if defined(ARDUINO) && ARDUINO >=100
30 static uint8_t usb_error = 0;
31 static uint8_t usb_task_state;
46 return ( usb_task_state);
50 usb_task_state = state;
61 for (uint8_t i = 0; i < p->
epcount; i++) {
62 if ((pep)->epAddr == ep)
89 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) {
137 bool direction =
false;
142 uint16_t nak_limit = 0;
144 rcode = SetAddress(addr, ep, &pep, nak_limit);
149 direction = ((bmReqType & 0x80) > 0);
170 uint16_t left = total;
176 uint16_t read = nbytes;
179 rcode = InTransfer(pep, nak_limit, &read, dataptr);
201 rcode = OutTransfer(pep, nak_limit, nbytes, dataptr);
215 uint8_t
USB::inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t* data) {
217 uint16_t nak_limit = 0;
219 uint8_t rcode = SetAddress(addr, ep, &pep, nak_limit);
225 return InTransfer(pep, nak_limit, nbytesptr, data);
228 uint8_t USB::InTransfer(
EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, uint8_t* data) {
232 uint16_t nbytes = *nbytesptr;
263 if (pktsize > nbytes) {
270 int16_t mem_left = (int16_t)nbytes - *((int16_t*)nbytesptr);
275 data =
bytesRd(
rRCVFIFO, ((pktsize > mem_left) ? mem_left : pktsize), data);
278 *nbytesptr += pktsize;
283 if ((pktsize < maxpktsize) || (*nbytesptr >= nbytes))
301 uint16_t nak_limit = 0;
303 uint8_t rcode = SetAddress(addr, ep, &pep, nak_limit);
308 return OutTransfer(pep, nak_limit, nbytes, data);
311 uint8_t USB::OutTransfer(
EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data) {
313 uint8_t *data_p = data;
314 uint16_t bytes_tosend, nak_count;
315 uint16_t bytes_left = nbytes;
319 if (maxpktsize < 1 || maxpktsize > 64)
329 bytes_tosend = (bytes_left >= maxpktsize) ? maxpktsize : bytes_left;
337 while (rcode && (timeout > millis())) {
341 if (nak_limit && (nak_count == nak_limit))
369 bytes_left -= bytes_tosend;
370 data_p += bytes_tosend;
387 uint8_t retry_count = 0;
388 uint16_t nak_count = 0;
390 while (timeout > millis()) {
394 while (timeout > millis())
398 if (tmpdata & bmHXFRDNIRQ) {
414 if (nak_limit && (nak_count == nak_limit))
435 static unsigned long delay = 0;
437 bool lowspeed =
false;
468 rcode = devConfig[i]->
Poll();
470 switch (usb_task_state) {
476 rcode = devConfig[i]->
Release();
485 if (delay < millis())
508 delay = millis() + 20;
551 p0->
lowspeed = (lowspeed) ?
true :
false;
554 uint8_t bAddress = addrPool.
AllocAddress(parent,
false, port);
564 p->lowspeed = lowspeed;
567 rcode =
setAddr(0, 0, bAddress);
577 uint8_t USB::AttemptConfig(uint8_t driver, uint8_t parent, uint8_t port,
bool lowspeed) {
592 rcode = devConfig[driver]->
Init(parent, port, lowspeed);
639 uint8_t devConfigIndex;
661 oldep_ptr = p->epinfo;
668 p->lowspeed = lowspeed;
673 p->epinfo = oldep_ptr;
692 for (devConfigIndex = 0; devConfigIndex <
USB_NUMDEVICES; devConfigIndex++) {
693 if (!devConfig[devConfigIndex])
continue;
694 if (devConfig[devConfigIndex]->GetAddress())
continue;
695 if (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->
DEVCLASSOK(klass)) {
696 rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed);
702 if (devConfigIndex < USB_NUMDEVICES) {
708 for (devConfigIndex = 0; devConfigIndex <
USB_NUMDEVICES; devConfigIndex++) {
709 if (!devConfig[devConfigIndex])
continue;
710 if (devConfig[devConfigIndex]->GetAddress())
continue;
711 if (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass))
continue;
712 rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed);
735 if(!devConfig[i])
continue;
736 if (devConfig[i]->GetAddress() == addr)
737 return devConfig[i]->
Release();
743 //get device descriptor
746 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, 0x00,
USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, nbytes, dataptr, NULL));
750 uint8_t
USB::getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr) {
751 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, conf,
USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, nbytes, dataptr, NULL));
755 const uint8_t bufSize = 64;
756 uint8_t buf[bufSize];
767 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, conf,
USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p));
772 uint8_t
USB::getStrDescr(uint8_t addr, uint8_t ep, uint16_t ns, uint8_t index, uint16_t langid, uint8_t* dataptr) {
773 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, index,
USB_DESCRIPTOR_STRING, langid, ns, ns, dataptr, NULL));
778 return (
ctrlReq(oldaddr, ep,
bmREQ_SET,
USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL));
783 return (
ctrlReq(addr, ep,
bmREQ_SET,
USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL));
786 #endif // defined(USB_METHODS_INLINE)