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;
135 uint16_t nak_limit = 0;
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);
194 rcode = OutTransfer(pep, nak_limit, nbytes, dataptr);
208 uint8_t
USB::inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t* data) {
210 uint16_t nak_limit = 0;
212 uint8_t rcode = SetAddress(addr, ep, &pep, nak_limit);
218 return InTransfer(pep, nak_limit, nbytesptr, data);
221 uint8_t USB::InTransfer(
EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, uint8_t* data) {
225 uint16_t nbytes = *nbytesptr;
256 if (pktsize > nbytes) {
263 int16_t mem_left = (int16_t)nbytes - *((int16_t*)nbytesptr);
268 data =
bytesRd(
rRCVFIFO, ((pktsize > mem_left) ? mem_left : pktsize), data);
271 *nbytesptr += pktsize;
276 if ((pktsize < maxpktsize) || (*nbytesptr >= nbytes))
294 uint16_t nak_limit = 0;
296 uint8_t rcode = SetAddress(addr, ep, &pep, nak_limit);
301 return OutTransfer(pep, nak_limit, nbytes, data);
304 uint8_t USB::OutTransfer(
EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data) {
306 uint8_t *data_p = data;
307 uint16_t bytes_tosend, nak_count;
308 uint16_t bytes_left = nbytes;
312 if (maxpktsize < 1 || maxpktsize > 64)
322 bytes_tosend = (bytes_left >= maxpktsize) ? maxpktsize : bytes_left;
330 while (rcode && (timeout > millis())) {
334 if (nak_limit && (nak_count == nak_limit))
362 bytes_left -= bytes_tosend;
363 data_p += bytes_tosend;
380 uint8_t retry_count = 0;
381 uint16_t nak_count = 0;
383 while (timeout > millis()) {
387 while (timeout > millis())
391 if (tmpdata & bmHXFRDNIRQ) {
407 if (nak_limit && (nak_count == nak_limit))
428 static unsigned long delay = 0;
430 bool lowspeed =
false;
461 rcode = devConfig[i]->
Poll();
463 switch (usb_task_state) {
469 rcode = devConfig[i]->
Release();
478 if (delay < millis())
501 delay = millis() + 20;
540 p0->
lowspeed = (lowspeed) ?
true :
false;
543 uint8_t bAddress = addrPool.
AllocAddress(parent,
false, port);
553 p->lowspeed = lowspeed;
556 rcode =
setAddr(0, 0, bAddress);
610 if (!devConfig[devConfigIndex])
613 rcode = devConfig[devConfigIndex]->
ConfigureDevice(parent, port, lowspeed);
626 rcode = devConfig[devConfigIndex]->
Init(parent, port, lowspeed);
654 if (devConfig[i]->GetAddress() == addr)
655 return devConfig[i]->
Release();
661 //get device descriptor
664 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, 0x00,
USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, nbytes, dataptr, NULL));
668 uint8_t
USB::getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr) {
669 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, conf,
USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, nbytes, dataptr, NULL));
673 const uint8_t bufSize = 64;
674 uint8_t buf[bufSize];
685 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, conf,
USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p));
690 uint8_t
USB::getStrDescr(uint8_t addr, uint8_t ep, uint16_t ns, uint8_t index, uint16_t langid, uint8_t* dataptr) {
691 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, index,
USB_DESCRIPTOR_STRING, langid, ns, ns, dataptr, NULL));
696 return (
ctrlReq(oldaddr, ep,
bmREQ_SET,
USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL));
701 return (
ctrlReq(addr, ep,
bmREQ_SET,
USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL));
704 #endif // defined(USB_METHODS_INLINE)