USB data stream stops

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

USB data stream stops

623 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mattes on Fri Mar 07 18:08:56 MST 2014
Working on the 11u14 and try to keep the USB communication (CDC) sane.
Start sending out data  to the PC and I use a simple 'cat /dev/TTYACM0' (or terminal program)
to display/receive the data. 

Once the program stops and restarts it will not receive any more data.
The debugger says it is somewhere in the USB stack.

I am currently do not have any event controls built on the 11u14
Looking into that I noticed that:
    Event_USB_Device_Connect and Event_USB_Device_Disconnect are never firing

I see other event coming though, like
   Event_USB_Device_Device_Configuration
   Event_USB_Device_ControlRequest
   Event_CDC_Device_LineEncodingChanged.

Any explanation why _connect and _disconnect are not seen.

What is the recommended way to keep the USB stack healthy and allow the PC software to
close the serial device and reopen it.

How do i know on the 11u14 side when the PC side is opening the device?

Sorry for all the Q, but I am kinda noob with USB stuff.

0 Kudos
3 Replies

468 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mattes on Wed Mar 12 11:35:37 MST 2014
I found a solution to my problem.
When closing the device/socket from the PC app,  (e.g. 'cat /dev/TTYACM0') and the reopen it later,
caused not to receive data from the usb device any longer.  See it as a sort of flow control to keep the USB stack happy. Not sure why this is necessary, (couldn't the usb stack do this itself?) but it does the trick.  

I added the following code:


#define VERSION 123
#define CDC_SET_CONTROL_LINE_STATE 0x22

volatile int32_t usbDeviceOpen = 0;
volatile int32_t promptFlag = 0;


void EVENT_USB_Device_ControlRequest(void)
{
   CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);

   switch ( USB_ControlRequest.bRequest )
   {
      case CDC_SET_CONTROL_LINE_STATE:
         if ( USB_ControlRequest.wValue ) {
              usbDeviceOpen = 1;
              promptFlag = 1;
         }
         else {
              usbDeviceOpen = 0;
         }
         break;
    }
}

main()
{
   init()

   while( 1 ) {

      if ( usbDeviceOpen && promptFlag ) {
         // say hello
         sprintf((char*)tiBuffer,"\nhello, you are connected %s\n\n", VERSION );
         len = strlen((char*)tBuffer);
         CDC_Device_SendData(&VirtualSerial_CDC_Interface, (char *)tBuffer, len);
         promptFlag = 0;

      if ( usbDeviceOpen && (counter %100000) ) {
         // make some noise
         sprintf((char*)tiBuffer,"\ncounter %d ", counter );
         len = strlen((char*)tBuffer);
         CDC_Device_SendData(&VirtualSerial_CDC_Interface, (char *)tBuffer, len);
      }
   }




Now I can start and stop my application any time without having the 11U14 hangup on me.
The app (cat /dev/ttyACM0) sets DTR when opening the device and resets DTR when closing the device.
On the lpc11u14 side this is recognized by CDC_SET_CONTROL_LINE_STATE request send to the
usb device.

hope that help other to figure it out faster then I did.
0 Kudos

468 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mattes on Sat Mar 08 11:05:48 MST 2014
I am using LPCOpen 2. Not sure how what the USB stack version is. But it is using the software stack 'lpcusblib' that comes with lpcOpen2.x. The 11u14 does not offer USB ROM support to my undertstanding.
The project is based on what is shown in  nxp_lpcxpresso_11u14_lpcusblib_VirtualSerialDevice, which implements a CDC-ACM class driver,

Started looking at http://docs.lpcware.com/nxpusblib/v0.98/group___group___events.html for more details
on this matter, and there is a lot of information. just haven't found yet anything on detecting a listening PC application (open/close socket).
I am currently using Linux for development, though I like to support Win and Mac platform as well.

Currently I am watching incoming bytes (CDC_Device_BytesReceived). The moment I received one char I assume that someone is listening and send out my data stream. And would be nice to know when the other end  stops listening (close socket) to stop sending.
  I think i could treat is like a serial device to manage DTR if that would make it work.

Though  do I really have to stop sending data when nobody is listening?
I wouldn't mind if the data just gets lost.  All I care about is that once the PC app reconnects (open socket)
that it is able to get the data again.

And that is currently not happening. the moment the pc app  disconnect and reconnect there is
no data getting accross any longer.

thanx for all your advice.
0 Kudos

468 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Tsuneo on Sat Mar 08 08:51:22 MST 2014
Unless you would specify "the USB stack", you should get just vague general answer.
Sound like one of LPCOpen (or LPCUSBlib) version, but which one?


Quote:
Any explanation why _connect and _disconnect are not seen.


Because these functions aren't implemented.
The function names appear just in comments.
Using "global search" of your editor, confirm it.


Quote:
What is the recommended way to keep the USB stack healthy and allow the PC software to
close the serial device and reopen it.

How do i know on the 11u14 side when the PC side is opening the device?


On major platform (Linux, Windows and MacOS), nothing is sent to USB device implicitly, when PC application opens/closes a COM port (**1). Therefore, your PC application has to send a signal explicitly.

DTR signaling is often applied for this purpose.
On a terminal app, you may enable "hardware handshake (DTR-DSR)".

Of course, a little customization is required on "the USB stack"
If you would give the exact version of "the USB stack", we may discuss on the implementation details.


(**1)
As an exception, Windows CloseHandle() drops DTR by sending SetControlLineState.
But CreateFile() puts nothing.

Tsuneo
0 Kudos