STM32 gotchas
232.What happens if LPTIM_CMP >= LPTIM_ARR?

The description of LPTIM_ARR in RM requires it to be set strictly higher than LPTIM_CMP. What happens if we disobey this rule?

As we already know, values of LPTIM_CMP between 0..(LPTIM_ARR - 1) correspond to pulse lengths of 1..LPTIM_ARR while the PWM period is (LPTIM_ARR + 1) (see also footnote there for discussion on "pulse" and its polarity).

If LPTIM_CMP is set to LPTIM_ARR, we observe a pulse of the expected length of (LPTIM_ARR + 1); however, the one tick long zero level interval after the pulse remains. In other words, the overall period is prolonged to (LPTIM_ARR + 2). By increasing LPTIM_CMP further, the period also increases and is equal to (LPTIM_CNT + 2), regardless of LPTIM_ARR value.

The inner working of LPTIM is probably this: at the active kernel clock edge, the previous value of LPTIM_CNT is latched, and starts to be incremented - note that the LPTIM counter is asynchronous i.e. it's a ripple counter. If the latched-LPTIM_CNT < LPTIM_CMP, compare is not achieved and (if LPTIM_CFGR.WAVPOL is set, as described previously), PWM output is high (this is the active portion of "pulse"). Otherwise, compare is achieved and pulse is finished; but only when this state is reached, in the next clock edge (and if needed, subsequent edges) is the latched-LPTIM_CNT compared to LPTIM_ARR to determine the cycle end.

This behaviour of course does not impact only the PWM output, but also the overall timing of LPTIM. The increased period may be surprising if LPTIM_ARR-match (overflow) interrupts are used for timing, for example.

As RM requires this setting is never used, what we describe here is just an anecdotal result of experiments on an 'L4, and not to be relied upon in practice. However, this information provides insight into the LPTIM's inner working, and it can also be helpful when debugging "weirdly behaving" LPTIM due to inadvertently incorrectly set LPTIM_CMP or LPTIM_ARR.