USB device stack, with KL25Z fixes for USB 3.0 hosts and sleep/resume interrupt handling

Dependents:   frdm_Slider_Keyboard idd_hw2_figlax_PanType idd_hw2_appachu_finger_chording idd_hw3_AngieWangAntonioDeLimaFernandesDanielLim_BladeSymphony ... more

Fork of USBDevice by mbed official

This is an overhauled version of the standard mbed USB device-side driver library, with bug fixes for KL25Z devices. It greatly improves reliability and stability of USB on the KL25Z, especially with devices using multiple endpoints concurrently.

I've had some nagging problems with the base mbed implementation for a long time, manifesting as occasional random disconnects that required rebooting the device. Recently (late 2015), I started implementing a USB device on the KL25Z that used multiple endpoints, and suddenly the nagging, occasional problems turned into frequent and predictable crashes. This forced me to delve into the USB stack and figure out what was really going on. Happily, the frequent crashes made it possible to track down and fix the problems. This new version is working very reliably in my testing - the random disconnects seem completely eradicated, even under very stressful conditions for the device.

Summary

  • Overall stability improvements
  • USB 3.0 host support
  • Stalled endpoint fixes
  • Sleep/resume notifications
  • Smaller memory footprint
  • General code cleanup

Update - 2/15/2016

My recent fixes introduced a new problem that made the initial connection fail most of the time on certain hosts. It's not clear if the common thread was a particular type of motherboard or USB chip set, or a specific version of Windows, or what, but several people ran into it. We tracked the problem down to the "stall" fixes in the earlier updates, which we now know weren't quite the right fixes after all. The latest update (2/15/2016) fixes this. It has new and improved "unstall" handling that so far works well with diverse hosts.

Race conditions and overall stability

The base mbed KL25Z implementation has a lot of problems with "race conditions" - timing problems that can happen when hardware interrupts occur at inopportune moments. The library shares a bunch of static variable data between interrupt handler context and regular application context. This isn't automatically a bad thing, but it does require careful coordination to make sure that the interrupt handler doesn't corrupt data that the other code was in the middle of updating when an interrupt occurs. The base mbed code, though, doesn't do any of the necessary coordination. This makes it kind of amazing that the base code worked at all for anyone, but I guess the interrupt rate is low enough in most applications that the glitch rate was below anyone's threshold to seriously investigate.

This overhaul adds the necessary coordination for the interrupt handlers to protect against these data corruptions. I think it's very solid now, and hopefully entirely free of the numerous race conditions in the old code. It's always hard to be certain that you've fixed every possible bug like this because they strike (effectively) at random, but I'm pretty confident: my test application was reliably able to trigger glitches in the base code in a matter of minutes, but the same application (with the overhauled library) now runs for days on end without dropping the connection.

Stalled endpoint fixes

USB has a standard way of handling communications errors called a "stall", which basically puts the connection into an error mode to let both sides know that they need to reset their internal states and sync up again. The original mbed version of the USB device library doesn't seem to have the necessary code to recover from this condition properly. The KL25Z hardware does some of the work, but it also seems to require the software to take some steps to "un-stall" the connection. (I keep saying "seems to" because the hardware reference material is very sketchy about all of this. Most of what I've figured out is from observing the device in action with a Windows host.) This new version adds code to do the necessary re-syncing and get the connection going again, automatically, and transparently to the user.

USB 3.0 Hosts

The original mbed code sometimes didn't work when connecting to hosts with USB 3.0 ports. This didn't affect every host, but it affected many of them. The common element seemed to be the Intel Haswell chip set on the host, but there may be other chip sets affected as well. In any case, the problem affected many PCs from the Windows 7 and 8 generation, as well as many Macs. It was possible to work around the problem by avoiding USB 3.0 ports - you could use a USB 2 port on the host, or plug a USB 2 hub between the host and device. But I wanted to just fix the problem and eliminate the need for such workarounds. This modified version of the library has such a fix, which so far has worked for everyone who's tried.

Sleep/resume notifications

