Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

895 lines
26KB

  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. init_Device_Pipe_Transfer_memory();
  103. for (int i=0; i < 32; i++) {
  104. periodictable[i] = 1;
  105. }
  106. port_state = PORT_STATE_DISCONNECTED;
  107. USBHS_USB_SBUSCFG = 1; // System Bus Interface Configuration
  108. // turn on the USBHS controller
  109. //USBHS_USBMODE = USBHS_USBMODE_TXHSD(5) | USBHS_USBMODE_CM(3); // host mode
  110. USBHS_USBMODE = USBHS_USBMODE_CM(3); // host mode
  111. USBHS_USBINTR = 0;
  112. USBHS_PERIODICLISTBASE = (uint32_t)periodictable;
  113. USBHS_FRINDEX = 0;
  114. USBHS_ASYNCLISTADDR = 0;
  115. USBHS_USBCMD = USBHS_USBCMD_ITC(8) | USBHS_USBCMD_RS |
  116. USBHS_USBCMD_ASP(3) | USBHS_USBCMD_ASPE |
  117. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(1); // periodic table is 32 pointers
  118. // turn on the USB port
  119. //USBHS_PORTSC1 = USBHS_PORTSC_PP;
  120. USBHS_PORTSC1 |= USBHS_PORTSC_PP;
  121. //USBHS_PORTSC1 |= USBHS_PORTSC_PFSC; // force 12 Mbit/sec
  122. //USBHS_PORTSC1 |= USBHS_PORTSC_PHCD; // phy off
  123. Serial.print("USBHS_ASYNCLISTADDR = ");
  124. Serial.println(USBHS_ASYNCLISTADDR, HEX);
  125. Serial.print("USBHS_PERIODICLISTBASE = ");
  126. Serial.println(USBHS_PERIODICLISTBASE, HEX);
  127. Serial.print("periodictable = ");
  128. Serial.println((uint32_t)periodictable, HEX);
  129. // enable interrupts, after this point interruts to all the work
  130. NVIC_ENABLE_IRQ(IRQ_USBHS);
  131. USBHS_USBINTR = USBHS_USBINTR_PCE | USBHS_USBINTR_TIE0;
  132. USBHS_USBINTR |= USBHS_USBINTR_UEE | USBHS_USBINTR_SEE;
  133. USBHS_USBINTR |= USBHS_USBINTR_AAE;
  134. USBHS_USBINTR |= USBHS_USBINTR_UPIE | USBHS_USBINTR_UAIE;
  135. delay(25);
  136. Serial.println("Plug in device...");
  137. digitalWrite(32, HIGH); // connect device
  138. #if 0
  139. delay(5000);
  140. Serial.println();
  141. Serial.println("Ring Doorbell");
  142. USBHS_USBCMD |= USBHS_USBCMD_IAA;
  143. if (rootdev) print(rootdev->control_pipe);
  144. #endif
  145. }
  146. void loop()
  147. {
  148. }
  149. void pulse(int usec)
  150. {
  151. // connect oscilloscope to see these pulses....
  152. digitalWriteFast(30, HIGH);
  153. delayMicroseconds(usec);
  154. digitalWriteFast(30, LOW);
  155. }
  156. // EHCI registers page default
  157. // -------------- ---- -------
  158. // USBHS_USBCMD 1599 00080000 USB Command
  159. // USBHS_USBSTS 1602 00000000 USB Status
  160. // USBHS_USBINTR 1606 00000000 USB Interrupt Enable
  161. // USBHS_FRINDEX 1609 00000000 Frame Index Register
  162. // USBHS_PERIODICLISTBASE 1610 undefine Periodic Frame List Base Address
  163. // USBHS_ASYNCLISTADDR 1612 undefine Asynchronous List Address
  164. // USBHS_PORTSC1 1619 00002000 Port Status and Control
  165. // USBHS_USBMODE 1629 00005000 USB Mode
  166. // USBHS_GPTIMERnCTL 1591 00000000 General Purpose Timer n Control
  167. // PORT_STATE_DISCONNECTED 0
  168. // PORT_STATE_DEBOUNCE 1
  169. // PORT_STATE_RESET 2
  170. // PORT_STATE_RECOVERY 3
  171. // PORT_STATE_ACTIVE 4
  172. void usbhs_isr(void)
  173. {
  174. uint32_t stat = USBHS_USBSTS;
  175. USBHS_USBSTS = stat; // clear pending interrupts
  176. //stat &= USBHS_USBINTR; // mask away unwanted interrupts
  177. Serial.println();
  178. Serial.print("ISR: ");
  179. Serial.print(stat, HEX);
  180. Serial.println();
  181. if (stat & USBHS_USBSTS_UI) Serial.println(" USB Interrupt");
  182. if (stat & USBHS_USBSTS_UEI) Serial.println(" USB Error");
  183. if (stat & USBHS_USBSTS_PCI) Serial.println(" Port Change");
  184. if (stat & USBHS_USBSTS_FRI) Serial.println(" Frame List Rollover");
  185. if (stat & USBHS_USBSTS_SEI) Serial.println(" System Error");
  186. if (stat & USBHS_USBSTS_AAI) Serial.println(" Async Advance (doorbell)");
  187. if (stat & USBHS_USBSTS_URI) Serial.println(" Reset Recv");
  188. if (stat & USBHS_USBSTS_SRI) Serial.println(" SOF");
  189. if (stat & USBHS_USBSTS_SLI) Serial.println(" Suspend");
  190. if (stat & USBHS_USBSTS_HCH) Serial.println(" Host Halted");
  191. if (stat & USBHS_USBSTS_RCL) Serial.println(" Reclamation");
  192. if (stat & USBHS_USBSTS_PS) Serial.println(" Periodic Sched En");
  193. if (stat & USBHS_USBSTS_AS) Serial.println(" Async Sched En");
  194. if (stat & USBHS_USBSTS_NAKI) Serial.println(" NAK");
  195. if (stat & USBHS_USBSTS_UAI) Serial.println(" USB Async");
  196. if (stat & USBHS_USBSTS_UPI) Serial.println(" USB Periodic");
  197. if (stat & USBHS_USBSTS_TI0) Serial.println(" Timer0");
  198. if (stat & USBHS_USBSTS_TI1) Serial.println(" Timer1");
  199. if (stat & USBHS_USBSTS_UAI) { // completed qTD(s) from the async schedule
  200. Serial.println("Async Followup");
  201. print(async_followup_first, async_followup_last);
  202. Transfer_t *p = async_followup_first;
  203. while (p) {
  204. if (followup_Transfer(p)) {
  205. // transfer completed
  206. Transfer_t *next = p->next_followup;
  207. remove_from_async_followup_list(p);
  208. free_Transfer(p);
  209. p = next;
  210. } else {
  211. // transfer still pending
  212. p = p->next_followup;
  213. }
  214. }
  215. print(async_followup_first, async_followup_last);
  216. }
  217. if (stat & USBHS_USBSTS_UPI) { // completed qTD(s) from the periodic schedule
  218. Serial.println("Periodic Followup");
  219. Transfer_t *p = periodic_followup_first;
  220. while (p) {
  221. if (followup_Transfer(p)) {
  222. // transfer completed
  223. Transfer_t *next = p->next_followup;
  224. remove_from_periodic_followup_list(p);
  225. free_Transfer(p);
  226. p = next;
  227. } else {
  228. // transfer still pending
  229. p = p->next_followup;
  230. }
  231. }
  232. }
  233. if (stat & USBHS_USBSTS_PCI) { // port change detected
  234. const uint32_t portstat = USBHS_PORTSC1;
  235. Serial.print("port change: ");
  236. Serial.print(portstat, HEX);
  237. Serial.println();
  238. USBHS_PORTSC1 = portstat | (USBHS_PORTSC_OCC|USBHS_PORTSC_PEC|USBHS_PORTSC_CSC);
  239. if (portstat & USBHS_PORTSC_OCC) {
  240. Serial.println(" overcurrent change");
  241. }
  242. if (portstat & USBHS_PORTSC_CSC) {
  243. if (portstat & USBHS_PORTSC_CCS) {
  244. Serial.println(" connect");
  245. if (port_state == PORT_STATE_DISCONNECTED
  246. || port_state == PORT_STATE_DEBOUNCE) {
  247. // 100 ms debounce (USB 2.0: TATTDB, page 150 & 188)
  248. port_state = PORT_STATE_DEBOUNCE;
  249. USBHS_GPTIMER0LD = 100000; // microseconds
  250. USBHS_GPTIMER0CTL =
  251. USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  252. stat &= ~USBHS_USBSTS_TI0;
  253. }
  254. // TODO: should ENHOSTDISCONDETECT be set? K66 ref, page 1701
  255. } else {
  256. Serial.println(" disconnect");
  257. port_state = PORT_STATE_DISCONNECTED;
  258. // TODO: delete & clean up device state...
  259. }
  260. }
  261. if (portstat & USBHS_PORTSC_PEC) {
  262. // PEC bit only detects disable
  263. Serial.println(" disable");
  264. } else if (port_state == PORT_STATE_RESET && portstat & USBHS_PORTSC_PE) {
  265. Serial.println(" port enabled");
  266. port_state = PORT_STATE_RECOVERY;
  267. // 10 ms reset recover (USB 2.0: TRSTRCY, page 151 & 188)
  268. USBHS_GPTIMER0LD = 10000; // microseconds
  269. USBHS_GPTIMER0CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  270. }
  271. if (portstat & USBHS_PORTSC_FPR) {
  272. Serial.println(" force resume");
  273. }
  274. pulse(1);
  275. }
  276. if (stat & USBHS_USBSTS_TI0) { // timer 0
  277. Serial.println("timer");
  278. pulse(2);
  279. if (port_state == PORT_STATE_DEBOUNCE) {
  280. port_state = PORT_STATE_RESET;
  281. USBHS_PORTSC1 |= USBHS_PORTSC_PR; // begin reset sequence
  282. Serial.println(" begin reset");
  283. } else if (port_state == PORT_STATE_RECOVERY) {
  284. port_state = PORT_STATE_ACTIVE;
  285. Serial.println(" end recovery");
  286. // HCSPARAMS TTCTRL page 1671
  287. uint32_t speed = (USBHS_PORTSC1 >> 26) & 3;
  288. rootdev = new_Device(speed, 0, 0);
  289. }
  290. }
  291. }
  292. void mk_setup(setup_t &s, uint32_t bmRequestType, uint32_t bRequest,
  293. uint32_t wValue, uint32_t wIndex, uint32_t wLength)
  294. {
  295. s.word1 = bmRequestType | (bRequest << 8) | (wValue << 16);
  296. s.word2 = wIndex | (wLength << 16);
  297. }
  298. static uint8_t enumbuf[255];
  299. void enumeration(const Transfer_t *transfer)
  300. {
  301. Serial.print(" CALLBACK: ");
  302. uint8_t *p = (uint8_t *)transfer->buffer;
  303. for (uint32_t i=0; i < transfer->length; i++) {
  304. Serial.print(*p++, HEX);
  305. Serial.print(' ');
  306. }
  307. Serial.println();
  308. //print(transfer);
  309. Device_t *dev = transfer->pipe->device;
  310. switch (dev->enum_state) {
  311. case 0: // read 8 bytes of device desc, set max packet, and send set address
  312. pipe_set_maxlen(dev->control_pipe, enumbuf[7]);
  313. mk_setup(dev->setup, 0, 5, assign_addr(), 0, 0); // 5=SET_ADDRESS
  314. new_Transfer(dev->control_pipe, NULL, 0);
  315. dev->enum_state = 1;
  316. break;
  317. case 1: // request all 18 bytes of device descriptor
  318. Serial.println("TODO: request 18 byte device descriptor");
  319. break;
  320. case 2: // read 18 device desc bytes, request first 9 bytes of config desc
  321. break;
  322. case 3: // read 9 bytes, request all of config desc
  323. break;
  324. case 4: // read all config desc, send set config
  325. break;
  326. default:
  327. break;
  328. }
  329. }
  330. uint32_t assign_addr(void)
  331. {
  332. return 29; // TODO: when multiple devices, assign a unique address
  333. }
  334. void pipe_set_maxlen(Pipe_t *pipe, uint32_t maxlen)
  335. {
  336. Serial.print("pipe_set_maxlen ");
  337. Serial.println(maxlen);
  338. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0x8000FFFF) | (maxlen << 16);
  339. }
  340. void pipe_set_addr(Pipe_t *pipe, uint32_t addr)
  341. {
  342. Serial.print("pipe_set_addr ");
  343. Serial.println(addr);
  344. pipe->qh.capabilities[0] = (pipe->qh.capabilities[0] & 0xFFFFFF80) | addr;
  345. }
  346. // Create a new device and begin the enumeration process
  347. //
  348. Device_t * new_Device(uint32_t speed, uint32_t hub_addr, uint32_t hub_port)
  349. {
  350. Device_t *dev;
  351. Serial.print("new_Device: ");
  352. switch (speed) {
  353. case 0: Serial.print("12"); break;
  354. case 1: Serial.print("1.5"); break;
  355. case 2: Serial.print("480"); break;
  356. default: Serial.print("??");
  357. }
  358. Serial.println(" Mbit/sec");
  359. dev = allocate_Device();
  360. if (!dev) return NULL;
  361. memset(dev, 0, sizeof(Device_t));
  362. dev->speed = speed;
  363. dev->address = 0;
  364. dev->hub_address = hub_addr;
  365. dev->hub_port = hub_port;
  366. dev->control_pipe = new_Pipe(dev, 0, 0, 0, 8);
  367. if (!dev->control_pipe) {
  368. free_Device(dev);
  369. return NULL;
  370. }
  371. dev->control_pipe->callback_function = &enumeration;
  372. dev->control_pipe->direction = 1; // 1=IN
  373. mk_setup(dev->setup, 0x80, 6, 0x0100, 0, 8); // 6=GET_DESCRIPTOR
  374. new_Transfer(dev->control_pipe, enumbuf, 8);
  375. return dev;
  376. }
  377. static uint32_t QH_capabilities1(uint32_t nak_count_reload, uint32_t control_endpoint_flag,
  378. uint32_t max_packet_length, uint32_t head_of_list, uint32_t data_toggle_control,
  379. uint32_t speed, uint32_t endpoint_number, uint32_t inactivate, uint32_t address)
  380. {
  381. return ( (nak_count_reload << 28) | (control_endpoint_flag << 27) |
  382. (max_packet_length << 16) | (head_of_list << 15) |
  383. (data_toggle_control << 14) | (speed << 12) | (endpoint_number << 8) |
  384. (inactivate << 7) | (address << 0) );
  385. }
  386. static uint32_t QH_capabilities2(uint32_t high_bw_mult, uint32_t hub_port_number,
  387. uint32_t hub_address, uint32_t split_completion_mask, uint32_t interrupt_schedule_mask)
  388. {
  389. return ( (high_bw_mult << 30) | (hub_port_number << 23) | (hub_address << 16) |
  390. (split_completion_mask << 8) | (interrupt_schedule_mask << 0) );
  391. }
  392. // Create a new pipe. It's QH is added to the async or periodic schedule,
  393. // and a halt qTD is added to the QH, so we can grow the qTD list later.
  394. //
  395. Pipe_t * new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint, uint32_t direction,
  396. uint32_t max_packet_len)
  397. {
  398. Pipe_t *pipe;
  399. Transfer_t *halt;
  400. uint32_t c=0, dtc=0;
  401. Serial.println("new_Pipe");
  402. pipe = allocate_Pipe();
  403. if (!pipe) return NULL;
  404. halt = allocate_Transfer();
  405. if (!halt) {
  406. free_Pipe(pipe);
  407. return NULL;
  408. }
  409. memset(pipe, 0, sizeof(Pipe_t));
  410. memset(halt, 0, sizeof(Transfer_t));
  411. halt->qtd.next = 1;
  412. halt->qtd.token = 0x40;
  413. pipe->device = dev;
  414. pipe->qh.next = (uint32_t)halt;
  415. pipe->qh.alt_next = 1;
  416. pipe->direction = direction;
  417. pipe->type = type;
  418. if (type == 0) {
  419. // control
  420. if (dev->speed < 2) c = 1;
  421. dtc = 1;
  422. } else if (type == 2) {
  423. // bulk
  424. } else if (type == 3) {
  425. // interrupt
  426. }
  427. pipe->qh.capabilities[0] = QH_capabilities1(15, c, max_packet_len, 0,
  428. dtc, dev->speed, endpoint, 0, dev->address);
  429. pipe->qh.capabilities[1] = QH_capabilities2(1, dev->hub_port,
  430. dev->hub_address, 0, 0);
  431. if (type == 0 || type == 2) {
  432. // control or bulk: add to async queue
  433. Pipe_t *list = (Pipe_t *)USBHS_ASYNCLISTADDR;
  434. if (list == NULL) {
  435. pipe->qh.capabilities[0] |= 0x8000; // H bit
  436. pipe->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2; // 2=QH
  437. USBHS_ASYNCLISTADDR = (uint32_t)&(pipe->qh);
  438. USBHS_USBCMD |= USBHS_USBCMD_ASE; // enable async schedule
  439. Serial.println(" first in async list");
  440. } else {
  441. // EHCI 1.0: section 4.8.1, page 72
  442. pipe->qh.horizontal_link = list->qh.horizontal_link;
  443. list->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2;
  444. Serial.println(" added to async list");
  445. }
  446. } else if (type == 3) {
  447. // interrupt: add to periodic schedule
  448. // TODO: link it into the periodic table
  449. }
  450. return pipe;
  451. }
  452. // Fill in the qTD fields (token & data)
  453. // t the Transfer qTD to initialize
  454. // buf data to transfer
  455. // len length of data
  456. // pid type of packet: 0=OUT, 1=IN, 2=SETUP
  457. // data01 value of DATA0/DATA1 toggle on 1st packet
  458. // irq whether to generate an interrupt when transfer complete
  459. //
  460. void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  461. uint32_t pid, uint32_t data01, bool irq)
  462. {
  463. t->qtd.alt_next = 1; // 1=terminate
  464. if (data01) data01 = 0x80000000;
  465. t->qtd.token = data01 | (len << 16) | (irq ? 0x8000 : 0) | (pid << 8) | 0x80;
  466. uint32_t addr = (uint32_t)buf;
  467. t->qtd.buffer[0] = addr;
  468. addr &= 0xFFFFF000;
  469. t->qtd.buffer[1] = addr + 0x1000;
  470. t->qtd.buffer[2] = addr + 0x2000;
  471. t->qtd.buffer[3] = addr + 0x3000;
  472. t->qtd.buffer[4] = addr + 0x4000;
  473. }
  474. // Create a Transfer and queue it
  475. //
  476. bool new_Transfer(Pipe_t *pipe, void *buffer, uint32_t len)
  477. {
  478. Serial.println("new_Transfer");
  479. Transfer_t *transfer = allocate_Transfer();
  480. if (!transfer) return false;
  481. if (pipe->type == 0) {
  482. // control transfer
  483. Transfer_t *data, *status;
  484. uint32_t status_direction;
  485. if (len > 16384) {
  486. // hopefully we never need more
  487. // than 16K in a control transfer
  488. free_Transfer(transfer);
  489. return false;
  490. }
  491. status = allocate_Transfer();
  492. if (!status) {
  493. free_Transfer(transfer);
  494. return false;
  495. }
  496. if (len > 0) {
  497. data = allocate_Transfer();
  498. if (!data) {
  499. free_Transfer(transfer);
  500. free_Transfer(status);
  501. return false;
  502. }
  503. init_qTD(data, buffer, len, pipe->direction, 1, false);
  504. transfer->qtd.next = (uint32_t)data;
  505. data->qtd.next = (uint32_t)status;
  506. status_direction = pipe->direction ^ 1;
  507. } else {
  508. transfer->qtd.next = (uint32_t)status;
  509. status_direction = 1; // always IN, USB 2.0 page 226
  510. }
  511. Serial.print("setup address ");
  512. Serial.println((uint32_t)&pipe->device->setup, HEX);
  513. init_qTD(transfer, &pipe->device->setup, 8, 2, 0, false);
  514. init_qTD(status, NULL, 0, status_direction, 1, true);
  515. status->pipe = pipe;
  516. status->buffer = buffer;
  517. status->length = len;
  518. status->qtd.next = 1;
  519. } else {
  520. // bulk, interrupt or isochronous transfer
  521. free_Transfer(transfer);
  522. return false;
  523. }
  524. // find halt qTD
  525. Transfer_t *halt = (Transfer_t *)(pipe->qh.next);
  526. while (!(halt->qtd.token & 0x40)) halt = (Transfer_t *)(halt->qtd.next);
  527. // transfer's token
  528. uint32_t token = transfer->qtd.token;
  529. // transfer becomes new halt qTD
  530. transfer->qtd.token = 0x40;
  531. // copy transfer non-token fields to halt
  532. halt->qtd.next = transfer->qtd.next;
  533. halt->qtd.alt_next = transfer->qtd.alt_next;
  534. halt->qtd.buffer[0] = transfer->qtd.buffer[0]; // TODO: optimize...
  535. halt->qtd.buffer[1] = transfer->qtd.buffer[1];
  536. halt->qtd.buffer[2] = transfer->qtd.buffer[2];
  537. halt->qtd.buffer[3] = transfer->qtd.buffer[3];
  538. halt->qtd.buffer[4] = transfer->qtd.buffer[4];
  539. halt->pipe = pipe;
  540. // find the last qTD we're adding
  541. Transfer_t *last = halt;
  542. while ((uint32_t)(last->qtd.next) != 1) last = (Transfer_t *)(last->qtd.next);
  543. // last points to transfer (which becomes new halt)
  544. last->qtd.next = (uint32_t)transfer;
  545. transfer->qtd.next = 1;
  546. // link all the new qTD by next_followup & prev_followup
  547. Transfer_t *prev = NULL;
  548. Transfer_t *p = halt;
  549. while (p->qtd.next != (uint32_t)transfer) {
  550. Transfer_t *next = (Transfer_t *)p->qtd.next;
  551. p->prev_followup = prev;
  552. p->next_followup = next;
  553. prev = p;
  554. p = next;
  555. }
  556. p->prev_followup = prev;
  557. p->next_followup = NULL;
  558. print(halt, p);
  559. // add them to a followup list
  560. if (pipe->type == 0 || pipe->type == 2) {
  561. // control or bulk
  562. add_to_async_followup_list(halt, p);
  563. } else {
  564. // interrupt
  565. add_to_periodic_followup_list(halt, p);
  566. }
  567. // old halt becomes new transfer, this commits all new qTDs to QH
  568. halt->qtd.token = token;
  569. return true;
  570. }
  571. bool followup_Transfer(Transfer_t *transfer)
  572. {
  573. Serial.print(" Followup ");
  574. Serial.println((uint32_t)transfer, HEX);
  575. if (!(transfer->qtd.token & 0x80)) {
  576. // TODO: check error status
  577. if (transfer->qtd.token & 0x8000) {
  578. // this transfer caused an interrupt
  579. if (transfer->pipe->callback_function) {
  580. // do the callback
  581. (*(transfer->pipe->callback_function))(transfer);
  582. }
  583. }
  584. // do callback function...
  585. Serial.println(" completed");
  586. return true;
  587. }
  588. return false;
  589. }
  590. static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last)
  591. {
  592. last->next_followup = NULL; // always add to end of list
  593. if (async_followup_last == NULL) {
  594. first->prev_followup = NULL;
  595. async_followup_first = first;
  596. } else {
  597. first->prev_followup = async_followup_last;
  598. async_followup_last->next_followup = first;
  599. }
  600. async_followup_last = last;
  601. }
  602. static void remove_from_async_followup_list(Transfer_t *transfer)
  603. {
  604. Transfer_t *next = transfer->next_followup;
  605. Transfer_t *prev = transfer->prev_followup;
  606. if (prev) {
  607. prev->next_followup = next;
  608. } else {
  609. async_followup_first = next;
  610. }
  611. if (next) {
  612. next->prev_followup = prev;
  613. } else {
  614. async_followup_last = prev;
  615. }
  616. }
  617. static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last)
  618. {
  619. last->next_followup = NULL; // always add to end of list
  620. if (periodic_followup_last == NULL) {
  621. first->prev_followup = NULL;
  622. periodic_followup_first = first;
  623. } else {
  624. first->prev_followup = periodic_followup_last;
  625. periodic_followup_last->next_followup = first;
  626. }
  627. periodic_followup_last = last;
  628. }
  629. static void remove_from_periodic_followup_list(Transfer_t *transfer)
  630. {
  631. Transfer_t *next = transfer->next_followup;
  632. Transfer_t *prev = transfer->prev_followup;
  633. if (prev) {
  634. prev->next_followup = next;
  635. } else {
  636. periodic_followup_first = next;
  637. }
  638. if (next) {
  639. next->prev_followup = prev;
  640. } else {
  641. periodic_followup_last = prev;
  642. }
  643. }
  644. void print(const Transfer_t *transfer)
  645. {
  646. if (!((uint32_t)transfer & 0xFFFFFFE0)) return;
  647. Serial.print("Transfer @ ");
  648. Serial.println(((uint32_t)transfer & 0xFFFFFFE0), HEX);
  649. Serial.print(" next: ");
  650. Serial.println(transfer->qtd.next, HEX);
  651. Serial.print(" anext: ");
  652. Serial.println(transfer->qtd.alt_next, HEX);
  653. Serial.print(" token: ");
  654. Serial.println(transfer->qtd.token, HEX);
  655. Serial.print(" bufs: ");
  656. for (int i=0; i < 5; i++) {
  657. Serial.print(transfer->qtd.buffer[i], HEX);
  658. if (i < 4) Serial.print(',');
  659. }
  660. Serial.println();
  661. }
  662. void print(const Transfer_t *first, const Transfer_t *last)
  663. {
  664. Serial.print("Transfer Followup List ");
  665. Serial.print((uint32_t)first, HEX);
  666. Serial.print(" to ");
  667. Serial.println((uint32_t)last, HEX);
  668. Serial.println(" forward:");
  669. while (first) {
  670. Serial.print(" ");
  671. Serial.print((uint32_t)first, HEX);
  672. print_token(first->qtd.token);
  673. first = first->next_followup;
  674. }
  675. Serial.println(" backward:");
  676. while (last) {
  677. Serial.print(" ");
  678. Serial.print((uint32_t)last, HEX);
  679. print_token(last->qtd.token);
  680. last = last->prev_followup;
  681. }
  682. }
  683. void print_token(uint32_t token)
  684. {
  685. switch ((token >> 8) & 3) {
  686. case 0:
  687. Serial.print(" OUT ");
  688. Serial.println((token >> 16) & 0x7FFF);
  689. break;
  690. case 1:
  691. Serial.print(" IN ");
  692. Serial.println((token >> 16) & 0x7FFF);
  693. break;
  694. case 2:
  695. Serial.println(" SETUP");
  696. break;
  697. default:
  698. Serial.println(" unknown");
  699. }
  700. }
  701. void print(const Pipe_t *pipe)
  702. {
  703. if (!((uint32_t)pipe & 0xFFFFFFE0)) return;
  704. Serial.print("Pipe ");
  705. if (pipe->type == 0) Serial.print("control");
  706. else if (pipe->type == 1) Serial.print("isochronous");
  707. else if (pipe->type == 2) Serial.print("bulk");
  708. else if (pipe->type == 3) Serial.print("interrupt");
  709. Serial.print(pipe->direction ? " IN" : " OUT");
  710. Serial.print(" @ ");
  711. Serial.println((uint32_t)pipe, HEX);
  712. Serial.print(" horiz link: ");
  713. Serial.println(pipe->qh.horizontal_link, HEX);
  714. Serial.print(" capabilities: ");
  715. Serial.print(pipe->qh.capabilities[0], HEX);
  716. Serial.print(',');
  717. Serial.println(pipe->qh.capabilities[1], HEX);
  718. Serial.println(" overlay:");
  719. Serial.print(" cur: ");
  720. Serial.println(pipe->qh.current, HEX);
  721. Serial.print(" next: ");
  722. Serial.println(pipe->qh.next, HEX);
  723. Serial.print(" anext: ");
  724. Serial.println(pipe->qh.alt_next, HEX);
  725. Serial.print(" token: ");
  726. Serial.println(pipe->qh.token, HEX);
  727. Serial.print(" bufs: ");
  728. for (int i=0; i < 5; i++) {
  729. Serial.print(pipe->qh.buffer[i], HEX);
  730. if (i < 4) Serial.print(',');
  731. }
  732. Serial.println();
  733. const Transfer_t *t = (Transfer_t *)pipe->qh.next;
  734. while (((uint32_t)t & 0xFFFFFFE0)) {
  735. print(t);
  736. t = (Transfer_t *)t->qtd.next;
  737. }
  738. //Serial.print();
  739. }
  740. void print(const char *s)
  741. {
  742. Serial.println(s);
  743. delay(10);
  744. }
  745. void print(const char *s, int num)
  746. {
  747. Serial.print(s);
  748. Serial.println(num);
  749. delay(10);
  750. }
  751. // Memory allocation
  752. static Device_t memory_Device[3];
  753. static Pipe_t memory_Pipe[6] __attribute__ ((aligned(64)));
  754. static Transfer_t memory_Transfer[24] __attribute__ ((aligned(64)));
  755. Device_t * free_Device_list = NULL;
  756. Pipe_t * free_Pipe_list = NULL;
  757. Transfer_t * free_Transfer_list = NULL;
  758. void init_Device_Pipe_Transfer_memory(void)
  759. {
  760. Device_t *end_device = memory_Device + sizeof(memory_Device)/sizeof(Device_t);
  761. for (Device_t *device = memory_Device; device < end_device; device++) {
  762. free_Device(device);
  763. }
  764. Pipe_t *end_pipe = memory_Pipe + sizeof(memory_Pipe)/sizeof(Pipe_t);
  765. for (Pipe_t *pipe = memory_Pipe; pipe < end_pipe; pipe++) {
  766. free_Pipe(pipe);
  767. }
  768. Transfer_t *end_transfer = memory_Transfer + sizeof(memory_Transfer)/sizeof(Transfer_t);
  769. for (Transfer_t *transfer = memory_Transfer; transfer < end_transfer; transfer++) {
  770. free_Transfer(transfer);
  771. }
  772. }
  773. Device_t * allocate_Device(void)
  774. {
  775. Device_t *device = free_Device_list;
  776. if (device) free_Device_list = *(Device_t **)device;
  777. return device;
  778. }
  779. void free_Device(Device_t *device)
  780. {
  781. *(Device_t **)device = free_Device_list;
  782. free_Device_list = device;
  783. }
  784. Pipe_t * allocate_Pipe(void)
  785. {
  786. Pipe_t *pipe = free_Pipe_list;
  787. if (pipe) free_Pipe_list = *(Pipe_t **)pipe;
  788. return pipe;
  789. }
  790. void free_Pipe(Pipe_t *pipe)
  791. {
  792. *(Pipe_t **)pipe = free_Pipe_list;
  793. free_Pipe_list = pipe;
  794. }
  795. Transfer_t * allocate_Transfer(void)
  796. {
  797. Transfer_t *transfer = free_Transfer_list;
  798. if (transfer) free_Transfer_list = *(Transfer_t **)transfer;
  799. return transfer;
  800. }
  801. void free_Transfer(Transfer_t *transfer)
  802. {
  803. *(Transfer_t **)transfer = free_Transfer_list;
  804. free_Transfer_list = transfer;
  805. }