diff --git a/usbhost.h b/usbhost.h index e25e6805..538d9641 100644 --- a/usbhost.h +++ b/usbhost.h @@ -59,6 +59,11 @@ typedef SPi< Pb5, Pb3, Pb4, Pb2 > spi; typedef SPi< Pb7, Pb5, Pb6, Pb4 > spi; #endif +typedef enum VBUS_t { + on = 0, + off = GPX_VBDET +}; + template< typename SS, typename INTR > class MAX3421e /* : public spi */ { static uint8_t vbusState; @@ -72,6 +77,11 @@ public: uint8_t gpioRd(); uint16_t reset(); int8_t Init(); + int8_t Init(int mseconds); + + void vbusPower(VBUS_t state) { + regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL | state)); + } uint8_t getVbusState(void) { return vbusState; @@ -207,17 +217,6 @@ uint16_t MAX3421e< SS, INTR >::reset() { } return( i); } -///* initialize MAX3421E. Set Host mode, pullups, and stuff. Returns 0 if success, -1 if not */ -//template< typename SS, typename INTR > -//int8_t MAX3421e< SS, INTR >::Init() -//{ -// if( reset() == 0 ) { //OSCOKIRQ hasn't asserted in time -// return ( -1 ); -// } -// regWr( rMODE, bmDPPULLDN|bmDMPULLDN|bmHOST ); // set pull-downs, Host -// -// return( 0 ); -//} /* initialize MAX3421E. Set Host mode, pullups, and stuff. Returns 0 if success, -1 if not */ template< typename SS, typename INTR > @@ -226,8 +225,36 @@ int8_t MAX3421e< SS, INTR >::Init() { return( -1); } - // Delay 1 second to ensure any capacitors are drained. - delay(1000); + // GPX pin on. + regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL)); + + regWr(rMODE, bmDPPULLDN | bmDMPULLDN | bmHOST); // set pull-downs, Host + + regWr(rHIEN, bmCONDETIE | bmFRAMEIE); //connection detection + + /* check if device is connected */ + regWr(rHCTL, bmSAMPLEBUS); // sample USB bus + while(!(regRd(rHCTL) & bmSAMPLEBUS)); //wait for sample operation to finish + + busprobe(); //check if anything is connected + + regWr(rHIRQ, bmCONDETIRQ); //clear connection detect interrupt + regWr(rCPUCTL, 0x01); //enable interrupt pin + + return( 0); +} + +/* initialize MAX3421E. Set Host mode, pullups, and stuff. Returns 0 if success, -1 if not */ +template< typename SS, typename INTR > +int8_t MAX3421e< SS, INTR >::Init(int mseconds) { + if(reset() == 0) { //OSCOKIRQ hasn't asserted in time + return( -1); + } + + // Delay a minimum of 1 second to ensure any capacitors are drained. + // 1 second is required to make sure we do not smoke a Microdrive! + if(mseconds < 1000) mseconds = 1000; + delay(mseconds); regWr(rMODE, bmDPPULLDN | bmDMPULLDN | bmHOST); // set pull-downs, Host