This modified version also contains an innocuous change to the KL25Z USB HAL code to handle sleep and resume interrupts with calls to suspendStateChanged(). The original KL25Z code omitted these calls (and in fact didn't even enable the interrupts), but I think this was an unintentional oversight - the notifier function is part of the generic API, and other supported boards all implement it. I use this feature in my own application so that I can distinguish sleep mode from actual disconnects and handle the two conditions correctly.

Smaller memory footprint

The base mbed version of the code allocates twice as much memory for USB buffers as it really needed to. It looks like the original developers intended to implement the KL25Z USB hardware's built-in double-buffering mechanism, but they ultimately abandoned that effort. But they left in the double memory allocation. This version removes that and allocates only what's actually needed. The USB buffers aren't that big (128 bytes per endpoint), so this doesn't save a ton of memory, but even a little memory is pretty precious on this machine given that it only has 16K.

(I did look into adding the double-buffering support that the original developers abandoned, but after some experimentation I decided they were right to skip it. It just doesn't seem to mesh well with the design of the rest of the mbed USB code. I think it would take a major rewrite to make it work, and it doesn't seem worth the effort given that most applications don't need it - it would only benefit applications that are moving so much data through USB that they're pushing the limits of the CPU. And even for those, I think it would be a lot simpler to build a purely software-based buffer rotation mechanism.)

General code cleanup

The KL25Z HAL code in this version has greatly expanded commentary and a lot of general cleanup. Some of the hardware constants were given the wrong symbolic names (e.g., EVEN and ODD were reversed), and many were just missing (written as hard-coded numbers without explanation). I fixed the misnomers and added symbolic names for formerly anonymous numbers. Hopefully the next person who has to overhaul this code will at least have an easier time understanding what I thought I was doing!

Revision:
50:946bc763c068
Parent:
49:03527ce6840e
Child:
51:666cc4fedd3f
--- a/USBDevice/USBDevice.cpp	Fri Feb 26 18:41:47 2016 +0000
+++ b/USBDevice/USBDevice.cpp	Wed Apr 27 01:50:32 2016 +0000
@@ -22,13 +22,47 @@
 #include "USBDevice.h"
 #include "USBDescriptor.h"
 
-//#define DEBUG
-#ifdef DEBUG
+//#define DEBUG_WITH_PRINTF
+#ifdef DEBUG_WITH_PRINTF
+// debug printf; does a regular printf() in debug mode, nothing in
+// normal mode.  Note that many of our routines are called in ISR
+// context, so printf should really never be used here.  But in
+// practice we can get away with it enough that it can be helpful
+// as a limited debugging tool.
 #define printd(fmt, ...)  printf(fmt, __VA_ARGS__)
 #else
 #define printd(fmt, ...)
 #endif
 
+// Makeshift HAL debug instrumentation.  This is a safer and better
+// alternative to printf() that gathers event information in a 
+// circular buffer for later useoutside of interrupt context, such 
+// as printf() display at intervals in the main program loop.  
+//
+// Timing is critical to USB, so debug instrumentation is inherently 
+// problematic in that it can affect the timing and thereby change 
+// the behavior of what we're trying to debug.  Small timing changes
+// can create new errors that wouldn't be there otherwise, or even
+// accidentally fix the bug were trying to find (e.g., by changing
+// the timing enough to avoid a race condition).  To minimize these 
+// effects, we use a small buffer and very terse event codes - 
+// generally one character per event.  That makes for a cryptic 
+// debug log, but it results in almost zero timing effects, allowing
+// us to see a more faithful version of the subject program.
+//
+// NB: Implemented only for KL25Z.
+//#define DEBUG_WITH_EVENTS
+#ifdef DEBUG_WITH_EVENTS
+extern void HAL_DEBUG_EVENT(char c);
+extern void HAL_DEBUG_EVENT(char a, char b);
+extern void HAL_DEBUG_EVENT(char a, char b, char c);
+extern void HAL_DEBUG_EVENT(const char *s);
+extern void HAL_DEBUG_EVENTF(const char *f, ...);
+#else
+#define HAL_DEBUG_EVENT(...)
+#define HAL_DEBUG_EVENTF(...)
+#endif
+
 /* Device status */
 #define DEVICE_STATUS_SELF_POWERED  (1U<<0)
 #define DEVICE_STATUS_REMOTE_WAKEUP (1U<<1)
@@ -44,106 +78,110 @@
 #define WINDEX_TO_PHYSICAL(endpoint) (((endpoint & 0x0f) << 1) + \
     ((endpoint & 0x80) ? 1 : 0))
 
