Skip to main content
Marc1
Associate III
Oct 21, 2026 5:10 PM UTC
Question

STM32L052 random TIM2_CR1 register reset

Hi there,

I’m using STM32L052 for PWM generation with TIM2.

TIM2->CR1 is initialized once at start of the program and never touched again during runtime:

TIM2->CR1 = 0x00000081;

Even though, in some random cases TIM2->CR1 switches back to 0. How can that happen? How can TIM2->CR1 accidentally switch back to 0? Is there a known issue? I really don’t know how to debug such an error. TIM2->CR1 is the only register that changes randomly.

Thanks in advance for your help!

 

32 replies

TDK
Oct 21, 2026 5:24 PM UTC

CR1 does not spontaneously change back to 0. If the clock is disabled, it may be read back as 0. Or if the chip is reset, it will have its reset value.

Instrument your code to find out where/when it gets changed. It’s possible a bad pointer or a stack overflow is causing the issue. Unfortunately, hardware watchpoints do not work on registers.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Marc1
Marc1Author
Associate III
Oct 21, 2026 6:01 PM UTC

Hi TDK,

Thank you for your response.

> If the clock is disabled, it may be read back as 0.
Clock is still running: APB1ENR = 0x98800001

> Or if the chip is reset, it will have its reset value.
Chip reset would also reset all other registers.

> It’s possible a bad pointer or a stack overflow is causing the issue.
This was my first thought and I’m still searching.

TIM2->CR1 is at address 0X4000 0000 and there is no register or RAM below.
Address range 0X2000 2000 - 0X3FFF FFFF is marked as “reserved”.

gbm
Super User
Oct 21, 2026 6:41 PM UTC

Check all the occurrences of TIM2 symbol in your code. I suspect some incorrect register assignment or DMA transfer setup.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Marc1
Marc1Author
Associate III
Oct 22, 2026 7:06 AM UTC

The error does not occur on every device. Some devices are affected and on other devices the error never occur.

Associate II
Oct 22, 2026 7:25 AM UTC

how many pcs has this error while how many pcs do not have this error? Any trigger point?

Marc1
Marc1Author
Associate III
Oct 22, 2026 7:48 AM UTC

About 5% have that error (100 / 2000).

waclawek.jan
Super User
Oct 22, 2026 9:12 AM UTC

>  in some random cases TIM2->CR1 switches back to 0

How do you know?

If running with debugger attached, you can place a data breakpoint (“watchpoint”) on this register. It will then break the execution a couple of instructions after the point where this register was written, in most cases allowing to observe what code was it.

TIM2_CR1 can be reset also by resetting selectively TIM2 through RCC_APB1RSTR.TIM2RST bit; that wouldn’t affect registers of other peripherals, but it would clear all TIM2 registers.

JW

Marc1
Marc1Author
Associate III
Oct 22, 2026 10:08 AM UTC

> How do you know?

I read it.

> TIM2_CR1 can be reset also by resetting selectively TIM2 through RCC_APB1RSTR.TIM2RST bit; that wouldn’t affect registers of other peripherals, but it would clear all TIM2 registers.

Other TIM2 registers are not cleared, only TIM2->CR1.

waclawek.jan
Super User
Oct 22, 2026 11:27 AM UTC

> I read it.

Using debugger?

If yes, then I’d suggest to try placing watchpoint on that register, as I’ve mentioned above.

[EDIT] Now I see @TDK above to say that watchpoints don’t work on registers - don’t they? I did use them successfully in the past, but in Cortex-M4, not Cortex-M0+. However, I don’t see why wouldn’t they work there either, provided they are available at all in that architecture.

JW

Marc1
Marc1Author
Associate III
Oct 27, 2026 3:44 PM UTC

The error does not occur very often, so it is difficult to catch it with a hardware debugger.

A couple of customers are currently using a debug firmware. If the error occurs (if the timer stops), the customer can trigger a register read and send the results to us.

Maybe the following information about the application firmware could be relevant:

(a) The system clock changes frequently during runtime. For power saving reasons the device always uses the lowest possible clock frequency. System clock source changes between MSI and HSI and also the clock dividers change.

