| @@ -1,19 +1,26 @@ | |||
| // usb host experiments.... | |||
| //void * periodictable[64] __attribute__ ((section(".dmabuffers"), used, aligned(4096))) | |||
| uint32_t periodictable[64] __attribute__ ((aligned(4096))); | |||
| void setup() | |||
| { | |||
| pinMode(32, OUTPUT); // pin 32 = USB switch, high=connect device | |||
| digitalWrite(32, LOW); | |||
| while (!Serial) ; // wait | |||
| print("USB Host Testing"); | |||
| MPU_RGDAAC0 |= 0x30000000; | |||
| MCG_C1 |= MCG_C1_IRCLKEN; // enable MCGIRCLK 32kHz | |||
| OSC0_CR |= OSC_ERCLKEN; | |||
| SIM_SOPT2 |= SIM_SOPT2_USBREGEN; // turn on USB regulator | |||
| SIM_SOPT2 &= ~SIM_SOPT2_USBSLSRC; // use IRC for slow clock | |||
| print("power up USBHS PHY"); | |||
| SIM_USBPHYCTL |= SIM_USBPHYCTL_USBDISILIM; // disable USB current limit | |||
| SIM_USBPHYCTL = SIM_USBPHYCTL_USBDISILIM | SIM_USBPHYCTL_USB3VOUTTRG(6); // pg 237 | |||
| //SIM_USBPHYCTL = SIM_USBPHYCTL_USBDISILIM | SIM_USBPHYCTL_USB3VOUTTRG(6); // pg 237 | |||
| SIM_SCGC3 |= SIM_SCGC3_USBHSDCD | SIM_SCGC3_USBHSPHY | SIM_SCGC3_USBHS; | |||
| USBHSDCD_CLOCK = 33 << 2; | |||
| print("init USBHS PHY & PLL"); | |||
| // init process: page 1681-1682 | |||
| USBPHY_CTRL &= ~USBPHY_CTRL_SFTRST; // CTRL pg 1698 | |||
| @@ -52,15 +59,32 @@ void setup() | |||
| // USBHS_PORTSC 1619 00002000 | |||
| // USBHS_USBMODE 1629 00005000 | |||
| print("begin ehci reset"); | |||
| USBHS_USBCMD |= USBHS_USBCMD_RST; | |||
| count = 0; | |||
| while (USBHS_USBCMD & USBHS_USBCMD_RST) { | |||
| count; | |||
| } | |||
| print(" reset waited ", count); | |||
| for (int i=0; i < 64; i++) { | |||
| //periodictable[i] = (void *)1; | |||
| periodictable[i] = 1; | |||
| } | |||
| USBHS_USBINTR = 0; | |||
| USBHS_PERIODICLISTBASE = 0; // TODO: data.. | |||
| USBHS_PERIODICLISTBASE = (uint32_t)periodictable; | |||
| USBHS_FRINDEX = 0; | |||
| USBHS_ASYNCLISTADDR = 0; // TODO: data.. | |||
| // turn on the USBHS controller | |||
| USBHS_USBMODE = USBHS_USBMODE_TXHSD(5) | USBHS_USBMODE_CM(3); // host mode | |||
| USBHS_USBCMD = USBHS_USBCMD_ITC(8) | USBHS_USBCMD_RS | | |||
| USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(0); // periodic table is 64 pointers | |||
| USBHS_USBCMD = USBHS_USBCMD_ITC(0) | USBHS_USBCMD_RS | USBHS_USBCMD_ASP(3) | | |||
| USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(0) | // periodic table is 64 pointers | |||
| USBHS_USBCMD_PSE; | |||
| USBHS_PORTSC1 |= USBHS_PORTSC_PP; | |||
| //USBHS_PORTSC1 |= USBHS_PORTSC_PFSC; // force 12 Mbit/sec | |||
| //USBHS_PORTSC1 |= USBHS_PORTSC_PHCD; // phy off | |||
| } | |||
| void port_status() | |||
| @@ -77,12 +101,14 @@ void port_status() | |||
| } else { | |||
| Serial.print("off "); | |||
| } | |||
| if (n & USBHS_PORTSC_PHCD) { | |||
| Serial.print("phyoff "); | |||
| } | |||
| if (n & USBHS_PORTSC_PE) { | |||
| if (USBHS_PORTSC_SUSP) { | |||
| if (n & USBHS_PORTSC_SUSP) { | |||
| Serial.print("suspend "); | |||
| } else { | |||
| Serial.print("enable "); // not working... why? | |||
| Serial.print("enable "); | |||
| } | |||
| } else { | |||
| Serial.print("disable "); | |||
| @@ -101,10 +127,24 @@ void port_status() | |||
| Serial.print("overcurrent "); | |||
| } | |||
| if (n & USBHS_PORTSC_CCS) { | |||
| Serial.print("connected"); | |||
| Serial.print("connected "); | |||
| } else { | |||
| Serial.print("not-connected"); | |||
| Serial.print("not-connected "); | |||
| } | |||
| // print info about the EHCI status | |||
| Serial.print(" run="); | |||
| Serial.print(USBHS_USBCMD & 1); // running mode | |||
| Serial.print(",halt="); | |||
| Serial.print((USBHS_USBSTS >> 12) & 1); // halted mode | |||
| Serial.print(",err="); | |||
| Serial.print((USBHS_USBSTS >> 4) & 1); // error encountered! | |||
| Serial.print(",asyn="); | |||
| Serial.print((USBHS_USBSTS >> 15) & 1); // running the async schedule | |||
| Serial.print(",per="); | |||
| Serial.print((USBHS_USBSTS >> 14) & 1); // running the periodic schedule | |||
| Serial.print(",index="); | |||
| Serial.print(USBHS_FRINDEX); // periodic index | |||
| Serial.println(); | |||
| } | |||
| @@ -120,15 +160,17 @@ void loop() | |||
| Serial.println("Plug in device..."); | |||
| digitalWrite(32, HIGH); // connect device | |||
| } | |||
| if (count == 6) { | |||
| if (count == 5) { | |||
| Serial.println("Initiate Reset Sequence..."); | |||
| USBHS_PORTSC1 |= USBHS_PORTSC_PR; | |||
| } | |||
| if (count == 16) { | |||
| if (count == 15) { | |||
| Serial.println("End Reset Sequence..."); | |||
| USBHS_PORTSC1 &= ~USBHS_PORTSC_PR; | |||
| } | |||
| if (count > 5000) { | |||
| while (1) ; // stop here | |||
| } | |||
| } | |||
| void print(const char *s) | |||