-
 bool USBDevice::requestGetDescriptor(void)
 {
     bool success = false;
     printd("get descr: type: %d\r\n", DESCRIPTOR_TYPE(transfer.setup.wValue));
     switch (DESCRIPTOR_TYPE(transfer.setup.wValue))
     {
-        case DEVICE_DESCRIPTOR:
-            if (deviceDesc() != NULL)
-            {
-                if ((deviceDesc()[0] == DEVICE_DESCRIPTOR_LENGTH) \
-                    && (deviceDesc()[1] == DEVICE_DESCRIPTOR))
-                {
-                    printd("device descr\r\n");
-                    transfer.remaining = DEVICE_DESCRIPTOR_LENGTH;
-                    transfer.ptr = deviceDesc();
-                    transfer.direction = DEVICE_TO_HOST;
-                    success = true;
-                }
-            }
-            break;
-        case CONFIGURATION_DESCRIPTOR:
-            if (configurationDesc() != NULL)
+    case DEVICE_DESCRIPTOR:
+        if (deviceDesc() != NULL)
+        {
+            if ((deviceDesc()[0] == DEVICE_DESCRIPTOR_LENGTH) \
+                && (deviceDesc()[1] == DEVICE_DESCRIPTOR))
             {
-                if ((configurationDesc()[0] == CONFIGURATION_DESCRIPTOR_LENGTH) \
-                    && (configurationDesc()[1] == CONFIGURATION_DESCRIPTOR))
-                {
-                    printd("conf descr request\r\n");
-
-                    /* Get wTotalLength */
-                    transfer.remaining = configurationDesc()[2] \
-                        | (configurationDesc()[3] << 8);
+                printd("device descr\r\n");
+                transfer.remaining = DEVICE_DESCRIPTOR_LENGTH;
+                transfer.ptr = deviceDesc();
+                transfer.direction = DEVICE_TO_HOST;
+                success = true;
+            }
+        }
+        break;
 
-                    transfer.ptr = configurationDesc();
-                    transfer.direction = DEVICE_TO_HOST;
-                    success = true;
-                }
-            }
-            break;
-        case STRING_DESCRIPTOR:
-            printd("str descriptor\r\n");
-            switch (DESCRIPTOR_INDEX(transfer.setup.wValue))
+    case CONFIGURATION_DESCRIPTOR:
+        if (configurationDesc() != NULL)
+        {
+            if ((configurationDesc()[0] == CONFIGURATION_DESCRIPTOR_LENGTH)
+                && (configurationDesc()[1] == CONFIGURATION_DESCRIPTOR))
             {
-                            case STRING_OFFSET_LANGID:
-                                printd("1\r\n");
-                                transfer.remaining = stringLangidDesc()[0];
-                                transfer.ptr = stringLangidDesc();
-                                transfer.direction = DEVICE_TO_HOST;
-                                success = true;
-                                break;
-                            case STRING_OFFSET_IMANUFACTURER:
-                                printd("2\r\n");
-                                transfer.remaining =  stringImanufacturerDesc()[0];
-                                transfer.ptr = stringImanufacturerDesc();
-                                transfer.direction = DEVICE_TO_HOST;
-                                success = true;
-                                break;
-                            case STRING_OFFSET_IPRODUCT:
-                                printd("3\r\n");
-                                transfer.remaining = stringIproductDesc()[0];
-                                transfer.ptr = stringIproductDesc();
-                                transfer.direction = DEVICE_TO_HOST;
-                                success = true;
-                                break;
-                            case STRING_OFFSET_ISERIAL:
-                                printd("4\r\n");
-                                transfer.remaining = stringIserialDesc()[0];
-                                transfer.ptr = stringIserialDesc();
-                                transfer.direction = DEVICE_TO_HOST;
-                                success = true;
-                                break;
-                            case STRING_OFFSET_ICONFIGURATION:
-                                printd("5\r\n");
-                                transfer.remaining = stringIConfigurationDesc()[0];
-                                transfer.ptr = stringIConfigurationDesc();
-                                transfer.direction = DEVICE_TO_HOST;
-                                success = true;
-                                break;
-                            case STRING_OFFSET_IINTERFACE:
-                                printd("6\r\n");
-                                transfer.remaining = stringIinterfaceDesc()[0];
-                                transfer.ptr = stringIinterfaceDesc();
-                                transfer.direction = DEVICE_TO_HOST;
-                                success = true;
-                                break;
+                printd("conf descr request\r\n");
+
+                /* Get wTotalLength */
+                transfer.remaining = configurationDesc()[2] | (configurationDesc()[3] << 8);
+                transfer.ptr = configurationDesc();
+                transfer.direction = DEVICE_TO_HOST;
+                success = true;
             }
-            break;
-            
-        case INTERFACE_DESCRIPTOR:
-            printd("interface descr\r\n");
+        }
+        break;
+
+    case STRING_DESCRIPTOR:
+        printd("str descriptor\r\n");
+        switch (DESCRIPTOR_INDEX(transfer.setup.wValue))
+        {
+        case STRING_OFFSET_LANGID:
+            printd("1\r\n");
+            transfer.remaining = stringLangidDesc()[0];
+            transfer.ptr = stringLangidDesc();
+            transfer.direction = DEVICE_TO_HOST;
+            success = true;
             break;
 
-        case ENDPOINT_DESCRIPTOR:
-            /* TODO: Support is optional, not implemented here */
-            printd("endpoint descr\r\n");
+        case STRING_OFFSET_IMANUFACTURER:
+            printd("2\r\n");
+            transfer.remaining =  stringImanufacturerDesc()[0];
+            transfer.ptr = stringImanufacturerDesc();
+            transfer.direction = DEVICE_TO_HOST;
+            success = true;
+            break;
+
+        case STRING_OFFSET_IPRODUCT:
+            printd("3\r\n");
+            transfer.remaining = stringIproductDesc()[0];
+            transfer.ptr = stringIproductDesc();
+            transfer.direction = DEVICE_TO_HOST;
+            success = true;
+            break;
+
+        case STRING_OFFSET_ISERIAL:
+            printd("4\r\n");
+            transfer.remaining = stringIserialDesc()[0];
+            transfer.ptr = stringIserialDesc();
+            transfer.direction = DEVICE_TO_HOST;
+            success = true;
             break;
 
-        default:
-            printd("ERROR - unknown descriptor type in GET DESCRIPTOR\r\n");
+        case STRING_OFFSET_ICONFIGURATION:
+            printd("5\r\n");
+            transfer.remaining = stringIConfigurationDesc()[0];
+            transfer.ptr = stringIConfigurationDesc();
+            transfer.direction = DEVICE_TO_HOST;
+            success = true;
+            break;
+
+        case STRING_OFFSET_IINTERFACE:
+            printd("6\r\n");
+            transfer.remaining = stringIinterfaceDesc()[0];
+            transfer.ptr = stringIinterfaceDesc();
+            transfer.direction = DEVICE_TO_HOST;
+            success = true;
             break;
+        }
+        break;
+        
+    case INTERFACE_DESCRIPTOR:
+        printd("interface descr\r\n");
+        break;
+
+    case ENDPOINT_DESCRIPTOR:
+        /* TODO: Support is optional, not implemented here */
+        printd("endpoint descr\r\n");
+        break;
+
+    default:
+        printd("ERROR - unknown descriptor type in GET DESCRIPTOR\r\n");
+        break;
     }
 
     return success;
