21 static uint8_t usb_error = 0;
22 static uint8_t usb_task_state;
37 return ( usb_task_state);
41 usb_task_state = state;
52 for(uint8_t i = 0; i < p->
epcount; i++) {
53 if((pep)->epAddr == ep)
80 uint8_t USB::SetAddress(uint8_t addr, uint8_t ep,
EpInfo **ppep, uint16_t *nak_limit) {
126 uint8_t
USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi,
127 uint16_t wInd, uint16_t total, uint16_t nbytes, uint8_t* dataptr,
USBReadParser *p) {
128 bool direction =
false;
133 uint16_t nak_limit = 0;
135 rcode = SetAddress(addr, ep, &pep, &nak_limit);
140 direction = ((bmReqType & 0x80) > 0);
161 uint16_t left = total;
167 #if defined(ESP8266) || defined(ESP32)
170 uint16_t read = nbytes;
173 rcode = InTransfer(pep, nak_limit, &read, dataptr);
195 rcode = OutTransfer(pep, nak_limit, nbytes, dataptr);
209 uint8_t
USB::inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t* data, uint8_t bInterval ) {
211 uint16_t nak_limit = 0;
213 uint8_t rcode = SetAddress(addr, ep, &pep, &nak_limit);
216 USBTRACE3(
"(USB::InTransfer) SetAddress Failed ", rcode, 0x81);
217 USBTRACE3(
"(USB::InTransfer) addr requested ", addr, 0x81);
218 USBTRACE3(
"(USB::InTransfer) ep requested ", ep, 0x81);
221 return InTransfer(pep, nak_limit, nbytesptr, data, bInterval);
224 uint8_t USB::InTransfer(
EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, uint8_t* data, uint8_t bInterval ) {
228 uint16_t nbytes = *nbytesptr;
237 #if defined(ESP8266) || defined(ESP32)
266 if(pktsize > nbytes) {
273 int16_t mem_left = (int16_t)nbytes - *((int16_t*)nbytesptr);
278 data =
bytesRd(
rRCVFIFO, ((pktsize > mem_left) ? mem_left : pktsize), data);
281 *nbytesptr += pktsize;
286 if((pktsize < maxpktsize) || (*nbytesptr >= nbytes))
293 }
else if(bInterval > 0)
305 uint16_t nak_limit = 0;
307 uint8_t rcode = SetAddress(addr, ep, &pep, &nak_limit);
312 return OutTransfer(pep, nak_limit, nbytes, data);
315 uint8_t USB::OutTransfer(
EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data) {
317 uint8_t *data_p = data;
318 uint16_t bytes_tosend, nak_count;
319 uint16_t bytes_left = nbytes;
323 if(maxpktsize < 1 || maxpktsize > 64)
331 #if defined(ESP8266) || defined(ESP32)
336 bytes_tosend = (bytes_left >= maxpktsize) ? maxpktsize : bytes_left;
341 #if defined(ESP8266) || defined(ESP32)
348 while(rcode && ((int32_t)((uint32_t)millis() - timeout) < 0
L)) {
349 #if defined(ESP8266) || defined(ESP32)
355 if(nak_limit && (nak_count == nak_limit))
380 #if defined(ESP8266) || defined(ESP32)
387 bytes_left -= bytes_tosend;
388 data_p += bytes_tosend;
410 uint8_t retry_count = 0;
411 uint16_t nak_count = 0;
413 while((int32_t)((uint32_t)millis() - timeout) < 0
L) {
414 #if defined(ESP8266) || defined(ESP32)
420 while((int32_t)((uint32_t)millis() - timeout) < 0
L)
422 #if defined(ESP8266) || defined(ESP32)
443 if(nak_limit && (nak_count == nak_limit))
464 static uint32_t delay = 0;
466 bool lowspeed =
false;
497 rcode = devConfig[i]->
Poll();
499 switch(usb_task_state) {
505 rcode = devConfig[i]->
Release();
514 if((int32_t)((uint32_t)millis() - delay) >= 0
L)
537 delay = (uint32_t)millis() + 20;
580 p0->
lowspeed = (lowspeed) ?
true :
false;
583 uint8_t bAddress = addrPool.
AllocAddress(parent,
false, port);
593 p->lowspeed = lowspeed;
596 rcode =
setAddr(0, 0, bAddress);
606 uint8_t USB::AttemptConfig(uint8_t driver, uint8_t parent, uint8_t port,
bool lowspeed) {
611 uint8_t rcode = devConfig[driver]->
ConfigureDevice(parent, port, lowspeed);
621 }
else if(rcode ==
hrJERR && retries < 3) {
628 rcode = devConfig[driver]->
Init(parent, port, lowspeed);
629 if(rcode ==
hrJERR && retries < 3) {
691 uint8_t devConfigIndex;
738 uint16_t vid = udd->idVendor;
739 uint16_t pid = udd->idProduct;
740 uint8_t klass = udd->bDeviceClass;
741 uint8_t subklass = udd->bDeviceSubClass;
748 for(devConfigIndex = 0; devConfigIndex <
USB_NUMDEVICES; devConfigIndex++) {
749 if(!devConfig[devConfigIndex])
continue;
750 if(devConfig[devConfigIndex]->GetAddress())
continue;
751 if(devConfig[devConfigIndex]->DEVSUBCLASSOK(subklass) && (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass))) {
752 rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed);
764 for(devConfigIndex = 0; devConfigIndex <
USB_NUMDEVICES; devConfigIndex++) {
765 if(!devConfig[devConfigIndex])
continue;
766 if(devConfig[devConfigIndex]->GetAddress())
continue;
767 if(devConfig[devConfigIndex]->DEVSUBCLASSOK(subklass) && (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass)))
continue;
768 rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed);
791 if(!devConfig[i])
continue;
792 if(devConfig[i]->GetAddress() == addr)
793 return devConfig[i]->
Release();
799 //get device descriptor
802 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, 0x00,
USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, nbytes, dataptr, NULL));
806 uint8_t
USB::getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr) {
807 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, conf,
USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, nbytes, dataptr, NULL));
813 const uint8_t bufSize = 64;
814 uint8_t buf[bufSize];
826 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, conf,
USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p));
831 uint8_t
USB::getStrDescr(uint8_t addr, uint8_t ep, uint16_t ns, uint8_t index, uint16_t langid, uint8_t* dataptr) {
832 return (
ctrlReq(addr, ep,
bmREQ_GET_DESCR,
USB_REQUEST_GET_DESCRIPTOR, index,
USB_DESCRIPTOR_STRING, langid, ns, ns, dataptr, NULL));
837 uint8_t rcode =
ctrlReq(oldaddr, ep,
bmREQ_SET,
USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL);
846 return (
ctrlReq(addr, ep,
bmREQ_SET,
USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, NULL, NULL));
849 #endif // defined(USB_METHODS_INLINE)