My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Usb.cpp 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /**
  2. * Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Contact information
  19. * -------------------
  20. *
  21. * Circuits At Home, LTD
  22. * Web : https://www.circuitsathome.com
  23. * e-mail : support@circuitsathome.com
  24. */
  25. //
  26. // USB functions supporting Flash Drive
  27. //
  28. #include "../../../inc/MarlinConfigPre.h"
  29. #if ENABLED(USB_FLASH_DRIVE_SUPPORT) && DISABLED(USE_UHS3_USB)
  30. #include "Usb.h"
  31. static uint8_t usb_error = 0;
  32. static uint8_t usb_task_state;
  33. /* constructor */
  34. USB::USB() : bmHubPre(0) {
  35. usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; // Set up state machine
  36. init();
  37. }
  38. /* Initialize data structures */
  39. void USB::init() {
  40. //devConfigIndex = 0;
  41. bmHubPre = 0;
  42. }
  43. uint8_t USB::getUsbTaskState() { return usb_task_state; }
  44. void USB::setUsbTaskState(uint8_t state) { usb_task_state = state; }
  45. EpInfo* USB::getEpInfoEntry(uint8_t addr, uint8_t ep) {
  46. UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
  47. if (!p || !p->epinfo)
  48. return nullptr;
  49. EpInfo *pep = p->epinfo;
  50. for (uint8_t i = 0; i < p->epcount; i++) {
  51. if ((pep)->epAddr == ep)
  52. return pep;
  53. pep++;
  54. }
  55. return nullptr;
  56. }
  57. /**
  58. * Set device table entry
  59. * Each device is different and has different number of endpoints.
  60. * This function plugs endpoint record structure, defined in application, to devtable
  61. */
  62. uint8_t USB::setEpInfoEntry(uint8_t addr, uint8_t epcount, EpInfo* eprecord_ptr) {
  63. if (!eprecord_ptr)
  64. return USB_ERROR_INVALID_ARGUMENT;
  65. UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
  66. if (!p)
  67. return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
  68. p->address.devAddress = addr;
  69. p->epinfo = eprecord_ptr;
  70. p->epcount = epcount;
  71. return 0;
  72. }
  73. uint8_t USB::SetAddress(uint8_t addr, uint8_t ep, EpInfo **ppep, uint16_t *nak_limit) {
  74. UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
  75. if (!p)
  76. return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
  77. if (!p->epinfo)
  78. return USB_ERROR_EPINFO_IS_NULL;
  79. *ppep = getEpInfoEntry(addr, ep);
  80. if (!*ppep)
  81. return USB_ERROR_EP_NOT_FOUND_IN_TBL;
  82. *nak_limit = (0x0001UL << (((*ppep)->bmNakPower > USB_NAK_MAX_POWER) ? USB_NAK_MAX_POWER : (*ppep)->bmNakPower));
  83. (*nak_limit)--;
  84. /*
  85. USBTRACE2("\r\nAddress: ", addr);
  86. USBTRACE2(" EP: ", ep);
  87. USBTRACE2(" NAK Power: ",(*ppep)->bmNakPower);
  88. USBTRACE2(" NAK Limit: ", nak_limit);
  89. USBTRACE("\r\n");
  90. */
  91. regWr(rPERADDR, addr); // Set peripheral address
  92. uint8_t mode = regRd(rMODE);
  93. //Serial.print("\r\nMode: ");
  94. //Serial.println( mode, HEX);
  95. //Serial.print("\r\nLS: ");
  96. //Serial.println(p->lowspeed, HEX);
  97. // Set bmLOWSPEED and bmHUBPRE in case of low-speed device, reset them otherwise
  98. regWr(rMODE, (p->lowspeed) ? mode | bmLOWSPEED | bmHubPre : mode & ~(bmHUBPRE | bmLOWSPEED));
  99. return 0;
  100. }
  101. /* Control transfer. Sets address, endpoint, fills control packet with necessary data, dispatches control packet, and initiates bulk IN transfer, */
  102. /* depending on request. Actual requests are defined as inlines */
  103. /* return codes: */
  104. /* 00 = success */
  105. /* 01-0f = non-zero HRSLT */
  106. uint8_t USB::ctrlReq(uint8_t addr, uint8_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi,
  107. uint16_t wInd, uint16_t total, uint16_t nbytes, uint8_t* dataptr, USBReadParser *p) {
  108. bool direction = false; // Request direction, IN or OUT
  109. uint8_t rcode;
  110. SETUP_PKT setup_pkt;
  111. EpInfo *pep = nullptr;
  112. uint16_t nak_limit = 0;
  113. rcode = SetAddress(addr, ep, &pep, &nak_limit);
  114. if (rcode) return rcode;
  115. direction = ((bmReqType & 0x80) > 0);
  116. /* fill in setup packet */
  117. setup_pkt.ReqType_u.bmRequestType = bmReqType;
  118. setup_pkt.bRequest = bRequest;
  119. setup_pkt.wVal_u.wValueLo = wValLo;
  120. setup_pkt.wVal_u.wValueHi = wValHi;
  121. setup_pkt.wIndex = wInd;
  122. setup_pkt.wLength = total;
  123. bytesWr(rSUDFIFO, 8, (uint8_t*) & setup_pkt); // Transfer to setup packet FIFO
  124. rcode = dispatchPkt(tokSETUP, ep, nak_limit); // Dispatch packet
  125. if (rcode) return rcode; // Return HRSLT if not zero
  126. if (dataptr) { // Data stage, if present
  127. if (direction) { // IN transfer
  128. uint16_t left = total;
  129. pep->bmRcvToggle = 1; // BmRCVTOG1;
  130. while (left) {
  131. // Bytes read into buffer
  132. uint16_t read = nbytes;
  133. //uint16_t read = (left<nbytes) ? left : nbytes;
  134. rcode = InTransfer(pep, nak_limit, &read, dataptr);
  135. if (rcode == hrTOGERR) {
  136. // Yes, we flip it wrong here so that next time it is actually correct!
  137. pep->bmRcvToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 0 : 1;
  138. continue;
  139. }
  140. if (rcode) return rcode;
  141. // Invoke callback function if inTransfer completed successfully and callback function pointer is specified
  142. if (!rcode && p) ((USBReadParser*)p)->Parse(read, dataptr, total - left);
  143. left -= read;
  144. if (read < nbytes) break;
  145. }
  146. }
  147. else { // OUT transfer
  148. pep->bmSndToggle = 1; // BmSNDTOG1;
  149. rcode = OutTransfer(pep, nak_limit, nbytes, dataptr);
  150. }
  151. if (rcode) return rcode; // Return error
  152. }
  153. // Status stage
  154. return dispatchPkt((direction) ? tokOUTHS : tokINHS, ep, nak_limit); // GET if direction
  155. }
  156. /**
  157. * IN transfer to arbitrary endpoint. Assumes PERADDR is set. Handles multiple packets if necessary. Transfers 'nbytes' bytes.
  158. * Keep sending INs and writes data to memory area pointed by 'data'
  159. * rcode 0 if no errors. rcode 01-0f is relayed from dispatchPkt(). Rcode f0 means RCVDAVIRQ error, fe = USB xfer timeout
  160. */
  161. uint8_t USB::inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t* data, uint8_t bInterval /*= 0*/) {
  162. EpInfo *pep = nullptr;
  163. uint16_t nak_limit = 0;
  164. uint8_t rcode = SetAddress(addr, ep, &pep, &nak_limit);
  165. if (rcode) {
  166. USBTRACE3("(USB::InTransfer) SetAddress Failed ", rcode, 0x81);
  167. USBTRACE3("(USB::InTransfer) addr requested ", addr, 0x81);
  168. USBTRACE3("(USB::InTransfer) ep requested ", ep, 0x81);
  169. return rcode;
  170. }
  171. return InTransfer(pep, nak_limit, nbytesptr, data, bInterval);
  172. }
  173. uint8_t USB::InTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t *nbytesptr, uint8_t* data, uint8_t bInterval /*= 0*/) {
  174. uint8_t rcode = 0;
  175. uint8_t pktsize;
  176. uint16_t nbytes = *nbytesptr;
  177. //printf("Requesting %i bytes ", nbytes);
  178. uint8_t maxpktsize = pep->maxPktSize;
  179. *nbytesptr = 0;
  180. regWr(rHCTL, (pep->bmRcvToggle) ? bmRCVTOG1 : bmRCVTOG0); // Set toggle value
  181. // Use a 'break' to exit this loop
  182. for (;;) {
  183. rcode = dispatchPkt(tokIN, pep->epAddr, nak_limit); // IN packet to EP-'endpoint'. Function takes care of NAKS.
  184. if (rcode == hrTOGERR) {
  185. // Yes, we flip it wrong here so that next time it is actually correct!
  186. pep->bmRcvToggle = (regRd(rHRSL) & bmRCVTOGRD) ? 0 : 1;
  187. regWr(rHCTL, (pep->bmRcvToggle) ? bmRCVTOG1 : bmRCVTOG0); // Set toggle value
  188. continue;
  189. }
  190. if (rcode) {
  191. //printf(">>>>>>>> Problem! dispatchPkt %2.2x\r\n", rcode);
  192. break; // Should be 0, indicating ACK. Else return error code.
  193. }
  194. /* check for RCVDAVIRQ and generate error if not present */
  195. /* the only case when absence of RCVDAVIRQ makes sense is when toggle error occurred. Need to add handling for that */
  196. if ((regRd(rHIRQ) & bmRCVDAVIRQ) == 0) {
  197. //printf(">>>>>>>> Problem! NO RCVDAVIRQ!\r\n");
  198. rcode = 0xF0; // Receive error
  199. break;
  200. }
  201. pktsize = regRd(rRCVBC); // Number of received bytes
  202. //printf("Got %i bytes \r\n", pktsize);
  203. // This would be OK, but...
  204. //assert(pktsize <= nbytes);
  205. if (pktsize > nbytes) {
  206. // This can happen. Use of assert on Arduino locks up the Arduino.
  207. // So I will trim the value, and hope for the best.
  208. //printf(">>>>>>>> Problem! Wanted %i bytes but got %i.\r\n", nbytes, pktsize);
  209. pktsize = nbytes;
  210. }
  211. int16_t mem_left = (int16_t)nbytes - *((int16_t*)nbytesptr);
  212. if (mem_left < 0) mem_left = 0;
  213. data = bytesRd(rRCVFIFO, ((pktsize > mem_left) ? mem_left : pktsize), data);
  214. regWr(rHIRQ, bmRCVDAVIRQ); // Clear the IRQ & free the buffer
  215. *nbytesptr += pktsize; // Add this packet's byte count to total transfer length
  216. /* The transfer is complete under two conditions: */
  217. /* 1. The device sent a short packet (L.T. maxPacketSize) */
  218. /* 2. 'nbytes' have been transferred. */
  219. if (pktsize < maxpktsize || *nbytesptr >= nbytes) { // Transferred 'nbytes' bytes?
  220. // Save toggle value
  221. pep->bmRcvToggle = ((regRd(rHRSL) & bmRCVTOGRD)) ? 1 : 0;
  222. //printf("\r\n");
  223. rcode = 0;
  224. break;
  225. }
  226. else if (bInterval > 0)
  227. delay(bInterval); // Delay according to polling interval
  228. }
  229. return rcode;
  230. }
  231. /**
  232. * OUT transfer to arbitrary endpoint. Handles multiple packets if necessary. Transfers 'nbytes' bytes.
  233. * Handles NAK bug per Maxim Application Note 4000 for single buffer transfer
  234. * rcode 0 if no errors. rcode 01-0f is relayed from HRSL
  235. */
  236. uint8_t USB::outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* data) {
  237. EpInfo *pep = nullptr;
  238. uint16_t nak_limit = 0;
  239. uint8_t rcode = SetAddress(addr, ep, &pep, &nak_limit);
  240. if (rcode) return rcode;
  241. return OutTransfer(pep, nak_limit, nbytes, data);
  242. }
  243. uint8_t USB::OutTransfer(EpInfo *pep, uint16_t nak_limit, uint16_t nbytes, uint8_t *data) {
  244. uint8_t rcode = hrSUCCESS, retry_count;
  245. uint8_t *data_p = data; // Local copy of the data pointer
  246. uint16_t bytes_tosend, nak_count;
  247. uint16_t bytes_left = nbytes;
  248. uint8_t maxpktsize = pep->maxPktSize;
  249. if (maxpktsize < 1 || maxpktsize > 64)
  250. return USB_ERROR_INVALID_MAX_PKT_SIZE;
  251. uint32_t timeout = (uint32_t)millis() + USB_XFER_TIMEOUT;
  252. regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); // Set toggle value
  253. while (bytes_left) {
  254. retry_count = 0;
  255. nak_count = 0;
  256. bytes_tosend = (bytes_left >= maxpktsize) ? maxpktsize : bytes_left;
  257. bytesWr(rSNDFIFO, bytes_tosend, data_p); // Filling output FIFO
  258. regWr(rSNDBC, bytes_tosend); // Set number of bytes
  259. regWr(rHXFR, (tokOUT | pep->epAddr)); // Dispatch packet
  260. while (!(regRd(rHIRQ) & bmHXFRDNIRQ)); // Wait for the completion IRQ
  261. regWr(rHIRQ, bmHXFRDNIRQ); // Clear IRQ
  262. rcode = (regRd(rHRSL) & 0x0F);
  263. while (rcode && ((int32_t)((uint32_t)millis() - timeout) < 0L)) {
  264. switch (rcode) {
  265. case hrNAK:
  266. nak_count++;
  267. if (nak_limit && (nak_count == nak_limit))
  268. goto breakout;
  269. //return rcode;
  270. break;
  271. case hrTIMEOUT:
  272. retry_count++;
  273. if (retry_count == USB_RETRY_LIMIT)
  274. goto breakout;
  275. //return rcode;
  276. break;
  277. case hrTOGERR:
  278. // Yes, we flip it wrong here so that next time it is actually correct!
  279. pep->bmSndToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 0 : 1;
  280. regWr(rHCTL, (pep->bmSndToggle) ? bmSNDTOG1 : bmSNDTOG0); // Set toggle value
  281. break;
  282. default:
  283. goto breakout;
  284. }
  285. /* process NAK according to Host out NAK bug */
  286. regWr(rSNDBC, 0);
  287. regWr(rSNDFIFO, *data_p);
  288. regWr(rSNDBC, bytes_tosend);
  289. regWr(rHXFR, (tokOUT | pep->epAddr)); // Dispatch packet
  290. while (!(regRd(rHIRQ) & bmHXFRDNIRQ)); // Wait for the completion IRQ
  291. regWr(rHIRQ, bmHXFRDNIRQ); // Clear IRQ
  292. rcode = (regRd(rHRSL) & 0x0F);
  293. } // While rcode && ....
  294. bytes_left -= bytes_tosend;
  295. data_p += bytes_tosend;
  296. } // While bytes_left...
  297. breakout:
  298. pep->bmSndToggle = (regRd(rHRSL) & bmSNDTOGRD) ? 1 : 0; // BmSNDTOG1 : bmSNDTOG0; // Update toggle
  299. return ( rcode); // Should be 0 in all cases
  300. }
  301. /**
  302. * Dispatch USB packet. Assumes peripheral address is set and relevant buffer is loaded/empty
  303. * If NAK, tries to re-send up to nak_limit times
  304. * If nak_limit == 0, do not count NAKs, exit after timeout
  305. * If bus timeout, re-sends up to USB_RETRY_LIMIT times
  306. * return codes 0x00-0x0F are HRSLT( 0x00 being success ), 0xFF means timeout
  307. */
  308. uint8_t USB::dispatchPkt(uint8_t token, uint8_t ep, uint16_t nak_limit) {
  309. uint32_t timeout = (uint32_t)millis() + USB_XFER_TIMEOUT;
  310. uint8_t tmpdata;
  311. uint8_t rcode = hrSUCCESS;
  312. uint8_t retry_count = 0;
  313. uint16_t nak_count = 0;
  314. while ((int32_t)((uint32_t)millis() - timeout) < 0L) {
  315. #if defined(ESP8266) || defined(ESP32)
  316. yield(); // Needed in order to reset the watchdog timer on the ESP8266
  317. #endif
  318. regWr(rHXFR, (token | ep)); // Launch the transfer
  319. rcode = USB_ERROR_TRANSFER_TIMEOUT;
  320. while ((int32_t)((uint32_t)millis() - timeout) < 0L) { // Wait for transfer completion
  321. #if defined(ESP8266) || defined(ESP32)
  322. yield(); // Needed to reset the watchdog timer on the ESP8266
  323. #endif
  324. tmpdata = regRd(rHIRQ);
  325. if (tmpdata & bmHXFRDNIRQ) {
  326. regWr(rHIRQ, bmHXFRDNIRQ); // Clear the interrupt
  327. rcode = 0x00;
  328. break;
  329. }
  330. } // While millis() < timeout
  331. //if (rcode != 0x00) return rcode; // Exit if timeout
  332. rcode = (regRd(rHRSL) & 0x0F); // Analyze transfer result
  333. switch (rcode) {
  334. case hrNAK:
  335. nak_count++;
  336. if (nak_limit && (nak_count == nak_limit))
  337. return (rcode);
  338. break;
  339. case hrTIMEOUT:
  340. retry_count++;
  341. if (retry_count == USB_RETRY_LIMIT)
  342. return (rcode);
  343. break;
  344. default:
  345. return (rcode);
  346. }
  347. } // While timeout > millis()
  348. return rcode;
  349. }
  350. // USB main task. Performs enumeration/cleanup
  351. void USB::Task() { // USB state machine
  352. uint8_t rcode;
  353. uint8_t tmpdata;
  354. static uint32_t delay = 0;
  355. //USB_FD_DEVICE_DESCRIPTOR buf;
  356. bool lowspeed = false;
  357. MAX3421E::Task();
  358. tmpdata = getVbusState();
  359. /* modify USB task state if Vbus changed */
  360. switch (tmpdata) {
  361. case SE1: // Illegal state
  362. usb_task_state = USB_DETACHED_SUBSTATE_ILLEGAL;
  363. lowspeed = false;
  364. break;
  365. case SE0: // Disconnected
  366. if ((usb_task_state & USB_STATE_MASK) != USB_STATE_DETACHED)
  367. usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
  368. lowspeed = false;
  369. break;
  370. case LSHOST:
  371. lowspeed = true;
  372. // Intentional fallthrough
  373. case FSHOST: // Attached
  374. if ((usb_task_state & USB_STATE_MASK) == USB_STATE_DETACHED) {
  375. delay = (uint32_t)millis() + USB_SETTLE_DELAY;
  376. usb_task_state = USB_ATTACHED_SUBSTATE_SETTLE;
  377. }
  378. break;
  379. }
  380. for (uint8_t i = 0; i < USB_NUMDEVICES; i++)
  381. if (devConfig[i]) rcode = devConfig[i]->Poll();
  382. switch (usb_task_state) {
  383. case USB_DETACHED_SUBSTATE_INITIALIZE:
  384. init();
  385. for (uint8_t i = 0; i < USB_NUMDEVICES; i++)
  386. if (devConfig[i])
  387. rcode = devConfig[i]->Release();
  388. usb_task_state = USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE;
  389. break;
  390. case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE: // Just sit here
  391. break;
  392. case USB_DETACHED_SUBSTATE_ILLEGAL: // Just sit here
  393. break;
  394. case USB_ATTACHED_SUBSTATE_SETTLE: // Settle time for just attached device
  395. if ((int32_t)((uint32_t)millis() - delay) >= 0L)
  396. usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE;
  397. else break; // Don't fall through
  398. case USB_ATTACHED_SUBSTATE_RESET_DEVICE:
  399. regWr(rHCTL, bmBUSRST); // Issue bus reset
  400. usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE;
  401. break;
  402. case USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE:
  403. if ((regRd(rHCTL) & bmBUSRST) == 0) {
  404. tmpdata = regRd(rMODE) | bmSOFKAENAB; // Start SOF generation
  405. regWr(rMODE, tmpdata);
  406. usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_SOF;
  407. //delay = (uint32_t)millis() + 20; // 20ms wait after reset per USB spec
  408. }
  409. break;
  410. case USB_ATTACHED_SUBSTATE_WAIT_SOF: // Todo: change check order
  411. if (regRd(rHIRQ) & bmFRAMEIRQ) {
  412. // When first SOF received _and_ 20ms has passed we can continue
  413. /*
  414. if (delay < (uint32_t)millis()) // 20ms passed
  415. usb_task_state = USB_STATE_CONFIGURING;
  416. */
  417. usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET;
  418. delay = (uint32_t)millis() + 20;
  419. }
  420. break;
  421. case USB_ATTACHED_SUBSTATE_WAIT_RESET:
  422. if ((int32_t)((uint32_t)millis() - delay) >= 0L) usb_task_state = USB_STATE_CONFIGURING;
  423. else break; // Don't fall through
  424. case USB_STATE_CONFIGURING:
  425. //Serial.print("\r\nConf.LS: ");
  426. //Serial.println(lowspeed, HEX);
  427. rcode = Configuring(0, 0, lowspeed);
  428. if (!rcode)
  429. usb_task_state = USB_STATE_RUNNING;
  430. else if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE) {
  431. usb_error = rcode;
  432. usb_task_state = USB_STATE_ERROR;
  433. }
  434. break;
  435. case USB_STATE_RUNNING:
  436. break;
  437. case USB_STATE_ERROR:
  438. //MAX3421E::Init();
  439. break;
  440. }
  441. }
  442. uint8_t USB::DefaultAddressing(uint8_t parent, uint8_t port, bool lowspeed) {
  443. //uint8_t buf[12];
  444. uint8_t rcode;
  445. UsbDevice *p0 = nullptr, *p = nullptr;
  446. // Get pointer to pseudo device with address 0 assigned
  447. p0 = addrPool.GetUsbDevicePtr(0);
  448. if (!p0) return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
  449. if (!p0->epinfo) return USB_ERROR_EPINFO_IS_NULL;
  450. p0->lowspeed = lowspeed;
  451. // Allocate new address according to device class
  452. uint8_t bAddress = addrPool.AllocAddress(parent, false, port);
  453. if (!bAddress) return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
  454. p = addrPool.GetUsbDevicePtr(bAddress);
  455. if (!p) return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
  456. p->lowspeed = lowspeed;
  457. // Assign new address to the device
  458. rcode = setAddr(0, 0, bAddress);
  459. if (rcode) {
  460. addrPool.FreeAddress(bAddress);
  461. bAddress = 0;
  462. }
  463. return rcode;
  464. }
  465. uint8_t USB::AttemptConfig(uint8_t driver, uint8_t parent, uint8_t port, bool lowspeed) {
  466. //printf("AttemptConfig: parent = %i, port = %i\r\n", parent, port);
  467. uint8_t retries = 0;
  468. again:
  469. uint8_t rcode = devConfig[driver]->ConfigureDevice(parent, port, lowspeed);
  470. if (rcode == USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET) {
  471. if (parent == 0) {
  472. // Send a bus reset on the root interface.
  473. regWr(rHCTL, bmBUSRST); // Issue bus reset
  474. delay(102); // Delay 102ms, compensate for clock inaccuracy.
  475. }
  476. else {
  477. // Reset parent port
  478. devConfig[parent]->ResetHubPort(port);
  479. }
  480. }
  481. else if (rcode == hrJERR && retries < 3) { // Some devices returns this when plugged in - trying to initialize the device again usually works
  482. delay(100);
  483. retries++;
  484. goto again;
  485. }
  486. else if (rcode)
  487. return rcode;
  488. rcode = devConfig[driver]->Init(parent, port, lowspeed);
  489. if (rcode == hrJERR && retries < 3) { // Some devices returns this when plugged in - trying to initialize the device again usually works
  490. delay(100);
  491. retries++;
  492. goto again;
  493. }
  494. if (rcode) {
  495. // Issue a bus reset, because the device may be in a limbo state
  496. if (parent == 0) {
  497. // Send a bus reset on the root interface.
  498. regWr(rHCTL, bmBUSRST); // Issue bus reset
  499. delay(102); // Delay 102ms, compensate for clock inaccuracy.
  500. }
  501. else {
  502. // Reset parent port
  503. devConfig[parent]->ResetHubPort(port);
  504. }
  505. }
  506. return rcode;
  507. }
  508. /**
  509. * This is broken. It needs to enumerate differently.
  510. * It causes major problems with several devices if detected in an unexpected order.
  511. *
  512. * Oleg - I wouldn't do anything before the newly connected device is considered sane.
  513. * i.e.(delays are not indicated for brevity):
  514. * 1. reset
  515. * 2. GetDevDescr();
  516. * 3a. If ACK, continue with allocating address, addressing, etc.
  517. * 3b. Else reset again, count resets, stop at some number (5?).
  518. * 4. When max.number of resets is reached, toggle power/fail
  519. * If desired, this could be modified by performing two resets with GetDevDescr() in the middle - however, from my experience, if a device answers to GDD()
  520. * it doesn't need to be reset again
  521. * New steps proposal:
  522. * 1: get address pool instance. exit on fail
  523. * 2: pUsb->getDevDescr(0, 0, constBufSize, (uint8_t*)buf). exit on fail.
  524. * 3: bus reset, 100ms delay
  525. * 4: set address
  526. * 5: pUsb->setEpInfoEntry(bAddress, 1, epInfo), exit on fail
  527. * 6: while (configurations) {
  528. * for (each configuration) {
  529. * for (each driver) {
  530. * 6a: Ask device if it likes configuration. Returns 0 on OK.
  531. * If successful, the driver configured device.
  532. * The driver now owns the endpoints, and takes over managing them.
  533. * The following will need codes:
  534. * Everything went well, instance consumed, exit with success.
  535. * Instance already in use, ignore it, try next driver.
  536. * Not a supported device, ignore it, try next driver.
  537. * Not a supported configuration for this device, ignore it, try next driver.
  538. * Could not configure device, fatal, exit with fail.
  539. * }
  540. * }
  541. * }
  542. * 7: for (each driver) {
  543. * 7a: Ask device if it knows this VID/PID. Acts exactly like 6a, but using VID/PID
  544. * 8: if we get here, no driver likes the device plugged in, so exit failure.
  545. */
  546. uint8_t USB::Configuring(uint8_t parent, uint8_t port, bool lowspeed) {
  547. //uint8_t bAddress = 0;
  548. //printf("Configuring: parent = %i, port = %i\r\n", parent, port);
  549. uint8_t devConfigIndex;
  550. uint8_t rcode = 0;
  551. uint8_t buf[sizeof (USB_FD_DEVICE_DESCRIPTOR)];
  552. USB_FD_DEVICE_DESCRIPTOR *udd = reinterpret_cast<USB_FD_DEVICE_DESCRIPTOR *>(buf);
  553. UsbDevice *p = nullptr;
  554. EpInfo *oldep_ptr = nullptr;
  555. EpInfo epInfo;
  556. epInfo.epAddr = 0;
  557. epInfo.maxPktSize = 8;
  558. epInfo.bmSndToggle = 0;
  559. epInfo.bmRcvToggle = 0;
  560. epInfo.bmNakPower = USB_NAK_MAX_POWER;
  561. //delay(2000);
  562. AddressPool &addrPool = GetAddressPool();
  563. // Get pointer to pseudo device with address 0 assigned
  564. p = addrPool.GetUsbDevicePtr(0);
  565. if (!p) {
  566. //printf("Configuring error: USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL\r\n");
  567. return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
  568. }
  569. // Save old pointer to EP_RECORD of address 0
  570. oldep_ptr = p->epinfo;
  571. // Temporary assign new pointer to epInfo to p->epinfo in order to
  572. // Avoid toggle inconsistence
  573. p->epinfo = &epInfo;
  574. p->lowspeed = lowspeed;
  575. // Get device descriptor
  576. rcode = getDevDescr(0, 0, sizeof (USB_FD_DEVICE_DESCRIPTOR), (uint8_t*)buf);
  577. // Restore p->epinfo
  578. p->epinfo = oldep_ptr;
  579. if (rcode) {
  580. //printf("Configuring error: Can't get USB_FD_DEVICE_DESCRIPTOR\r\n");
  581. return rcode;
  582. }
  583. // To-do?
  584. // Allocate new address according to device class
  585. //bAddress = addrPool.AllocAddress(parent, false, port);
  586. uint16_t vid = udd->idVendor, pid = udd->idProduct;
  587. uint8_t klass = udd->bDeviceClass, subklass = udd->bDeviceSubClass;
  588. // Attempt to configure if VID/PID or device class matches with a driver
  589. // Qualify with subclass too.
  590. //
  591. // VID/PID & class tests default to false for drivers not yet ported
  592. // Subclass defaults to true, so you don't have to define it if you don't have to.
  593. //
  594. for (devConfigIndex = 0; devConfigIndex < USB_NUMDEVICES; devConfigIndex++) {
  595. if (!devConfig[devConfigIndex]) continue; // No driver
  596. if (devConfig[devConfigIndex]->GetAddress()) continue; // Consumed
  597. if (devConfig[devConfigIndex]->DEVSUBCLASSOK(subklass) && (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass))) {
  598. rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed);
  599. if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED)
  600. break;
  601. }
  602. }
  603. if (devConfigIndex < USB_NUMDEVICES) return rcode;
  604. // Blindly attempt to configure
  605. for (devConfigIndex = 0; devConfigIndex < USB_NUMDEVICES; devConfigIndex++) {
  606. if (!devConfig[devConfigIndex]) continue;
  607. if (devConfig[devConfigIndex]->GetAddress()) continue; // Consumed
  608. if (devConfig[devConfigIndex]->DEVSUBCLASSOK(subklass) && (devConfig[devConfigIndex]->VIDPIDOK(vid, pid) || devConfig[devConfigIndex]->DEVCLASSOK(klass))) continue; // If this is true it means it must have returned USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED above
  609. rcode = AttemptConfig(devConfigIndex, parent, port, lowspeed);
  610. //printf("ERROR ENUMERATING %2.2x\r\n", rcode);
  611. if (!(rcode == USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED || rcode == USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE)) {
  612. // In case of an error dev_index should be reset to 0
  613. // in order to start from the very beginning the
  614. // next time the program gets here
  615. //if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE)
  616. //devConfigIndex = 0;
  617. return rcode;
  618. }
  619. }
  620. // Arriving here means the device class is unsupported by registered classes
  621. return DefaultAddressing(parent, port, lowspeed);
  622. }
  623. uint8_t USB::ReleaseDevice(uint8_t addr) {
  624. if (addr) {
  625. for (uint8_t i = 0; i < USB_NUMDEVICES; i++) {
  626. if (!devConfig[i]) continue;
  627. if (devConfig[i]->GetAddress() == addr)
  628. return devConfig[i]->Release();
  629. }
  630. }
  631. return 0;
  632. }
  633. // Get device descriptor
  634. uint8_t USB::getDevDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t* dataptr) {
  635. return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, nbytes, dataptr, nullptr);
  636. }
  637. // Get configuration descriptor
  638. uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t conf, uint8_t* dataptr) {
  639. return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, nbytes, dataptr, nullptr);
  640. }
  641. /**
  642. * Requests Configuration Descriptor. Sends two Get Conf Descr requests.
  643. * The first one gets the total length of all descriptors, then the second one requests this
  644. * total length. The length of the first request can be shorter (4 bytes), however, there are
  645. * devices which won't work unless this length is set to 9.
  646. */
  647. uint8_t USB::getConfDescr(uint8_t addr, uint8_t ep, uint8_t conf, USBReadParser *p) {
  648. const uint8_t bufSize = 64;
  649. uint8_t buf[bufSize];
  650. USB_FD_CONFIGURATION_DESCRIPTOR *ucd = reinterpret_cast<USB_FD_CONFIGURATION_DESCRIPTOR *>(buf);
  651. uint8_t ret = getConfDescr(addr, ep, 9, conf, buf);
  652. if (ret) return ret;
  653. uint16_t total = ucd->wTotalLength;
  654. //USBTRACE2("\r\ntotal conf.size:", total);
  655. return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p);
  656. }
  657. // Get string descriptor
  658. uint8_t USB::getStrDescr(uint8_t addr, uint8_t ep, uint16_t ns, uint8_t index, uint16_t langid, uint8_t* dataptr) {
  659. return ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, langid, ns, ns, dataptr, nullptr);
  660. }
  661. // Set address
  662. uint8_t USB::setAddr(uint8_t oldaddr, uint8_t ep, uint8_t newaddr) {
  663. uint8_t rcode = ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, nullptr, nullptr);
  664. //delay(2); // Per USB 2.0 sect.9.2.6.3
  665. delay(300); // Older spec says you should wait at least 200ms
  666. return rcode;
  667. //return ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, nullptr, nullptr);
  668. }
  669. // Set configuration
  670. uint8_t USB::setConf(uint8_t addr, uint8_t ep, uint8_t conf_value) {
  671. return ctrlReq(addr, ep, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, nullptr, nullptr);
  672. }
  673. #endif // USB_FLASH_DRIVE_SUPPORT