STM32 gotchas
125. USB/DFU bootloader in 'F4/'F7 may start unreliably with higher frequency crystals

The default bootloader in system memory conveniently provides several ways to load a program into an STM32. In most models with USB, the bootloader incorporates also an option to use USB DFU bootloader.

However, as USB requires a higher clock precision than that provided by the internal RC oscillators of STM32 (HSI, MSI), in models which don't support "crystal-less USB", the DFU bootloader requires a crystal to be connected to the HSE pins. Frequency of the crystal cannot be arbitrary, and AN2606 describes the particular requirements on crystal frequencies for individual STM32 models.

For the 'F4/'F7 families, the crystal has to have frequency which is integer multiple of 1MHz, between 4MHz and 26MHz. The exact mechanism how bootloader detects, which of the possible crystals is present, is the following:

This method involves several rounding and granularity errors. As the basis of measurement is the ratio between HSE/HSI, the errors get more pronounced as this ratio decreases i.e. HSE frequency increases. As long as HSI is within the 1% precision given by factory calibration, this method correctly measures all HSE values between 4MHz and 26MHz. However, the 1% factory calibration is guaranteed only for temperature of 25°C. HSI frequency varies quite significantly with temperature (which is the temperature of the silicon die, and that may be surprisingly higher from ambient by quite a couple of degrees very quickly).

At around 2% of HSI error, measurements of the highest frequencies (25MHz, 26MHz) start to fail. The consequence is, that the bootloader sets up PLL incorrectly, thus USB module is not clocked at 48MHz and the difference is too big for USB to work successfully.

ST recognizes this problem, as in AN2606, the descriptions for 'F4 and 'F7 bootloaders contain the following footnote:

Due to HSI deviation and since HSI is used to detect HSE value, the user must use low frequency rather than high frequency HSE crystal values (low frequency values are better detected due to larger error margin). For example, it is better to use 8 MHz instead of 25 MHz.

Indeed, the measurement for 8MHz HSE is good up to cca 6.5% error of HSI. For the maxumum error of HSI given by datasheet as -8% at the highest temperature range, only 4MHz and 5MHz are "safe" values.


This issue has been mentioned here, in the context of using the DFU bootloader as indication of whether the hardware USB connection for a 'F411 is OK. Interestingly - and unfortunately - this problem with the USB/DFU bootloader makes this an unreliable "test"/indicator, if a higher-frequency crystal is used (25MHz in this case). The original manufacturer of the popular "Black pill" board in question also recognizes the problem and recommends to "heat up" the chip in case of problems with the DFU bootloader to 25°C.

The problem of granularity of measurement in bootloaders is not new/unique. Here for example is description of autobauding problem in bootloader of a venerable 8051 derivative, resulting in similar unreliability of starting bootloader, puzzling users maybe 20 years ago...