Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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