How To: STM32F103C8T6 As An USB Device ( Virtual Serial Port / CDC )


How To: STM32F103C8T6 As An USB Device ( Virtual Serial Port / CDC )

Download

In this video I share my knowledge on how to create a STM32F103C8T6 project with virtual serial port (STM32 acting as USB device).

You can buy STM32 board using following affiliate links (no extra cost to you, if you use these links):
Banggood:
https://www.banggood.com/custlink/KGDGh1kL6P
EBay: https://rover.ebay.com/rover/1/711-53200-19255-0/1?ff3=4&toolid=11800&pub=5575260842&campid=5338019830&mpre=http%3A%2F%2Fwww.ebay.com%2Fsch%2Fi.html%3F_from%3DR40%26_sacat%3D0%26_nkw%3DSTM32F103C8T6%2Bboard%26_sop%3D15

EDIT: If you are running Windows version prior to 10, you may need to install drivers. http://www.st.com/en/development-tools/stsw-stm32102.html

These $2 STM32 development boards have been featured in multiple Hackaday posts past few days and that inspired me to start playing with them. I had previously installed all the development tools, but something went wrong back then and I ended up putting the boards away and kept playing with other development boards.
Now I reinstalled System Workbench for STM32 and STM32CubeMX. Everything worked flawlessly and I had project with virtual serial port (using STM32 as an USB device) running in no time.

TIMELINE
1:07 – Generating the project “skeleton” with STM32CubeMX
4:10 – Making it compile in SystemWorkbench for STM32
5:12 – Demonstrating the serial communication – Receiving data
9:24 – Demonstrating – Receiving and transfering data

Software:
STM32CubeMX: http://www.st.com/en/development-tools/stm32cubemx.html
SystemWorkbench for Stm32: http://www.st.com/en/development-tools/sw4stm32.html

Hardware:
“BluePill” STM32F103C8T6 -development board and ST-Link V2 compatible programmer/debugger. Wires between GND, SWDIO and SWDCLK pins of programmer and dev board. Board was powered through the microUSB -port.

Thanks to Al Williams for sharing his notes on using this board with SystemWorkbench for STM32: https://hackaday.io/project/20879-notes-on-using-systemworkbench-with-stm32-bluepill/log/56408-notes

If you liked the video, please show it and hit the Like-button!
Check out my other videos:
https://www.youtube.com/user/hugatry/videos
Subscribe:
https://www.youtube.com/user/hugatry?sub_confirmation=1 .

Download

source

Have any Question or Comment?

39 comments on “How To: STM32F103C8T6 As An USB Device ( Virtual Serial Port / CDC )

macos catalina无法识别USB设备

Reply

Thanks huge help man , cool

Reply

I have an error code 10 on my Virtual Com in Device Manager. I managed it to work one time but after i disconnect my usb and connect ST-Link programmator to flash new program it stops working and gives error code 10… i don't know what's the problem, it can be the USB cable but I tried on 2 cables and it still doesn't work. I'm using Win10.

Reply

i have problem with opening port in windows 10 but when i download stm vertual com port driver for windows 10 the issue was solved 🙂 hope help anybody else

Reply

There is always that dude with the accent that will help you in your hardest times 😀 Thanks mate

Reply

Is there any way that I can program my STM32F103C8T6 through a USB cable?

Reply

Thanks you so much for share this information and thanks for this tutorial.

Reply

Great video! I've been struggling to get ST-Link V2 and source code debugging (OpenOCD) to work from within SW4STM32 eclipse IDE for several days now, finally managed to get it working thanks to this video.
The two main items I've been missing all along were – 1) enabling SWD in CubeMX, and 2) manually editing the reset line in the <project>.cfg file. Thanks!

Reply

Hi, Thanks for detailed explanation. Quite useful..!I'm using TrueSTUDIO Toolchain/IDE. IDE is not detecting target (STM32F103C8T6). Do you suggest any recommended settings for TrueSTUDIO?I see only below 3 Debugger in TrueSTUDIO. What is work-around for this issue?1) ST-LINK2) SEGGER J-TRACE Cortex-M3) SEGGER J-LINK

Reply

please send me your email id, i want to learn about stm32f103c8t6, i am new to use keil uvision

Reply

how to do it in keil with codes not with cubemx?

Reply

