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.

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