<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic USB device CDC driver gets stuck after disconnection in MQX Software Solutions</title>
    <link>https://community.nxp.com/t5/MQX-Software-Solutions/USB-device-CDC-driver-gets-stuck-after-disconnection/m-p/345668#M11140</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have come across a problem with the MQX device CDC driver getting into a situation where it will successfully receive characters but not send them.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The problem arises if the device (MQX) is disconnected from the host (e.g. a PC) while the port is open and data is in flight. Upon reconnection the device enumerates correctly; the host can open the port; characters sent from the host are correctly received by the device; but characters sent by the application on the device just queue up in the driver and are never sent to the host.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Since this problem happens only after disconnection and reconnection, you will likely only see it if the device is self-powered. If it is bus-powered then it will power-down on disconnection and reboot on reconnection, and everything is fine. However, even with a bus-powered device you might see it if you get a USB error severe enough to cause a bus reset, e.g. an electrical transient due to EMC or ESD.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The problem is that some initialisation of the queues associated with the IN and OUT endpoints happens only in USB_Class_CDC_Init(), i.e. one-time when the driver is installed. These queues (specifically the head and tail pointers which are named bin_consumer and bin_producer) need to be reinitialised after reconnection. I have found an effective place to do this is in USB_Class_CDC_Event(), in the code that handles the USB_APP_ENUM_COMPLETE event.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here the patch for MQX/usb/device/source/classes/cdc/usb_cdc.c&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="c++" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_14229968260032044" jivemacro_uid="_14229968260032044"&gt;
&lt;P&gt;Index: usb_cdc.c&lt;/P&gt;
&lt;P&gt;===================================================================&lt;/P&gt;
&lt;P&gt;--- usb_cdc.c (revision 541)&lt;/P&gt;
&lt;P&gt;+++ usb_cdc.c (revision 542)&lt;/P&gt;
&lt;P&gt;@@ -340,6 +340,9 @@&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; default : break;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;+&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cdc_obj_ptr-&amp;gt;ep[count].bin_consumer = 0;&lt;/P&gt;
&lt;P&gt;+&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cdc_obj_ptr-&amp;gt;ep[count].bin_producer = 0;&lt;/P&gt;
&lt;P&gt;+&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* set the EndPoint Status as Idle in the device layer */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* (no need to specify direction for this case) */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (void)_usb_device_set_status(cdc_obj_ptr-&amp;gt;controller_handle,&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This applies to MQX 4.1.1 but I imagine this problem has been present in other releases and a similar fix would apply. This change has completely fixed the problem that I was seeing.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 03 Feb 2015 21:14:19 GMT</pubDate>
    <dc:creator>matthewkendall</dc:creator>
    <dc:date>2015-02-03T21:14:19Z</dc:date>
    <item>
      <title>USB device CDC driver gets stuck after disconnection</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/USB-device-CDC-driver-gets-stuck-after-disconnection/m-p/345668#M11140</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have come across a problem with the MQX device CDC driver getting into a situation where it will successfully receive characters but not send them.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The problem arises if the device (MQX) is disconnected from the host (e.g. a PC) while the port is open and data is in flight. Upon reconnection the device enumerates correctly; the host can open the port; characters sent from the host are correctly received by the device; but characters sent by the application on the device just queue up in the driver and are never sent to the host.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Since this problem happens only after disconnection and reconnection, you will likely only see it if the device is self-powered. If it is bus-powered then it will power-down on disconnection and reboot on reconnection, and everything is fine. However, even with a bus-powered device you might see it if you get a USB error severe enough to cause a bus reset, e.g. an electrical transient due to EMC or ESD.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The problem is that some initialisation of the queues associated with the IN and OUT endpoints happens only in USB_Class_CDC_Init(), i.e. one-time when the driver is installed. These queues (specifically the head and tail pointers which are named bin_consumer and bin_producer) need to be reinitialised after reconnection. I have found an effective place to do this is in USB_Class_CDC_Event(), in the code that handles the USB_APP_ENUM_COMPLETE event.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here the patch for MQX/usb/device/source/classes/cdc/usb_cdc.c&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="c++" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_14229968260032044" jivemacro_uid="_14229968260032044"&gt;
&lt;P&gt;Index: usb_cdc.c&lt;/P&gt;
&lt;P&gt;===================================================================&lt;/P&gt;
&lt;P&gt;--- usb_cdc.c (revision 541)&lt;/P&gt;
&lt;P&gt;+++ usb_cdc.c (revision 542)&lt;/P&gt;
&lt;P&gt;@@ -340,6 +340,9 @@&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; default : break;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;+&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cdc_obj_ptr-&amp;gt;ep[count].bin_consumer = 0;&lt;/P&gt;
&lt;P&gt;+&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cdc_obj_ptr-&amp;gt;ep[count].bin_producer = 0;&lt;/P&gt;
&lt;P&gt;+&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* set the EndPoint Status as Idle in the device layer */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* (no need to specify direction for this case) */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (void)_usb_device_set_status(cdc_obj_ptr-&amp;gt;controller_handle,&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This applies to MQX 4.1.1 but I imagine this problem has been present in other releases and a similar fix would apply. This change has completely fixed the problem that I was seeing.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Feb 2015 21:14:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/USB-device-CDC-driver-gets-stuck-after-disconnection/m-p/345668#M11140</guid>
      <dc:creator>matthewkendall</dc:creator>
      <dc:date>2015-02-03T21:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: USB device CDC driver gets stuck after disconnection</title>
      <link>https://community.nxp.com/t5/MQX-Software-Solutions/USB-device-CDC-driver-gets-stuck-after-disconnection/m-p/345669#M11141</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Matthew:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for your sharing.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Have a nice day,&lt;BR /&gt;Daniel&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Feb 2015 02:12:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/MQX-Software-Solutions/USB-device-CDC-driver-gets-stuck-after-disconnection/m-p/345669#M11141</guid>
      <dc:creator>danielchen</dc:creator>
      <dc:date>2015-02-06T02:12:51Z</dc:date>
    </item>
  </channel>
</rss>

