2024-08-30 08:38 AM - edited 2024-08-30 08:43 AM
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?
2024-08-30 02:55 PM
> 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.
2024-08-31 08:09 PM
@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
2024-09-03 10:17 AM
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.
2024-09-03 11:38 PM - edited 2024-09-04 12:01 AM
@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.
2024-09-25 09:56 AM
@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