Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STM32 STM32CubeF4 USB CDC operation

I built the code from the STM32CubeF4 for the USB CDC example. I added the missing receive code for CDC_Receive_FS() in usbd_cdc_if.c. I loaded this into my STM32F4 Discovery and it works. A character typed on Tera Term returns and is displayed on Tera Term.

I am hoping that someone here, could give me some knowledge about how this USB CDC firmware works, specifically, is this being driven by an interrupt that is generated when there is a level shift in voltage on the USB -D and +D pins, or is there an infinite while loop that was launched somewhere, and it's just polling waiting for some data to appear? What prompted my question is that I see that one can blink the LEDs on this board by toggling the state of the GPIO pins within an infinite while loop in main.c. However, there is nothing within this while loop at all within main.c for USB. So how does this USB CDC firmware get and send a character from/to Tera Term.

like image 453
Richard Avatar asked May 13 '15 23:05

Richard


People also ask

Does STM32 support USB?

The STM32 USB hardware and software are compliant with USB1. 1 and USB2.

What is USB OTG FS?

Abstract: USB On-the-Go (OTG) allows two USB devices to talk to each other without requiring the services of a personal computer. Although OTG appears to add \"peer to peer\" connections to USB, it does not. Instead, USB OTG retains the standard USB host/peripheral model, where a single host talks to USB peripherals.

What is CDC ECM?

This embedded USB Communications Device Class – Ethernet Control Module subclass (CDC-ECM) class driver is used to send and receive Ethernet frames over USB. The package is a function device implementation of this class. It emulates an Ethernet device but uses USB instead of Ethernet as the medium.

What is CDC ACM?

The CDC (ACM) class in the USB Component is used for data communication. You can typically use it in applications that previously used a serial COM or UART communication. Refer to: CDC: Communication Device Class for an overview of the CDC class.


2 Answers

I will take the 2 minutes to answer you instead of lecturing you. Receive is done through interrupts. Very, very simply, the hardware sees the voltage change on the D+/D- and flags an interrupt based on the intialization functions. The interrupt calls HAL_PCD_IRQHandler, which calls USBD_LL_DataInStage in the usbd_conf.c file. That ends up calling the function USBD_CDC_DataIn in the usbd_cdc.c file. There is your starting point, but it is not simple. To do what you want you might have to stop the output to UART and just handle it in the main loop.

like image 128
Rick Avatar answered Nov 15 '22 12:11

Rick


This question is to broad for this forum and not an actual question for a specific problem. However, as some hints, you might

  • Read the USB-specs, at least some basic overview (just start at wikipedia). USB does not work by toogling a GPIO in software (see next point)
  • Read the STM32F4xx reference manual. This is quite comprehensive.
  • Read the source code of the demo. This should answer all questions.
  • To track execution paths, you should remember that C always starts with the main() function, so this is a good start to see what's going on. (disclaimer: I know pretty well, it starts with startup, but this might confuse a beginner even more).

If you want to work with USB, you will have to do this all anyway, so you might start with it as well right now. Yes, this will take some time; no surprise, engineers have learned all this for years before they start with larger projects.

All information is available legal and for free on the web.

And, yes, USB is most likely interrupt-driven and might also use DMA to transfer data.

like image 36
too honest for this site Avatar answered Nov 15 '22 11:11

too honest for this site