STM32G431 Encoder Mode Issue


I have configured the timer clock to 170MHz. According to the datasheet, the maximum fENC value is 42.5MHz when the timer clock is 170MHz, and the tW is specified as 2~3Tck.
I am using a quadrature encoder with two channels that provides 1024 pulses per revolution.
I have set TIM4 to encoder mode, as shown in the attached image, and I am monitoring the htim4->Instance->CNT value using the debugger.
The issue is that when I turn the motor slowly by hand, the counter works correctly, counting from 0 to 4095 as expected. However, when I start rotating it faster, the counter starts missing counts, and at higher speeds, the counter doesn't increase at all.
I'm not sure what the issue could be. Where should I start looking to troubleshoot this problem?
