STM32 gotchas
238. SPI/PWM etc. at high data rate does not work or does not work properly, if its output pins are not set to high speed in GPIO

IO pins in STM32 have implemented a variable slew output, depending on the GPIO_OSPEEDR setting for given pin. The selection of slew rates are typically given as "Low speed", "Medium speed", "Fast speed", "High speed"; or in older documents as "nominal frequencies" i.e. maximum frequencies at a fairly high loading capacitance (particular values for different loading capacitances are then given in the IO AC characteristics table in the respective datasheets).

The particular set of nominal slew rates depend on the given STM32 family, and generally there are 3 or 4 selections. For example in the higher-end part of 'F4 family the "nominal" frequencies are 2MHz/25MHz/50MHz/100MHz (where the highest rates require the compensation cell to be switched on). As another example, in 'F0 it's 2MHz/10MHz/50MHz.

The default setting of GPIO_OSPEEDR is such that the lowest slew rate is selected, usually around 2MHz 1. That means, that if a peripheral such as SPI or PWM is to operate at higher speeds/baudrate, GPIO_OSPEEDR setting has to be changed (and if needed, the compensation cell switched on).

Users sometimes are not aware of this requirement, and are caught by surprise if the given peripheral does not work properly, or at all.


1. Why is the lowest output slew rate the default, and why are there lower than the highest slew rate used at all? Lower output switching slew rate results in reduction of the power supply current peaks during switching, and that in turn decreases power supply and ground noise. Too high slew rate may also cause reflections and "ringing", if signals are not terminated appropriately.