Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

752 lines
24KB

  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 <Arduino.h>
  24. #include "USBHost.h"
  25. #define PERIODIC_LIST_SIZE 32
  26. static uint32_t periodictable[PERIODIC_LIST_SIZE] __attribute__ ((aligned(4096), used));
  27. static uint8_t port_state;
  28. #define PORT_STATE_DISCONNECTED 0
  29. #define PORT_STATE_DEBOUNCE 1
  30. #define PORT_STATE_RESET 2
  31. #define PORT_STATE_RECOVERY 3
  32. #define PORT_STATE_ACTIVE 4
  33. static Device_t *rootdev=NULL;
  34. static Transfer_t *async_followup_first=NULL;
  35. static Transfer_t *async_followup_last=NULL;
  36. static Transfer_t *periodic_followup_first=NULL;
  37. static Transfer_t *periodic_followup_last=NULL;
  38. static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  39. uint32_t pid, uint32_t data01, bool irq);
  40. static bool followup_Transfer(Transfer_t *transfer);
  41. static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last);
  42. static void remove_from_async_followup_list(Transfer_t *transfer);
  43. static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last);
  44. static void remove_from_periodic_followup_list(Transfer_t *transfer);
  45. static bool allocate_interrupt_pipe_bandwidth(uint32_t speed, uint32_t maxlen,
  46. uint32_t interval, uint32_t direction, uint32_t *offset, uint32_t *smask,
  47. uint32_t *cmask);
  48. void USBHost::begin()
  49. {
  50. // Teensy 3.6 has USB host power controlled by PTE6
  51. PORTE_PCR6 = PORT_PCR_MUX(1);
  52. GPIOE_PDDR |= (1<<6);
  53. GPIOE_PSOR = (1<<6); // turn on USB host power
  54. Serial.print("sizeof Device = ");
  55. Serial.println(sizeof(Device_t));
  56. Serial.print("sizeof Pipe = ");
  57. Serial.println(sizeof(Pipe_t));
  58. Serial.print("sizeof Transfer = ");
  59. Serial.println(sizeof(Transfer_t));
  60. // configure the MPU to allow USBHS DMA to access memory
  61. MPU_RGDAAC0 |= 0x30000000;
  62. Serial.print("MPU_RGDAAC0 = ");
  63. Serial.println(MPU_RGDAAC0, HEX);
  64. // turn on clocks
  65. MCG_C1 |= MCG_C1_IRCLKEN; // enable MCGIRCLK 32kHz
  66. OSC0_CR |= OSC_ERCLKEN;
  67. SIM_SOPT2 |= SIM_SOPT2_USBREGEN; // turn on USB regulator
  68. SIM_SOPT2 &= ~SIM_SOPT2_USBSLSRC; // use IRC for slow clock
  69. print("power up USBHS PHY");
  70. SIM_USBPHYCTL |= SIM_USBPHYCTL_USBDISILIM; // disable USB current limit
  71. //SIM_USBPHYCTL = SIM_USBPHYCTL_USBDISILIM | SIM_USBPHYCTL_USB3VOUTTRG(6); // pg 237
  72. SIM_SCGC3 |= SIM_SCGC3_USBHSDCD | SIM_SCGC3_USBHSPHY | SIM_SCGC3_USBHS;
  73. USBHSDCD_CLOCK = 33 << 2;
  74. print("init USBHS PHY & PLL");
  75. // init process: page 1681-1682
  76. USBPHY_CTRL_CLR = (USBPHY_CTRL_SFTRST | USBPHY_CTRL_CLKGATE); // // CTRL pg 1698
  77. USBPHY_CTRL_SET = USBPHY_CTRL_ENUTMILEVEL2 | USBPHY_CTRL_ENUTMILEVEL3;
  78. //USBPHY_CTRL_SET = USBPHY_CTRL_FSDLL_RST_EN; // TODO: what does this do??
  79. USBPHY_TRIM_OVERRIDE_EN_SET = 1;
  80. USBPHY_PLL_SIC = USBPHY_PLL_SIC_PLL_POWER | USBPHY_PLL_SIC_PLL_ENABLE |
  81. USBPHY_PLL_SIC_PLL_DIV_SEL(1) | USBPHY_PLL_SIC_PLL_EN_USB_CLKS;
  82. // wait for the PLL to lock
  83. int count=0;
  84. while ((USBPHY_PLL_SIC & USBPHY_PLL_SIC_PLL_LOCK) == 0) {
  85. count++;
  86. }
  87. Serial.print("PLL locked, waited ");
  88. Serial.println(count);
  89. // turn on power to PHY
  90. USBPHY_PWD = 0;
  91. delay(10);
  92. // sanity check, connect 470K pullup & 100K pulldown and watch D+ voltage change
  93. //USBPHY_ANACTRL_CLR = (1<<10); // turn off both 15K pulldowns... works! :)
  94. // sanity check, output clocks on pin 9 for testing
  95. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(3); // LPO 1kHz
  96. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(2); // Flash
  97. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(6); // XTAL
  98. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(7); // IRC 48MHz
  99. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(4); // MCGIRCLK
  100. //CORE_PIN9_CONFIG = PORT_PCR_MUX(5); // CLKOUT on PTC3 Alt5 (Arduino pin 9)
  101. // now with the PHY up and running, start up USBHS
  102. print("begin ehci reset");
  103. USBHS_USBCMD |= USBHS_USBCMD_RST;
  104. count = 0;
  105. while (USBHS_USBCMD & USBHS_USBCMD_RST) {
  106. count++;
  107. }
  108. print(" reset waited ", count);
  109. init_Device_Pipe_Transfer_memory();
  110. for (int i=0; i < 32; i++) {
  111. periodictable[i] = 1;
  112. }
  113. port_state = PORT_STATE_DISCONNECTED;
  114. USBHS_USB_SBUSCFG = 1; // System Bus Interface Configuration
  115. // turn on the USBHS controller
  116. //USBHS_USBMODE = USBHS_USBMODE_TXHSD(5) | USBHS_USBMODE_CM(3); // host mode
  117. USBHS_USBMODE = USBHS_USBMODE_CM(3); // host mode
  118. USBHS_USBINTR = 0;
  119. USBHS_PERIODICLISTBASE = (uint32_t)periodictable;
  120. USBHS_FRINDEX = 0;
  121. USBHS_ASYNCLISTADDR = 0;
  122. USBHS_USBCMD = USBHS_USBCMD_ITC(8) | USBHS_USBCMD_RS |
  123. USBHS_USBCMD_ASP(3) | USBHS_USBCMD_ASPE | USBHS_USBCMD_PSE |
  124. #if PERIODIC_LIST_SIZE == 8
  125. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(3);
  126. #elif PERIODIC_LIST_SIZE == 16
  127. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(2);
  128. #elif PERIODIC_LIST_SIZE == 32
  129. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(1);
  130. #elif PERIODIC_LIST_SIZE == 64
  131. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(0);
  132. #elif PERIODIC_LIST_SIZE == 128
  133. USBHS_USBCMD_FS(3);
  134. #elif PERIODIC_LIST_SIZE == 256
  135. USBHS_USBCMD_FS(2);
  136. #elif PERIODIC_LIST_SIZE == 512
  137. USBHS_USBCMD_FS(1);
  138. #elif PERIODIC_LIST_SIZE == 1024
  139. USBHS_USBCMD_FS(0);
  140. #else
  141. #error "Unsupported PERIODIC_LIST_SIZE"
  142. #endif
  143. // turn on the USB port
  144. //USBHS_PORTSC1 = USBHS_PORTSC_PP;
  145. USBHS_PORTSC1 |= USBHS_PORTSC_PP;
  146. //USBHS_PORTSC1 |= USBHS_PORTSC_PFSC; // force 12 Mbit/sec
  147. //USBHS_PORTSC1 |= USBHS_PORTSC_PHCD; // phy off
  148. Serial.print("USBHS_ASYNCLISTADDR = ");
  149. Serial.println(USBHS_ASYNCLISTADDR, HEX);
  150. Serial.print("USBHS_PERIODICLISTBASE = ");
  151. Serial.println(USBHS_PERIODICLISTBASE, HEX);
  152. Serial.print("periodictable = ");
  153. Serial.println((uint32_t)periodictable, HEX);
  154. // enable interrupts, after this point interruts to all the work
  155. attachInterruptVector(IRQ_USBHS, isr);
  156. NVIC_ENABLE_IRQ(IRQ_USBHS);
  157. USBHS_USBINTR = USBHS_USBINTR_PCE | USBHS_USBINTR_TIE0;
  158. USBHS_USBINTR |= USBHS_USBINTR_UEE | USBHS_USBINTR_SEE;
  159. USBHS_USBINTR |= USBHS_USBINTR_AAE;
  160. USBHS_USBINTR |= USBHS_USBINTR_UPIE | USBHS_USBINTR_UAIE;
  161. }
  162. // EHCI registers page default
  163. // -------------- ---- -------
  164. // USBHS_USBCMD 1599 00080000 USB Command
  165. // USBHS_USBSTS 1602 00000000 USB Status
  166. // USBHS_USBINTR 1606 00000000 USB Interrupt Enable
  167. // USBHS_FRINDEX 1609 00000000 Frame Index Register
  168. // USBHS_PERIODICLISTBASE 1610 undefine Periodic Frame List Base Address
  169. // USBHS_ASYNCLISTADDR 1612 undefine Asynchronous List Address
  170. // USBHS_PORTSC1 1619 00002000 Port Status and Control
  171. // USBHS_USBMODE 1629 00005000 USB Mode
  172. // USBHS_GPTIMERnCTL 1591 00000000 General Purpose Timer n Control
  173. // PORT_STATE_DISCONNECTED 0
  174. // PORT_STATE_DEBOUNCE 1
  175. // PORT_STATE_RESET 2
  176. // PORT_STATE_RECOVERY 3
  177. // PORT_STATE_ACTIVE 4
  178. void USBHost::isr()
  179. {
  180. uint32_t stat = USBHS_USBSTS;
  181. USBHS_USBSTS = stat; // clear pending interrupts
  182. //stat &= USBHS_USBINTR; // mask away unwanted interrupts
  183. Serial.println();
  184. Serial.print("ISR: ");
  185. Serial.print(stat, HEX);
  186. Serial.println();
  187. if (stat & USBHS_USBSTS_UI) Serial.println(" USB Interrupt");
  188. if (stat & USBHS_USBSTS_UEI) Serial.println(" USB Error");
  189. if (stat & USBHS_USBSTS_PCI) Serial.println(" Port Change");
  190. if (stat & USBHS_USBSTS_FRI) Serial.println(" Frame List Rollover");
  191. if (stat & USBHS_USBSTS_SEI) Serial.println(" System Error");
  192. if (stat & USBHS_USBSTS_AAI) Serial.println(" Async Advance (doorbell)");
  193. if (stat & USBHS_USBSTS_URI) Serial.println(" Reset Recv");
  194. if (stat & USBHS_USBSTS_SRI) Serial.println(" SOF");
  195. if (stat & USBHS_USBSTS_SLI) Serial.println(" Suspend");
  196. if (stat & USBHS_USBSTS_HCH) Serial.println(" Host Halted");
  197. if (stat & USBHS_USBSTS_RCL) Serial.println(" Reclamation");
  198. if (stat & USBHS_USBSTS_PS) Serial.println(" Periodic Sched En");
  199. if (stat & USBHS_USBSTS_AS) Serial.println(" Async Sched En");
  200. if (stat & USBHS_USBSTS_NAKI) Serial.println(" NAK");
  201. if (stat & USBHS_USBSTS_UAI) Serial.println(" USB Async");
  202. if (stat & USBHS_USBSTS_UPI) Serial.println(" USB Periodic");
  203. if (stat & USBHS_USBSTS_TI0) Serial.println(" Timer0");
  204. if (stat & USBHS_USBSTS_TI1) Serial.println(" Timer1");
  205. if (stat & USBHS_USBSTS_UAI) { // completed qTD(s) from the async schedule
  206. Serial.println("Async Followup");
  207. print(async_followup_first, async_followup_last);
  208. Transfer_t *p = async_followup_first;
  209. while (p) {
  210. if (followup_Transfer(p)) {
  211. // transfer completed
  212. Transfer_t *next = p->next_followup;
  213. remove_from_async_followup_list(p);
  214. free_Transfer(p);
  215. p = next;
  216. } else {
  217. // transfer still pending
  218. p = p->next_followup;
  219. }
  220. }
  221. print(async_followup_first, async_followup_last);
  222. }
  223. if (stat & USBHS_USBSTS_UPI) { // completed qTD(s) from the periodic schedule
  224. Serial.println("Periodic Followup");
  225. Transfer_t *p = periodic_followup_first;
  226. while (p) {
  227. if (followup_Transfer(p)) {
  228. // transfer completed
  229. Transfer_t *next = p->next_followup;
  230. remove_from_periodic_followup_list(p);
  231. free_Transfer(p);
  232. p = next;
  233. } else {
  234. // transfer still pending
  235. p = p->next_followup;
  236. }
  237. }
  238. }
  239. if (stat & USBHS_USBSTS_PCI) { // port change detected
  240. const uint32_t portstat = USBHS_PORTSC1;
  241. Serial.print("port change: ");
  242. Serial.print(portstat, HEX);
  243. Serial.println();
  244. USBHS_PORTSC1 = portstat | (USBHS_PORTSC_OCC|USBHS_PORTSC_PEC|USBHS_PORTSC_CSC);
  245. if (portstat & USBHS_PORTSC_OCC) {
  246. Serial.println(" overcurrent change");
  247. }
  248. if (portstat & USBHS_PORTSC_CSC) {
  249. if (portstat & USBHS_PORTSC_CCS) {
  250. Serial.println(" connect");
  251. if (port_state == PORT_STATE_DISCONNECTED
  252. || port_state == PORT_STATE_DEBOUNCE) {
  253. // 100 ms debounce (USB 2.0: TATTDB, page 150 & 188)
  254. port_state = PORT_STATE_DEBOUNCE;
  255. USBHS_GPTIMER0LD = 100000; // microseconds
  256. USBHS_GPTIMER0CTL =
  257. USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  258. stat &= ~USBHS_USBSTS_TI0;
  259. }
  260. } else {
  261. Serial.println(" disconnect");
  262. port_state = PORT_STATE_DISCONNECTED;
  263. USBPHY_CTRL_CLR = USBPHY_CTRL_ENHOSTDISCONDETECT;
  264. // TODO: delete & clean up device state...
  265. }
  266. }
  267. if (portstat & USBHS_PORTSC_PEC) {
  268. // PEC bit only detects disable
  269. Serial.println(" disable");
  270. } else if (port_state == PORT_STATE_RESET && portstat & USBHS_PORTSC_PE) {
  271. Serial.println(" port enabled");
  272. port_state = PORT_STATE_RECOVERY;
  273. // 10 ms reset recover (USB 2.0: TRSTRCY, page 151 & 188)
  274. USBHS_GPTIMER0LD = 10000; // microseconds
  275. USBHS_GPTIMER0CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  276. if (USBHS_PORTSC1 & USBHS_PORTSC_HSP) {
  277. // turn on high-speed disconnect detector
  278. USBPHY_CTRL_SET = USBPHY_CTRL_ENHOSTDISCONDETECT;
  279. }
  280. }
  281. if (portstat & USBHS_PORTSC_FPR) {
  282. Serial.println(" force resume");
  283. }
  284. }
  285. if (stat & USBHS_USBSTS_TI0) { // timer 0
  286. Serial.println("timer");
  287. if (port_state == PORT_STATE_DEBOUNCE) {
  288. port_state = PORT_STATE_RESET;
  289. USBHS_PORTSC1 |= USBHS_PORTSC_PR; // begin reset sequence
  290. Serial.println(" begin reset");
  291. } else if (port_state == PORT_STATE_RECOVERY) {
  292. port_state = PORT_STATE_ACTIVE;
  293. Serial.println(" end recovery");
  294. // HCSPARAMS TTCTRL page 1671
  295. uint32_t speed = (USBHS_PORTSC1 >> 26) & 3;
  296. rootdev = new_Device(speed, 0, 0);
  297. }
  298. }
  299. }
  300. static uint32_t QH_capabilities1(uint32_t nak_count_reload, uint32_t control_endpoint_flag,
  301. uint32_t max_packet_length, uint32_t head_of_list, uint32_t data_toggle_control,
  302. uint32_t speed, uint32_t endpoint_number, uint32_t inactivate, uint32_t address)
  303. {
  304. return ( (nak_count_reload << 28) | (control_endpoint_flag << 27) |
  305. (max_packet_length << 16) | (head_of_list << 15) |
  306. (data_toggle_control << 14) | (speed << 12) | (endpoint_number << 8) |
  307. (inactivate << 7) | (address << 0) );
  308. }
  309. static uint32_t QH_capabilities2(uint32_t high_bw_mult, uint32_t hub_port_number,
  310. uint32_t hub_address, uint32_t split_completion_mask, uint32_t interrupt_schedule_mask)
  311. {
  312. return ( (high_bw_mult << 30) | (hub_port_number << 23) | (hub_address << 16) |
  313. (split_completion_mask << 8) | (interrupt_schedule_mask << 0) );
  314. }
  315. // Create a new pipe. It's QH is added to the async or periodic schedule,
  316. // and a halt qTD is added to the QH, so we can grow the qTD list later.
  317. // dev: device owning this pipe/endpoint
  318. // type: 0=control, 2=bulk, 3=interrupt
  319. // endpoint: 0 for control, 1-15 for bulk or interrupt
  320. // direction: 0=OUT, 1=IN (unused for control)
  321. // maxlen: maximum packet size
  322. // interval: polling interval for interrupt, power of 2, unused if control or bulk
  323. //
  324. Pipe_t * USBHost::new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
  325. uint32_t direction, uint32_t maxlen, uint32_t interval)
  326. {
  327. Pipe_t *pipe;
  328. Transfer_t *halt;
  329. uint32_t c=0, dtc=0, smask=0, cmask=0, offset=0;
  330. Serial.println("new_Pipe");
  331. pipe = allocate_Pipe();
  332. if (!pipe) return NULL;
  333. halt = allocate_Transfer();
  334. if (!halt) {
  335. free_Pipe(pipe);
  336. return NULL;
  337. }
  338. if (type == 3) {
  339. // interrupt transfers require bandwidth & microframe scheduling
  340. if (interval > PERIODIC_LIST_SIZE*8) interval = PERIODIC_LIST_SIZE*8;
  341. if (dev->speed < 2 && interval < 8) interval = 8;
  342. if (!allocate_interrupt_pipe_bandwidth(dev->speed,
  343. maxlen, interval, direction, &offset, &smask, &cmask)) {
  344. free_Transfer(halt);
  345. free_Pipe(pipe);
  346. return NULL;
  347. }
  348. }
  349. memset(pipe, 0, sizeof(Pipe_t));
  350. memset(halt, 0, sizeof(Transfer_t));
  351. halt->qtd.next = 1;
  352. halt->qtd.token = 0x40;
  353. pipe->device = dev;
  354. pipe->qh.next = (uint32_t)halt;
  355. pipe->qh.alt_next = 1;
  356. pipe->direction = direction;
  357. pipe->type = type;
  358. if (type == 0) {
  359. // control
  360. if (dev->speed < 2) c = 1;
  361. dtc = 1;
  362. } else if (type == 2) {
  363. // bulk
  364. } else if (type == 3) {
  365. // interrupt
  366. }
  367. pipe->qh.capabilities[0] = QH_capabilities1(15, c, maxlen, 0,
  368. dtc, dev->speed, endpoint, 0, dev->address);
  369. pipe->qh.capabilities[1] = QH_capabilities2(1, dev->hub_port,
  370. dev->hub_address, cmask, smask);
  371. if (type == 0 || type == 2) {
  372. // control or bulk: add to async queue
  373. Pipe_t *list = (Pipe_t *)USBHS_ASYNCLISTADDR;
  374. if (list == NULL) {
  375. pipe->qh.capabilities[0] |= 0x8000; // H bit
  376. pipe->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2; // 2=QH
  377. USBHS_ASYNCLISTADDR = (uint32_t)&(pipe->qh);
  378. USBHS_USBCMD |= USBHS_USBCMD_ASE; // enable async schedule
  379. Serial.println(" first in async list");
  380. } else {
  381. // EHCI 1.0: section 4.8.1, page 72
  382. pipe->qh.horizontal_link = list->qh.horizontal_link;
  383. list->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2;
  384. Serial.println(" added to async list");
  385. }
  386. } else if (type == 3) {
  387. // interrupt: add to periodic schedule
  388. // TODO: link it into the periodic table
  389. // TODO: built tree...
  390. //uint32_t finterval = interval >> 3;
  391. //for (uint32_t i=offset; i < PERIODIC_LIST_SIZE; i += finterval) {
  392. // uint32_t list = periodictable[i];
  393. //}
  394. // quick hack for testing, just put it into the first table entry
  395. pipe->qh.horizontal_link = periodictable[0];
  396. periodictable[0] = (uint32_t)&(pipe->qh) | 2; // 2=QH
  397. Serial.print("init periodictable with ");
  398. Serial.println(periodictable[0], HEX);
  399. }
  400. return pipe;
  401. }
  402. // Fill in the qTD fields (token & data)
  403. // t the Transfer qTD to initialize
  404. // buf data to transfer
  405. // len length of data
  406. // pid type of packet: 0=OUT, 1=IN, 2=SETUP
  407. // data01 value of DATA0/DATA1 toggle on 1st packet
  408. // irq whether to generate an interrupt when transfer complete
  409. //
  410. static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  411. uint32_t pid, uint32_t data01, bool irq)
  412. {
  413. t->qtd.alt_next = 1; // 1=terminate
  414. if (data01) data01 = 0x80000000;
  415. t->qtd.token = data01 | (len << 16) | (irq ? 0x8000 : 0) | (pid << 8) | 0x80;
  416. uint32_t addr = (uint32_t)buf;
  417. t->qtd.buffer[0] = addr;
  418. addr &= 0xFFFFF000;
  419. t->qtd.buffer[1] = addr + 0x1000;
  420. t->qtd.buffer[2] = addr + 0x2000;
  421. t->qtd.buffer[3] = addr + 0x3000;
  422. t->qtd.buffer[4] = addr + 0x4000;
  423. }
  424. // Create a Control Transfer and queue it
  425. //
  426. bool USBHost::queue_Control_Transfer(Device_t *dev, setup_t *setup, void *buf, USBDriver *driver)
  427. {
  428. Transfer_t *transfer, *data, *status;
  429. uint32_t status_direction;
  430. Serial.println("new_Control_Transfer");
  431. if (setup->wLength > 16384) return false; // max 16K data for control
  432. transfer = allocate_Transfer();
  433. if (!transfer) return false;
  434. status = allocate_Transfer();
  435. if (!status) {
  436. free_Transfer(transfer);
  437. return false;
  438. }
  439. if (setup->wLength > 0) {
  440. data = allocate_Transfer();
  441. if (!data) {
  442. free_Transfer(transfer);
  443. free_Transfer(status);
  444. return false;
  445. }
  446. uint32_t pid = (setup->bmRequestType & 0x80) ? 1 : 0;
  447. init_qTD(data, buf, setup->wLength, pid, 1, false);
  448. transfer->qtd.next = (uint32_t)data;
  449. data->qtd.next = (uint32_t)status;
  450. status_direction = pid ^ 1;
  451. } else {
  452. transfer->qtd.next = (uint32_t)status;
  453. status_direction = 1; // always IN, USB 2.0 page 226
  454. }
  455. Serial.print("setup address ");
  456. Serial.println((uint32_t)setup, HEX);
  457. init_qTD(transfer, setup, 8, 2, 0, false);
  458. init_qTD(status, NULL, 0, status_direction, 1, true);
  459. status->pipe = dev->control_pipe;
  460. status->buffer = buf;
  461. status->length = setup->wLength;
  462. status->setup = setup;
  463. status->driver = driver;
  464. status->qtd.next = 1;
  465. return queue_Transfer(dev->control_pipe, transfer);
  466. }
  467. // Create a Bulk or Interrupt Transfer and queue it
  468. //
  469. bool USBHost::queue_Data_Transfer(Pipe_t *pipe, void *buffer, uint32_t len, USBDriver *driver)
  470. {
  471. Transfer_t *transfer, *data, *next;
  472. uint8_t *p = (uint8_t *)buffer;
  473. uint32_t count;
  474. bool last = false;
  475. // TODO: option for zero length packet? Maybe in Pipe_t fields?
  476. Serial.println("new_Data_Transfer");
  477. // allocate qTDs
  478. transfer = allocate_Transfer();
  479. if (!transfer) return false;
  480. data = transfer;
  481. for (count=(len >> 14); count; count--) {
  482. next = allocate_Transfer();
  483. if (!next) {
  484. // free already-allocated qTDs
  485. while (1) {
  486. next = (Transfer_t *)transfer->qtd.next;
  487. free_Transfer(transfer);
  488. if (transfer == data) break;
  489. transfer = next;
  490. }
  491. return false;
  492. }
  493. data->qtd.next = (uint32_t)next;
  494. data = next;
  495. }
  496. // last qTD needs info for followup
  497. data->qtd.next = 1;
  498. data->pipe = pipe;
  499. data->buffer = buffer;
  500. data->length = len;
  501. data->setup = NULL;
  502. data->driver = driver;
  503. // initialize all qTDs
  504. data = transfer;
  505. while (1) {
  506. uint32_t count = len;
  507. if (count > 16384) {
  508. count = 16384;
  509. } else {
  510. last = true;
  511. }
  512. init_qTD(data, p, count, pipe->direction, 0, last);
  513. if (last) break;
  514. p += count;
  515. len -= count;
  516. data = (Transfer_t *)(data->qtd.next);
  517. }
  518. return queue_Transfer(pipe, transfer);
  519. }
  520. bool USBHost::queue_Transfer(Pipe_t *pipe, Transfer_t *transfer)
  521. {
  522. // find halt qTD
  523. Transfer_t *halt = (Transfer_t *)(pipe->qh.next);
  524. while (!(halt->qtd.token & 0x40)) halt = (Transfer_t *)(halt->qtd.next);
  525. // transfer's token
  526. uint32_t token = transfer->qtd.token;
  527. // transfer becomes new halt qTD
  528. transfer->qtd.token = 0x40;
  529. // copy transfer non-token fields to halt
  530. halt->qtd.next = transfer->qtd.next;
  531. halt->qtd.alt_next = transfer->qtd.alt_next;
  532. halt->qtd.buffer[0] = transfer->qtd.buffer[0]; // TODO: optimize memcpy, all
  533. halt->qtd.buffer[1] = transfer->qtd.buffer[1]; // fields except token
  534. halt->qtd.buffer[2] = transfer->qtd.buffer[2];
  535. halt->qtd.buffer[3] = transfer->qtd.buffer[3];
  536. halt->qtd.buffer[4] = transfer->qtd.buffer[4];
  537. halt->pipe = pipe;
  538. halt->buffer = transfer->buffer;
  539. halt->length = transfer->length;
  540. halt->setup = transfer->setup;
  541. halt->driver = transfer->driver;
  542. // find the last qTD we're adding
  543. Transfer_t *last = halt;
  544. while ((uint32_t)(last->qtd.next) != 1) last = (Transfer_t *)(last->qtd.next);
  545. // last points to transfer (which becomes new halt)
  546. last->qtd.next = (uint32_t)transfer;
  547. transfer->qtd.next = 1;
  548. // link all the new qTD by next_followup & prev_followup
  549. Transfer_t *prev = NULL;
  550. Transfer_t *p = halt;
  551. while (p->qtd.next != (uint32_t)transfer) {
  552. Transfer_t *next = (Transfer_t *)p->qtd.next;
  553. p->prev_followup = prev;
  554. p->next_followup = next;
  555. prev = p;
  556. p = next;
  557. }
  558. p->prev_followup = prev;
  559. p->next_followup = NULL;
  560. print(halt, p);
  561. // add them to a followup list
  562. if (pipe->type == 0 || pipe->type == 2) {
  563. // control or bulk
  564. add_to_async_followup_list(halt, p);
  565. } else {
  566. // interrupt
  567. add_to_periodic_followup_list(halt, p);
  568. }
  569. // old halt becomes new transfer, this commits all new qTDs to QH
  570. halt->qtd.token = token;
  571. return true;
  572. }
  573. static bool followup_Transfer(Transfer_t *transfer)
  574. {
  575. Serial.print(" Followup ");
  576. Serial.println((uint32_t)transfer, HEX);
  577. if (!(transfer->qtd.token & 0x80)) {
  578. // TODO: check error status
  579. if (transfer->qtd.token & 0x8000) {
  580. // this transfer caused an interrupt
  581. if (transfer->pipe->callback_function) {
  582. // do the callback
  583. (*(transfer->pipe->callback_function))(transfer);
  584. }
  585. }
  586. // do callback function...
  587. Serial.println(" completed");
  588. return true;
  589. }
  590. return false;
  591. }
  592. static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last)
  593. {
  594. last->next_followup = NULL; // always add to end of list
  595. if (async_followup_last == NULL) {
  596. first->prev_followup = NULL;
  597. async_followup_first = first;
  598. } else {
  599. first->prev_followup = async_followup_last;
  600. async_followup_last->next_followup = first;
  601. }
  602. async_followup_last = last;
  603. }
  604. static void remove_from_async_followup_list(Transfer_t *transfer)
  605. {
  606. Transfer_t *next = transfer->next_followup;
  607. Transfer_t *prev = transfer->prev_followup;
  608. if (prev) {
  609. prev->next_followup = next;
  610. } else {
  611. async_followup_first = next;
  612. }
  613. if (next) {
  614. next->prev_followup = prev;
  615. } else {
  616. async_followup_last = prev;
  617. }
  618. }
  619. static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last)
  620. {
  621. last->next_followup = NULL; // always add to end of list
  622. if (periodic_followup_last == NULL) {
  623. first->prev_followup = NULL;
  624. periodic_followup_first = first;
  625. } else {
  626. first->prev_followup = periodic_followup_last;
  627. periodic_followup_last->next_followup = first;
  628. }
  629. periodic_followup_last = last;
  630. }
  631. static void remove_from_periodic_followup_list(Transfer_t *transfer)
  632. {
  633. Transfer_t *next = transfer->next_followup;
  634. Transfer_t *prev = transfer->prev_followup;
  635. if (prev) {
  636. prev->next_followup = next;
  637. } else {
  638. periodic_followup_first = next;
  639. }
  640. if (next) {
  641. next->prev_followup = prev;
  642. } else {
  643. periodic_followup_last = prev;
  644. }
  645. }
  646. // Allocate bandwidth for an interrupt pipe. Given the packet size
  647. // and other parameters, find the best place to schedule this pipe.
  648. // Returns true if enough bandwidth is available, and the best
  649. // frame offset, smask and cmask. Or returns false if no group
  650. // of microframes has enough bandwidth available.
  651. //
  652. // speed: [in] 0=full speed, 1=low speed, 2=high speed
  653. // maxlen: [in] maximum packet length
  654. // interval: [in] polling interval, in 125 us micro frames
  655. // direction: [in] 0=OUT, 1=IN
  656. // offset: [out] frame offset, 0 to PERIODIC_LIST_SIZE-1
  657. // smask: [out] Start Mask
  658. // cmask: [out] Complete Mask
  659. //
  660. static bool allocate_interrupt_pipe_bandwidth(uint32_t speed, uint32_t maxlen,
  661. uint32_t interval, uint32_t direction, uint32_t *offset, uint32_t *smask,
  662. uint32_t *cmask)
  663. {
  664. // TODO: actual bandwidth planning needs to go here... but for
  665. // now we'll just always pile up everything at the same offset
  666. // and same microframe schedule for split transactions, without
  667. // even the slighest check whether it all fits.
  668. if (speed == 2) {
  669. // high speed 480 Mbit/sec
  670. if (interval == 1) {
  671. *smask = 0xFF;
  672. } else if (interval == 2) {
  673. *smask = 0x55;
  674. } else if (interval <= 4) {
  675. *smask = 0x11;
  676. } else {
  677. *smask = 0x01;
  678. }
  679. *cmask = 0;
  680. *offset = 0;
  681. } else {
  682. // full speed 12 Mbit/sec or low speed 1.5 Mbit/sec
  683. *smask = 0x01;
  684. *cmask = 0x3C;
  685. *offset = 0;
  686. }
  687. return true;
  688. }