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

689 行
21KB

  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. for (int i=0; i < 32; i++) {
  103. periodictable[i] = 1;
  104. }
  105. port_state = PORT_STATE_DISCONNECTED;
  106. USBHS_USB_SBUSCFG = 1; // System Bus Interface Configuration
  107. // turn on the USBHS controller
  108. //USBHS_USBMODE = USBHS_USBMODE_TXHSD(5) | USBHS_USBMODE_CM(3); // host mode
  109. USBHS_USBMODE = USBHS_USBMODE_CM(3); // host mode
  110. USBHS_USBINTR = 0;
  111. USBHS_PERIODICLISTBASE = (uint32_t)periodictable;
  112. USBHS_FRINDEX = 0;
  113. USBHS_ASYNCLISTADDR = 0;
  114. USBHS_USBCMD = USBHS_USBCMD_ITC(8) | USBHS_USBCMD_RS |
  115. USBHS_USBCMD_ASP(3) | USBHS_USBCMD_ASPE |
  116. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(1); // periodic table is 32 pointers
  117. // turn on the USB port
  118. //USBHS_PORTSC1 = USBHS_PORTSC_PP;
  119. USBHS_PORTSC1 |= USBHS_PORTSC_PP;
  120. //USBHS_PORTSC1 |= USBHS_PORTSC_PFSC; // force 12 Mbit/sec
  121. //USBHS_PORTSC1 |= USBHS_PORTSC_PHCD; // phy off
  122. Serial.print("USBHS_ASYNCLISTADDR = ");
  123. Serial.println(USBHS_ASYNCLISTADDR, HEX);
  124. Serial.print("USBHS_PERIODICLISTBASE = ");
  125. Serial.println(USBHS_PERIODICLISTBASE, HEX);
  126. Serial.print("periodictable = ");
  127. Serial.println((uint32_t)periodictable, HEX);
  128. // enable interrupts, after this point interruts to all the work
  129. NVIC_ENABLE_IRQ(IRQ_USBHS);
  130. USBHS_USBINTR = USBHS_USBINTR_PCE | USBHS_USBINTR_TIE0;
  131. USBHS_USBINTR |= USBHS_USBINTR_UEE | USBHS_USBINTR_SEE;
  132. USBHS_USBINTR |= USBHS_USBINTR_AAE;
  133. USBHS_USBINTR |= USBHS_USBINTR_UPIE | USBHS_USBINTR_UAIE;
  134. delay(25);
  135. Serial.println("Plug in device...");
  136. digitalWrite(32, HIGH); // connect device
  137. delay(5000);
  138. Serial.println();
  139. Serial.println("Ring Doorbell");
  140. USBHS_USBCMD |= USBHS_USBCMD_IAA;
  141. if (rootdev) print(rootdev->control_pipe);
  142. }
  143. void loop()
  144. {
  145. }
  146. void pulse(int usec)
  147. {
  148. // connect oscilloscope to see these pulses....
  149. digitalWriteFast(30, HIGH);
  150. delayMicroseconds(usec);
  151. digitalWriteFast(30, LOW);
  152. }
  153. // EHCI registers page default
  154. // -------------- ---- -------
  155. // USBHS_USBCMD 1599 00080000 USB Command
  156. // USBHS_USBSTS 1602 00000000 USB Status
  157. // USBHS_USBINTR 1606 00000000 USB Interrupt Enable
  158. // USBHS_FRINDEX 1609 00000000 Frame Index Register
  159. // USBHS_PERIODICLISTBASE 1610 undefine Periodic Frame List Base Address
  160. // USBHS_ASYNCLISTADDR 1612 undefine Asynchronous List Address
  161. // USBHS_PORTSC1 1619 00002000 Port Status and Control
  162. // USBHS_USBMODE 1629 00005000 USB Mode
  163. // USBHS_GPTIMERnCTL 1591 00000000 General Purpose Timer n Control
  164. // PORT_STATE_DISCONNECTED 0
  165. // PORT_STATE_DEBOUNCE 1
  166. // PORT_STATE_RESET 2
  167. // PORT_STATE_RECOVERY 3
  168. // PORT_STATE_ACTIVE 4
  169. void usbhs_isr(void)
  170. {
  171. uint32_t stat = USBHS_USBSTS;
  172. USBHS_USBSTS = stat; // clear pending interrupts
  173. //stat &= USBHS_USBINTR; // mask away unwanted interrupts
  174. Serial.println();
  175. Serial.print("ISR: ");
  176. Serial.print(stat, HEX);
  177. Serial.println();
  178. if (stat & USBHS_USBSTS_UI) Serial.println(" USB Interrupt");
  179. if (stat & USBHS_USBSTS_UEI) Serial.println(" USB Error");
  180. if (stat & USBHS_USBSTS_PCI) Serial.println(" Port Change");
  181. if (stat & USBHS_USBSTS_FRI) Serial.println(" Frame List Rollover");
  182. if (stat & USBHS_USBSTS_SEI) Serial.println(" System Error");
  183. if (stat & USBHS_USBSTS_AAI) Serial.println(" Async Advance (doorbell)");
  184. if (stat & USBHS_USBSTS_URI) Serial.println(" Reset Recv");
  185. if (stat & USBHS_USBSTS_SRI) Serial.println(" SOF");
  186. if (stat & USBHS_USBSTS_SLI) Serial.println(" Suspend");
  187. if (stat & USBHS_USBSTS_HCH) Serial.println(" Host Halted");
  188. if (stat & USBHS_USBSTS_RCL) Serial.println(" Reclamation");
  189. if (stat & USBHS_USBSTS_PS) Serial.println(" Periodic Sched En");
  190. if (stat & USBHS_USBSTS_AS) Serial.println(" Async Sched En");
  191. if (stat & USBHS_USBSTS_NAKI) Serial.println(" NAK");
  192. if (stat & USBHS_USBSTS_UAI) Serial.println(" USB Async");
  193. if (stat & USBHS_USBSTS_UPI) Serial.println(" USB Periodic");
  194. if (stat & USBHS_USBSTS_TI0) Serial.println(" Timer0");
  195. if (stat & USBHS_USBSTS_TI1) Serial.println(" Timer1");
  196. if (stat & USBHS_USBSTS_UAI) { // completed qTD(s) from the async schedule
  197. Serial.println("Async Followup");
  198. Transfer_t *p, *prev=NULL, *next;
  199. p = async_followup_first;
  200. while (p) {
  201. next = p->next_followup;
  202. if (followup_Transfer(p)) {
  203. // transfer completed
  204. if (prev) {
  205. prev->next_followup = next;
  206. } else {
  207. async_followup_first = next;
  208. }
  209. } else {
  210. // transfer still pending
  211. prev = p;
  212. }
  213. p = next;
  214. }
  215. async_followup_last = prev;
  216. }
  217. if (stat & USBHS_USBSTS_UPI) { // completed qTD(s) from the periodic schedule
  218. }
  219. if (stat & USBHS_USBSTS_PCI) { // port change detected
  220. const uint32_t portstat = USBHS_PORTSC1;
  221. Serial.print("port change: ");
  222. Serial.print(portstat, HEX);
  223. Serial.println();
  224. USBHS_PORTSC1 = portstat | (USBHS_PORTSC_OCC|USBHS_PORTSC_PEC|USBHS_PORTSC_CSC);
  225. if (portstat & USBHS_PORTSC_OCC) {
  226. Serial.println(" overcurrent change");
  227. }
  228. if (portstat & USBHS_PORTSC_CSC) {
  229. if (portstat & USBHS_PORTSC_CCS) {
  230. Serial.println(" connect");
  231. if (port_state == PORT_STATE_DISCONNECTED
  232. || port_state == PORT_STATE_DEBOUNCE) {
  233. // 100 ms debounce (USB 2.0: TATTDB, page 150 & 188)
  234. port_state = PORT_STATE_DEBOUNCE;
  235. USBHS_GPTIMER0LD = 100000; // microseconds
  236. USBHS_GPTIMER0CTL =
  237. USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  238. stat &= ~USBHS_USBSTS_TI0;
  239. }
  240. // TODO: should ENHOSTDISCONDETECT be set? K66 ref, page 1701
  241. } else {
  242. Serial.println(" disconnect");
  243. port_state = PORT_STATE_DISCONNECTED;
  244. // TODO: delete & clean up device state...
  245. }
  246. }
  247. if (portstat & USBHS_PORTSC_PEC) {
  248. // PEC bit only detects disable
  249. Serial.println(" disable");
  250. } else if (port_state == PORT_STATE_RESET && portstat & USBHS_PORTSC_PE) {
  251. Serial.println(" port enabled");
  252. port_state = PORT_STATE_RECOVERY;
  253. // 10 ms reset recover (USB 2.0: TRSTRCY, page 151 & 188)
  254. USBHS_GPTIMER0LD = 10000; // microseconds
  255. USBHS_GPTIMER0CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  256. }
  257. if (portstat & USBHS_PORTSC_FPR) {
  258. Serial.println(" force resume");
  259. }
  260. pulse(1);
  261. }
  262. if (stat & USBHS_USBSTS_TI0) { // timer 0
  263. Serial.println("timer");
  264. pulse(2);
  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. // Create a new device and begin the enumeration process
  279. //
  280. Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port)
  281. {
  282. Device_t *dev;
  283. Serial.print("new_Device: ");
  284. switch (speed) {
  285. case 0: Serial.print("12"); break;
  286. case 1: Serial.print("1.5"); break;
  287. case 2: Serial.print("480"); break;
  288. default: Serial.print("??");
  289. }
  290. Serial.println(" Mbit/sec");
  291. dev = allocate_Device();
  292. if (!dev) return NULL;
  293. dev->speed = speed;
  294. dev->address = 0;
  295. dev->hub_address = hub_addr;
  296. dev->hub_port = hub_port;
  297. dev->control_pipe = new_Pipe(dev, 0, 0, 0, 8);
  298. if (!dev->control_pipe) {
  299. free_Device(dev);
  300. return NULL;
  301. }
  302. static uint8_t buffer[8];
  303. dev->control_pipe->direction = 1; // 1=IN
  304. dev->setup.bmRequestType = 0x80;
  305. dev->setup.bRequest = 0x06; // 6=GET_DESCRIPTOR
  306. dev->setup.wValue = 0x0100;
  307. dev->setup.wIndex = 0x0000;
  308. dev->setup.wLength = 8;
  309. new_Transfer(dev->control_pipe, buffer, 8);
  310. return dev;
  311. }
  312. static uint32_t QH_capabilities1(uint32_t nak_count_reload, uint32_t control_endpoint_flag,
  313. uint32_t max_packet_length, uint32_t head_of_list, uint32_t data_toggle_control,
  314. uint32_t speed, uint32_t endpoint_number, uint32_t inactivate, uint32_t address)
  315. {
  316. return ( (nak_count_reload << 28) | (control_endpoint_flag << 27) |
  317. (max_packet_length << 16) | (head_of_list << 15) |
  318. (data_toggle_control << 14) | (speed << 12) | (endpoint_number << 8) |
  319. (inactivate << 7) | (address << 0) );
  320. }
  321. static uint32_t QH_capabilities2(uint32_t high_bw_mult, uint32_t hub_port_number,
  322. uint32_t hub_address, uint32_t split_completion_mask, uint32_t interrupt_schedule_mask)
  323. {
  324. return ( (high_bw_mult << 30) | (hub_port_number << 23) | (hub_address << 16) |
  325. (split_completion_mask << 8) | (interrupt_schedule_mask << 0) );
  326. }
  327. // Create a new pipe. It's QH is added to the async or periodic schedule,
  328. // and a halt qTD is added to the QH, so we can grow the qTD list later.
  329. //
  330. Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint, uint32_t direction,
  331. uint32_t max_packet_len)
  332. {
  333. Pipe_t *pipe;
  334. Transfer_t *halt;
  335. uint32_t c=0, dtc=0;
  336. Serial.println("new_Pipe");
  337. pipe = allocate_Pipe();
  338. if (!pipe) return NULL;
  339. halt = allocate_Transfer();
  340. if (!halt) {
  341. free_Pipe(pipe);
  342. return NULL;
  343. }
  344. memset(pipe, 0, sizeof(Pipe_t));
  345. memset(halt, 0, sizeof(Transfer_t));
  346. halt->qtd.next = 1;
  347. halt->qtd.token = 0x40;
  348. pipe->device = dev;
  349. pipe->qh.next = (uint32_t)halt;
  350. pipe->qh.alt_next = 1;
  351. pipe->direction = direction;
  352. pipe->type = type;
  353. if (type == 0) {
  354. // control
  355. if (dev->speed < 2) c = 1;
  356. dtc = 1;
  357. } else if (type == 2) {
  358. // bulk
  359. } else if (type == 3) {
  360. // interrupt
  361. }
  362. pipe->qh.capabilities[0] = QH_capabilities1(15, c, max_packet_len, 0,
  363. dtc, dev->speed, endpoint, 0, dev->address);
  364. pipe->qh.capabilities[1] = QH_capabilities2(1, dev->hub_port,
  365. dev->hub_address, 0, 0);
  366. if (type == 0 || type == 2) {
  367. // control or bulk: add to async queue
  368. Pipe_t *list = (Pipe_t *)USBHS_ASYNCLISTADDR;
  369. if (list == NULL) {
  370. pipe->qh.capabilities[0] |= 0x8000; // H bit
  371. pipe->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2; // 2=QH
  372. USBHS_ASYNCLISTADDR = (uint32_t)&(pipe->qh);
  373. USBHS_USBCMD |= USBHS_USBCMD_ASE; // enable async schedule
  374. Serial.println(" first in async list");
  375. } else {
  376. // EHCI 1.0: section 4.8.1, page 72
  377. pipe->qh.horizontal_link = list->qh.horizontal_link;
  378. list->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2;
  379. Serial.println(" added to async list");
  380. }
  381. } else if (type == 3) {
  382. // interrupt: add to periodic schedule
  383. // TODO: link it into the periodic table
  384. }
  385. return pipe;
  386. }
  387. // Fill in the qTD fields (token & data)
  388. // t the Transfer qTD to initialize
  389. // buf data to transfer
  390. // len length of data
  391. // pid type of packet: 0=OUT, 1=IN, 2=SETUP
  392. // data01 value of DATA0/DATA1 toggle on 1st packet
  393. // irq whether to generate an interrupt when transfer complete
  394. //
  395. void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  396. uint32_t pid, uint32_t data01, bool irq)
  397. {
  398. t->qtd.alt_next = 1; // 1=terminate
  399. if (data01) data01 = 0x80000000;
  400. t->qtd.token = data01 | (len << 16) | (irq ? 0x8000 : 0) | (pid << 8) | 0x80;
  401. uint32_t addr = (uint32_t)buf;
  402. t->qtd.buffer[0] = addr;
  403. addr &= 0xFFFFF000;
  404. t->qtd.buffer[1] = addr + 0x1000;
  405. t->qtd.buffer[2] = addr + 0x2000;
  406. t->qtd.buffer[3] = addr + 0x3000;
  407. t->qtd.buffer[4] = addr + 0x4000;
  408. }
  409. // Create a Transfer and queue it
  410. //
  411. bool new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len)
  412. {
  413. Serial.println("new_Transfer");
  414. Transfer_t *transfer = allocate_Transfer();
  415. if (!transfer) return false;
  416. transfer->pipe = pipe;
  417. if (pipe->type == 0) {
  418. // control transfer
  419. Transfer_t *data, *status;
  420. uint32_t status_direction;
  421. if (len > 16384) {
  422. free_Transfer(transfer);
  423. return false;
  424. }
  425. status = allocate_Transfer();
  426. if (!status) {
  427. free_Transfer(transfer);
  428. return false;
  429. }
  430. if (len > 0) {
  431. data = allocate_Transfer();
  432. if (!data) {
  433. free_Transfer(transfer);
  434. free_Transfer(status);
  435. return false;
  436. }
  437. init_qTD(data, buffer, len, pipe->direction, 1, false);
  438. transfer->qtd.next = (uint32_t)data;
  439. data->qtd.next = (uint32_t)status;
  440. data->pipe = pipe;
  441. status_direction = pipe->direction ^ 1;
  442. } else {
  443. transfer->qtd.next = (uint32_t)status;
  444. status_direction = 1; // always IN, USB 2.0 page 226
  445. }
  446. Serial.print("setup address ");
  447. Serial.println((uint32_t)&pipe->device->setup, HEX);
  448. init_qTD(transfer, &pipe->device->setup, 8, 2, 0, false);
  449. init_qTD(status, NULL, 0, status_direction, 1, true);
  450. status->pipe = pipe;
  451. status->qtd.next = 1;
  452. } else {
  453. // bulk, interrupt or isochronous transfer
  454. free_Transfer(transfer);
  455. return false;
  456. }
  457. // find halt qTD
  458. Transfer_t *halt = (Transfer_t *)(pipe->qh.next);
  459. while (!(halt->qtd.token & 0x40)) halt = (Transfer_t *)(halt->qtd.next);
  460. // transfer's token
  461. uint32_t token = transfer->qtd.token;
  462. // transfer becomes new halt qTD
  463. transfer->qtd.token = 0x40;
  464. // copy transfer non-token fields to halt
  465. halt->qtd.next = transfer->qtd.next;
  466. halt->qtd.alt_next = transfer->qtd.alt_next;
  467. halt->qtd.buffer[0] = transfer->qtd.buffer[0];
  468. halt->qtd.buffer[1] = transfer->qtd.buffer[1];
  469. halt->qtd.buffer[2] = transfer->qtd.buffer[2];
  470. halt->qtd.buffer[3] = transfer->qtd.buffer[3];
  471. halt->qtd.buffer[4] = transfer->qtd.buffer[4];
  472. halt->pipe = pipe;
  473. // find the last qTD we're adding
  474. Transfer_t *last = halt;
  475. while ((uint32_t)(last->qtd.next) != 1) last = (Transfer_t *)(last->qtd.next);
  476. // last points to transfer (which becomes new halt)
  477. last->qtd.next = (uint32_t)transfer;
  478. transfer->qtd.next = 1;
  479. // link all the new qTD by next_followup
  480. Transfer_t *p = halt;
  481. while (p->qtd.next != (uint32_t)transfer) {
  482. Transfer_t *n = (Transfer_t *)p->qtd.next;
  483. p->next_followup = n;
  484. p = n;
  485. }
  486. p->next_followup = NULL;
  487. // add them to a followup list
  488. if (pipe->type == 0 || pipe->type == 2) {
  489. // control or bulk
  490. if (async_followup_first == NULL) {
  491. async_followup_first = halt;
  492. } else {
  493. async_followup_last->next_followup = halt;
  494. }
  495. async_followup_last = p;
  496. } else {
  497. // interrupt
  498. if (periodic_followup_first == NULL) {
  499. periodic_followup_first = halt;
  500. } else {
  501. periodic_followup_last->next_followup = halt;
  502. }
  503. periodic_followup_last = p;
  504. }
  505. // old halt becomes new transfer, this commits all new qTDs to QH
  506. halt->qtd.token = token;
  507. return true;
  508. }
  509. bool followup_Transfer(Transfer_t *transfer)
  510. {
  511. Serial.print(" Followup ");
  512. Serial.println((uint32_t)transfer, HEX);
  513. if (!(transfer->qtd.token & 0x80)) {
  514. // TODO: check error status
  515. // do callback function...
  516. Serial.println(" completed");
  517. free_Transfer(transfer);
  518. return true;
  519. }
  520. return false;
  521. }
  522. void print(const Transfer_t *transfer)
  523. {
  524. if (!((uint32_t)transfer & 0xFFFFFFE0)) return;
  525. Serial.print("Transfer @ ");
  526. Serial.println(((uint32_t)transfer & 0xFFFFFFE0), HEX);
  527. Serial.print(" next: ");
  528. Serial.println(transfer->qtd.next, HEX);
  529. Serial.print(" anext: ");
  530. Serial.println(transfer->qtd.alt_next, HEX);
  531. Serial.print(" token: ");
  532. Serial.println(transfer->qtd.token, HEX);
  533. Serial.print(" bufs: ");
  534. for (int i=0; i < 5; i++) {
  535. Serial.print(transfer->qtd.buffer[i], HEX);
  536. if (i < 4) Serial.print(',');
  537. }
  538. Serial.println();
  539. }
  540. void print(const Pipe_t *pipe)
  541. {
  542. if (!((uint32_t)pipe & 0xFFFFFFE0)) return;
  543. Serial.print("Pipe ");
  544. if (pipe->type == 0) Serial.print("control");
  545. else if (pipe->type == 1) Serial.print("isochronous");
  546. else if (pipe->type == 2) Serial.print("bulk");
  547. else if (pipe->type == 3) Serial.print("interrupt");
  548. Serial.print(pipe->direction ? " IN" : " OUT");
  549. Serial.print(" @ ");
  550. Serial.println((uint32_t)pipe, HEX);
  551. Serial.print(" horiz link: ");
  552. Serial.println(pipe->qh.horizontal_link, HEX);
  553. Serial.print(" capabilities: ");
  554. Serial.print(pipe->qh.capabilities[0], HEX);
  555. Serial.print(',');
  556. Serial.println(pipe->qh.capabilities[1], HEX);
  557. Serial.println(" overlay:");
  558. Serial.print(" cur: ");
  559. Serial.println(pipe->qh.current, HEX);
  560. Serial.print(" next: ");
  561. Serial.println(pipe->qh.next, HEX);
  562. Serial.print(" anext: ");
  563. Serial.println(pipe->qh.alt_next, HEX);
  564. Serial.print(" token: ");
  565. Serial.println(pipe->qh.token, HEX);
  566. Serial.print(" bufs: ");
  567. for (int i=0; i < 5; i++) {
  568. Serial.print(pipe->qh.buffer[i], HEX);
  569. if (i < 4) Serial.print(',');
  570. }
  571. Serial.println();
  572. const Transfer_t *t = (Transfer_t *)pipe->qh.next;
  573. while (((uint32_t)t & 0xFFFFFFE0)) {
  574. print(t);
  575. t = (Transfer_t *)t->qtd.next;
  576. }
  577. //Serial.print();
  578. }
  579. void print(const char *s)
  580. {
  581. Serial.println(s);
  582. delay(10);
  583. }
  584. void print(const char *s, int num)
  585. {
  586. Serial.print(s);
  587. Serial.println(num);
  588. delay(10);
  589. }
  590. // Memory allocation... for now, just simplest leaky way to get started
  591. Device_t * allocate_Device(void)
  592. {
  593. static Device_t mem[3];
  594. static size_t count=0;
  595. if (count >= sizeof(mem)/sizeof(Device_t)) return NULL;
  596. return &mem[count++];
  597. }
  598. void free_Device(Device_t *q)
  599. {
  600. }
  601. Pipe_t * allocate_Pipe(void)
  602. {
  603. static Pipe_t mem[6] __attribute__ ((aligned(64)));
  604. static size_t count=0;
  605. if (count >= sizeof(mem)/sizeof(Pipe_t)) return NULL;
  606. return &mem[count++];
  607. }
  608. void free_Pipe(Pipe_t *q)
  609. {
  610. }
  611. Transfer_t * allocate_Transfer(void)
  612. {
  613. static Transfer_t mem[22] __attribute__ ((aligned(64)));
  614. static size_t count=0;
  615. if (count >= sizeof(mem)/sizeof(Transfer_t)) return NULL;
  616. return &mem[count++];
  617. }
  618. void free_Transfer(Transfer_t *q)
  619. {
  620. }