您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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