Content originally posted in LPCWare by Rob65 on Tue Aug 16 23:13:29 MST 2011
J.
this all depends on the USB stack you are using (or actually not ...)
The Keil example (the one named usbcdc) shows the use of the global USB_Configuration variable (or should I say misused) to detect if the device is connected. USB_Configurations is given a value somewhere during the initialization process but I have no idea when...
I use the USB stack from Bertrik Sikken which can be found in the RDB1768cmsis examples. This stack has a number of advantages over the Keil stack. For one it has no limitations on distribution as either binary or source code (you are not allowed to distribute source code of the Keil stack) but it is also much nicer to configure.
If you look at the usb_serial example in the mentioned example set, you'll see that almost everything is placed in main_serial.c.
In the main() function USB is initialized and the interrupt handlers for the endpoints are registered.
I am not sure how familiar you are with USB, I will try (if not for you then maybe for others who read this :rolleyes:) to explain very shortly how USB is used.
During initialization the host reads the device descriptors of the device.
These descriptors contain the information about the device (ID, serial no., name, device type, ...) including information on the end points.
End points are the main interface between the USB host and your application.
For the CDC There are two end points: BULK_IN_EP and BULK_OUT_EP. The host (PC) reads the BULK_IN and writes to BULK_OUT in certain intervals (as configured in the device descriptors).
This bulk end points contain 0 or more bytes of data, any characters send towards the PC are to be placed in the IN end point (IN for the PC), any characters send from the PC are received in the OUT end point.
If you look at the source you will see that there are two functions BulkIn() and BulkOut(), these are being called by the USB stack to tell the program USB want to read or write data.
A third function that might be of interest is HandleClassRequest(). This function is called when the PC wants to configure the (virtual) serial port (e.g. set the baud rate).
This is (almost) all you need to know about USB, most USB devices work in a similar way. End points may vary in type and number and the HandleClassRequest of course contains different requests depending of the device but as you look at the different USB examples (in the RDB1768cmsis examples) you will see how this is done.
Knowing this, it is a simple task to include your own 'Yes, we are alive!" checks.
You can set a flag as soon as you the BulkIn() function is called. This then signals that the PC is now polling your device for data.
You can also set a flag when HandleClassRequest() is called, but you have to try a bit to see if, and when, this function is called. I am not sure if this function is always called or at what time it is called.
What I do not know is if there are ways to tell if an application has the device open or not. I.e: I have my device attached to the PC, open a (terminal) program that uses the device, close the program, open it again, etc...
I have not checked if this open/close sequence is visible on the lpc1754 side of the USB stack.
This should also be possible with the Keil stack but I have no idea where this is placed inside the code of that stack.
Hope this helps.
Regards,[INDENT]Rob
[/INDENT]