選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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