USB device stack

Dependents:   mbed-mX-USB-TEST1 USBMSD_SD_HID_HelloWorld HidTest MIDI_usb_bridge ... more

Legacy Warning

This is an mbed 2 library. To learn more about mbed OS 5, visit the docs.

Pull requests against this repository are no longer supported. Please raise against mbed OS 5 as documented above.

Files at this revision

API Documentation at this revision

Comitter:
bogdanm
Date:
Thu Sep 12 14:45:27 2013 +0100
Parent:
13:16731886c049
Child:
15:849c0c0f2769
Commit message:
Synchronized with git revision b9d52bda50a692c05a4587bcc8d3219997444f58

Changed in this revision

USBDevice/USBHAL_KL25Z.cpp Show annotated file Show diff for this revision Revisions of this file
USBMSD/USBMSD.cpp Show annotated file Show diff for this revision Revisions of this file
USBMSD/USBMSD.h Show annotated file Show diff for this revision Revisions of this file
USBSerial/CircBuffer.h Show annotated file Show diff for this revision Revisions of this file
--- a/USBDevice/USBHAL_KL25Z.cpp	Tue Sep 10 15:14:55 2013 +0300
+++ b/USBDevice/USBHAL_KL25Z.cpp	Thu Sep 12 14:45:27 2013 +0100
@@ -169,6 +169,16 @@
     USB0->CTL &= ~USB_CTL_USBENSOFEN_MASK;
     // Pull up disable
     USB0->CONTROL &= ~USB_CONTROL_DPPULLUPNONOTG_MASK;
+
+    //Free buffers if required:
+    for (int i = 0; i<(NUMBER_OF_PHYSICAL_ENDPOINTS - 2) * 2; i++) {
+        free(endpoint_buffer[i]);
+        endpoint_buffer[i] = NULL;
+    }
+    free(endpoint_buffer_iso[2]);
+    endpoint_buffer_iso[2] = NULL;
+    free(endpoint_buffer_iso[0]);
+    endpoint_buffer_iso[0] = NULL;
 }
 
 void USBHAL::configureDevice(void) {
@@ -200,18 +210,22 @@
     if ((flags & ISOCHRONOUS) == 0) {
         handshake_flag = USB_ENDPT_EPHSHK_MASK;
         if (IN_EP(endpoint)) {
-            endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD )] = (uint8_t *) malloc (64*2);
-            buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD )][0];
+            if (endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] == NULL)
+                endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)] = (uint8_t *) malloc (64*2);
+            buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, TX, ODD)][0];
         } else {
-            endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD )] = (uint8_t *) malloc (64*2);
-            buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD )][0];
+            if (endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] == NULL)
+                endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)] = (uint8_t *) malloc (64*2);
+            buf = &endpoint_buffer[EP_BDT_IDX(log_endpoint, RX, ODD)][0];
         }
     } else {
         if (IN_EP(endpoint)) {
-            endpoint_buffer_iso[2] = (uint8_t *) malloc (1023*2);
+            if (endpoint_buffer_iso[2] == NULL)
+                endpoint_buffer_iso[2] = (uint8_t *) malloc (1023*2);
             buf = &endpoint_buffer_iso[2][0];
         } else {
-            endpoint_buffer_iso[0] = (uint8_t *) malloc (1023*2);
+            if (endpoint_buffer_iso[0] == NULL)
+                endpoint_buffer_iso[0] = (uint8_t *) malloc (1023*2);
             buf = &endpoint_buffer_iso[0][0];
         }
     }
--- a/USBMSD/USBMSD.cpp	Tue Sep 10 15:14:55 2013 +0300
+++ b/USBMSD/USBMSD.cpp	Thu Sep 12 14:45:27 2013 +0100
@@ -67,8 +67,12 @@
     stage = READ_CBW;
     memset((void *)&cbw, 0, sizeof(CBW));
     memset((void *)&csw, 0, sizeof(CSW));
+    page = NULL;
 }
 
+USBMSD::~USBMSD() {
+    disconnect();
+}
 
 
 // Called in ISR context to process a class specific request
@@ -117,6 +121,7 @@
     if (BlockCount > 0) {
         BlockSize = MemorySize / BlockCount;
         if (BlockSize != 0) {
+            free(page);
             page = (uint8_t *)malloc(BlockSize * sizeof(uint8_t));
             if (page == NULL)
                 return false;
@@ -130,6 +135,12 @@
     return true;
 }
 
+void USBMSD::disconnect() {
+    //De-allocate MSD page size:
+    free(page);
+    page = NULL;
+    USBDevice::disconnect();
+}
 
 void USBMSD::reset() {
     stage = READ_CBW;
--- a/USBMSD/USBMSD.h	Tue Sep 10 15:14:55 2013 +0300
+++ b/USBMSD/USBMSD.h	Thu Sep 12 14:45:27 2013 +0100
@@ -74,6 +74,15 @@
     */
     bool connect();
 
+    /**
+    * Disconnect the USB MSD device.
+    */
+    void disconnect();
+    
+    /**
+    * Destructor
+    */
+    ~USBMSD();
 
 protected:
 
--- a/USBSerial/CircBuffer.h	Tue Sep 10 15:14:55 2013 +0300
+++ b/USBSerial/CircBuffer.h	Thu Sep 12 14:45:27 2013 +0100
@@ -29,6 +29,10 @@
         buf = (T *)malloc(size * sizeof(T));
     };
 
+    ~CircBuffer() {
+        free(buf);
+     }
+
     bool isFull() {
         return ((write + 1) % size == read);
     };