@@ -176,12 +214,10 @@
          * We seem to have a pending device-to-host transfer.  The host must have
          * sent a new control request without waiting for us to finish processing
          * the previous one.  This appears to happen when we're connected to certain 
-         * USB 3.0 host chip sets.  Do a zero-length send to tell the host we're not
-         * ready for the new request - that'll make it resend - and then just
-         * pretend we were successful here so that the pending transfer can finish.
+         * USB 3.0 host chip sets.  Do a zero-length send and return failure to tell 
+         * the host we're not ready for the new request.  That'll make it resend.
          */
-        uint8_t buf[1] = { 0 };
-        EP0write(buf, 0);
+        EP0write(NULL, 0);
                   
         /* execute our pending transfer */
         controlIn();
@@ -218,6 +254,7 @@
             USBCallback_requestCompleted(buffer, packetSize);
             transfer.notify = false;
         }
+
         /* Status stage */
         EP0write(NULL, 0);
     }
@@ -253,11 +290,11 @@
             transfer.notify = false;
         }
 
-        EP0read();
+       //$$$ EP0read();
         EP0readStage();
 
         /* Completed */
-        transfer.direction = HOST_TO_DEVICE;
+        //$$$transfer.direction = HOST_TO_DEVICE;
         return true;
     }
 
