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

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