選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

656 行
19KB

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