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

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