Thank you. I followed your steps for my Stm32F4Discovery board, and the same instructions were exactly working for me. I used CubeMX 4.26.1

Reply

I get issue "Unknown USB Device (Device Descriptor Request Failed" in Device Manager.
I run on Windows 10 and the STM32 Virtual COM Port Driver is installed.

Reply

Hello friend! I hope everything still good.
Please help me if you know what could be happening to me, I am having issues when trying to upload the code to the microcontroller, I get the error:
"OpenOCD child process termination
Reason: Wrong devide detected"
I am doing exactly the same than before, and I previously could upload the code with no problems, following your steps.
I think the only thing I have made different is that once I tried to upload de code using Rigth Click > Target > Program Chip. It failed then I tried to use your method as always but now I cannot make it work again, I have tried changing a configurations but still the same. (I have also tried removing the Launch Configuration and creating a new one, changing the text in the .cfg file, etc)

In my console I get the following log:
Open On-Chip Debugger 0.10.0-dev-00010-g96aab7b (2018-07-13-06:51)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
trst_only separate trst_push_pull
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter_nsrst_delay: 100
adapter speed: 950 kHz
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v29 API v2 SWIM v7 VID 0x0483 PID 0x3748
Info : vid/pid are not identical: 0x0483/0x374B 0x0483/0x3748
Info : using stlink api v2
Info : Target voltage: 3.216284
Error: init mode failed (unable to connect to the target)
in procedure 'program'
in procedure 'init' called at file "embedded:startup.tcl", line 485
in procedure 'ocd_bouncer'
** OpenOCD init failed **
shutdown command invoked

Sorry for the long comment, I hope you can help me. Thanks sir!

Reply

Hi, Can I change to higher baud rate for example 115200. And what role of it exist in system? like one of UART? or separate CDC with special handling input/output method?

Reply

This is a fantastic tutorial. Thank you very much.

Reply

Thanks Hugatry, that's a wonderful video! I needed to learn how to do USB and this taught me instantly. It worked right out, with no compile error (I am using Atollic TrueStudio). One subtlety:

When I subsequently implemented it in my own project hardware (STM32F446), it still worked EXCEPT that the terminal text was always "LED 0" (number 0, not letter O), rather than "LED ON" or "LED OFF". It didn't worry me TOO much since I won't really use USB in the same way as this simple test, in my project – but it did worry me a bit because it's not nice to leave little mysteries sleeping in code. I couldn't understand why that worked on the Blue Pill but not on my STM32F446. So I asked my good STM32-expert friend, who replied:

"Your unsigned char data[] = "LED ON" will be created on the stack at run-time, and will only exist until execution leaves the 'if {} else {}' construct. But the USB message will only get sent some time later when the USB stack runs … but which time it is very likely that the text on the stack will have been over-written. This is a rather obscure version of the classic (and extremely common bug) caused by passing a pointer to an automatic variable back up to the calling function."

And so it does indeed turn out to be! When I replace the line with the CDC_Transmit_FS call with (for example):
CDC_Transmit_FS((uint8_t *)"LED ON", 6);
Then everything works as it should, I get "LED ON", "LED OFF" as I should.

Just a minor comment in case it helps somebody…

Many thanks again for the great video!

Hans
http://qrp-labs.com

Reply

hey nice tutorial, please make more such tutorials with CubeMx and STWorkbench. i am running your project on my Stm32f4 disco. i usb serial device need a driver update.did you have a situation like this ?

Reply

Well explained and straigh to the point. Thank you a lot for this video, saved me a couple of hours ̶y̶e̶a̶r̶s̶
.

Reply

The Best video of all manuals for SMT32!

Reply

saved me a couple of hours! thanks a lot!

Reply

Excellent video, informative and easy to follow!

Reply

I was wondering if you have experimented with any of the other USB classes. I am struggling to get a usb hid working. I think I need to set the HID ID somewhere. The examples use USBD_HID_SendReport(&hUsbDeviceFS, buffer, 8); but this does not include the ID byte at the start of the report.

Reply

Thank you for making this video. Very good explanation. I did not have the build error you mentioned.

Reply

Great video. Explains the basic information in a clear way making it easy to understand.

Reply

Extremely useful video as introduction to STM32. Inspires me to start writing some code for it myself. Thank you.

Reply

Very informative, usefully for me!
Thanks

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *