Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

189 lines
5.3KB

  1. // usb host experiments....
  2. //void * periodictable[64] __attribute__ ((section(".dmabuffers"), used, aligned(4096)))
  3. uint32_t periodictable[64] __attribute__ ((aligned(4096)));
  4. void setup()
  5. {
  6. pinMode(32, OUTPUT); // pin 32 = USB switch, high=connect device
  7. digitalWrite(32, LOW);
  8. while (!Serial) ; // wait
  9. print("USB Host Testing");
  10. MPU_RGDAAC0 |= 0x30000000;
  11. MCG_C1 |= MCG_C1_IRCLKEN; // enable MCGIRCLK 32kHz
  12. OSC0_CR |= OSC_ERCLKEN;
  13. SIM_SOPT2 |= SIM_SOPT2_USBREGEN; // turn on USB regulator
  14. SIM_SOPT2 &= ~SIM_SOPT2_USBSLSRC; // use IRC for slow clock
  15. print("power up USBHS PHY");
  16. SIM_USBPHYCTL |= SIM_USBPHYCTL_USBDISILIM; // disable USB current limit
  17. //SIM_USBPHYCTL = SIM_USBPHYCTL_USBDISILIM | SIM_USBPHYCTL_USB3VOUTTRG(6); // pg 237
  18. SIM_SCGC3 |= SIM_SCGC3_USBHSDCD | SIM_SCGC3_USBHSPHY | SIM_SCGC3_USBHS;
  19. USBHSDCD_CLOCK = 33 << 2;
  20. print("init USBHS PHY & PLL");
  21. // init process: page 1681-1682
  22. USBPHY_CTRL &= ~USBPHY_CTRL_SFTRST; // CTRL pg 1698
  23. USBPHY_CTRL &= ~USBPHY_CTRL_CLKGATE;
  24. USBPHY_PLL_SIC |= USBPHY_PLL_SIC_PLL_POWER;
  25. USBPHY_PLL_SIC |= USBPHY_PLL_SIC_PLL_DIV_SEL(1); // PLL_SIC pg 1708
  26. // wait for the PLL to lock
  27. int count=0;
  28. while ((USBPHY_PLL_SIC & USBPHY_PLL_SIC_PLL_LOCK) == 0) {
  29. count++;
  30. }
  31. print("PLL locked, waited ", count);
  32. USBPHY_PLL_SIC |= USBPHY_PLL_SIC_PLL_EN_USB_CLKS;
  33. USBPHY_PWD = 0;
  34. delay(10);
  35. // sanity check, connect 470K pullup & 100K pulldown and watch D+ voltage change
  36. //USBPHY_ANACTRL_CLR = (1<<10); // turn off both 15K pulldowns... works! :)
  37. // sanity check, output clocks on pin 9 for testing
  38. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(3); // LPO 1kHz
  39. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(2); // Flash
  40. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(6); // XTAL
  41. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(7); // IRC 48MHz
  42. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(4); // MCGIRCLK
  43. //CORE_PIN9_CONFIG = PORT_PCR_MUX(5); // CLKOUT on PTC3 Alt5 (Arduino pin 9)
  44. // EHCI registers page default
  45. // -------------- ---- -------
  46. // USBHS_USBCMD 1599 00080000
  47. // USBHS_USBSTS 1602 00000000
  48. // USBHS_USBINTR 1606 00000000
  49. // USBHS_FRINDEX 1609 00000000
  50. // USBHS_PERIODICLISTBASE 1610 undefine
  51. // USBHS_ASYNCLISTADDR 1612 undefine
  52. // USBHS_PORTSC 1619 00002000
  53. // USBHS_USBMODE 1629 00005000
  54. print("begin ehci reset");
  55. USBHS_USBCMD |= USBHS_USBCMD_RST;
  56. count = 0;
  57. while (USBHS_USBCMD & USBHS_USBCMD_RST) {
  58. count;
  59. }
  60. print(" reset waited ", count);
  61. for (int i=0; i < 64; i++) {
  62. //periodictable[i] = (void *)1;
  63. periodictable[i] = 1;
  64. }
  65. USBHS_USBINTR = 0;
  66. USBHS_PERIODICLISTBASE = (uint32_t)periodictable;
  67. USBHS_FRINDEX = 0;
  68. USBHS_ASYNCLISTADDR = 0; // TODO: data..
  69. // turn on the USBHS controller
  70. USBHS_USBMODE = USBHS_USBMODE_TXHSD(5) | USBHS_USBMODE_CM(3); // host mode
  71. USBHS_USBCMD = USBHS_USBCMD_ITC(0) | USBHS_USBCMD_RS | USBHS_USBCMD_ASP(3) |
  72. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(0) | // periodic table is 64 pointers
  73. USBHS_USBCMD_PSE;
  74. USBHS_PORTSC1 |= USBHS_PORTSC_PP;
  75. //USBHS_PORTSC1 |= USBHS_PORTSC_PFSC; // force 12 Mbit/sec
  76. //USBHS_PORTSC1 |= USBHS_PORTSC_PHCD; // phy off
  77. }
  78. void port_status()
  79. {
  80. uint32_t n;
  81. Serial.print("Port: ");
  82. n = USBHS_PORTSC1;
  83. if (n & USBHS_PORTSC_PR) {
  84. Serial.print("reset ");
  85. }
  86. if (n & USBHS_PORTSC_PP) {
  87. Serial.print("on ");
  88. } else {
  89. Serial.print("off ");
  90. }
  91. if (n & USBHS_PORTSC_PHCD) {
  92. Serial.print("phyoff ");
  93. }
  94. if (n & USBHS_PORTSC_PE) {
  95. if (n & USBHS_PORTSC_SUSP) {
  96. Serial.print("suspend ");
  97. } else {
  98. Serial.print("enable ");
  99. }
  100. } else {
  101. Serial.print("disable ");
  102. }
  103. Serial.print("speed=");
  104. switch ((n >> 26) & 3) {
  105. case 0: Serial.print("12 Mbps "); break;
  106. case 1: Serial.print("1.5 Mbps "); break;
  107. case 2: Serial.print("480 Mbps "); break;
  108. default: Serial.print("(undef) ");
  109. }
  110. if (n & USBHS_PORTSC_HSP) {
  111. Serial.print("highspeed ");
  112. }
  113. if (n & USBHS_PORTSC_OCA) {
  114. Serial.print("overcurrent ");
  115. }
  116. if (n & USBHS_PORTSC_CCS) {
  117. Serial.print("connected ");
  118. } else {
  119. Serial.print("not-connected ");
  120. }
  121. // print info about the EHCI status
  122. Serial.print(" run=");
  123. Serial.print(USBHS_USBCMD & 1); // running mode
  124. Serial.print(",halt=");
  125. Serial.print((USBHS_USBSTS >> 12) & 1); // halted mode
  126. Serial.print(",err=");
  127. Serial.print((USBHS_USBSTS >> 4) & 1); // error encountered!
  128. Serial.print(",asyn=");
  129. Serial.print((USBHS_USBSTS >> 15) & 1); // running the async schedule
  130. Serial.print(",per=");
  131. Serial.print((USBHS_USBSTS >> 14) & 1); // running the periodic schedule
  132. Serial.print(",index=");
  133. Serial.print(USBHS_FRINDEX); // periodic index
  134. Serial.println();
  135. }
  136. void loop()
  137. {
  138. static unsigned int count=0;
  139. port_status();
  140. delay(1);
  141. count++;
  142. if (count == 2) {
  143. Serial.println("Plug in device...");
  144. digitalWrite(32, HIGH); // connect device
  145. }
  146. if (count == 5) {
  147. Serial.println("Initiate Reset Sequence...");
  148. USBHS_PORTSC1 |= USBHS_PORTSC_PR;
  149. }
  150. if (count == 15) {
  151. Serial.println("End Reset Sequence...");
  152. USBHS_PORTSC1 &= ~USBHS_PORTSC_PR;
  153. }
  154. if (count > 5000) {
  155. while (1) ; // stop here
  156. }
  157. }
  158. void print(const char *s)
  159. {
  160. Serial.println(s);
  161. delay(10);
  162. }
  163. void print(const char *s, int num)
  164. {
  165. Serial.print(s);
  166. Serial.println(num);
  167. delay(10);
  168. }