cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CUBEIDE USB optimization level on USB CDC mode

enginehonestfan
Associate II

Dear Developers,

I am encountering an error and would like to determine if it is related to my code or an issue with the ST USB library.

Initially, I suspected that the problem might be caused by another part of my program, which is quite large and uses FreeRTOS. However, after simplifying the program as shown below, it appears that the issue lies within the ST USB code library.

I am using an STM32F429BIT6 chip with STM32CubeMX version 6.12 and F4 pack version 1.28.1. The core clock is set to 168MHz with a 25MHz external clock, and the USB clock is set to 48MHz. I added USB functionality, trying both HS and FS modes (individually and simultaneously), and included CDC support.

The problem arises when I use any optimization level (O1 to O3) in my program. Without optimization, the same code works correctly, whether FreeRTOS is enabled or not. With optimization, however, the mentioned code fails to work, regardless of whether FreeRTOS is used. In the working scenario, the PC successfully enumerates the USB device, and data transfer is fine. In the non-working scenario, Windows detects some activity on the ports, but it doesn’t show any device or even an error message indicating a failed descriptor request—only a refresh of the Device Manager screen.

I have searched the forums and found that some variables in the library might need to be declared with the volatile keyword but are not.

Does anyone have suggestions on how to resolve this issue?

5 REPLIES 5
Pavel A.
Evangelist III

Does anyone have suggestions on how to resolve this issue?

Obviously, try what suggested on these forums: declare these variables volatile? Have you tried this?

You can enable optimization on most code and disable only on suspicious modules.

 

@Pavel A. I think you may remember this Post of the forum: but it seems developers have changed that code and volatile keyword has been added to that post. So it seems the only problem remains is that optimization change the timing and it is bad for a library, it seems it is based on delay, rather using timers for making delays, since this time either the optimizer omit those delays, or change the way converting from c code to assembly so make it non-working

Hi @enginehonestfan 

Indeed, wait loops in MW can be affected by optimization. Replacing these loops with timer-based delays can provide more consistent timings but may be challenging in some use cases. 

Small heap can cause issues with memory allocation. You can start with increasing heap size.

In GCC, you can use __asm__ __volatile__ ("nop"); to insert a no-operation instruction that the optimizer cannot remove that might help debug.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

@FBL  Ok, but that's a library. And a library from ST meant that it has been tested by many experts and it works in different situations. or the nonworking situations has been documented.  But it seems testing stage has been out-sourced to developers and testers outside of ST headquarter :grinning_face:.

Moreover, library like USB combined with something like Chan Fatfs (Using the native one in ST cube) need a lot of time of testing, let alone different classes that USB have ( Host like HID (keyboard mouse printer etc) MSC, and act as device like MSC and other devices which connect through USB ).

To an extent, I should add that SD card combined with Chan fatfs  (Using the native one in ST cube) works in different situation, no matter what is the optimization level, no matter freertos is present or not.

enginehonestfan
Associate II

@FBL  I have found that the reason arise from updating old projects. If one create project purely by the newest software, that problem wont happen and all things work correctly, On the contrary, if the project is created by the past versions and then upgraded, it wont make the device descriptor code and hence, although the code is created by the latest version, it doesn't shown in device manager, here is a minimal project.
https://github.com/vahidajalluian/USB_Test_Not_working
https://github.com/vahidajalluian/USB_TEST_Working