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.

932 lines
28KB

  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[255];
  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. switch (dev->enum_state) {
  311. case 0: // read 8 bytes of device desc, set max packet, and send set address
  312. pipe_set_maxlen(dev->control_pipe, enumbuf[7]);
  313. mk_setup(dev->setup, 0, 5, assign_addr(), 0, 0); // 5=SET_ADDRESS
  314. new_Transfer(dev->control_pipe, NULL, 0);
  315. dev->enum_state = 1;
  316. break;
  317. case 1: // request all 18 bytes of device descriptor
  318. pipe_set_addr(dev->control_pipe, dev->setup.wValue);
  319. mk_setup(dev->setup, 0x80, 6, 0x0100, 0, 18); // 6=GET_DESCRIPTOR
  320. new_Transfer(dev->control_pipe, enumbuf, 18);
  321. dev->enum_state = 2;
  322. break;
  323. case 2: // read 18 device desc bytes, request first 9 bytes of config desc
  324. // TODO: actually do something with device descriptor?
  325. mk_setup(dev->setup, 0x80, 6, 0x0200, 0, 9); // 6=GET_DESCRIPTOR
  326. new_Transfer(dev->control_pipe, enumbuf, 9);
  327. dev->enum_state = 3;
  328. break;
  329. case 3: // read 9 bytes, request all of config desc
  330. len = enumbuf[2] | (enumbuf[3] << 8);
  331. Serial.print("Config data length = ");
  332. Serial.println(len);
  333. if (len > sizeof(enumbuf)) {
  334. // TODO: how to handle device with too much config data
  335. }
  336. mk_setup(dev->setup, 0x80, 6, 0x0200, 0, len); // 6=GET_DESCRIPTOR
  337. new_Transfer(dev->control_pipe, enumbuf, len);
  338. dev->enum_state = 4;
  339. break;
  340. case 4: // read all config desc, send set config
  341. Serial.print("bNumInterfaces = ");
  342. Serial.println(enumbuf[4]);
  343. Serial.print("bConfigurationValue = ");
  344. Serial.println(enumbuf[5]);
  345. // TODO: actually do something with interface descriptor?
  346. mk_setup(dev->setup, 0, 9, enumbuf[5], 0, 0); // 9=SET_CONFIGURATION
  347. new_Transfer(dev->control_pipe, NULL, 0);
  348. dev->enum_state = 5;
  349. break;
  350. case 5: // device is now configured
  351. // TODO: initialize drivers??
  352. dev->enum_state = 6;
  353. break;
  354. case 6: // control transfers for other stuff??
  355. default:
  356. break;
  357. }
  358. }
  359. uint32_t assign_addr(void)
  360. {
  361. return 29; // TODO: when multiple devices, assign a unique address
  362. }
  363. void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen)
  364. {
  365. Serial.print("pipe_set_maxlen ");
  366. Serial.println(maxlen);
  367. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0x8000FFFF) | (maxlen << 16);
  368. }
  369. void pipe_set_addr(Pipe_t *pipe, uint32_t addr)
  370. {
  371. Serial.print("pipe_set_addr ");
  372. Serial.println(addr);
  373. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0xFFFFFF80) | addr;
  374. }
  375. uint32_t pipe_get_addr(Pipe_t *pipe)
  376. {
  377. return pipe->qh.capabilities[0] & 0xFFFFFF80;
  378. }
  379. // Create a new device and begin the enumeration process
  380. //
  381. Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port)
  382. {
  383. Device_t *dev;
  384. Serial.print("new_Device: ");
  385. switch (speed) {
  386. case 0: Serial.print("12"); break;
  387. case 1: Serial.print("1.5"); break;
  388. case 2: Serial.print("480"); break;
  389. default: Serial.print("??");
  390. }
  391. Serial.println(" Mbit/sec");
  392. dev = allocate_Device();
  393. if (!dev) return NULL;
  394. memset(dev, 0, sizeof(Device_t));
  395. dev->speed = speed;
  396. dev->address = 0;
  397. dev->hub_address = hub_addr;
  398. dev->hub_port = hub_port;
  399. dev->control_pipe = new_Pipe(dev, 0, 0, 0, 8);
  400. if (!dev->control_pipe) {
  401. free_Device(dev);
  402. return NULL;
  403. }
  404. dev->control_pipe->callback_function = &enumeration;
  405. dev->control_pipe->direction = 1; // 1=IN
  406. mk_setup(dev->setup, 0x80, 6, 0x0100, 0, 8); // 6=GET_DESCRIPTOR
  407. new_Transfer(dev->control_pipe, enumbuf, 8);
  408. return dev;
  409. }
  410. static uint32_t QH_capabilities1(uint32_t nak_count_reload, uint32_t control_endpoint_flag,
  411. uint32_t max_packet_length, uint32_t head_of_list, uint32_t data_toggle_control,
  412. uint32_t speed, uint32_t endpoint_number, uint32_t inactivate, uint32_t address)
  413. {
  414. return ( (nak_count_reload << 28) | (control_endpoint_flag << 27) |
  415. (max_packet_length << 16) | (head_of_list << 15) |
  416. (data_toggle_control << 14) | (speed << 12) | (endpoint_number << 8) |
  417. (inactivate << 7) | (address << 0) );
  418. }
  419. static uint32_t QH_capabilities2(uint32_t high_bw_mult, uint32_t hub_port_number,
  420. uint32_t hub_address, uint32_t split_completion_mask, uint32_t interrupt_schedule_mask)
  421. {
  422. return ( (high_bw_mult << 30) | (hub_port_number << 23) | (hub_address << 16) |
  423. (split_completion_mask << 8) | (interrupt_schedule_mask << 0) );
  424. }
  425. // Create a new pipe. It's QH is added to the async or periodic schedule,
  426. // and a halt qTD is added to the QH, so we can grow the qTD list later.
  427. //
  428. Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint, uint32_t direction,
  429. uint32_t max_packet_len)
  430. {
  431. Pipe_t *pipe;
  432. Transfer_t *halt;
  433. uint32_t c=0, dtc=0;
  434. Serial.println("new_Pipe");
  435. pipe = allocate_Pipe();
  436. if (!pipe) return NULL;
  437. halt = allocate_Transfer();
  438. if (!halt) {
  439. free_Pipe(pipe);
  440. return NULL;
  441. }
  442. memset(pipe, 0, sizeof(Pipe_t));
  443. memset(halt, 0, sizeof(Transfer_t));
  444. halt->qtd.next = 1;
  445. halt->qtd.token = 0x40;
  446. pipe->device = dev;
  447. pipe->qh.next = (uint32_t)halt;
  448. pipe->qh.alt_next = 1;
  449. pipe->direction = direction;
  450. pipe->type = type;
  451. if (type == 0) {
  452. // control
  453. if (dev->speed < 2) c = 1;
  454. dtc = 1;
  455. } else if (type == 2) {
  456. // bulk
  457. } else if (type == 3) {
  458. // interrupt
  459. }
  460. pipe->qh.capabilities[0] = QH_capabilities1(15, c, max_packet_len, 0,
  461. dtc, dev->speed, endpoint, 0, dev->address);
  462. pipe->qh.capabilities[1] = QH_capabilities2(1, dev->hub_port,
  463. dev->hub_address, 0, 0);
  464. if (type == 0 || type == 2) {
  465. // control or bulk: add to async queue
  466. Pipe_t *list = (Pipe_t *)USBHS_ASYNCLISTADDR;
  467. if (list == NULL) {
  468. pipe->qh.capabilities[0] |= 0x8000; // H bit
  469. pipe->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2; // 2=QH
  470. USBHS_ASYNCLISTADDR = (uint32_t)&(pipe->qh);
  471. USBHS_USBCMD |= USBHS_USBCMD_ASE; // enable async schedule
  472. Serial.println(" first in async list");
  473. } else {
  474. // EHCI 1.0: section 4.8.1, page 72
  475. pipe->qh.horizontal_link = list->qh.horizontal_link;
  476. list->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2;
  477. Serial.println(" added to async list");
  478. }
  479. } else if (type == 3) {
  480. // interrupt: add to periodic schedule
  481. // TODO: link it into the periodic table
  482. }
  483. return pipe;
  484. }
  485. // Fill in the qTD fields (token & data)
  486. // t the Transfer qTD to initialize
  487. // buf data to transfer
  488. // len length of data
  489. // pid type of packet: 0=OUT, 1=IN, 2=SETUP
  490. // data01 value of DATA0/DATA1 toggle on 1st packet
  491. // irq whether to generate an interrupt when transfer complete
  492. //
  493. void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  494. uint32_t pid, uint32_t data01, bool irq)
  495. {
  496. t->qtd.alt_next = 1; // 1=terminate
  497. if (data01) data01 = 0x80000000;
  498. t->qtd.token = data01 | (len << 16) | (irq ? 0x8000 : 0) | (pid << 8) | 0x80;
  499. uint32_t addr = (uint32_t)buf;
  500. t->qtd.buffer[0] = addr;
  501. addr &= 0xFFFFF000;
  502. t->qtd.buffer[1] = addr + 0x1000;
  503. t->qtd.buffer[2] = addr + 0x2000;
  504. t->qtd.buffer[3] = addr + 0x3000;
  505. t->qtd.buffer[4] = addr + 0x4000;
  506. }
  507. // Create a Transfer and queue it
  508. //
  509. bool new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len)
  510. {
  511. Serial.println("new_Transfer");
  512. Transfer_t *transfer = allocate_Transfer();
  513. if (!transfer) return false;
  514. if (pipe->type == 0) {
  515. // control transfer
  516. Transfer_t *data, *status;
  517. uint32_t status_direction;
  518. if (len > 16384) {
  519. // hopefully we never need more
  520. // than 16K in a control transfer
  521. free_Transfer(transfer);
  522. return false;
  523. }
  524. status = allocate_Transfer();
  525. if (!status) {
  526. free_Transfer(transfer);
  527. return false;
  528. }
  529. if (len > 0) {
  530. data = allocate_Transfer();
  531. if (!data) {
  532. free_Transfer(transfer);
  533. free_Transfer(status);
  534. return false;
  535. }
  536. init_qTD(data, buffer, len, pipe->direction, 1, false);
  537. transfer->qtd.next = (uint32_t)data;
  538. data->qtd.next = (uint32_t)status;
  539. status_direction = pipe->direction ^ 1;
  540. } else {
  541. transfer->qtd.next = (uint32_t)status;
  542. status_direction = 1; // always IN, USB 2.0 page 226
  543. }
  544. Serial.print("setup address ");
  545. Serial.println((uint32_t)&pipe->device->setup, HEX);
  546. init_qTD(transfer, &pipe->device->setup, 8, 2, 0, false);
  547. init_qTD(status, NULL, 0, status_direction, 1, true);
  548. status->pipe = pipe;
  549. status->buffer = buffer;
  550. status->length = len;
  551. status->qtd.next = 1;
  552. } else {
  553. // bulk, interrupt or isochronous transfer
  554. free_Transfer(transfer);
  555. return false;
  556. }
  557. // find halt qTD
  558. Transfer_t *halt = (Transfer_t *)(pipe->qh.next);
  559. while (!(halt->qtd.token & 0x40)) halt = (Transfer_t *)(halt->qtd.next);
  560. // transfer's token
  561. uint32_t token = transfer->qtd.token;
  562. // transfer becomes new halt qTD
  563. transfer->qtd.token = 0x40;
  564. // copy transfer non-token fields to halt
  565. halt->qtd.next = transfer->qtd.next;
  566. halt->qtd.alt_next = transfer->qtd.alt_next;
  567. halt->qtd.buffer[0] = transfer->qtd.buffer[0]; // TODO: optimize...
  568. halt->qtd.buffer[1] = transfer->qtd.buffer[1];
  569. halt->qtd.buffer[2] = transfer->qtd.buffer[2];
  570. halt->qtd.buffer[3] = transfer->qtd.buffer[3];
  571. halt->qtd.buffer[4] = transfer->qtd.buffer[4];
  572. halt->pipe = pipe;
  573. // find the last qTD we're adding
  574. Transfer_t *last = halt;
  575. while ((uint32_t)(last->qtd.next) != 1) last = (Transfer_t *)(last->qtd.next);
  576. // last points to transfer (which becomes new halt)
  577. last->qtd.next = (uint32_t)transfer;
  578. transfer->qtd.next = 1;
  579. // link all the new qTD by next_followup & prev_followup
  580. Transfer_t *prev = NULL;
  581. Transfer_t *p = halt;
  582. while (p->qtd.next != (uint32_t)transfer) {
  583. Transfer_t *next = (Transfer_t *)p->qtd.next;
  584. p->prev_followup = prev;
  585. p->next_followup = next;
  586. prev = p;
  587. p = next;
  588. }
  589. p->prev_followup = prev;
  590. p->next_followup = NULL;
  591. print(halt, p);
  592. // add them to a followup list
  593. if (pipe->type == 0 || pipe->type == 2) {
  594. // control or bulk
  595. add_to_async_followup_list(halt, p);
  596. } else {
  597. // interrupt
  598. add_to_periodic_followup_list(halt, p);
  599. }
  600. // old halt becomes new transfer, this commits all new qTDs to QH
  601. halt->qtd.token = token;
  602. return true;
  603. }
  604. bool followup_Transfer(Transfer_t *transfer)
  605. {
  606. Serial.print(" Followup ");
  607. Serial.println((uint32_t)transfer, HEX);
  608. if (!(transfer->qtd.token & 0x80)) {
  609. // TODO: check error status
  610. if (transfer->qtd.token & 0x8000) {
  611. // this transfer caused an interrupt
  612. if (transfer->pipe->callback_function) {
  613. // do the callback
  614. (*(transfer->pipe->callback_function))(transfer);
  615. }
  616. }
  617. // do callback function...
  618. Serial.println(" completed");
  619. return true;
  620. }
  621. return false;
  622. }
  623. static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last)
  624. {
  625. last->next_followup = NULL; // always add to end of list
  626. if (async_followup_last == NULL) {
  627. first->prev_followup = NULL;
  628. async_followup_first = first;
  629. } else {
  630. first->prev_followup = async_followup_last;
  631. async_followup_last->next_followup = first;
  632. }
  633. async_followup_last = last;
  634. }
  635. static void remove_from_async_followup_list(Transfer_t *transfer)
  636. {
  637. Transfer_t *next = transfer->next_followup;
  638. Transfer_t *prev = transfer->prev_followup;
  639. if (prev) {
  640. prev->next_followup = next;
  641. } else {
  642. async_followup_first = next;
  643. }
  644. if (next) {
  645. next->prev_followup = prev;
  646. } else {
  647. async_followup_last = prev;
  648. }
  649. }
  650. static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last)
  651. {
  652. last->next_followup = NULL; // always add to end of list
  653. if (periodic_followup_last == NULL) {
  654. first->prev_followup = NULL;
  655. periodic_followup_first = first;
  656. } else {
  657. first->prev_followup = periodic_followup_last;
  658. periodic_followup_last->next_followup = first;
  659. }
  660. periodic_followup_last = last;
  661. }
  662. static void remove_from_periodic_followup_list(Transfer_t *transfer)
  663. {
  664. Transfer_t *next = transfer->next_followup;
  665. Transfer_t *prev = transfer->prev_followup;
  666. if (prev) {
  667. prev->next_followup = next;
  668. } else {
  669. periodic_followup_first = next;
  670. }
  671. if (next) {
  672. next->prev_followup = prev;
  673. } else {
  674. periodic_followup_last = prev;
  675. }
  676. }
  677. void print(const Transfer_t *transfer)
  678. {
  679. if (!((uint32_t)transfer & 0xFFFFFFE0)) return;
  680. Serial.print("Transfer @ ");
  681. Serial.println(((uint32_t)transfer & 0xFFFFFFE0), HEX);
  682. Serial.print(" next: ");
  683. Serial.println(transfer->qtd.next, HEX);
  684. Serial.print(" anext: ");
  685. Serial.println(transfer->qtd.alt_next, HEX);
  686. Serial.print(" token: ");
  687. Serial.println(transfer->qtd.token, HEX);
  688. Serial.print(" bufs: ");
  689. for (int i=0; i < 5; i++) {
  690. Serial.print(transfer->qtd.buffer[i], HEX);
  691. if (i < 4) Serial.print(',');
  692. }
  693. Serial.println();
  694. }
  695. void print(const Transfer_t *first, const Transfer_t *last)
  696. {
  697. Serial.print("Transfer Followup List ");
  698. Serial.print((uint32_t)first, HEX);
  699. Serial.print(" to ");
  700. Serial.println((uint32_t)last, HEX);
  701. Serial.println(" forward:");
  702. while (first) {
  703. Serial.print(" ");
  704. Serial.print((uint32_t)first, HEX);
  705. print_token(first->qtd.token);
  706. first = first->next_followup;
  707. }
  708. Serial.println(" backward:");
  709. while (last) {
  710. Serial.print(" ");
  711. Serial.print((uint32_t)last, HEX);
  712. print_token(last->qtd.token);
  713. last = last->prev_followup;
  714. }
  715. }
  716. void print_token(uint32_t token)
  717. {
  718. switch ((token >> 8) & 3) {
  719. case 0:
  720. Serial.print(" OUT ");
  721. Serial.println((token >> 16) & 0x7FFF);
  722. break;
  723. case 1:
  724. Serial.print(" IN ");
  725. Serial.println((token >> 16) & 0x7FFF);
  726. break;
  727. case 2:
  728. Serial.println(" SETUP");
  729. break;
  730. default:
  731. Serial.println(" unknown");
  732. }
  733. }
  734. void print(const Pipe_t *pipe)
  735. {
  736. if (!((uint32_t)pipe & 0xFFFFFFE0)) return;
  737. Serial.print("Pipe ");
  738. if (pipe->type == 0) Serial.print("control");
  739. else if (pipe->type == 1) Serial.print("isochronous");
  740. else if (pipe->type == 2) Serial.print("bulk");
  741. else if (pipe->type == 3) Serial.print("interrupt");
  742. Serial.print(pipe->direction ? " IN" : " OUT");
  743. Serial.print(" @ ");
  744. Serial.println((uint32_t)pipe, HEX);
  745. Serial.print(" horiz link: ");
  746. Serial.println(pipe->qh.horizontal_link, HEX);
  747. Serial.print(" capabilities: ");
  748. Serial.print(pipe->qh.capabilities[0], HEX);
  749. Serial.print(',');
  750. Serial.println(pipe->qh.capabilities[1], HEX);
  751. Serial.println(" overlay:");
  752. Serial.print(" cur: ");
  753. Serial.println(pipe->qh.current, HEX);
  754. Serial.print(" next: ");
  755. Serial.println(pipe->qh.next, HEX);
  756. Serial.print(" anext: ");
  757. Serial.println(pipe->qh.alt_next, HEX);
  758. Serial.print(" token: ");
  759. Serial.println(pipe->qh.token, HEX);
  760. Serial.print(" bufs: ");
  761. for (int i=0; i < 5; i++) {
  762. Serial.print(pipe->qh.buffer[i], HEX);
  763. if (i < 4) Serial.print(',');
  764. }
  765. Serial.println();
  766. const Transfer_t *t = (Transfer_t *)pipe->qh.next;
  767. while (((uint32_t)t & 0xFFFFFFE0)) {
  768. print(t);
  769. t = (Transfer_t *)t->qtd.next;
  770. }
  771. //Serial.print();
  772. }
  773. void print_hexbytes(const void *ptr, uint32_t len)
  774. {
  775. if (ptr == NULL || len == 0) return;
  776. const uint8_t *p = (const uint8_t *)ptr;
  777. do {
  778. if (*p < 16) Serial.print('0');
  779. Serial.print(*p++, HEX);
  780. Serial.print(' ');
  781. } while (--len);
  782. Serial.println();
  783. }
  784. void print(const char *s)
  785. {
  786. Serial.println(s);
  787. delay(10);
  788. }
  789. void print(const char *s, int num)
  790. {
  791. Serial.print(s);
  792. Serial.println(num);
  793. delay(10);
  794. }
  795. // Memory allocation
  796. static Device_t memory_Device[3];
  797. static Pipe_t memory_Pipe[6] __attribute__ ((aligned(64)));
  798. static Transfer_t memory_Transfer[24] __attribute__ ((aligned(64)));
  799. Device_t * free_Device_list = NULL;
  800. Pipe_t * free_Pipe_list = NULL;
  801. Transfer_t * free_Transfer_list = NULL;
  802. void init_Device_Pipe_Transfer_memory(void)
  803. {
  804. Device_t *end_device = memory_Device + sizeof(memory_Device)/sizeof(Device_t);
  805. for (Device_t *device = memory_Device; device < end_device; device++) {
  806. free_Device(device);
  807. }
  808. Pipe_t *end_pipe = memory_Pipe + sizeof(memory_Pipe)/sizeof(Pipe_t);
  809. for (Pipe_t *pipe = memory_Pipe; pipe < end_pipe; pipe++) {
  810. free_Pipe(pipe);
  811. }
  812. Transfer_t *end_transfer = memory_Transfer + sizeof(memory_Transfer)/sizeof(Transfer_t);
  813. for (Transfer_t *transfer = memory_Transfer; transfer < end_transfer; transfer++) {
  814. free_Transfer(transfer);
  815. }
  816. }
  817. Device_t * allocate_Device(void)
  818. {
  819. Device_t *device = free_Device_list;
  820. if (device) free_Device_list = *(Device_t **)device;
  821. return device;
  822. }
  823. void free_Device(Device_t *device)
  824. {
  825. *(Device_t **)device = free_Device_list;
  826. free_Device_list = device;
  827. }
  828. Pipe_t * allocate_Pipe(void)
  829. {
  830. Pipe_t *pipe = free_Pipe_list;
  831. if (pipe) free_Pipe_list = *(Pipe_t **)pipe;
  832. return pipe;
  833. }
  834. void free_Pipe(Pipe_t *pipe)
  835. {
  836. *(Pipe_t **)pipe = free_Pipe_list;
  837. free_Pipe_list = pipe;
  838. }
  839. Transfer_t * allocate_Transfer(void)
  840. {
  841. Transfer_t *transfer = free_Transfer_list;
  842. if (transfer) free_Transfer_list = *(Transfer_t **)transfer;
  843. return transfer;
  844. }
  845. void free_Transfer(Transfer_t *transfer)
  846. {
  847. *(Transfer_t **)transfer = free_Transfer_list;
  848. free_Transfer_list = transfer;
  849. }