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

846 lines
27KB

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