Timers only running correctly in Debug mode and not Release mode on STMF411
Our project is run on the STM32F411E-DISCO board running under Segger Embedded Studio. All Timers TIM2/TIM5/TIM9/TIM10/TIM11 are being used. Under a debug session everything runs correctly. If we leave the board programmed with the debug code and power it up without the J-Link connection attached everything runs correctly. However, if we run a release build then the 32-bit timers run correctly but the 16-bit timers do not. From what we can see the 16-bit timers are running too fast as if the check we do for the rollover TIMx->SR & TIM_SR_UIF is always returning true. We looked at the RCC register settings in a debug session and none of the RCC or APB1/2ENR registers appear to have any bits enabled that weren't set by our code.
This issue was actually solved while this post was being written, so this is being posted anyway in case someone else runs into the same issue. Our initialization code runs generically for all timers and the final two lines are:
t->SR = (uint16_t)0x00;
t->CR1 |= TIM_CR1_CEN;
If the lines are swapped then the code runs as expected for both debug and release settings. This behavior has been seen on other ST processors where enabling the timer immediately caused UIF to assert, whether anything to do with the timer interrupts are enabled or not. For the F411 this appears to only be affecting the 16-bit timers with the 32-bit timers being fine, but why the debug build of the code is blocking this behavior is a mystery.
