This problem is in fact a generic C-usage problem, which is linked to STM32 only coincidentally, through a programming practice favored by ST's programmers.
In the various libraries produced by ST (SPL, Cube/HAL, Cube/LL), function calls often use, as a parameter, a pointer to a struct, which contains several members detemining behaviour of the peripheral to be controlled by given function. The function then combines those struct members into one ore more values to be written into actual peripheral registers.
Sometimes users unaware of this problem define such struct as a local variable, and they fail to fill in all its members, leaving some members uninitialized. As local variables are located at stack, their content is not defined. This is mostly due to the fact, that users rarely review the definition of the given struct or read the documentation to functions they are using, and they usually focus only on members which are straighforwardly required. It also happens that users copy an example code, while defining their own local struct, being unaware of the fact that the original had the struct defined as global or explicitly initialized at definition, or not being aware of the distinction between the various forms of variable definiton.
Struct fields with undefined content then get combined into the resulting values written into the peripheral registers, resulting in wrong and very confusing behaviour of given peripheral.
If this problem happens, it may be not easy to debug. The wrongly set struct field may for example result in overwriting a field in target peripheral register entirely unrelated to the intended usage of the uninitialized struct member, as the value in that struct member may be outside of its intended range. Debugging of this problem is worsened by the fact that users use "libraries" often in order to avoid working with the "low-level" hardware, so they are naturally prejudiced against searching problems by looking at content of hardware registers, not even at the machine code which sets them.
Things are made even worse by the fact, that uninitialized local variables don't have an entirely random value; rather, their value depends on the previous history of program execution. Under certain circumstances, it may quite well happen, that value of an uninitialized struct member will be consistently 0 or any concrete number. It means, that the problem may result in a "mostly working", or "it works for me" kind of application.
A minor secondary issue with uninitialized struct members is, that it prevents the optimizer to do its job properly.
They are several potential remedies; however, the user has to be aware of the root problem:
- ST library functions have implemented sets of assertions, intended to ensure that no parameter departs from its intended range.
How meticulously are these assertions implemented, is left on conscience of ST's developers.
Also, undefined struct members may have values falling into the allowed range, yet still resulting in unwanted behaviour.
As values of undefined struct members depend on previous run history, it may also happen, that assertions won't be able to catch such problem ever in a testing environment, resulting in unpleasant surprises when deployed.
Moreover, using assertions is costly in terms of program memory and execution time. Granted, they can be switched on only during development.debugging, but also conceptually the idea of developing/debugging a different binary than is finally deployed, is patently wrong. - Structs can be defined as global variables. This results in increased consumption of RAM, especially if the struct is only for a single use, which is often the case (e.g. at mcu startup).
- Structs can be defined as explicitly initialized when defined. As initialization of a single member of function causes implicit zeroing the whole struct, it is enough to initialize one member at the struct definition, other members may be assigned later in the program. This is what CubeMX-generated functions do.
- Structs can be defined as explicitly initialized constant variable. This usually results in struct being placed into FLASH, sparing precious RAM; sometimes optimizers may be able to remove the struct itself even from FLASH.
- Compound literals can be used as parameters to those functions. This is perhaps the best solution, both in terms of cost and methodology. However, compound literals are a relatively new feature (if 20+ years of C99 can be "new") and many users are simply not aware of their existence.
Since CubeMX started to generate functions with such structs explicitly initialized to {0}, frequency of this kind of problem reported on the STM32 forum significantly decreased (e.g. an example here); but this makes it perhaps even more surprising.