Timer reset using RCC
The problem: if TIM2 is used prior to my code, then it gets stuck at a certain point, even though all the registers are properly set.
* Here is the configuration of TIM2 registers when it works fine:
(gdb) x/21 $TIM2_BASE
0x40000000: 0x00000001 0x00000000 0x00000000 0x00000003
0x40000010: 0x00000000 0x00000000 0x00000000 0x00000000
0x40000020: 0x00000002 0x00024c10 0x0000059f 0xffffffff
0x40000030: 0x00000000 0x00026e8c 0x00000000 0x00000000
0x40000040: 0x00000000 0x00000000 0x00000000 0x00000001
0x40000050: 0x00000000
where 0x24c10 is the increasing counter, and 0x26e8c the next comparison.
* If I run my code after a bootloader that also uses TIM2, then I get this:
0x40000000: 0x00000001 0x00000000 0x00000000 0x00000003
0x40000010: 0x00000000 0x00000000 0x00000000 0x00000000
0x40000020: 0x00000002 0x0000249d 0x0000059f 0xffffffff
0x40000030: 0x00000000 0x00004bab 0x00000000 0x00000000
0x40000040: 0x00000000 0x00000000 0x00000000 0x00000001
0x40000050: 0x00000000
* At startup of my code (post-bootloader), all registers are null and TIM2 clock is disabled:
(gdb) x/21 $TIM2_BASE
0x40000000: 0x00000000 0x00000000 0x00000000 0x00000000
0x40000010: 0x00000000 0x00000000 0x00000000 0x00000000
0x40000020: 0x00000000 0x00000000 0x00000000 0x00000000
0x40000030: 0x00000000 0x00000000 0x00000000 0x00000000
0x40000040: 0x00000000 0x00000000 0x00000000 0x00000000
0x40000050: 0x00000000
(gdb) x $RCC_BASE + $RCC_APB1ENR
0x40023840: 0x00000000
* Enable TIM2 clock in RCC => registers are non-null, because TIM2 was used beforehand
(gdb) set *(uint32_t*)($RCC_BASE + $RCC_APB1ENR ) = 0x01
(gdb) x/21 $TIM2_BASE
0x40000000: 0x00000001 0x00000000 0x00000000 0x00000002
0x40000010: 0x00000000 0x00000000 0x00000000 0x00000000
0x40000020: 0x00000000 0x005b8da1 0x0000006b 0xffffffff
0x40000030: 0x00000000 0x005b9183 0x00000000 0x00000000
0x40000040: 0x00000000 0x00000000 0x00000000 0x00000001
0x40000050: 0x00000000
* Reset TIM2 via RCC => registers = default values
(gdb) set *(uint32_t*)($RCC_BASE + $RCC_APB1RSTR) = 0x01
(gdb) set *(uint32_t*)($RCC_BASE + $RCC_APB1RSTR) = 0x00
(gdb) x/21 $TIM2_BASE
0x40000000: 0x00000000 0x00000000 0x00000000 0x00000000
0x40000010: 0x00000000 0x00000000 0x00000000 0x00000000
0x40000020: 0x00000000 0x00000000 0x00000000 0xffffffff
0x40000030: 0x00000000 0x00000000 0x00000000 0x00000000
0x40000040: 0x00000000 0x00000000 0x00000000 0x00000000
0x40000050: 0x00000000
* Do the usual configuration -> timer is stuck at 0x249d
(I wondered why register +0x4c (TIMx_DMAR) copies register +0x00 (TIMx_CR1), but the first reply answers it).
In any cases, how am I supposed to reset TIM2 properly so that I can use it again?
