2014-03-13 05:20 PM
Hello,
I am trying to use the Virtual Comm Port (VCP) capability in the
STM32 USB Host Device Library V2.1.0 on a STM32F207. But I am relatively new to implementing a USB device and the STM32 USB libraries, so I would appreciate any comments on the following points:
I am building a device that is to log text based data then upload it to a purpose written application when plugged into a (Widows based) Host PC. As I see it, I want to implement this connection as a Virtual Comm Port over USB, for the following reasons:
- I can use ST’s Virtual Comm Port Driver at the PC end and not have to write any sort of USB driver code at the PC end - my purpose written PC application only needs to know how to operate a (virtual) serial port,
- I have significant experience at communicating with embedded microcontrollers across simple RS-232 serial interfaces, which I understand the VCP connection will emulate, so I expect to be able to use my established techniques,
- And of-course, RS-232 ports on PCs have become rare and USB is built into the STM32…
I assume my thinking is all reasonable so far, though if anyone thinks differently I would be interested to know your thinking on this.
In practice, I will need to send commands (tens of bytes) from the Host PC to the STM32 and receive kilobytes or megabytes of data back (in some sort of block protocol, perhaps 1Kbyte at a time). So, the first question is, what sort of transfer rate (STM32 to PC) can I realisticly expect ? I understand the serial port baud rate is meaningless on a USB VCP connection, but I believe there is a limit (on USB 2.0 and using the STM32’s OTG_FS port) of about 6Kbyte per second, due to bandwidth sharing on the USB connection. Is this correct ? If it is correct, am I likely to achieve this, or in practice are there other factors that are likely to limit the throughput further ? 6Kbyte per second is acceptable, though faster would be better - should I be considering a different USB based approach ?
Also, I have been experimenting with ST’s VCP driver, and I don’t quite seem to understand how to use the VCP_TxData function. I understand I use this method to copy the data I want to send to the Host PC into a ring buffer, and the VCP driver periodically transfers the data I have placed in this buffer up to the Host PC. But how do I know when I can add more data to the buffer – how do I know when the buffer is full, or empty ? How do I avoid overwriting data in the buffer that hasn’t been sent yet ? It seems to me (and some entries on this forum seem to confirm this) that it is simply assumed you wont generate outgoing data faster than the VCP driver can dispose of it, but in my case, that is not really correct. While I could simply make the ring buffer bigger, that’s not really the answer, is it ? Shouldn’t there be a way to test if the buffer is full or empty or how much space is currently available in it ? If this function has been provided, I seem to have missed it, so I would appreciate anybody explaining to me how this works. Or if this function hasn’t been provided, I would appreciate anybody explaining how they have dealt with this problem.
Thanking you for your assistance,
mai-au.2014-03-14 06:04 AM
Hi
''I am building a device that is to log text based data then upload it to a purpose written application when plugged into a (Widows based) Host PC.
'' I just want to check : Do you understand that you need to implement USB OTG? The STM32 needs to be a USB VCP host when connected to the loging thing. The STM32 needs to be a USB CDC device when connected to the PC2014-03-14 01:12 PM
A virtual comm port is really a USB CDC device. No driver is necessary for Win/Linux/iOS. Driver is supplied. For Windows, you will need to create your own .inf file and google shows many templates.
When you say data, do you mean binary data, or ascii strings representing that data? If so, just OpenFile() the device and read/write, if ascii the you can use the SerialPort class. See Jan Axelson web siteThe only way to go faster is to use a High Speed device such as a STM32F4xx device.Full speed is 64 bytes per packet, high speed is 512 bytes per packet.you need to develop a command parser for the device to do the appropriate work for each command. Then build the response receiver2014-03-14 07:24 PM
Hello sung.chen_chung,
Thank you for your interest in my question – you were also good enough to respond to my last question on the forum about the STM32 ADC in single conversion mode…
Anyway, regarding today’s questions, it seems I didn’t explain myself quite clearly enough. My STM32 based device is the device logging the data (after obtaining it from some other equipment that it is permanently controlling via some GPIO pins and one of it’s UARTs, then recording the data into a SD-Card via it’s SDIO interface). (The SD-Card is being used only as a mass memory, and will be permanently installed inside the STM32 device – it cannot be removed from the device to read it in a PC and I am not implementing a file system on it). Instead, the data logged on the SD-Card will be retrieved via the STM32 responding to commands received across the USB interface when the USB interface is connected to the Host PC.
So, the USB interface will be used only to (infrequently) connect the STM32 based device to a Host PC, to allow the Host PC to send commands to the STM32 to manage the device it is controlling and to retrieve the logged data, and to allow the STM32 to send command responses and the logged data back to the Host PC. At this stage, I plan to implement this communication across the USB via a Virtual Comm Port connection and drivers.
So, as I see it, the STM32 will only need to operate it’s USB Port as a Device, supporting a VCP connection ?
Perhaps I am miss-understanding something, but I am a little bit confused by your mention of ''USB VCP Host'' or ''USB CDC Device''. I believe I am clear about the meanings of Host and Device (essentially is the STM32 an ordinary ''device'' connecting to a ''host'' PC, or is the STM32 ''pretending'' to be a ''host'' PC and connecting to another ordinary ''device''). But I thought CDC and VCP mean much the same thing, in that I though VCP (Virtual Comm Port) was a sub-type of CDC (Communication Device Class) ? You seem to suggest they are different things ? I would appreciate your comments on all this, and whether I am on the right track.
But in particular I am concerned over my question about how to avoid overwriting the ''IN'' buffer provided by the ST USB Library’s VCP_TxData function, given that in my case I can provide data faster than the VCP interface can probably transmit it to the host. It seems to me I need to dig a bit deeper into the USB library case, and replace the VCP_TxData function with new code that interacts more deeply with the library code. Can anyone verify this, or is there a better way (i.e. some of ''buffer is ready'' or ''data has been sent'' indication) that I am missing ?
Thanking you,
mai-au.
2014-03-14 08:15 PM
Hello rocketdawg,
Thank you for your interest in my questions. Working through your comments:
I understand I need to install ST’s “STSW-STM32102 STM32 Virtual COM Port Driver� (downloadable from the ST web-site) to allow a (Windows XP) PC to recognize the STM32 VCP as implemented using ST’s “STM32 USB Host Device Library V2.1.0� on a STM32F207. This has, in fact, been my experience – no STM32 Virtual COM Port Driver installed on PC –> unrecognized USB device, with STM32 Virtual COM Port Driver installed on PC -> new Virtual Comm Port appears on PC when STM32 device is plugged in. This seems to me to be the correct approach, or am I missing something ?
My data, by the time it is logged, is all ascii strings (lines of text terminated with carriage-returns). I am very familiar with processing this sort data across real serial ports (UART / RS-232 / RS-485), and I hope, for the time being, that I can use pretty much my existing techniques and code across a USB Virtual Comm Port.
The STM32F2 device I am using does support High Speed, and I understand High Speed USB should be faster than Full Speed USB, but for the moment Full Speed is where I’m at (USB 2.0 is the standard I am working to, there are other devices, cables, hubs, etc, involved). For the moment VCP across Full Speed USB will probably be fast enough, if it delivers something like 6Kbyte (or more) per second. I was hoping somebody could confirm if this was actually achievable in practice, and/or what throughput they have obtained using a STM32F2 with ST’s STM32 USB Host Device Library V2.1.0 and a Windows PC with ST’s STM32 Virtual COM Port Driver, or similar.
I understand I could get better throughput over USB2.0 if I use a different USB protocol to VCP, such as the Mass Storage Class, but for the moment, VCP and “simulated serial ports� is where I’m at (there’s always the next version…).
I understand the need for the command parser, etc.- I have been there and done that many times, and in fact I have existing code ready to roll (if VCP more or less simulates an “ordinary� serial port).
So at present I am interested in knowing what throughput I can realistically expect to achieve through a USB VCP connection implemented as above (USB 2.0 Full Speed, using ST’s drivers at the STM32 and the Host PC), and in particular, what other people know about managing the flow of data into ST’s VCP_TxData function at the STM32 end, and how to tell when this function is ready to receive more data without overwriting data in the “IN� buffer that hasn’t bent sent to the Host PC yet.
I would appreciate anybody’s thoughts and/or experiences on these matters.
Thanking you,
mai-au.
2014-03-14 08:41 PM
Hi again sung.chen_chung,
Just to clarify, I mentioned in my original post that I am using the “STM32 USB Host Device Library V2.1.0�. In fact the download file is actually called “stm32_f105-07_f2_f4_usb-host-device_lib.zip� and ST’s website refers to it as the “STM32F105/7, STM32F2 and STM32F4 USB on-the-go Host and device library� (see
http://www.st.com/web/en/catalog/tools/PF257882
#). But when you unzip the file, it unzips into a folder called “STM32_USB-Host-Device_Lib_V2.1.0�, which is how I (mistakenly ?) identified the library in my original post…
As you are probably aware, this library contains the USB_OTG driver, and the USB_Device_Library and the USB_Host_Library – the two libraries are built on-top-of the OTG driver. Perhaps you were trying to tell me this in your response to my original post, when you said “…you need to implement USB OTG�. I am in fact using the USB_OTG_Driver and the USB_Device_Library from this package, in line with what I explained in my first reply to your first reply to my original post...
I hope this will help clarify what I am doing / talking about.
Thank you again for you interest.
mai-au.
2014-03-17 05:36 AM
Hi
''So, as I see it, the STM32 will only need to operate it’s USB Port as a Device, supporting a VCP connection ?'' Yes, this is correct then. I did not understand your long description of what you had in the first post. '' But I thought CDC and VCP mean much the same thing, in that I though VCP (Virtual Comm Port) was a sub-type of CDC (Communication Device Class) ?'' There is no definite meaning to CDC and VCP! VCP - Virtual Comm Port - often refers to the Virtual Comm driver on a PC to talk to a USB CDC device. CDC - Communication Device Class - often refers to the USB device implementation on an embedded device. ''It seems to me I need to dig a bit deeper into the USB library case, and replace the VCP_TxData function with new code that interacts more deeply with the library code. Can anyone verify this, or is there a better way (i.e. some of ''buffer is ready'' or ''data has been sent'' indication) that I am missing ?'' Yes, that is correct. There does not seem to be a mechanism for the 'send to host' to know if the buffer has been over-written. I am not sure what triggers the 'send to host' to be triggered. I have not had the time to investigate it myself. Someone did post to suggest where to look.2014-03-17 06:02 AM
Hi again
This thread is talking about how to achieve max throughput via USB https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fSTM32Discovery%2fUSB%20HID%20or%20%20CDC&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https%3A%2F%2Fmy.st.com%2Fp...