Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

592 lines
20KB

  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 <Arduino.h>
  24. #include "USBHost.h"
  25. static uint32_t periodictable[32] __attribute__ ((aligned(4096), used));
  26. static uint8_t port_state;
  27. #define PORT_STATE_DISCONNECTED 0
  28. #define PORT_STATE_DEBOUNCE 1
  29. #define PORT_STATE_RESET 2
  30. #define PORT_STATE_RECOVERY 3
  31. #define PORT_STATE_ACTIVE 4
  32. static Device_t *rootdev=NULL;
  33. static Transfer_t *async_followup_first=NULL;
  34. static Transfer_t *async_followup_last=NULL;
  35. static Transfer_t *periodic_followup_first=NULL;
  36. static Transfer_t *periodic_followup_last=NULL;
  37. static void isr();
  38. static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  39. uint32_t pid, uint32_t data01, bool irq);
  40. static bool followup_Transfer(Transfer_t *transfer);
  41. static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last);
  42. static void remove_from_async_followup_list(Transfer_t *transfer);
  43. static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last);
  44. static void remove_from_periodic_followup_list(Transfer_t *transfer);
  45. void USBHost::begin()
  46. {
  47. // Teensy 3.6 has USB host power controlled by PTE6
  48. PORTE_PCR6 = PORT_PCR_MUX(1);
  49. GPIOE_PDDR |= (1<<6);
  50. GPIOE_PSOR = (1<<6); // turn on USB host power
  51. Serial.print("sizeof Device = ");
  52. Serial.println(sizeof(Device_t));
  53. Serial.print("sizeof Pipe = ");
  54. Serial.println(sizeof(Pipe_t));
  55. Serial.print("sizeof Transfer = ");
  56. Serial.println(sizeof(Transfer_t));
  57. // configure the MPU to allow USBHS DMA to access memory
  58. MPU_RGDAAC0 |= 0x30000000;
  59. Serial.print("MPU_RGDAAC0 = ");
  60. Serial.println(MPU_RGDAAC0, HEX);
  61. // turn on clocks
  62. MCG_C1 |= MCG_C1_IRCLKEN; // enable MCGIRCLK 32kHz
  63. OSC0_CR |= OSC_ERCLKEN;
  64. SIM_SOPT2 |= SIM_SOPT2_USBREGEN; // turn on USB regulator
  65. SIM_SOPT2 &= ~SIM_SOPT2_USBSLSRC; // use IRC for slow clock
  66. print("power up USBHS PHY");
  67. SIM_USBPHYCTL |= SIM_USBPHYCTL_USBDISILIM; // disable USB current limit
  68. //SIM_USBPHYCTL = SIM_USBPHYCTL_USBDISILIM | SIM_USBPHYCTL_USB3VOUTTRG(6); // pg 237
  69. SIM_SCGC3 |= SIM_SCGC3_USBHSDCD | SIM_SCGC3_USBHSPHY | SIM_SCGC3_USBHS;
  70. USBHSDCD_CLOCK = 33 << 2;
  71. print("init USBHS PHY & PLL");
  72. // init process: page 1681-1682
  73. USBPHY_CTRL_CLR = (USBPHY_CTRL_SFTRST | USBPHY_CTRL_CLKGATE); // // CTRL pg 1698
  74. USBPHY_CTRL_SET = USBPHY_CTRL_ENUTMILEVEL2 | USBPHY_CTRL_ENUTMILEVEL3;
  75. //USBPHY_CTRL_SET = USBPHY_CTRL_FSDLL_RST_EN; // TODO: what does this do??
  76. USBPHY_TRIM_OVERRIDE_EN_SET = 1;
  77. USBPHY_PLL_SIC = USBPHY_PLL_SIC_PLL_POWER | USBPHY_PLL_SIC_PLL_ENABLE |
  78. USBPHY_PLL_SIC_PLL_DIV_SEL(1) | USBPHY_PLL_SIC_PLL_EN_USB_CLKS;
  79. // wait for the PLL to lock
  80. int count=0;
  81. while ((USBPHY_PLL_SIC & USBPHY_PLL_SIC_PLL_LOCK) == 0) {
  82. count++;
  83. }
  84. Serial.print("PLL locked, waited ");
  85. Serial.println(count);
  86. // turn on power to PHY
  87. USBPHY_PWD = 0;
  88. delay(10);
  89. // sanity check, connect 470K pullup & 100K pulldown and watch D+ voltage change
  90. //USBPHY_ANACTRL_CLR = (1<<10); // turn off both 15K pulldowns... works! :)
  91. // sanity check, output clocks on pin 9 for testing
  92. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(3); // LPO 1kHz
  93. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(2); // Flash
  94. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(6); // XTAL
  95. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(7); // IRC 48MHz
  96. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(4); // MCGIRCLK
  97. //CORE_PIN9_CONFIG = PORT_PCR_MUX(5); // CLKOUT on PTC3 Alt5 (Arduino pin 9)
  98. // now with the PHY up and running, start up USBHS
  99. print("begin ehci reset");
  100. USBHS_USBCMD |= USBHS_USBCMD_RST;
  101. count = 0;
  102. while (USBHS_USBCMD & USBHS_USBCMD_RST) {
  103. count++;
  104. }
  105. print(" reset waited ", count);
  106. init_Device_Pipe_Transfer_memory();
  107. for (int i=0; i < 32; i++) {
  108. periodictable[i] = 1;
  109. }
  110. port_state = PORT_STATE_DISCONNECTED;
  111. USBHS_USB_SBUSCFG = 1; // System Bus Interface Configuration
  112. // turn on the USBHS controller
  113. //USBHS_USBMODE = USBHS_USBMODE_TXHSD(5) | USBHS_USBMODE_CM(3); // host mode
  114. USBHS_USBMODE = USBHS_USBMODE_CM(3); // host mode
  115. USBHS_USBINTR = 0;
  116. USBHS_PERIODICLISTBASE = (uint32_t)periodictable;
  117. USBHS_FRINDEX = 0;
  118. USBHS_ASYNCLISTADDR = 0;
  119. USBHS_USBCMD = USBHS_USBCMD_ITC(8) | USBHS_USBCMD_RS |
  120. USBHS_USBCMD_ASP(3) | USBHS_USBCMD_ASPE |
  121. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(1); // periodic table is 32 pointers
  122. // turn on the USB port
  123. //USBHS_PORTSC1 = USBHS_PORTSC_PP;
  124. USBHS_PORTSC1 |= USBHS_PORTSC_PP;
  125. //USBHS_PORTSC1 |= USBHS_PORTSC_PFSC; // force 12 Mbit/sec
  126. //USBHS_PORTSC1 |= USBHS_PORTSC_PHCD; // phy off
  127. Serial.print("USBHS_ASYNCLISTADDR = ");
  128. Serial.println(USBHS_ASYNCLISTADDR, HEX);
  129. Serial.print("USBHS_PERIODICLISTBASE = ");
  130. Serial.println(USBHS_PERIODICLISTBASE, HEX);
  131. Serial.print("periodictable = ");
  132. Serial.println((uint32_t)periodictable, HEX);
  133. // enable interrupts, after this point interruts to all the work
  134. attachInterruptVector(IRQ_USBHS, isr);
  135. NVIC_ENABLE_IRQ(IRQ_USBHS);
  136. USBHS_USBINTR = USBHS_USBINTR_PCE | USBHS_USBINTR_TIE0;
  137. USBHS_USBINTR |= USBHS_USBINTR_UEE | USBHS_USBINTR_SEE;
  138. USBHS_USBINTR |= USBHS_USBINTR_AAE;
  139. USBHS_USBINTR |= USBHS_USBINTR_UPIE | USBHS_USBINTR_UAIE;
  140. }
  141. // EHCI registers page default
  142. // -------------- ---- -------
  143. // USBHS_USBCMD 1599 00080000 USB Command
  144. // USBHS_USBSTS 1602 00000000 USB Status
  145. // USBHS_USBINTR 1606 00000000 USB Interrupt Enable
  146. // USBHS_FRINDEX 1609 00000000 Frame Index Register
  147. // USBHS_PERIODICLISTBASE 1610 undefine Periodic Frame List Base Address
  148. // USBHS_ASYNCLISTADDR 1612 undefine Asynchronous List Address
  149. // USBHS_PORTSC1 1619 00002000 Port Status and Control
  150. // USBHS_USBMODE 1629 00005000 USB Mode
  151. // USBHS_GPTIMERnCTL 1591 00000000 General Purpose Timer n Control
  152. // PORT_STATE_DISCONNECTED 0
  153. // PORT_STATE_DEBOUNCE 1
  154. // PORT_STATE_RESET 2
  155. // PORT_STATE_RECOVERY 3
  156. // PORT_STATE_ACTIVE 4
  157. void USBHost::isr()
  158. {
  159. uint32_t stat = USBHS_USBSTS;
  160. USBHS_USBSTS = stat; // clear pending interrupts
  161. //stat &= USBHS_USBINTR; // mask away unwanted interrupts
  162. Serial.println();
  163. Serial.print("ISR: ");
  164. Serial.print(stat, HEX);
  165. Serial.println();
  166. if (stat & USBHS_USBSTS_UI) Serial.println(" USB Interrupt");
  167. if (stat & USBHS_USBSTS_UEI) Serial.println(" USB Error");
  168. if (stat & USBHS_USBSTS_PCI) Serial.println(" Port Change");
  169. if (stat & USBHS_USBSTS_FRI) Serial.println(" Frame List Rollover");
  170. if (stat & USBHS_USBSTS_SEI) Serial.println(" System Error");
  171. if (stat & USBHS_USBSTS_AAI) Serial.println(" Async Advance (doorbell)");
  172. if (stat & USBHS_USBSTS_URI) Serial.println(" Reset Recv");
  173. if (stat & USBHS_USBSTS_SRI) Serial.println(" SOF");
  174. if (stat & USBHS_USBSTS_SLI) Serial.println(" Suspend");
  175. if (stat & USBHS_USBSTS_HCH) Serial.println(" Host Halted");
  176. if (stat & USBHS_USBSTS_RCL) Serial.println(" Reclamation");
  177. if (stat & USBHS_USBSTS_PS) Serial.println(" Periodic Sched En");
  178. if (stat & USBHS_USBSTS_AS) Serial.println(" Async Sched En");
  179. if (stat & USBHS_USBSTS_NAKI) Serial.println(" NAK");
  180. if (stat & USBHS_USBSTS_UAI) Serial.println(" USB Async");
  181. if (stat & USBHS_USBSTS_UPI) Serial.println(" USB Periodic");
  182. if (stat & USBHS_USBSTS_TI0) Serial.println(" Timer0");
  183. if (stat & USBHS_USBSTS_TI1) Serial.println(" Timer1");
  184. if (stat & USBHS_USBSTS_UAI) { // completed qTD(s) from the async schedule
  185. Serial.println("Async Followup");
  186. print(async_followup_first, async_followup_last);
  187. Transfer_t *p = async_followup_first;
  188. while (p) {
  189. if (followup_Transfer(p)) {
  190. // transfer completed
  191. Transfer_t *next = p->next_followup;
  192. remove_from_async_followup_list(p);
  193. free_Transfer(p);
  194. p = next;
  195. } else {
  196. // transfer still pending
  197. p = p->next_followup;
  198. }
  199. }
  200. print(async_followup_first, async_followup_last);
  201. }
  202. if (stat & USBHS_USBSTS_UPI) { // completed qTD(s) from the periodic schedule
  203. Serial.println("Periodic Followup");
  204. Transfer_t *p = periodic_followup_first;
  205. while (p) {
  206. if (followup_Transfer(p)) {
  207. // transfer completed
  208. Transfer_t *next = p->next_followup;
  209. remove_from_periodic_followup_list(p);
  210. free_Transfer(p);
  211. p = next;
  212. } else {
  213. // transfer still pending
  214. p = p->next_followup;
  215. }
  216. }
  217. }
  218. if (stat & USBHS_USBSTS_PCI) { // port change detected
  219. const uint32_t portstat = USBHS_PORTSC1;
  220. Serial.print("port change: ");
  221. Serial.print(portstat, HEX);
  222. Serial.println();
  223. USBHS_PORTSC1 = portstat | (USBHS_PORTSC_OCC|USBHS_PORTSC_PEC|USBHS_PORTSC_CSC);
  224. if (portstat & USBHS_PORTSC_OCC) {
  225. Serial.println(" overcurrent change");
  226. }
  227. if (portstat & USBHS_PORTSC_CSC) {
  228. if (portstat & USBHS_PORTSC_CCS) {
  229. Serial.println(" connect");
  230. if (port_state == PORT_STATE_DISCONNECTED
  231. || port_state == PORT_STATE_DEBOUNCE) {
  232. // 100 ms debounce (USB 2.0: TATTDB, page 150 & 188)
  233. port_state = PORT_STATE_DEBOUNCE;
  234. USBHS_GPTIMER0LD = 100000; // microseconds
  235. USBHS_GPTIMER0CTL =
  236. USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  237. stat &= ~USBHS_USBSTS_TI0;
  238. }
  239. } else {
  240. Serial.println(" disconnect");
  241. port_state = PORT_STATE_DISCONNECTED;
  242. USBPHY_CTRL_CLR = USBPHY_CTRL_ENHOSTDISCONDETECT;
  243. // TODO: delete & clean up device state...
  244. }
  245. }
  246. if (portstat & USBHS_PORTSC_PEC) {
  247. // PEC bit only detects disable
  248. Serial.println(" disable");
  249. } else if (port_state == PORT_STATE_RESET && portstat & USBHS_PORTSC_PE) {
  250. Serial.println(" port enabled");
  251. port_state = PORT_STATE_RECOVERY;
  252. // 10 ms reset recover (USB 2.0: TRSTRCY, page 151 & 188)
  253. USBHS_GPTIMER0LD = 10000; // microseconds
  254. USBHS_GPTIMER0CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  255. if (USBHS_PORTSC1 & USBHS_PORTSC_HSP) {
  256. // turn on high-speed disconnect detector
  257. USBPHY_CTRL_SET = USBPHY_CTRL_ENHOSTDISCONDETECT;
  258. }
  259. }
  260. if (portstat & USBHS_PORTSC_FPR) {
  261. Serial.println(" force resume");
  262. }
  263. }
  264. if (stat & USBHS_USBSTS_TI0) { // timer 0
  265. Serial.println("timer");
  266. if (port_state == PORT_STATE_DEBOUNCE) {
  267. port_state = PORT_STATE_RESET;
  268. USBHS_PORTSC1 |= USBHS_PORTSC_PR; // begin reset sequence
  269. Serial.println(" begin reset");
  270. } else if (port_state == PORT_STATE_RECOVERY) {
  271. port_state = PORT_STATE_ACTIVE;
  272. Serial.println(" end recovery");
  273. // HCSPARAMS TTCTRL page 1671
  274. uint32_t speed = (USBHS_PORTSC1 >> 26) & 3;
  275. rootdev = new_Device(speed, 0, 0);
  276. }
  277. }
  278. }
  279. static uint32_t QH_capabilities1(uint32_t nak_count_reload, uint32_t control_endpoint_flag,
  280. uint32_t max_packet_length, uint32_t head_of_list, uint32_t data_toggle_control,
  281. uint32_t speed, uint32_t endpoint_number, uint32_t inactivate, uint32_t address)
  282. {
  283. return ( (nak_count_reload << 28) | (control_endpoint_flag << 27) |
  284. (max_packet_length << 16) | (head_of_list << 15) |
  285. (data_toggle_control << 14) | (speed << 12) | (endpoint_number << 8) |
  286. (inactivate << 7) | (address << 0) );
  287. }
  288. static uint32_t QH_capabilities2(uint32_t high_bw_mult, uint32_t hub_port_number,
  289. uint32_t hub_address, uint32_t split_completion_mask, uint32_t interrupt_schedule_mask)
  290. {
  291. return ( (high_bw_mult << 30) | (hub_port_number << 23) | (hub_address << 16) |
  292. (split_completion_mask << 8) | (interrupt_schedule_mask << 0) );
  293. }
  294. // Create a new pipe. It's QH is added to the async or periodic schedule,
  295. // and a halt qTD is added to the QH, so we can grow the qTD list later.
  296. //
  297. Pipe_t * USBHost::new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
  298. uint32_t direction, uint32_t max_packet_len)
  299. {
  300. Pipe_t *pipe;
  301. Transfer_t *halt;
  302. uint32_t c=0, dtc=0;
  303. Serial.println("new_Pipe");
  304. pipe = allocate_Pipe();
  305. if (!pipe) return NULL;
  306. halt = allocate_Transfer();
  307. if (!halt) {
  308. free_Pipe(pipe);
  309. return NULL;
  310. }
  311. memset(pipe, 0, sizeof(Pipe_t));
  312. memset(halt, 0, sizeof(Transfer_t));
  313. halt->qtd.next = 1;
  314. halt->qtd.token = 0x40;
  315. pipe->device = dev;
  316. pipe->qh.next = (uint32_t)halt;
  317. pipe->qh.alt_next = 1;
  318. pipe->direction = direction;
  319. pipe->type = type;
  320. if (type == 0) {
  321. // control
  322. if (dev->speed < 2) c = 1;
  323. dtc = 1;
  324. } else if (type == 2) {
  325. // bulk
  326. } else if (type == 3) {
  327. // interrupt
  328. }
  329. pipe->qh.capabilities[0] = QH_capabilities1(15, c, max_packet_len, 0,
  330. dtc, dev->speed, endpoint, 0, dev->address);
  331. pipe->qh.capabilities[1] = QH_capabilities2(1, dev->hub_port,
  332. dev->hub_address, 0, 0);
  333. if (type == 0 || type == 2) {
  334. // control or bulk: add to async queue
  335. Pipe_t *list = (Pipe_t *)USBHS_ASYNCLISTADDR;
  336. if (list == NULL) {
  337. pipe->qh.capabilities[0] |= 0x8000; // H bit
  338. pipe->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2; // 2=QH
  339. USBHS_ASYNCLISTADDR = (uint32_t)&(pipe->qh);
  340. USBHS_USBCMD |= USBHS_USBCMD_ASE; // enable async schedule
  341. Serial.println(" first in async list");
  342. } else {
  343. // EHCI 1.0: section 4.8.1, page 72
  344. pipe->qh.horizontal_link = list->qh.horizontal_link;
  345. list->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2;
  346. Serial.println(" added to async list");
  347. }
  348. } else if (type == 3) {
  349. // interrupt: add to periodic schedule
  350. // TODO: link it into the periodic table
  351. }
  352. return pipe;
  353. }
  354. // Fill in the qTD fields (token & data)
  355. // t the Transfer qTD to initialize
  356. // buf data to transfer
  357. // len length of data
  358. // pid type of packet: 0=OUT, 1=IN, 2=SETUP
  359. // data01 value of DATA0/DATA1 toggle on 1st packet
  360. // irq whether to generate an interrupt when transfer complete
  361. //
  362. static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  363. uint32_t pid, uint32_t data01, bool irq)
  364. {
  365. t->qtd.alt_next = 1; // 1=terminate
  366. if (data01) data01 = 0x80000000;
  367. t->qtd.token = data01 | (len << 16) | (irq ? 0x8000 : 0) | (pid << 8) | 0x80;
  368. uint32_t addr = (uint32_t)buf;
  369. t->qtd.buffer[0] = addr;
  370. addr &= 0xFFFFF000;
  371. t->qtd.buffer[1] = addr + 0x1000;
  372. t->qtd.buffer[2] = addr + 0x2000;
  373. t->qtd.buffer[3] = addr + 0x3000;
  374. t->qtd.buffer[4] = addr + 0x4000;
  375. }
  376. // Create a Transfer and queue it
  377. //
  378. bool USBHost::new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len)
  379. {
  380. Serial.println("new_Transfer");
  381. Transfer_t *transfer = allocate_Transfer();
  382. if (!transfer) return false;
  383. if (pipe->type == 0) {
  384. // control transfer
  385. Transfer_t *data, *status;
  386. uint32_t status_direction;
  387. if (len > 16384) {
  388. // hopefully we never need more
  389. // than 16K in a control transfer
  390. free_Transfer(transfer);
  391. return false;
  392. }
  393. status = allocate_Transfer();
  394. if (!status) {
  395. free_Transfer(transfer);
  396. return false;
  397. }
  398. if (len > 0) {
  399. data = allocate_Transfer();
  400. if (!data) {
  401. free_Transfer(transfer);
  402. free_Transfer(status);
  403. return false;
  404. }
  405. init_qTD(data, buffer, len, pipe->direction, 1, false);
  406. transfer->qtd.next = (uint32_t)data;
  407. data->qtd.next = (uint32_t)status;
  408. status_direction = pipe->direction ^ 1;
  409. } else {
  410. transfer->qtd.next = (uint32_t)status;
  411. status_direction = 1; // always IN, USB 2.0 page 226
  412. }
  413. Serial.print("setup address ");
  414. Serial.println((uint32_t)&pipe->device->setup, HEX);
  415. init_qTD(transfer, &pipe->device->setup, 8, 2, 0, false);
  416. init_qTD(status, NULL, 0, status_direction, 1, true);
  417. status->pipe = pipe;
  418. status->buffer = buffer;
  419. status->length = len;
  420. status->qtd.next = 1;
  421. } else {
  422. // bulk, interrupt or isochronous transfer
  423. free_Transfer(transfer);
  424. return false;
  425. }
  426. // find halt qTD
  427. Transfer_t *halt = (Transfer_t *)(pipe->qh.next);
  428. while (!(halt->qtd.token & 0x40)) halt = (Transfer_t *)(halt->qtd.next);
  429. // transfer's token
  430. uint32_t token = transfer->qtd.token;
  431. // transfer becomes new halt qTD
  432. transfer->qtd.token = 0x40;
  433. // copy transfer non-token fields to halt
  434. halt->qtd.next = transfer->qtd.next;
  435. halt->qtd.alt_next = transfer->qtd.alt_next;
  436. halt->qtd.buffer[0] = transfer->qtd.buffer[0]; // TODO: optimize...
  437. halt->qtd.buffer[1] = transfer->qtd.buffer[1];
  438. halt->qtd.buffer[2] = transfer->qtd.buffer[2];
  439. halt->qtd.buffer[3] = transfer->qtd.buffer[3];
  440. halt->qtd.buffer[4] = transfer->qtd.buffer[4];
  441. halt->pipe = pipe;
  442. // find the last qTD we're adding
  443. Transfer_t *last = halt;
  444. while ((uint32_t)(last->qtd.next) != 1) last = (Transfer_t *)(last->qtd.next);
  445. // last points to transfer (which becomes new halt)
  446. last->qtd.next = (uint32_t)transfer;
  447. transfer->qtd.next = 1;
  448. // link all the new qTD by next_followup & prev_followup
  449. Transfer_t *prev = NULL;
  450. Transfer_t *p = halt;
  451. while (p->qtd.next != (uint32_t)transfer) {
  452. Transfer_t *next = (Transfer_t *)p->qtd.next;
  453. p->prev_followup = prev;
  454. p->next_followup = next;
  455. prev = p;
  456. p = next;
  457. }
  458. p->prev_followup = prev;
  459. p->next_followup = NULL;
  460. print(halt, p);
  461. // add them to a followup list
  462. if (pipe->type == 0 || pipe->type == 2) {
  463. // control or bulk
  464. add_to_async_followup_list(halt, p);
  465. } else {
  466. // interrupt
  467. add_to_periodic_followup_list(halt, p);
  468. }
  469. // old halt becomes new transfer, this commits all new qTDs to QH
  470. halt->qtd.token = token;
  471. return true;
  472. }
  473. static bool followup_Transfer(Transfer_t *transfer)
  474. {
  475. Serial.print(" Followup ");
  476. Serial.println((uint32_t)transfer, HEX);
  477. if (!(transfer->qtd.token & 0x80)) {
  478. // TODO: check error status
  479. if (transfer->qtd.token & 0x8000) {
  480. // this transfer caused an interrupt
  481. if (transfer->pipe->callback_function) {
  482. // do the callback
  483. (*(transfer->pipe->callback_function))(transfer);
  484. }
  485. }
  486. // do callback function...
  487. Serial.println(" completed");
  488. return true;
  489. }
  490. return false;
  491. }
  492. static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last)
  493. {
  494. last->next_followup = NULL; // always add to end of list
  495. if (async_followup_last == NULL) {
  496. first->prev_followup = NULL;
  497. async_followup_first = first;
  498. } else {
  499. first->prev_followup = async_followup_last;
  500. async_followup_last->next_followup = first;
  501. }
  502. async_followup_last = last;
  503. }
  504. static void remove_from_async_followup_list(Transfer_t *transfer)
  505. {
  506. Transfer_t *next = transfer->next_followup;
  507. Transfer_t *prev = transfer->prev_followup;
  508. if (prev) {
  509. prev->next_followup = next;
  510. } else {
  511. async_followup_first = next;
  512. }
  513. if (next) {
  514. next->prev_followup = prev;
  515. } else {
  516. async_followup_last = prev;
  517. }
  518. }
  519. static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last)
  520. {
  521. last->next_followup = NULL; // always add to end of list
  522. if (periodic_followup_last == NULL) {
  523. first->prev_followup = NULL;
  524. periodic_followup_first = first;
  525. } else {
  526. first->prev_followup = periodic_followup_last;
  527. periodic_followup_last->next_followup = first;
  528. }
  529. periodic_followup_last = last;
  530. }
  531. static void remove_from_periodic_followup_list(Transfer_t *transfer)
  532. {
  533. Transfer_t *next = transfer->next_followup;
  534. Transfer_t *prev = transfer->prev_followup;
  535. if (prev) {
  536. prev->next_followup = next;
  537. } else {
  538. periodic_followup_first = next;
  539. }
  540. if (next) {
  541. next->prev_followup = prev;
  542. } else {
  543. periodic_followup_last = prev;
  544. }
  545. }