STM32 STM32CubeF4 USB CDC operation
Asked Answered
C

2

5

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.

Corina answered 13/5, 2015 at 23:37 Comment(0)
P
7

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.

Peak answered 15/5, 2015 at 23:37 Comment(2)
Most wonderful and thank you so much, that is exactly the starting point I had been looking for. Given the complexity of USB I was unable to see or find this. FWIW, RS-232 is what I grew up with.Corina
It is certainly not as simple as a "voltage change" on the lines. There is a whole serial transmission stack involved for this. However, you get what you ask for and some ppl may prefer simple answers for complex systems. Recommending to read the documentation and work through the software stack is certainly not tutoring, but a good response in general. And for a complex system even more.Plossl
P
2

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.

Plossl answered 14/5, 2015 at 0:27 Comment(1)
I just wanted someone to put their finger on the specific piece of code that starts it off. In main.c I see, MX_USB_DEVICE_Init, and I follow that and I see, USBD_Start, and I follow that and I see, USBD_LL_Start, and I follow that and I see, HAL_PCD_Start, and I follow that and I see, USB_DevConnect. This last one, the comment is thta it starts the USB OTG Device.Corina

© 2022 - 2025 — McMap. All rights reserved.