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) {
128 uint8_t
USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi,
129 uint16_t wInd, uint16_t total, uint16_t nbytes, uint8_t* dataptr,
USBReadParser *p) {
130 bool direction =
false;
137 rcode = SetAddress(addr, ep, &pep, nak_limit);
142 direction = ((bmReqType & 0x80) > 0);
163 uint16_t left = total;
169 uint16_t read = nbytes;
172 rcode = InTransfer(pep, nak_limit, &read, dataptr);
189 rcode = OutTransfer(pep, nak_limit, nbytes, dataptr);
203 uint8_t
USB::inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t* data) {
205 uint16_t nak_limit = 0;
207 uint8_t rcode = SetAddress(addr, ep, &pep, nak_limit);
213 return InTransfer(pep, nak_limit, nbytesptr, data);
216 uint8_t USB::InTransfer(
EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, uint8_t* data) {
220 uint16_t nbytes = *nbytesptr;
244 assert(pktsize <= nbytes);
246 int16_t mem_left = (int16_t)nbytes - *((int16_t*)nbytesptr);
251 data =
bytesRd(
rRCVFIFO, ((pktsize > mem_left) ? mem_left : pktsize), data);
254 *nbytesptr += pktsize;
259 if ((pktsize < maxpktsize) || (*nbytesptr >= nbytes))
279 uint8_t rcode = SetAddress(addr, ep, &pep, nak_limit);
284 return OutTransfer(pep, nak_limit, nbytes, data);
287 uint8_t USB::OutTransfer(
EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data) {
289 uint8_t *data_p = data;
290 uint16_t bytes_tosend, nak_count;
291 uint16_t bytes_left = nbytes;
295 if (maxpktsize < 1 || maxpktsize > 64)
305 bytes_tosend = (bytes_left >= maxpktsize) ? maxpktsize : bytes_left;
313 while (rcode && (timeout > millis())) {
317 if (nak_limit && (nak_count == nak_limit))
338 bytes_left -= bytes_tosend;
339 data_p += bytes_tosend;
354 uint8_t retry_count = 0;
355 uint16_t nak_count = 0;
357 while (timeout > millis()) {
361 while (millis() < timeout)
365 if (tmpdata & bmHXFRDNIRQ) {
381 if (nak_limit && (nak_count == nak_limit))
402 static unsigned long delay = 0;
404 bool lowspeed =
false;
433 rcode = devConfig[i]->
Poll();
435 switch (usb_task_state) {
441 rcode = devConfig[i]->
Release();
450 if (delay < millis())
462 delay = millis() + 20;
468 if (delay < millis())
504 p0->
lowspeed = (lowspeed) ?
true :
false;
507 uint8_t bAddress = addrPool.
AllocAddress(parent,
false, port);
517 p->lowspeed = lowspeed;
520 rcode =
setAddr(0, 0, bAddress);
535 if (!devConfig[devConfigIndex])
538 rcode = devConfig[devConfigIndex]->
Init(parent, port, lowspeed);
567 if (devConfig[i]->GetAddress() == addr)
568 return devConfig[i]->
Release();
574 //get device descriptor
577 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, 0x00,
USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, nbytes, dataptr, NULL));
581 uint8_t
USB::getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr) {
582 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, conf,
USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, nbytes, dataptr, NULL));
586 const uint8_t bufSize = 64;
587 uint8_t buf[bufSize];
598 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, conf,
USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p));
603 uint8_t
USB::getStrDescr(uint8_t addr, uint8_t ep, uint16_t ns, uint8_t index, uint16_t langid, uint8_t* dataptr) {
604 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, index,
USB_DESCRIPTOR_STRING, langid, ns, ns, dataptr, NULL));
609 return (
ctrlReq(oldaddr, ep,
bmREQ_SET,
USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL));
614 return (
ctrlReq(addr, ep,
bmREQ_SET,
USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL));
617 #endif // defined(USB_METHODS_INLINE)