cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 USB CDC problem

Cyada.1
Associate II

I'm working on a project based on STM32l496rg and trying to send data from USB CDC to terminal. I`ve created a project in STM32CubeIDE (the latest version available), where I`ve set up clocks, enabled USB_FS_Device and USB CDC middleware and all the settings remained default.

After this, I`ve generated code in STM32 Cube IDE, compiled and run with making only one changes into the code which is in Write function in syscalls.c file.

0693W000002li0lQAA.jpg

 if I try to call CDC_Transmit_FS through printf, then it returns USBD_OK on first few times calling and then after USBD_BUSY on all the next calls. However, I cannot receive anything on terminal.

So, what I`m doing wrong, how to troubleshoot USB CDC connection and why STM32CubeIDE generated code gives such error?

#STM32CubeIDE​ #[STM32 MCUs]​ #STM32L4​ #USB​ #cdc 

1 ACCEPTED SOLUTION

Accepted Solutions
Access/modification of the TxState variable is not done in a thread-safe manner.
One fix is to disable interrupts while it is called.
Another fix is to call it only within one scope, such as the main thread.
If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

5 REPLIES 5
TDK
Guru

> because of connection error

Explain what you mean by this. Does an error message show up somewhere?

If you feel a post has answered your question, please click "Accept as Solution".
Cyada.1
Associate II

Thanks for reply, ​that means, I'm not receiving data on my terminal through usbcdc after some time. That means it's properly work 1 or 2 minutes(received proper data on terminal) and then stop working(not received any data on terminal). After I checked in my code i found the error occurred which is USBD_BUSY. And connection error that means it's return USBD_BUSY.

I'm also used one interrupt ( which is another sensor interrupt) in my project , when interrupt trigger on that time I'm send data on my terminal through usbcdc( for confirmation interrupt trigger or not), its work properly sometimes and then it stop working usb cdc that means stop receiving data on my terminal.

I also noticed one thing, when I use interrupt on that time it's stop working (stop receiving data on terminal)after some time interval. ​

TDK
Guru

Are you calling CDC_Transmit_FS in both the interrupt and in the main thread? That will cause issues eventually. It is not thread-safe.

If you feel a post has answered your question, please click "Accept as Solution".
Cyada.1
Associate II

Thanks for reply,

Yes I'm calling CDC_Transmit_FS in both ​main function and in interrupt function. Then tell me how to fix this. Is it possible or not.

>That will cause issues eventually. It is not thread-safe.

I didn't get you exactly. Please give me some explanation about it why is it not working. And what is the reason behind it.

Access/modification of the TxState variable is not done in a thread-safe manner.
One fix is to disable interrupts while it is called.
Another fix is to call it only within one scope, such as the main thread.
If you feel a post has answered your question, please click "Accept as Solution".