From e43f86ad33b875fc3115d10b21c11b4c66329179 Mon Sep 17 00:00:00 2001 From: Jun Wako Date: Thu, 22 Jul 2021 13:45:31 +0900 Subject: [PATCH] Improve plugin detection in busprobe() MAX3421E CONDETIRQ fails to detect device plugging sometimes, we have to check bus lines regularly during bus state is SE0 to know device insertion. https://github.com/tmk/USB_Host_Shield_2.0/issues/9 --- usbhost.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/usbhost.h b/usbhost.h index de34813b..eef072f4 100644 --- a/usbhost.h +++ b/usbhost.h @@ -503,9 +503,28 @@ int8_t MAX3421e< SPI_SS, INTR >::Init(int mseconds) { /* probe bus to determine device presence and speed and switch host to this speed */ template< typename SPI_SS, typename INTR > void MAX3421e< SPI_SS, INTR >::busprobe() { + static uint8_t prev_bus = -1; uint8_t bus_sample; + + if (regRd(rHIRQ) & bmCONDETIRQ) { + // device plug/unplug is detected + // clear CONDETIRQ + regWr(rHIRQ, bmCONDETIRQ); + } else { + // check current bus state when no device(SE0), + // otherwise transient state will be read due to bus activity. + if (vbusState != SE0) return; + + // read current bus state + regWr(rHCTL, bmSAMPLEBUS); + } + bus_sample = regRd(rHRSL); //Get J,K status bus_sample &= (bmJSTATUS | bmKSTATUS); //zero the rest of the byte + + if (prev_bus == bus_sample) { return; } + prev_bus = bus_sample; + switch(bus_sample) { //start full-speed or low-speed host case( bmJSTATUS): if((regRd(rMODE) & bmLOWSPEED) == 0) { @@ -538,6 +557,7 @@ void MAX3421e< SPI_SS, INTR >::busprobe() { /* MAX3421 state change task and interrupt handler */ template< typename SPI_SS, typename INTR > uint8_t MAX3421e< SPI_SS, INTR >::Task(void) { + /* uint8_t rcode = 0; uint8_t pinvalue; //USB_HOST_SERIAL.print("Vbus state: "); @@ -553,6 +573,9 @@ uint8_t MAX3421e< SPI_SS, INTR >::Task(void) { // } // usbSM(); //USB state machine return ( rcode); + */ + busprobe(); + return 0; } template< typename SPI_SS, typename INTR >