(b) The device is able to go into deep sleep mode. In this case, the device disables TIM2 clock (RCC_APB1ENR_TIM2EN), while TIM2 stays enabled (TIM_CR1_CEN). After waking up, TIM_CR1_CENTIM2 is set again.

rich.g.williams
Senior
Oct 27, 2026 7:13 PM UTC

If there is a DMA channel set up for TIM2 with an active linked list it can rewrite TIM2->CR1 if its configured so to do.

Bill's Notes
Marc1
Marc1Author
Associate III
Oct 28, 2026 2:31 AM UTC

There is a DMA channel, linked to TIM2 CCR1 register. It is used for wave form generation.

DMA1_Channel2->CPAR = (uint32_t) &TIM2->CCR1;

TIM2->CCR1 is the destination address.

DMA1_Channel2->CCR |= DMA_CCR_DIR;

rich.g.williams
Senior
Oct 28, 2026 4:13 AM UTC

​@Marc1 

Ah so there is an area in code containing a lookup table with the waveform in it and DMA continuously updates the value in TIM2_CR1 for it probably.

Hal does this by using a "linked list" to control reload, it is explained in the reference. I suggest you look for something that can corrupt the linked list held in RAM maybe, some maths fluke etc, you could try using a different counter to narrow the problem down etc or some other method to converge on the problem 

Bill's Notes
gbm
Super User
Oct 28, 2026 6:30 AM UTC

Looks like an error in the code reconfiguring the timer. Very likely the problem originates from using bitwise (boolean) operations on CR1 instead of assignments - just my wild guess.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Marc1
Marc1Author
Associate III
Oct 28, 2026 11:21 AM UTC

The software only touches TIM2->CR1 once during the init phase of the program.

rich.g.williams
Senior
Oct 28, 2026 6:43 AM UTC

​@Marc1 

I had a look at RM0376 and realise that STM32L052 DMA is simpler, it still has a circular mode.

“DMA channel x configuration register (DMA_CCRx)”   “Bit 5 CIRC: circular mode,  0: disabled,  1: enabled”  So circular mode will spin through the lookup table (in flash or ram) and constantly update “TIMx capture/compare register 1 (TIMx_CCR1)” 

“CCR1 is the value to be loaded in the actual capture/compare 1 register (preload value).”  With this in mind its unlikely that the DMA will also touch TIM2_CR1 register ??

TIM2_CR1 = 0x00000081;  so that’s “Bit 7 ARPE: Auto-reload preload enable” and  “Bit 0 CEN: Counter enable”

“in some random cases TIM2_CR1 switches back to 0”  if this is correctly happening could you write a very simple code (part of main loop) insertion that does for example “read TIM2_CR1, if TIM2_CR1 not equal to 0x00000081 then turn on a LED and leave it on? To prove during run time.

What possibly could write to TIM2_CR1 and why?  a) Some rogue piece of code (eg interrupt corrupting something) b) the complex system clock changes during runtime  c) “System clock source changes between MSI and HSI and also the clock dividers change” Are you sure that your code checks carefully any wait flags as these changes are made? 

Why would it affect “About 5% have that error (100 / 2000).”  I’m thinking slight differences in clock frequencies, suggest you bring out MSI, HSI16. HSI48 to port pins and check their exact frequencies on the 5% bad versus the 95% good,    using a high stab frequency counter,   you might see a pattern, if yes you see a pattern try  heating or cooling the device might make the fault more likely to occur ?? or even try an adjustable external clock.   

The only other thing I can think of is Flash Wait states - is it possible some marginal situation is reading code incorrectly.

Rather than totally rely on the debugger think of ways you could insert your own simple code to check (like turning the LED on as above). 

What I do often is copy code from Flash into SRAM at reset time and execute from SRAM - maybe not practical for STM32L052?  You could do a CRC check on Flash code during run time and update - any CRC error to turn that LED mentioned above on?

 

 

 

 

 

Bill's Notes
Marc1
Marc1Author
Associate III
Oct 28, 2026 11:57 AM UTC

The DMA should not touch TIM2->CR1, but only TIM2->CCR1, from my understanding.