@@ -282,8 +319,8 @@
     transfer.remaining -= packetSize;
     
     /* are we done? */
-    if (transfer.remaining == 0)
-        transfer.direction = HOST_TO_DEVICE;
+ //$$$   if (transfer.remaining == 0)
+ //$$$       transfer.direction = HOST_TO_DEVICE;
 
     /* success */
     return true;
@@ -296,11 +333,11 @@
 
     if (transfer.setup.wValue == 0)
     {
-        device.state = DEFAULT;
+        setDeviceState(DEFAULT);
     }
     else
     {
-        device.state = ADDRESS;
+        setDeviceState(ADDRESS);
     }
 
     return true;
@@ -308,14 +345,13 @@
 
 bool USBDevice::requestSetConfiguration(void)
 {
-
+    /* Set the device configuration */
     device.configuration = transfer.setup.wValue;
-    /* Set the device configuration */
     if (device.configuration == 0)
     {
         /* Not configured */
         unconfigureDevice();
-        device.state = ADDRESS;
+        setDeviceState(ADDRESS);
     }
     else
     {
@@ -323,7 +359,7 @@
         {
             /* Valid configuration */
             configureDevice();
-            device.state = CONFIGURED;
+            setDeviceState(CONFIGURED);
         }
         else
         {
@@ -448,37 +484,37 @@
     {
         /* Endpoint or interface must be zero */
         if (transfer.setup.wIndex != 0)
-        {
             return false;
-        }
     }
 
     switch (transfer.setup.bmRequestType.Recipient)
     {
-        case DEVICE_RECIPIENT:
-            /* TODO: Currently only supports self powered devices */
-            status = DEVICE_STATUS_SELF_POWERED;
-            success = true;
-            break;
-        case INTERFACE_RECIPIENT:
+    case DEVICE_RECIPIENT:
+        /* TODO: Currently only supports self powered devices */
+        status = DEVICE_STATUS_SELF_POWERED;
+        success = true;
+        break;
+
+    case INTERFACE_RECIPIENT:
+        status = 0;
+        success = true;
+        break;
+
+    case ENDPOINT_RECIPIENT:
+        /* TODO: We should check that the endpoint number is valid */
+        if (getEndpointStallState(WINDEX_TO_PHYSICAL(transfer.setup.wIndex)))
+        {
+            status = ENDPOINT_STATUS_HALT;
+        }
+        else
+        {
             status = 0;
-            success = true;
-            break;
-        case ENDPOINT_RECIPIENT:
-            /* TODO: We should check that the endpoint number is valid */
-            if (getEndpointStallState(
-                WINDEX_TO_PHYSICAL(transfer.setup.wIndex)))
-            {
-                status = ENDPOINT_STATUS_HALT;
-            }
-            else
-            {
-                status = 0;
-            }
-            success = true;
-            break;
-        default:
-            break;
+        }
+        success = true;
+        break;
+        
+    default:
+        break;
     }
 
     if (success)
@@ -501,39 +537,49 @@
     {
         switch (transfer.setup.bRequest)
         {
-             case GET_STATUS:
-                 success = requestGetStatus();
-                 break;
-             case CLEAR_FEATURE:
-                 success = requestClearFeature();
-                 break;
-             case SET_FEATURE:
-                 success = requestSetFeature();
-                 break;
-             case SET_ADDRESS:
-                 success = requestSetAddress();
-                 break;
-             case GET_DESCRIPTOR:
-                 success = requestGetDescriptor();
-                 break;
-             case SET_DESCRIPTOR:
-                 /* TODO: Support is optional, not implemented here */
-                 success = false;
-                 break;
-             case GET_CONFIGURATION:
-                 success = requestGetConfiguration();
-                 break;
-             case SET_CONFIGURATION:
-                 success = requestSetConfiguration();
-                 break;
-             case GET_INTERFACE:
-                 success = requestGetInterface();
-                 break;
-             case SET_INTERFACE:
-                 success = requestSetInterface();
-                 break;
-             default:
-                 break;
+        case GET_STATUS:
+            success = requestGetStatus();
+            break;
+            
+        case CLEAR_FEATURE:
+            success = requestClearFeature();
+            break;
+            
+        case SET_FEATURE:
+            success = requestSetFeature();
+            break;
+            
+        case SET_ADDRESS:
+            success = requestSetAddress();
+            break;
+            
+        case GET_DESCRIPTOR:
+            success = requestGetDescriptor();
+            break;
+            
+        case SET_DESCRIPTOR:
+            /* TODO: Support is optional, not implemented here */
+            success = false;
+            break;
+            
+        case GET_CONFIGURATION:
+            success = requestGetConfiguration();
+            break;
+            
+        case SET_CONFIGURATION:
+            success = requestSetConfiguration();
+            break;
+            
+        case GET_INTERFACE:
+            success = requestGetInterface();
+            break;
+            
+        case SET_INTERFACE:
+            success = requestSetInterface();
+            break;
+            
+        default:
+            break;
         }
     }
 
@@ -594,9 +640,7 @@
             /* Transfer must be less than or equal to the size */
             /* requested by the host */
             if (transfer.remaining > transfer.setup.wLength)
-            {
                 transfer.remaining = transfer.setup.wLength;
-            }
         }
         else
         {
@@ -631,12 +675,11 @@
     /* Data or status stage if applicable */
     if (transfer.setup.wLength > 0)
     {
-        if (transfer.setup.bmRequestType.dataTransferDirection \
-            == DEVICE_TO_HOST)
+        if (transfer.setup.bmRequestType.dataTransferDirection == DEVICE_TO_HOST)
         {
             /* Check if we'll need to send a zero length packet at */
             /* the end of this transfer */
-            if (transfer.setup.wLength > transfer.remaining)
+            if (transfer.setup.wLength >= transfer.remaining)
             {
                 /* Device wishes to transfer less than host requested */
                 if ((transfer.remaining % MAX_PACKET_SIZE_EP0) == 0)
@@ -666,10 +709,19 @@
 
 void USBDevice::busReset(void)
 {
-    device.state = DEFAULT;
+    // reset the device state
+    memset(&device, 0, sizeof(device));
+    setDeviceState(DEFAULT);
     device.configuration = 0;
     device.suspended = false;
 
+    // reset the transfer state
+    memset(&transfer, 0, sizeof(transfer));
+    
+    // reset interface state
+    currentInterface = 0;
+    currentAlternate = 0;
+
     /* Call class / vendor specific busReset function */
     USBCallback_busReset();
 }
@@ -728,7 +780,7 @@
     USBHAL::disconnect();
     
     /* Set initial device state */
-    device.state = POWERED;
+    setDeviceState(POWERED);
     device.configuration = 0;
     device.suspended = false;
 }
@@ -808,6 +860,14 @@
 {
 }
 
+void USBDevice::sleepStateChanged(unsigned int sleep)
+{
+    // If we got a Sleep signal while in ADDRESS mode, it means that the
+    // initial connection setup failed.
+    if (sleep && device.state == ADDRESS)
+        connectFailed();
+}
+
 
 USBDevice::USBDevice(uint16_t vendor_id, uint16_t product_id, uint16_t product_release){
     VENDOR_ID = vendor_id;
@@ -815,7 +875,7 @@
     PRODUCT_RELEASE = product_release;
 
     /* Set initial device state */
-    device.state = POWERED;
+    setDeviceState(POWERED);
     device.configuration = 0;
     device.suspended = false;
 };