You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

736 lines
23KB

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