I don’t have access to many of the affected devices. But I wrote a workaround. The workaround now checks frequently the content of TIM2->CR1. If it is not equal to 0x00000081 it writes a flag into the EEPROM and sets back TIM2->CR1 to 0x00000081. This is not a fix, but is intended to calm down the customers until the error is fixed.

I’m sharing my system clock functions, which I use for changing the system clock frequency during runtime:

#include "define.h"
#include "system_clock.h"

const uint32_t system_clock_freq[] = \
{
// 1048000,
// 1000000,
2097000,
2000000,
4194000,
4000000,
8000000,
16000000,
32000000,
64000000
};

void system_clock_config_start(void)
{
RCC->APB1ENR |= RCC_APB1ENR_PWREN;
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;

// Enable LSI oscillator
SET_BIT(RCC->CSR, RCC_CSR_LSION);

system_clock_config_update(SYSTEM_CORE_CLOCK_USB);
}

void system_clock_config_USB(void)
{
if (SystemCoreClock != SYSTEM_CORE_CLOCK_USB)
{ system_clock_config_update(SYSTEM_CORE_CLOCK_USB); }

// Enable USB HSI48 oscillator
SET_BIT(RCC->CRRCR, RCC_CRRCR_HSI48ON);
SYSCFG->CFGR3 |= SYSCFG_CFGR3_ENREF_HSI48;

// Wait until HSI48 oscillator is ready
while ((RCC->CRRCR & RCC_CRRCR_HSI48RDY) != RCC_CRRCR_HSI48RDY) {}

// select RC48 clock as HSI48 clock
RCC->CCIPR |= RCC_CCIPR_HSI48SEL;

// USB clock recovery
RCC->APB1ENR |= RCC_APB1ENR_CRSEN;
CRS->CFGR = (CRS->CFGR & ~CRS_CFGR_SYNCSRC) | (0b10 << CRS_CFGR_SYNCSRC_Pos);
CRS->CR |= CRS_CR_CEN | CRS_CR_AUTOTRIMEN;
}

void system_clock_config_batt(void)
{
// deselect RC48 clock as HSI48 clock
RCC->CCIPR &= ~RCC_CCIPR_HSI48SEL;

// Disable USB HSI48 oscillator
CLEAR_BIT(RCC->CRRCR, RCC_CRRCR_HSI48ON);
SYSCFG->CFGR3 &= ~SYSCFG_CFGR3_ENREF_HSI48;

// Disable USB clock recovery
RCC->APB1ENR &= ~RCC_APB1ENR_CRSEN;
CRS->CR &= ~(CRS_CR_CEN | CRS_CR_AUTOTRIMEN);

system_clock_config_update(SYSTEM_CORE_CLOCK_BATT);
}

void system_clock_config_update(uint32_t sys_clock)
{
while ((PWR->CSR & PWR_CSR_VOSF) == PWR_CSR_VOSF);

// MSI clock
if ((sys_clock == 1048000) || (sys_clock == 2097000) || (sys_clock == 4194000))
{
PWR->CR = PWR_CR_VOS_1; // range 2
while ((PWR->CSR & PWR_CSR_VOSF) == PWR_CSR_VOSF);

// select frequency
if (sys_clock == 1048000)
{ MODIFY_REG(RCC->ICSCR, RCC_ICSCR_MSIRANGE, RCC_ICSCR_MSIRANGE_4); }

else if (sys_clock == 2097000)
{ MODIFY_REG(RCC->ICSCR, RCC_ICSCR_MSIRANGE, RCC_ICSCR_MSIRANGE_5); }

else if (sys_clock == 4194000)
{MODIFY_REG(RCC->ICSCR, RCC_ICSCR_MSIRANGE, RCC_ICSCR_MSIRANGE_6); }

// Enable MSI oscillator
SET_BIT(RCC->CR, RCC_CR_MSION);

// Wait until MSI oscillator is ready
while ((RCC->CR & RCC_CR_MSIRDY) != RCC_CR_MSIRDY) {}

// Switch to MSI oscillator
MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_MSI);

// Wait until the switch is done
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_MSI) {}

// Disable the PLL
CLEAR_BIT(RCC->CR, RCC_CR_PLLON);

