Repository for import to local machine

Dependencies:   DMBasicGUI DMSupport

Revision:
1:a5258871b33d
Parent:
0:47c880c1463d
--- a/USBHostGC.cpp	Wed Jan 13 13:17:05 2016 +0000
+++ b/USBHostGC.cpp	Thu Jul 20 08:42:29 2017 +0000
@@ -13,6 +13,12 @@
 
 extern void EasyGUIDebugPrint(char *stuffToPrint, short X, short Y);
 
+/*
+    Displays the specified text string at the specified location in the currently-displayed easyGUI page.
+    
+    Defined in main.cpp - and omits the 'ALLOW_DEBUG_PRINTS' #define
+*/
+extern void SpecialDebugPrint(char *stuffToPrint, short X, short Y);
 
 USBHostGC::USBHostGC()
 {
@@ -52,6 +58,11 @@
     }
 }
 
+/*
+    Data sent to/from the GC over the USB link has to be in 'safe' memory - 
+    it will crash if ordinary memory is used. This function allocates 
+    a buffer in safe memory
+*/
 void USBHostGC::AllocateReportBuffer(void)
 {
     int reportSize = GC_MESSAGE_LENGTH;
@@ -66,6 +77,9 @@
     reportBuffer = usbHost->getSafeMem(reportSize);
 }
 
+/*
+    Attach the specified USB device to this instance
+*/
 bool USBHostGC::AttachGCDevice(USBDeviceConnected* usbDeviceConnected)
 {
     intInEndpointGC = usbDeviceConnected->getEndpoint(intfGC, INTERRUPT_ENDPOINT, IN);
@@ -92,6 +106,9 @@
     return true;
 }
 
+/*
+    Invoked by the OS when a USB read (from the GC) has completed
+*/
 void USBHostGC::RXHandler(void)
 {
     responseReceived = true;
@@ -101,14 +118,35 @@
     }
 }
 
+/*
+    The SetDeviceReport function is not re-entrant (and even if it were, the GC is not, so simultaneous calls 
+    to SetDeviceReport would not work anyway).
+    
+    This function tells the caller whether or not a 'SetDeviceReport' call is in progress.
+    It returns true if so, false if not.
+    
+    The expectation is that the caller will wait until this function returns false
+    before calling SetDeviceReport.
+*/
 bool USBHostGC::ExecutingSetDeviceReport(void)
 {
     return alreadyInSetDeviceReport;
 }
 
+/*
+    Sends a command to the GC, gets a response back. In this context, what we would think of
+    as a 'command' is called a 'report' in the GC code - hence the use of the word 'report' here.
+    
+    Args: a pointer to the USB device corresponding to the GC
+          a pointer to the command/report to be sent to the GC
+          a pointer to the buffer to contain the GC's response
+          
+    Returns the USB_TYPE code giving the status of the transaction.
+*/
 USB_TYPE USBHostGC::SetDeviceReport(USBDeviceConnected * dev, const char* report, char* response)
 {
     // Neither this function, nor the GC itself, are re-entrant...
+    
     if(alreadyInSetDeviceReport) {
         return USB_TYPE_PROCESSING;
     }
@@ -132,7 +170,10 @@
     
     responseReceived = false; // Synchronise with RXHandler
     
-    //EasyGUIDebugPrint("SetDeviceReport - before controlWrite", 20,80);
+//#define ALLOW_DEBUG_PRINTS_HERE
+#ifdef ALLOW_DEBUG_PRINTS_HERE
+    SpecialDebugPrint("SetDeviceReport - before controlWrite", 20,80);
+#endif
     USB_TYPE t = usbHost->controlWrite( dev,
                          1, // Non-zero - tells GC - set report, not set configuration
                          SET_CONFIGURATION,
@@ -140,12 +181,21 @@
                          0, reportBuffer, GC_MESSAGE_LENGTH);
     //EasyGUIDebugPrint("SetDeviceReport - after controlWrite", 20,100);
     
+#ifdef ALLOW_DEBUG_PRINTS_HERE
+    SpecialDebugPrint("SetDeviceReport - waiting for response", 20,120);
+#endif    
+
     while (!responseReceived) {
         // wait...
+        Thread::wait(10); // *** With this, we can successfully call this function from the Ethernet thread ***
+                          // (without it, this function enters an infinite loop at this point when called 
+                          // from the Ethernet thread, although it is OK from the main thread)
     }
 
-    //EasyGUIDebugPrint("SetDeviceReport - after wait for response", 20,120);
-    
+#ifdef ALLOW_DEBUG_PRINTS_HERE
+    SpecialDebugPrint("SetDeviceReport - after wait for response", 20,120);
+#endif    
+
     // RXHandler has now received the response - return it to the caller
     
     for (i = 0; i < GC_MESSAGE_LENGTH; ++i) {
@@ -165,7 +215,7 @@
     sprintf(buff, "setVidPid(%d %X, %d %X)\n", vid, vid, pid, pid);
     DebugPrint(buff);
 
-    deviceIsGC = ((vid == GC_VENDOR_ID) && (pid == GC_PRODUCT_ID));
+    deviceIsGC = ((vid == GC_VENDOR_ID) && ((pid == GC_PRODUCT_ID) || (pid == GC_PRODUCT_ID_2)));
 
     DebugPrint(deviceIsGC ? "Device is a GC\n" : "Device is *not* a GC\n");
 }