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.

147 lines
4.0KB

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