// Wait until the PLL is fully stopped
while ((RCC->CR & RCC_CR_PLLRDY) == RCC_CR_PLLRDY) {}

// Switch the HSI oscillator off
CLEAR_BIT(RCC->CR, RCC_CR_HSION);
}

// HSI clock
else
{
if (sys_clock == 32000000) { PWR->CR = PWR_CR_VOS_0; } // range 1
else { PWR->CR = PWR_CR_VOS_1; } // range 2

while ((PWR->CSR & PWR_CSR_VOSF) == PWR_CSR_VOSF);

// Enable HSI oscillator
SET_BIT(RCC->CR, RCC_CR_HSION);

// Wait until HSI oscillator is ready
while ((RCC->CR & RCC_CR_HSIRDY) != RCC_CR_HSIRDY) {}

// Switch to HSI oscillator
MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_HSI);

// Wait until the switch is done
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI) {}

if (sys_clock <= 4000000) { SET_BIT(RCC->CR, RCC_CR_HSIDIVEN); }
else { CLEAR_BIT(RCC->CR, RCC_CR_HSIDIVEN); }

if (sys_clock >= 16000000)
{
// Flash latency 1 wait state
SET_BIT(FLASH->ACR, FLASH_ACR_LATENCY);
}

else { CLEAR_BIT(FLASH->ACR, FLASH_ACR_LATENCY); }

// 32 MHz HSI with PLL
if (sys_clock == 32000000)
{
// 32 MHz using the 16 MHz HSI oscillator multiply by 4 divide by 2
RCC->CFGR |= RCC_CFGR_PLLSRC_HSI | RCC_CFGR_PLLMUL4 | RCC_CFGR_PLLDIV2;

// Enable PLL
SET_BIT(RCC->CR, RCC_CR_PLLON);

// Wait until PLL is ready
while ((RCC->CR & RCC_CR_PLLRDY) != RCC_CR_PLLRDY) {}

// Select PLL as clock source
MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL);

// Wait until the switch is done
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) {}
}

else
{
// Disable the PLL
CLEAR_BIT(RCC->CR, RCC_CR_PLLON);

// Wait until the PLL is fully stopped
while ((RCC->CR & RCC_CR_PLLRDY) == RCC_CR_PLLRDY) {}
}

// Switch the MSI oscillator off
CLEAR_BIT(RCC->CR, RCC_CR_MSION);
}

if ((sys_clock == 8000000) || (sys_clock == 2000000))
{ MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_CFGR_HPRE_DIV2); }

else if (sys_clock == 1000000)
{ MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_CFGR_HPRE_DIV4); }

else { MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_CFGR_HPRE_DIV1); }

// Update variable
SystemCoreClock = sys_clock;
}

 

waclawek.jan
Super User
Oct 28, 2026 8:48 AM UTC

(b) The device is able to go into deep sleep mode.

 

Which one is it in terms of Table 32 - Low-power sleep?

 

In this case, the device disables TIM2 clock (RCC_APB1ENR_TIM2EN), while TIM2 stays enabled (TIM_CR1_CEN).

 

You may have utilized the RCC_APB1SMENR.TIM2SMEN bit - you’d need to clear it only once, at initial configuration, it’s a mask applied to clocks in sleep modes.

 

After waking up, TIM_CR1_CENTIM2 is set again.

 

How, exactly?

 

Generally, you may want to increase the frequency of occurence of the problem, by stressing the system as much as possible. For example, try to increase the frequency of stimulus upon which the device goes into sleep, dramatically. Will that increase occurence of the problem? 

JW

Marc1
Marc1Author
Associate III
Oct 28, 2026 11:41 AM UTC

> Which one is it in terms of Table 32 - Low-power sleep?

I guess it is called “STOP” mode”.

PWR->CR |= PWR_CR_CWUF | PWR_CR_LPSDSR;
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
__WFI();
 

It wakes up by LP-timer interrupt every 2 s. But the error does not occur during WAKE - STOP transisions.

The device also frequently goes into “low-power sleep” mode.

PWR->CR |= PWR_CR_CWUF | PWR_CR_LPSDSR;
__WFI();

It wakes up on any interrupt, at least every 10 ms.
 

> How, exactly?

RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;