2
\$\begingroup\$

I'm playing around with STM32F4 Discovery board and libopencm3. I want to send data acquired from ADC channels over usb and display some charts on the computer. After doing some research on USB class codes I'm a bit confused: which one should I choose for my application? At first I was thinking that USB-CDC it the way to go, since it's easier to implement but I don't think that it's the optimal solution.

\$\endgroup\$
  • 3
    \$\begingroup\$ ADC? Try USB Audio. Makes it way easy to access from many applications. \$\endgroup\$ – Passerby Jul 19 '14 at 1:03
  • \$\begingroup\$ From what I understand USB audio requires a defined sample rate. Will it cause problems if I choose a sample rate faster than 196kHz? \$\endgroup\$ – Ashton H. Jul 19 '14 at 1:15
  • \$\begingroup\$ I would say HID, purely from a "it sounds right" POV. \$\endgroup\$ – Ignacio Vazquez-Abrams Jul 19 '14 at 1:59
  • 2
    \$\begingroup\$ A single isochronous transfer can carry 1024 bytes, and can carry at most 256 samples (at 24/32 bits). This means that a single isochronous endpoint can transfer 42 channels at 48 kHz, or 10 channels at 192 kHz (assuming that High Speed USB is used - Full Speed USB cannot carry more than a single stereo IN and OUT pair at 48 kHz). see edn.com/design/consumer/4376143/Fundamentals-of-USB-Audio \$\endgroup\$ – Passerby Jul 19 '14 at 2:15
3
\$\begingroup\$

The USB CDC class is easy to implement and use, since on the PC side it creates a virtual COM port. You can easily test the interface with a program like RealTerm, which can display either ASCII or binary data. You will need to write a program to capture the data, convert it to ASCII, and save to a file for input to a spreadsheet program like Excel to do the charting.

The only gotcha is the COM port number that is assigned can't usually be predicated in advance so you have to have some sort of configuration file that specifies which one to use (once the system chooses a COM port for the USB device, it should continue to use the same one after that).

You don't mention what data rates you need to support, or how long a sample. At 115K baud, you can send one ADC sample (12 bits, expanded to 16) in a little under 175 µS, or about 5000 kHz. I'm guessing you need something faster than that. Since you have 192K RAM available, you can buffer up to 90,000 or so samples. At 44.1 kHz, that's a little over two seconds.

If you are going to sample first and then send, you might find the HID class even easier to use. It can present itself as a virtual keyboard, so there is no software to write on the PC side. You can open up a program like Excel, and as long as the firmware in the STM32F4 is sending the data as ASCII characters followed by a carriage/line feed for each sample, each line will be entered as an additional row in the spreadsheet, ready to graph.

\$\endgroup\$
  • \$\begingroup\$ Well explained, thank you. I guess I will stick to CDC for now and switch to HID after I get more acquainted with USB stack. \$\endgroup\$ – Ashton H. Jul 19 '14 at 15:55
  • \$\begingroup\$ Is there yet any way to have a CDC device with a custom vid/pid automatically use drivers which will exist in a typical Windows machine, as HID devices have been able to do from Day One? HID might not offer quite as much performance as a CDC would be able to, but there's a lot to be said for "plug and play". \$\endgroup\$ – supercat Jul 19 '14 at 18:04
  • \$\begingroup\$ @supercat I'm sure the reason all HID devices can use the same driver, is that there was a strict standard for the protocol for keyboard and mice so any BIOS could use them without special drivers. This was not a requirement for CDC devices, and different hardware required different drivers. After a while, the most popular brands (e.g. FTDI) got their drivers included with Windows; but the little guys have to ship a disk along with their product or provide a web address for a download. \$\endgroup\$ – tcrosley Jul 19 '14 at 18:46
  • \$\begingroup\$ As of 2018, Windows 10 does ship with out-of-the box support for USB CDC devices (as virtual COM ports). \$\endgroup\$ – minexew Dec 10 '18 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.