Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1004 rindas
31KB

  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "host.h"
  24. uint32_t periodictable[32] __attribute__ ((aligned(4096), used));
  25. uint8_t port_state;
  26. #define PORT_STATE_DISCONNECTED 0
  27. #define PORT_STATE_DEBOUNCE 1
  28. #define PORT_STATE_RESET 2
  29. #define PORT_STATE_RECOVERY 3
  30. #define PORT_STATE_ACTIVE 4
  31. Device_t *rootdev=NULL;
  32. Transfer_t *async_followup_first=NULL;
  33. Transfer_t *async_followup_last=NULL;
  34. Transfer_t *periodic_followup_first=NULL;
  35. Transfer_t *periodic_followup_last=NULL;
  36. void setup()
  37. {
  38. // Test board has a USB data mux (this won't be on final Teensy 3.6)
  39. pinMode(32, OUTPUT); // pin 32 = USB switch, high=connect device
  40. digitalWrite(32, LOW);
  41. pinMode(30, OUTPUT); // pin 30 = debug info - use oscilloscope
  42. digitalWrite(30, LOW);
  43. // Teensy 3.6 has USB host power controlled by PTE6
  44. PORTE_PCR6 = PORT_PCR_MUX(1);
  45. GPIOE_PDDR |= (1<<6);
  46. GPIOE_PSOR = (1<<6); // turn on USB host power
  47. while (!Serial) ; // wait
  48. Serial.println("USB Host Testing");
  49. Serial.print("sizeof Device = ");
  50. Serial.println(sizeof(Device_t));
  51. Serial.print("sizeof Pipe = ");
  52. Serial.println(sizeof(Pipe_t));
  53. Serial.print("sizeof Transfer = ");
  54. Serial.println(sizeof(Transfer_t));
  55. // configure the MPU to allow USBHS DMA to access memory
  56. MPU_RGDAAC0 |= 0x30000000;
  57. Serial.print("MPU_RGDAAC0 = ");
  58. Serial.println(MPU_RGDAAC0, HEX);
  59. // turn on clocks
  60. MCG_C1 |= MCG_C1_IRCLKEN; // enable MCGIRCLK 32kHz
  61. OSC0_CR |= OSC_ERCLKEN;
  62. SIM_SOPT2 |= SIM_SOPT2_USBREGEN; // turn on USB regulator
  63. SIM_SOPT2 &= ~SIM_SOPT2_USBSLSRC; // use IRC for slow clock
  64. print("power up USBHS PHY");
  65. SIM_USBPHYCTL |= SIM_USBPHYCTL_USBDISILIM; // disable USB current limit
  66. //SIM_USBPHYCTL = SIM_USBPHYCTL_USBDISILIM | SIM_USBPHYCTL_USB3VOUTTRG(6); // pg 237
  67. SIM_SCGC3 |= SIM_SCGC3_USBHSDCD | SIM_SCGC3_USBHSPHY | SIM_SCGC3_USBHS;
  68. USBHSDCD_CLOCK = 33 << 2;
  69. print("init USBHS PHY & PLL");
  70. // init process: page 1681-1682
  71. USBPHY_CTRL_CLR = (USBPHY_CTRL_SFTRST | USBPHY_CTRL_CLKGATE); // // CTRL pg 1698
  72. USBPHY_TRIM_OVERRIDE_EN_SET = 1;
  73. USBPHY_PLL_SIC = USBPHY_PLL_SIC_PLL_POWER | USBPHY_PLL_SIC_PLL_ENABLE |
  74. USBPHY_PLL_SIC_PLL_DIV_SEL(1) | USBPHY_PLL_SIC_PLL_EN_USB_CLKS;
  75. // wait for the PLL to lock
  76. int count=0;
  77. while ((USBPHY_PLL_SIC & USBPHY_PLL_SIC_PLL_LOCK) == 0) {
  78. count++;
  79. }
  80. Serial.print("PLL locked, waited ");
  81. Serial.println(count);
  82. // turn on power to PHY
  83. USBPHY_PWD = 0;
  84. delay(10);
  85. // sanity check, connect 470K pullup & 100K pulldown and watch D+ voltage change
  86. //USBPHY_ANACTRL_CLR = (1<<10); // turn off both 15K pulldowns... works! :)
  87. // sanity check, output clocks on pin 9 for testing
  88. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(3); // LPO 1kHz
  89. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(2); // Flash
  90. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(6); // XTAL
  91. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(7); // IRC 48MHz
  92. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(4); // MCGIRCLK
  93. //CORE_PIN9_CONFIG = PORT_PCR_MUX(5); // CLKOUT on PTC3 Alt5 (Arduino pin 9)
  94. // now with the PHY up and running, start up USBHS
  95. print("begin ehci reset");
  96. USBHS_USBCMD |= USBHS_USBCMD_RST;
  97. count = 0;
  98. while (USBHS_USBCMD & USBHS_USBCMD_RST) {
  99. count++;
  100. }
  101. print(" reset waited ", count);
  102. init_Device_Pipe_Transfer_memory();
  103. for (int i=0; i < 32; i++) {
  104. periodictable[i] = 1;
  105. }
  106. port_state = PORT_STATE_DISCONNECTED;
  107. USBHS_USB_SBUSCFG = 1; // System Bus Interface Configuration
  108. // turn on the USBHS controller
  109. //USBHS_USBMODE = USBHS_USBMODE_TXHSD(5) | USBHS_USBMODE_CM(3); // host mode
  110. USBHS_USBMODE = USBHS_USBMODE_CM(3); // host mode
  111. USBHS_USBINTR = 0;
  112. USBHS_PERIODICLISTBASE = (uint32_t)periodictable;
  113. USBHS_FRINDEX = 0;
  114. USBHS_ASYNCLISTADDR = 0;
  115. USBHS_USBCMD = USBHS_USBCMD_ITC(8) | USBHS_USBCMD_RS |
  116. USBHS_USBCMD_ASP(3) | USBHS_USBCMD_ASPE |
  117. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(1); // periodic table is 32 pointers
  118. // turn on the USB port
  119. //USBHS_PORTSC1 = USBHS_PORTSC_PP;
  120. USBHS_PORTSC1 |= USBHS_PORTSC_PP;
  121. //USBHS_PORTSC1 |= USBHS_PORTSC_PFSC; // force 12 Mbit/sec
  122. //USBHS_PORTSC1 |= USBHS_PORTSC_PHCD; // phy off
  123. Serial.print("USBHS_ASYNCLISTADDR = ");
  124. Serial.println(USBHS_ASYNCLISTADDR, HEX);
  125. Serial.print("USBHS_PERIODICLISTBASE = ");
  126. Serial.println(USBHS_PERIODICLISTBASE, HEX);
  127. Serial.print("periodictable = ");
  128. Serial.println((uint32_t)periodictable, HEX);
  129. // enable interrupts, after this point interruts to all the work
  130. NVIC_ENABLE_IRQ(IRQ_USBHS);
  131. USBHS_USBINTR = USBHS_USBINTR_PCE | USBHS_USBINTR_TIE0;
  132. USBHS_USBINTR |= USBHS_USBINTR_UEE | USBHS_USBINTR_SEE;
  133. USBHS_USBINTR |= USBHS_USBINTR_AAE;
  134. USBHS_USBINTR |= USBHS_USBINTR_UPIE | USBHS_USBINTR_UAIE;
  135. delay(25);
  136. Serial.println("Plug in device...");
  137. digitalWrite(32, HIGH); // connect device
  138. #if 0
  139. delay(5000);
  140. Serial.println();
  141. Serial.println("Ring Doorbell");
  142. USBHS_USBCMD |= USBHS_USBCMD_IAA;
  143. if (rootdev) print(rootdev->control_pipe);
  144. #endif
  145. }
  146. void loop()
  147. {
  148. }
  149. void pulse(int usec)
  150. {
  151. // connect oscilloscope to see these pulses....
  152. digitalWriteFast(30, HIGH);
  153. delayMicroseconds(usec);
  154. digitalWriteFast(30, LOW);
  155. }
  156. // EHCI registers page default
  157. // -------------- ---- -------
  158. // USBHS_USBCMD 1599 00080000 USB Command
  159. // USBHS_USBSTS 1602 00000000 USB Status
  160. // USBHS_USBINTR 1606 00000000 USB Interrupt Enable
  161. // USBHS_FRINDEX 1609 00000000 Frame Index Register
  162. // USBHS_PERIODICLISTBASE 1610 undefine Periodic Frame List Base Address
  163. // USBHS_ASYNCLISTADDR 1612 undefine Asynchronous List Address
  164. // USBHS_PORTSC1 1619 00002000 Port Status and Control
  165. // USBHS_USBMODE 1629 00005000 USB Mode
  166. // USBHS_GPTIMERnCTL 1591 00000000 General Purpose Timer n Control
  167. // PORT_STATE_DISCONNECTED 0
  168. // PORT_STATE_DEBOUNCE 1
  169. // PORT_STATE_RESET 2
  170. // PORT_STATE_RECOVERY 3
  171. // PORT_STATE_ACTIVE 4
  172. void usbhs_isr(void)
  173. {
  174. uint32_t stat = USBHS_USBSTS;
  175. USBHS_USBSTS = stat; // clear pending interrupts
  176. //stat &= USBHS_USBINTR; // mask away unwanted interrupts
  177. Serial.println();
  178. Serial.print("ISR: ");
  179. Serial.print(stat, HEX);
  180. Serial.println();
  181. if (stat & USBHS_USBSTS_UI) Serial.println(" USB Interrupt");
  182. if (stat & USBHS_USBSTS_UEI) Serial.println(" USB Error");
  183. if (stat & USBHS_USBSTS_PCI) Serial.println(" Port Change");
  184. if (stat & USBHS_USBSTS_FRI) Serial.println(" Frame List Rollover");
  185. if (stat & USBHS_USBSTS_SEI) Serial.println(" System Error");
  186. if (stat & USBHS_USBSTS_AAI) Serial.println(" Async Advance (doorbell)");
  187. if (stat & USBHS_USBSTS_URI) Serial.println(" Reset Recv");
  188. if (stat & USBHS_USBSTS_SRI) Serial.println(" SOF");
  189. if (stat & USBHS_USBSTS_SLI) Serial.println(" Suspend");
  190. if (stat & USBHS_USBSTS_HCH) Serial.println(" Host Halted");
  191. if (stat & USBHS_USBSTS_RCL) Serial.println(" Reclamation");
  192. if (stat & USBHS_USBSTS_PS) Serial.println(" Periodic Sched En");
  193. if (stat & USBHS_USBSTS_AS) Serial.println(" Async Sched En");
  194. if (stat & USBHS_USBSTS_NAKI) Serial.println(" NAK");
  195. if (stat & USBHS_USBSTS_UAI) Serial.println(" USB Async");
  196. if (stat & USBHS_USBSTS_UPI) Serial.println(" USB Periodic");
  197. if (stat & USBHS_USBSTS_TI0) Serial.println(" Timer0");
  198. if (stat & USBHS_USBSTS_TI1) Serial.println(" Timer1");
  199. if (stat & USBHS_USBSTS_UAI) { // completed qTD(s) from the async schedule
  200. Serial.println("Async Followup");
  201. print(async_followup_first, async_followup_last);
  202. Transfer_t *p = async_followup_first;
  203. while (p) {
  204. if (followup_Transfer(p)) {
  205. // transfer completed
  206. Transfer_t *next = p->next_followup;
  207. remove_from_async_followup_list(p);
  208. free_Transfer(p);
  209. p = next;
  210. } else {
  211. // transfer still pending
  212. p = p->next_followup;
  213. }
  214. }
  215. print(async_followup_first, async_followup_last);
  216. }
  217. if (stat & USBHS_USBSTS_UPI) { // completed qTD(s) from the periodic schedule
  218. Serial.println("Periodic Followup");
  219. Transfer_t *p = periodic_followup_first;
  220. while (p) {
  221. if (followup_Transfer(p)) {
  222. // transfer completed
  223. Transfer_t *next = p->next_followup;
  224. remove_from_periodic_followup_list(p);
  225. free_Transfer(p);
  226. p = next;
  227. } else {
  228. // transfer still pending
  229. p = p->next_followup;
  230. }
  231. }
  232. }
  233. if (stat & USBHS_USBSTS_PCI) { // port change detected
  234. const uint32_t portstat = USBHS_PORTSC1;
  235. Serial.print("port change: ");
  236. Serial.print(portstat, HEX);
  237. Serial.println();
  238. USBHS_PORTSC1 = portstat | (USBHS_PORTSC_OCC|USBHS_PORTSC_PEC|USBHS_PORTSC_CSC);
  239. if (portstat & USBHS_PORTSC_OCC) {
  240. Serial.println(" overcurrent change");
  241. }
  242. if (portstat & USBHS_PORTSC_CSC) {
  243. if (portstat & USBHS_PORTSC_CCS) {
  244. Serial.println(" connect");
  245. if (port_state == PORT_STATE_DISCONNECTED
  246. || port_state == PORT_STATE_DEBOUNCE) {
  247. // 100 ms debounce (USB 2.0: TATTDB, page 150 & 188)
  248. port_state = PORT_STATE_DEBOUNCE;
  249. USBHS_GPTIMER0LD = 100000; // microseconds
  250. USBHS_GPTIMER0CTL =
  251. USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  252. stat &= ~USBHS_USBSTS_TI0;
  253. }
  254. } else {
  255. Serial.println(" disconnect");
  256. port_state = PORT_STATE_DISCONNECTED;
  257. USBPHY_CTRL_CLR = USBPHY_CTRL_ENHOSTDISCONDETECT;
  258. // TODO: delete & clean up device state...
  259. }
  260. }
  261. if (portstat & USBHS_PORTSC_PEC) {
  262. // PEC bit only detects disable
  263. Serial.println(" disable");
  264. } else if (port_state == PORT_STATE_RESET && portstat & USBHS_PORTSC_PE) {
  265. Serial.println(" port enabled");
  266. port_state = PORT_STATE_RECOVERY;
  267. // 10 ms reset recover (USB 2.0: TRSTRCY, page 151 & 188)
  268. USBHS_GPTIMER0LD = 10000; // microseconds
  269. USBHS_GPTIMER0CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  270. if (USBHS_PORTSC1 & USBHS_PORTSC_HSP) {
  271. // turn on high-speed disconnect detector
  272. USBPHY_CTRL_SET = USBPHY_CTRL_ENHOSTDISCONDETECT;
  273. }
  274. }
  275. if (portstat & USBHS_PORTSC_FPR) {
  276. Serial.println(" force resume");
  277. }
  278. pulse(1);
  279. }
  280. if (stat & USBHS_USBSTS_TI0) { // timer 0
  281. Serial.println("timer");
  282. pulse(2);
  283. if (port_state == PORT_STATE_DEBOUNCE) {
  284. port_state = PORT_STATE_RESET;
  285. USBHS_PORTSC1 |= USBHS_PORTSC_PR; // begin reset sequence
  286. Serial.println(" begin reset");
  287. } else if (port_state == PORT_STATE_RECOVERY) {
  288. port_state = PORT_STATE_ACTIVE;
  289. Serial.println(" end recovery");
  290. // HCSPARAMS TTCTRL page 1671
  291. uint32_t speed = (USBHS_PORTSC1 >> 26) & 3;
  292. rootdev = new_Device(speed, 0, 0);
  293. }
  294. }
  295. }
  296. void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest,
  297. uint32_t wValue, uint32_t wIndex, uint32_t wLength)
  298. {
  299. s.word1 = bmRequestType | (bRequest << 8) | (wValue << 16);
  300. s.word2 = wIndex | (wLength << 16);
  301. }
  302. static uint8_t enumbuf[256] __attribute__ ((aligned(16)));
  303. void enumeration(const Transfer_t *transfer)
  304. {
  305. uint32_t len;
  306. Serial.print(" CALLBACK: ");
  307. print_hexbytes(transfer->buffer, transfer->length);
  308. //print(transfer);
  309. Device_t *dev = transfer->pipe->device;
  310. while (1) {
  311. // Within this large switch/case, "break" means we've done
  312. // some work, but more remains to be done in a different
  313. // state. Generally break is used after parsing received
  314. // data, but what happens next could be different states.
  315. // When completed, return is used. Generally, return happens
  316. // only after a new control transfer is queued, or when
  317. // enumeration is complete and no more communication is needed.
  318. switch (dev->enum_state) {
  319. case 0: // read 8 bytes of device desc, set max packet, and send set address
  320. pipe_set_maxlen(dev->control_pipe, enumbuf[7]);
  321. mk_setup(dev->setup, 0, 5, assign_addr(), 0, 0); // 5=SET_ADDRESS
  322. new_Transfer(dev->control_pipe, NULL, 0);
  323. dev->enum_state = 1;
  324. return;
  325. case 1: // request all 18 bytes of device descriptor
  326. pipe_set_addr(dev->control_pipe, dev->setup.wValue);
  327. mk_setup(dev->setup, 0x80, 6, 0x0100, 0, 18); // 6=GET_DESCRIPTOR
  328. new_Transfer(dev->control_pipe, enumbuf, 18);
  329. dev->enum_state = 2;
  330. return;
  331. case 2: // parse 18 device desc bytes
  332. dev->bDeviceClass = enumbuf[4];
  333. dev->bDeviceSubClass = enumbuf[5];
  334. dev->bDeviceProtocol = enumbuf[6];
  335. dev->idVendor = enumbuf[8] | (enumbuf[9] << 8);
  336. dev->idProduct = enumbuf[10] | (enumbuf[11] << 8);
  337. enumbuf[0] = enumbuf[14];
  338. enumbuf[1] = enumbuf[15];
  339. enumbuf[2] = enumbuf[16];
  340. if ((enumbuf[0] | enumbuf[1] | enumbuf[2]) > 0) {
  341. dev->enum_state = 3;
  342. } else {
  343. dev->enum_state = 11;
  344. }
  345. break;
  346. case 3: // request Language ID
  347. len = sizeof(enumbuf) - 4;
  348. mk_setup(dev->setup, 0x80, 6, 0x0300, 0, len); // 6=GET_DESCRIPTOR
  349. new_Transfer(dev->control_pipe, enumbuf + 4, len);
  350. dev->enum_state = 4;
  351. return;
  352. case 4: // parse Language ID
  353. if (enumbuf[4] < 4 || enumbuf[5] != 3) {
  354. dev->enum_state = 11;
  355. } else {
  356. dev->LanguageID = enumbuf[6] | (enumbuf[7] << 8);
  357. if (enumbuf[0]) dev->enum_state = 5;
  358. else if (enumbuf[1]) dev->enum_state = 7;
  359. else if (enumbuf[2]) dev->enum_state = 9;
  360. else dev->enum_state = 11;
  361. }
  362. break;
  363. case 5: // request Manufacturer string
  364. len = sizeof(enumbuf) - 4;
  365. mk_setup(dev->setup, 0x80, 6, 0x0300 | enumbuf[0], dev->LanguageID, len);
  366. new_Transfer(dev->control_pipe, enumbuf + 4, len);
  367. dev->enum_state = 6;
  368. return;
  369. case 6: // parse Manufacturer string
  370. // TODO: receive the string...
  371. if (enumbuf[1]) dev->enum_state = 7;
  372. else if (enumbuf[2]) dev->enum_state = 9;
  373. else dev->enum_state = 11;
  374. break;
  375. case 7: // request Product string
  376. len = sizeof(enumbuf) - 4;
  377. mk_setup(dev->setup, 0x80, 6, 0x0300 | enumbuf[1], dev->LanguageID, len);
  378. new_Transfer(dev->control_pipe, enumbuf + 4, len);
  379. dev->enum_state = 8;
  380. return;
  381. case 8: // parse Product string
  382. // TODO: receive the string...
  383. if (enumbuf[2]) dev->enum_state = 9;
  384. else dev->enum_state = 11;
  385. break;
  386. case 9: // request Serial Number string
  387. len = sizeof(enumbuf) - 4;
  388. mk_setup(dev->setup, 0x80, 6, 0x0300 | enumbuf[2], dev->LanguageID, len);
  389. new_Transfer(dev->control_pipe, enumbuf + 4, len);
  390. dev->enum_state = 10;
  391. return;
  392. case 10: // parse Serial Number string
  393. // TODO: receive the string...
  394. dev->enum_state = 11;
  395. break;
  396. case 11: // request first 9 bytes of config desc
  397. mk_setup(dev->setup, 0x80, 6, 0x0200, 0, 9); // 6=GET_DESCRIPTOR
  398. new_Transfer(dev->control_pipe, enumbuf, 9);
  399. dev->enum_state = 12;
  400. return;
  401. case 12: // read 9 bytes, request all of config desc
  402. len = enumbuf[2] | (enumbuf[3] << 8);
  403. Serial.print("Config data length = ");
  404. Serial.println(len);
  405. if (len > sizeof(enumbuf)) {
  406. // TODO: how to handle device with too much config data
  407. }
  408. mk_setup(dev->setup, 0x80, 6, 0x0200, 0, len); // 6=GET_DESCRIPTOR
  409. new_Transfer(dev->control_pipe, enumbuf, len);
  410. dev->enum_state = 13;
  411. return;
  412. case 13: // read all config desc, send set config
  413. Serial.print("bNumInterfaces = ");
  414. Serial.println(enumbuf[4]);
  415. Serial.print("bConfigurationValue = ");
  416. Serial.println(enumbuf[5]);
  417. // TODO: actually do something with interface descriptor?
  418. mk_setup(dev->setup, 0, 9, enumbuf[5], 0, 0); // 9=SET_CONFIGURATION
  419. new_Transfer(dev->control_pipe, NULL, 0);
  420. dev->enum_state = 14;
  421. return;
  422. case 14: // device is now configured
  423. // TODO: initialize drivers??
  424. dev->enum_state = 15;
  425. return;
  426. case 15: // control transfers for other stuff??
  427. default:
  428. return;
  429. }
  430. }
  431. }
  432. uint32_t assign_addr(void)
  433. {
  434. return 29; // TODO: when multiple devices, assign a unique address
  435. }
  436. void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen)
  437. {
  438. Serial.print("pipe_set_maxlen ");
  439. Serial.println(maxlen);
  440. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0x8000FFFF) | (maxlen << 16);
  441. }
  442. void pipe_set_addr(Pipe_t *pipe, uint32_t addr)
  443. {
  444. Serial.print("pipe_set_addr ");
  445. Serial.println(addr);
  446. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0xFFFFFF80) | addr;
  447. }
  448. uint32_t pipe_get_addr(Pipe_t *pipe)
  449. {
  450. return pipe->qh.capabilities[0] & 0xFFFFFF80;
  451. }
  452. // Create a new device and begin the enumeration process
  453. //
  454. Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port)
  455. {
  456. Device_t *dev;
  457. Serial.print("new_Device: ");
  458. switch (speed) {
  459. case 0: Serial.print("12"); break;
  460. case 1: Serial.print("1.5"); break;
  461. case 2: Serial.print("480"); break;
  462. default: Serial.print("??");
  463. }
  464. Serial.println(" Mbit/sec");
  465. dev = allocate_Device();
  466. if (!dev) return NULL;
  467. memset(dev, 0, sizeof(Device_t));
  468. dev->speed = speed;
  469. dev->address = 0;
  470. dev->hub_address = hub_addr;
  471. dev->hub_port = hub_port;
  472. dev->control_pipe = new_Pipe(dev, 0, 0, 0, 8);
  473. if (!dev->control_pipe) {
  474. free_Device(dev);
  475. return NULL;
  476. }
  477. dev->control_pipe->callback_function = &enumeration;
  478. dev->control_pipe->direction = 1; // 1=IN
  479. mk_setup(dev->setup, 0x80, 6, 0x0100, 0, 8); // 6=GET_DESCRIPTOR
  480. new_Transfer(dev->control_pipe, enumbuf, 8);
  481. return dev;
  482. }
  483. static uint32_t QH_capabilities1(uint32_t nak_count_reload, uint32_t control_endpoint_flag,
  484. uint32_t max_packet_length, uint32_t head_of_list, uint32_t data_toggle_control,
  485. uint32_t speed, uint32_t endpoint_number, uint32_t inactivate, uint32_t address)
  486. {
  487. return ( (nak_count_reload << 28) | (control_endpoint_flag << 27) |
  488. (max_packet_length << 16) | (head_of_list << 15) |
  489. (data_toggle_control << 14) | (speed << 12) | (endpoint_number << 8) |
  490. (inactivate << 7) | (address << 0) );
  491. }
  492. static uint32_t QH_capabilities2(uint32_t high_bw_mult, uint32_t hub_port_number,
  493. uint32_t hub_address, uint32_t split_completion_mask, uint32_t interrupt_schedule_mask)
  494. {
  495. return ( (high_bw_mult << 30) | (hub_port_number << 23) | (hub_address << 16) |
  496. (split_completion_mask << 8) | (interrupt_schedule_mask << 0) );
  497. }
  498. // Create a new pipe. It's QH is added to the async or periodic schedule,
  499. // and a halt qTD is added to the QH, so we can grow the qTD list later.
  500. //
  501. Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint, uint32_t direction,
  502. uint32_t max_packet_len)
  503. {
  504. Pipe_t *pipe;
  505. Transfer_t *halt;
  506. uint32_t c=0, dtc=0;
  507. Serial.println("new_Pipe");
  508. pipe = allocate_Pipe();
  509. if (!pipe) return NULL;
  510. halt = allocate_Transfer();
  511. if (!halt) {
  512. free_Pipe(pipe);
  513. return NULL;
  514. }
  515. memset(pipe, 0, sizeof(Pipe_t));
  516. memset(halt, 0, sizeof(Transfer_t));
  517. halt->qtd.next = 1;
  518. halt->qtd.token = 0x40;
  519. pipe->device = dev;
  520. pipe->qh.next = (uint32_t)halt;
  521. pipe->qh.alt_next = 1;
  522. pipe->direction = direction;
  523. pipe->type = type;
  524. if (type == 0) {
  525. // control
  526. if (dev->speed < 2) c = 1;
  527. dtc = 1;
  528. } else if (type == 2) {
  529. // bulk
  530. } else if (type == 3) {
  531. // interrupt
  532. }
  533. pipe->qh.capabilities[0] = QH_capabilities1(15, c, max_packet_len, 0,
  534. dtc, dev->speed, endpoint, 0, dev->address);
  535. pipe->qh.capabilities[1] = QH_capabilities2(1, dev->hub_port,
  536. dev->hub_address, 0, 0);
  537. if (type == 0 || type == 2) {
  538. // control or bulk: add to async queue
  539. Pipe_t *list = (Pipe_t *)USBHS_ASYNCLISTADDR;
  540. if (list == NULL) {
  541. pipe->qh.capabilities[0] |= 0x8000; // H bit
  542. pipe->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2; // 2=QH
  543. USBHS_ASYNCLISTADDR = (uint32_t)&(pipe->qh);
  544. USBHS_USBCMD |= USBHS_USBCMD_ASE; // enable async schedule
  545. Serial.println(" first in async list");
  546. } else {
  547. // EHCI 1.0: section 4.8.1, page 72
  548. pipe->qh.horizontal_link = list->qh.horizontal_link;
  549. list->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2;
  550. Serial.println(" added to async list");
  551. }
  552. } else if (type == 3) {
  553. // interrupt: add to periodic schedule
  554. // TODO: link it into the periodic table
  555. }
  556. return pipe;
  557. }
  558. // Fill in the qTD fields (token & data)
  559. // t the Transfer qTD to initialize
  560. // buf data to transfer
  561. // len length of data
  562. // pid type of packet: 0=OUT, 1=IN, 2=SETUP
  563. // data01 value of DATA0/DATA1 toggle on 1st packet
  564. // irq whether to generate an interrupt when transfer complete
  565. //
  566. void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  567. uint32_t pid, uint32_t data01, bool irq)
  568. {
  569. t->qtd.alt_next = 1; // 1=terminate
  570. if (data01) data01 = 0x80000000;
  571. t->qtd.token = data01 | (len << 16) | (irq ? 0x8000 : 0) | (pid << 8) | 0x80;
  572. uint32_t addr = (uint32_t)buf;
  573. t->qtd.buffer[0] = addr;
  574. addr &= 0xFFFFF000;
  575. t->qtd.buffer[1] = addr + 0x1000;
  576. t->qtd.buffer[2] = addr + 0x2000;
  577. t->qtd.buffer[3] = addr + 0x3000;
  578. t->qtd.buffer[4] = addr + 0x4000;
  579. }
  580. // Create a Transfer and queue it
  581. //
  582. bool new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len)
  583. {
  584. Serial.println("new_Transfer");
  585. Transfer_t *transfer = allocate_Transfer();
  586. if (!transfer) return false;
  587. if (pipe->type == 0) {
  588. // control transfer
  589. Transfer_t *data, *status;
  590. uint32_t status_direction;
  591. if (len > 16384) {
  592. // hopefully we never need more
  593. // than 16K in a control transfer
  594. free_Transfer(transfer);
  595. return false;
  596. }
  597. status = allocate_Transfer();
  598. if (!status) {
  599. free_Transfer(transfer);
  600. return false;
  601. }
  602. if (len > 0) {
  603. data = allocate_Transfer();
  604. if (!data) {
  605. free_Transfer(transfer);
  606. free_Transfer(status);
  607. return false;
  608. }
  609. init_qTD(data, buffer, len, pipe->direction, 1, false);
  610. transfer->qtd.next = (uint32_t)data;
  611. data->qtd.next = (uint32_t)status;
  612. status_direction = pipe->direction ^ 1;
  613. } else {
  614. transfer->qtd.next = (uint32_t)status;
  615. status_direction = 1; // always IN, USB 2.0 page 226
  616. }
  617. Serial.print("setup address ");
  618. Serial.println((uint32_t)&pipe->device->setup, HEX);
  619. init_qTD(transfer, &pipe->device->setup, 8, 2, 0, false);
  620. init_qTD(status, NULL, 0, status_direction, 1, true);
  621. status->pipe = pipe;
  622. status->buffer = buffer;
  623. status->length = len;
  624. status->qtd.next = 1;
  625. } else {
  626. // bulk, interrupt or isochronous transfer
  627. free_Transfer(transfer);
  628. return false;
  629. }
  630. // find halt qTD
  631. Transfer_t *halt = (Transfer_t *)(pipe->qh.next);
  632. while (!(halt->qtd.token & 0x40)) halt = (Transfer_t *)(halt->qtd.next);
  633. // transfer's token
  634. uint32_t token = transfer->qtd.token;
  635. // transfer becomes new halt qTD
  636. transfer->qtd.token = 0x40;
  637. // copy transfer non-token fields to halt
  638. halt->qtd.next = transfer->qtd.next;
  639. halt->qtd.alt_next = transfer->qtd.alt_next;
  640. halt->qtd.buffer[0] = transfer->qtd.buffer[0]; // TODO: optimize...
  641. halt->qtd.buffer[1] = transfer->qtd.buffer[1];
  642. halt->qtd.buffer[2] = transfer->qtd.buffer[2];
  643. halt->qtd.buffer[3] = transfer->qtd.buffer[3];
  644. halt->qtd.buffer[4] = transfer->qtd.buffer[4];
  645. halt->pipe = pipe;
  646. // find the last qTD we're adding
  647. Transfer_t *last = halt;
  648. while ((uint32_t)(last->qtd.next) != 1) last = (Transfer_t *)(last->qtd.next);
  649. // last points to transfer (which becomes new halt)
  650. last->qtd.next = (uint32_t)transfer;
  651. transfer->qtd.next = 1;
  652. // link all the new qTD by next_followup & prev_followup
  653. Transfer_t *prev = NULL;
  654. Transfer_t *p = halt;
  655. while (p->qtd.next != (uint32_t)transfer) {
  656. Transfer_t *next = (Transfer_t *)p->qtd.next;
  657. p->prev_followup = prev;
  658. p->next_followup = next;
  659. prev = p;
  660. p = next;
  661. }
  662. p->prev_followup = prev;
  663. p->next_followup = NULL;
  664. print(halt, p);
  665. // add them to a followup list
  666. if (pipe->type == 0 || pipe->type == 2) {
  667. // control or bulk
  668. add_to_async_followup_list(halt, p);
  669. } else {
  670. // interrupt
  671. add_to_periodic_followup_list(halt, p);
  672. }
  673. // old halt becomes new transfer, this commits all new qTDs to QH
  674. halt->qtd.token = token;
  675. return true;
  676. }
  677. bool followup_Transfer(Transfer_t *transfer)
  678. {
  679. Serial.print(" Followup ");
  680. Serial.println((uint32_t)transfer, HEX);
  681. if (!(transfer->qtd.token & 0x80)) {
  682. // TODO: check error status
  683. if (transfer->qtd.token & 0x8000) {
  684. // this transfer caused an interrupt
  685. if (transfer->pipe->callback_function) {
  686. // do the callback
  687. (*(transfer->pipe->callback_function))(transfer);
  688. }
  689. }
  690. // do callback function...
  691. Serial.println(" completed");
  692. return true;
  693. }
  694. return false;
  695. }
  696. static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last)
  697. {
  698. last->next_followup = NULL; // always add to end of list
  699. if (async_followup_last == NULL) {
  700. first->prev_followup = NULL;
  701. async_followup_first = first;
  702. } else {
  703. first->prev_followup = async_followup_last;
  704. async_followup_last->next_followup = first;
  705. }
  706. async_followup_last = last;
  707. }
  708. static void remove_from_async_followup_list(Transfer_t *transfer)
  709. {
  710. Transfer_t *next = transfer->next_followup;
  711. Transfer_t *prev = transfer->prev_followup;
  712. if (prev) {
  713. prev->next_followup = next;
  714. } else {
  715. async_followup_first = next;
  716. }
  717. if (next) {
  718. next->prev_followup = prev;
  719. } else {
  720. async_followup_last = prev;
  721. }
  722. }
  723. static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last)
  724. {
  725. last->next_followup = NULL; // always add to end of list
  726. if (periodic_followup_last == NULL) {
  727. first->prev_followup = NULL;
  728. periodic_followup_first = first;
  729. } else {
  730. first->prev_followup = periodic_followup_last;
  731. periodic_followup_last->next_followup = first;
  732. }
  733. periodic_followup_last = last;
  734. }
  735. static void remove_from_periodic_followup_list(Transfer_t *transfer)
  736. {
  737. Transfer_t *next = transfer->next_followup;
  738. Transfer_t *prev = transfer->prev_followup;
  739. if (prev) {
  740. prev->next_followup = next;
  741. } else {
  742. periodic_followup_first = next;
  743. }
  744. if (next) {
  745. next->prev_followup = prev;
  746. } else {
  747. periodic_followup_last = prev;
  748. }
  749. }
  750. void print(const Transfer_t *transfer)
  751. {
  752. if (!((uint32_t)transfer & 0xFFFFFFE0)) return;
  753. Serial.print("Transfer @ ");
  754. Serial.println(((uint32_t)transfer & 0xFFFFFFE0), HEX);
  755. Serial.print(" next: ");
  756. Serial.println(transfer->qtd.next, HEX);
  757. Serial.print(" anext: ");
  758. Serial.println(transfer->qtd.alt_next, HEX);
  759. Serial.print(" token: ");
  760. Serial.println(transfer->qtd.token, HEX);
  761. Serial.print(" bufs: ");
  762. for (int i=0; i < 5; i++) {
  763. Serial.print(transfer->qtd.buffer[i], HEX);
  764. if (i < 4) Serial.print(',');
  765. }
  766. Serial.println();
  767. }
  768. void print(const Transfer_t *first, const Transfer_t *last)
  769. {
  770. Serial.print("Transfer Followup List ");
  771. Serial.print((uint32_t)first, HEX);
  772. Serial.print(" to ");
  773. Serial.println((uint32_t)last, HEX);
  774. Serial.println(" forward:");
  775. while (first) {
  776. Serial.print(" ");
  777. Serial.print((uint32_t)first, HEX);
  778. print_token(first->qtd.token);
  779. first = first->next_followup;
  780. }
  781. Serial.println(" backward:");
  782. while (last) {
  783. Serial.print(" ");
  784. Serial.print((uint32_t)last, HEX);
  785. print_token(last->qtd.token);
  786. last = last->prev_followup;
  787. }
  788. }
  789. void print_token(uint32_t token)
  790. {
  791. switch ((token >> 8) & 3) {
  792. case 0:
  793. Serial.print(" OUT ");
  794. Serial.println((token >> 16) & 0x7FFF);
  795. break;
  796. case 1:
  797. Serial.print(" IN ");
  798. Serial.println((token >> 16) & 0x7FFF);
  799. break;
  800. case 2:
  801. Serial.println(" SETUP");
  802. break;
  803. default:
  804. Serial.println(" unknown");
  805. }
  806. }
  807. void print(const Pipe_t *pipe)
  808. {
  809. if (!((uint32_t)pipe & 0xFFFFFFE0)) return;
  810. Serial.print("Pipe ");
  811. if (pipe->type == 0) Serial.print("control");
  812. else if (pipe->type == 1) Serial.print("isochronous");
  813. else if (pipe->type == 2) Serial.print("bulk");
  814. else if (pipe->type == 3) Serial.print("interrupt");
  815. Serial.print(pipe->direction ? " IN" : " OUT");
  816. Serial.print(" @ ");
  817. Serial.println((uint32_t)pipe, HEX);
  818. Serial.print(" horiz link: ");
  819. Serial.println(pipe->qh.horizontal_link, HEX);
  820. Serial.print(" capabilities: ");
  821. Serial.print(pipe->qh.capabilities[0], HEX);
  822. Serial.print(',');
  823. Serial.println(pipe->qh.capabilities[1], HEX);
  824. Serial.println(" overlay:");
  825. Serial.print(" cur: ");
  826. Serial.println(pipe->qh.current, HEX);
  827. Serial.print(" next: ");
  828. Serial.println(pipe->qh.next, HEX);
  829. Serial.print(" anext: ");
  830. Serial.println(pipe->qh.alt_next, HEX);
  831. Serial.print(" token: ");
  832. Serial.println(pipe->qh.token, HEX);
  833. Serial.print(" bufs: ");
  834. for (int i=0; i < 5; i++) {
  835. Serial.print(pipe->qh.buffer[i], HEX);
  836. if (i < 4) Serial.print(',');
  837. }
  838. Serial.println();
  839. const Transfer_t *t = (Transfer_t *)pipe->qh.next;
  840. while (((uint32_t)t & 0xFFFFFFE0)) {
  841. print(t);
  842. t = (Transfer_t *)t->qtd.next;
  843. }
  844. //Serial.print();
  845. }
  846. void print_hexbytes(const void *ptr, uint32_t len)
  847. {
  848. if (ptr == NULL || len == 0) return;
  849. const uint8_t *p = (const uint8_t *)ptr;
  850. do {
  851. if (*p < 16) Serial.print('0');
  852. Serial.print(*p++, HEX);
  853. Serial.print(' ');
  854. } while (--len);
  855. Serial.println();
  856. }
  857. void print(const char *s)
  858. {
  859. Serial.println(s);
  860. delay(10);
  861. }
  862. void print(const char *s, int num)
  863. {
  864. Serial.print(s);
  865. Serial.println(num);
  866. delay(10);
  867. }
  868. // Memory allocation
  869. static Device_t memory_Device[3];
  870. static Pipe_t memory_Pipe[6] __attribute__ ((aligned(64)));
  871. static Transfer_t memory_Transfer[24] __attribute__ ((aligned(64)));
  872. Device_t * free_Device_list = NULL;
  873. Pipe_t * free_Pipe_list = NULL;
  874. Transfer_t * free_Transfer_list = NULL;
  875. void init_Device_Pipe_Transfer_memory(void)
  876. {
  877. Device_t *end_device = memory_Device + sizeof(memory_Device)/sizeof(Device_t);
  878. for (Device_t *device = memory_Device; device < end_device; device++) {
  879. free_Device(device);
  880. }
  881. Pipe_t *end_pipe = memory_Pipe + sizeof(memory_Pipe)/sizeof(Pipe_t);
  882. for (Pipe_t *pipe = memory_Pipe; pipe < end_pipe; pipe++) {
  883. free_Pipe(pipe);
  884. }
  885. Transfer_t *end_transfer = memory_Transfer + sizeof(memory_Transfer)/sizeof(Transfer_t);
  886. for (Transfer_t *transfer = memory_Transfer; transfer < end_transfer; transfer++) {
  887. free_Transfer(transfer);
  888. }
  889. }
  890. Device_t * allocate_Device(void)
  891. {
  892. Device_t *device = free_Device_list;
  893. if (device) free_Device_list = *(Device_t **)device;
  894. return device;
  895. }
  896. void free_Device(Device_t *device)
  897. {
  898. *(Device_t **)device = free_Device_list;
  899. free_Device_list = device;
  900. }
  901. Pipe_t * allocate_Pipe(void)
  902. {
  903. Pipe_t *pipe = free_Pipe_list;
  904. if (pipe) free_Pipe_list = *(Pipe_t **)pipe;
  905. return pipe;
  906. }
  907. void free_Pipe(Pipe_t *pipe)
  908. {
  909. *(Pipe_t **)pipe = free_Pipe_list;
  910. free_Pipe_list = pipe;
  911. }
  912. Transfer_t * allocate_Transfer(void)
  913. {
  914. Transfer_t *transfer = free_Transfer_list;
  915. if (transfer) free_Transfer_list = *(Transfer_t **)transfer;
  916. return transfer;
  917. }
  918. void free_Transfer(Transfer_t *transfer)
  919. {
  920. *(Transfer_t **)transfer = free_Transfer_list;
  921. free_Transfer_list = transfer;
  922. }