STM32 gotchas
78.Using TIM input channel filter may produce confusing results (if fed with periodic signal)

The TIM channels at their inputs incorporate a digital filter. This helps to filter out glitches and other unwanted signals, especially if the signal source is "real world" rather than other digital circuitry. The input signal is then used either to Capture the timer's counter current state, or as an input to the Slave-Mode controller.

The filter samples the input signal continuously, and if the input level is about to change, only if it sees N samples of this new level consecutively, the filter outputs this new level, otherwise its output does not change. N and the sampling frequency can be set for each channel separately in TIMx_CCMRx.ICxF.

N ranges from 2 to 8, and the sampling frequency is either equal to the internal clock of the timer, or is a fraction (1/2 to 1/32) of output of a prescaler called DTS (dead-time and sampling clock), derived from the same internal clock of timer, being equal to that clock or divided to 1/2 or 1/4, according to TIMx_CR1.CKD.

This works quite well for spurious oscillations coming from bouncing mechanical contacts of switches. However, if the input signal changes periodically, e.g. due to electromagnetic interference from a neighbouring circuit into a high-impedance circuit of a disconnected switch, the results might be strange to confusing.

If the input frequency is an exact multiple of the sampling frequency (which may easily happen if the interfering signal is derived from the same mcu clock than the timer's clock), the filter's output may be a steady level. For example, if the closed switch results in low input, but after opening the switch the interference would cause the filter sample low at every its sampling period, the net output is the same as if the switch would never open.

An even more confusing result stems from an input signal which period is slightly different from exact multiple of the sampling frequency. In such a case, the output of filter would change at a low rate, corresponding to the beat frequency between sampling and input signal, possibly misleading the program/observer to believe that the input signal is of a much lower frequency than it really is.

The corollary is, that while the input filter on timer channels may be a good tool to filter out spurious oscillations at signal's edges; where periodic input signal is expected, an external analog low-pass filter has to be included into the signal path.