USBHost library

Dependencies:   FATFileSystem mbed-rtos

Fork of USBHost by mbed official

Files at this revision

API Documentation at this revision

Comitter:
donatien
Date:
Thu Mar 06 14:00:18 2014 +0000
Parent:
22:f4191d3837dc
Child:
24:7e12cc0217a9
Commit message:
Fixed regression: Protection against concurrent access to USBHost using locking class

Changed in this revision

USBHost/USBHost.cpp Show annotated file Show diff for this revision Revisions of this file
USBHost/USBHost.h Show annotated file Show diff for this revision Revisions of this file
USBHost3GModule/WANDongle.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/USBHost/USBHost.cpp	Fri Jan 31 10:30:17 2014 +0000
+++ b/USBHost/USBHost.cpp	Thu Mar 06 14:00:18 2014 +0000
@@ -278,6 +278,15 @@
 #endif
 }
 
+USBHost::Lock::Lock(USBHost* pHost) : m_pHost(pHost)
+{
+  m_pHost->usb_mutex.lock();
+}
+
+USBHost::Lock::~Lock()
+{
+  m_pHost->usb_mutex.unlock();
+}
 
 void USBHost::transferCompleted(volatile uint32_t addr)
 {
--- a/USBHost/USBHost.h	Fri Jan 31 10:30:17 2014 +0000
+++ b/USBHost/USBHost.h	Thu Mar 06 14:00:18 2014 +0000
@@ -187,6 +187,18 @@
         }
     }
     
+    /**
+     * Instantiate to protect USB thread from accessing shared objects (USBConnectedDevices and Interfaces)
+     */
+    class Lock
+    {
+    public:
+      Lock(USBHost* pHost);  
+      ~Lock();  
+    private:
+      USBHost* m_pHost;
+    };
+    
     friend class USBHostHub;
 
 protected:
--- a/USBHost3GModule/WANDongle.cpp	Fri Jan 31 10:30:17 2014 +0000
+++ b/USBHost3GModule/WANDongle.cpp	Thu Mar 06 14:00:18 2014 +0000
@@ -20,11 +20,6 @@
 
 #ifdef USBHOST_3GMODULE
 
-#define __DEBUG__ 0
-#ifndef __MODULE__
-#define __MODULE__ "WANDongle.cpp"
-#endif
-
 #include "dbg.h"
 #include <stdint.h>
 #include "rtos.h"
@@ -50,10 +45,14 @@
   USB_DBG("Trying to connect device");
 
   if (dev_connected) {
+      USB_DBG("Device is already connected!");    
       return true;
   }
   
   m_pInitializer = NULL;
+  
+  //Protect from concurrent access from USB thread
+  USBHost::Lock lock(host);
 
   for (int i = 0; i < MAX_DEVICE_CONNECTED; i++)
   {