STM32 gotchas
113. Used this Cube/SPL function and it works weird (due to uninitialized struct as parameter)

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:

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.