Showing results for 
Search instead for 
Did you mean: 

The printf function causes code execution to hang in the USBH_UserProcess function.

Senior II

A USB drive is connected to the STM32F407VGT6.

The USBH_UserProcess function is located in the FatFs library for USB drives.

I wrote this code in the USBH_UserProcess function:


uint8_t Array[1000]={0};
for(int i=0; i<1000;i++)
printf("%d\n", Array[i]);


This code is located in the USBH_UserProcess function, in the usb_host.c file.
More details, including the USBH_UserProcess function code:



uint8_t Array[1000]={0};
for(int i=0; i<1000;i++)
printf("%d\n", Array[i]);



The USB is configured and working correctly, there are no problems with this.

The first error is that printf does not output anything to the console.
The second error is that this code freezes the execution of the code.

Four factors must coincide for such an error to occur.

1. The "Array" must be large. My Array size is 1000 bytes.
2. The "Array" must be in the USBH_UserProcess function. If you make the "Array" array global, the error disappears.
3. In the "for" loop, the increment must have a large number. I have i<1000;
4. You need to use printf("%d\n", Array[i]); in a cycle.

I need all 4 factors.
As I already wrote, the error disappears if you make the "Array" array global.

Has anyone encountered such a problem?
Fortunately, as I already wrote, I found a solution, but I’m wondering why this error occurs.

Chief II

The difference is that a local variables are created on a stack memory. Creating such a large variables on a stack memory can be justified in some special cases, but not in general.

Take a note that for ST's parody of a USB stack the functions must be called only from an interrupt of the same priority as an USB interrupt or from a critical section with the USB interrupt blocked. Also their FatFS implementation is outdated and broken. For a decent solution move to TinyUSB, take an up to date FatFS and implement it's driver code by yourself.

I created the same function with a "for" loop and a large local array in main.c.

In the main.c file this function works without problems.