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

Associate II
Oct 28, 2026 11:17 PM UTC

Failure scenario: It wakes up by LP-timer interrupt every 2 s. But the error does not occur during WAKE - STOP transisions. It wakes up on any interrupt, at least every 10 ms. 

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

Possible cause: 

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.

 

Please try the code below from AI feedback and check if it is helpful

===============================================================================================

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

// Valid system core frequency list, 64MHz removed (not supported by STM32L0)
const uint32_t system_clock_freq[] =
{
2097000,
2000000,
4194000,
4000000,
8000000,
16000000,
32000000
};
#define SYS_CLOCK_CNT (sizeof(system_clock_freq)/sizeof(system_clock_freq[0]))

/**
* @brief Check whether input clock frequency is supported
* @param clk: target system clock frequency
* @retval true: valid, false: invalid
*/
static bool system_clock_is_valid(uint32_t clk)
{
for(uint8_t i = 0; i < SYS_CLOCK_CNT; i++)
{
if(system_clock_freq[i] == clk)
return true;
}
return false;
}

/**
* @brief Initialize clock module at startup
*/
void system_clock_config_start(void)
{
RCC->APB1ENR |= RCC_APB1ENR_PWREN;
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;

// LSI can be disabled if RTC/IWDG is not used
// SET_BIT(RCC->CSR, RCC_CSR_LSION);

system_clock_config_update(SYSTEM_CORE_CLOCK_USB);
}

/**
* @brief Switch to USB working clock mode
* @note Enable HSI48 and CRS clock recovery for USB
*/
void system_clock_config_USB(void)
{
if (SystemCoreClock != SYSTEM_CORE_CLOCK_USB)
{
system_clock_config_update(SYSTEM_CORE_CLOCK_USB);
}

// Enable HSI48 oscillator for USB
SET_BIT(RCC->CRRCR, RCC_CRRCR_HSI48ON);
// Wait until HSI48 oscillator is ready
while ((RCC->CRRCR & RCC_CRRCR_HSI48RDY) != RCC_CRRCR_HSI48RDY) {}

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

// Enable CRS peripheral clock
RCC->APB1ENR |= RCC_APB1ENR_CRSEN;
// Configure CRS sync source
CRS->CFGR = (CRS->CFGR & ~CRS_CFGR_SYNCSRC) | (0b10 << CRS_CFGR_SYNCSRC_Pos);
// Enable CRS counter and automatic trimming
CRS->CR |= CRS_CR_CEN | CRS_CR_AUTOTRIMEN;
}

/**
* @brief Switch to battery low-power clock mode
* @note Disable HSI48 and CRS to reduce power consumption
*/
void system_clock_config_batt(void)
{
// Deselect RC48 as HSI48 clock source
RCC->CCIPR &= ~RCC_CCIPR_HSI48SEL;

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

// Stop CRS counter and auto trimming
CRS->CR &= ~(CRS_CR_CEN | CRS_CR_AUTOTRIMEN);
// Disable CRS peripheral clock
RCC->APB1ENR &= ~RCC_APB1ENR_CRSEN;

system_clock_config_update(SYSTEM_CORE_CLOCK_BATT);
}

/**
* @brief Update system core clock to target frequency
* @param sys_clock: target system clock frequency
* @note Cannot be called in ISR context
* @note Configure VOS, flash latency, prescaler BEFORE switching clock source
*/
void system_clock_config_update(uint32_t sys_clock)
{
// Reject unsupported clock frequency
if(!system_clock_is_valid(sys_clock))
{
return;
}

// Wait for VOS ready flag
while ((PWR->CSR & PWR_CSR_VOSF) == PWR_CSR_VOSF);

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

// Select MSI frequency range
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) {}

// Configure AHB prescaler in advance to avoid transient overclock
MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_CFGR_HPRE_DIV1);

// Switch system clock to MSI
MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_MSI);
// Wait for clock switch complete
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_MSI) {}

// Disable PLL
CLEAR_BIT(RCC->CR, RCC_CR_PLLON);
// Wait until PLL is fully stopped
while ((RCC->CR & RCC_CR_PLLRDY) == RCC_CR_PLLRDY) {}

// Turn off HSI oscillator
CLEAR_BIT(RCC->CR, RCC_CR_HSION);
}
// ===================== HSI clock path =====================
else
{
// Select voltage range
if (sys_clock == 32000000)
{
PWR->CR = PWR_CR_VOS_0; // Voltage range 1 (high performance, up to 32MHz)
}
else
{
PWR->CR = PWR_CR_VOS_1; // Voltage range 2 (low power, max 16MHz)
}
while ((PWR->CSR & PWR_CSR_VOSF) == PWR_CSR_VOSF);

// Set flash latency first
if (sys_clock >= 16000000)
{
SET_BIT(FLASH->ACR, FLASH_ACR_LATENCY); // 1 wait state
}
else
{
CLEAR_BIT(FLASH->ACR, FLASH_ACR_LATENCY); // 0 wait state
}

// 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) {}

// Configure HSI divider (HSIDIVEN: HSI = 16MHz / 4 = 4MHz)
if (sys_clock <= 4000000)
{
SET_BIT(RCC->CR, RCC_CR_HSIDIVEN);
}
else
{
CLEAR_BIT(RCC->CR, RCC_CR_HSIDIVEN);
}

// Configure AHB prescaler BEFORE clock switch, prevent transient overclock
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);
}

// Switch system clock to raw HSI first
MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_HSI);
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI) {}

if (sys_clock == 32000000)
{
// Clear PLL bitfield before new configuration to avoid residual setting
RCC->CFGR &= ~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL | RCC_CFGR_PLLDIV);
// PLL input = HSI16, x4, /2 → 32MHz
RCC->CFGR |= RCC_CFGR_PLLSRC_HSI | RCC_CFGR_PLLMUL4 | RCC_CFGR_PLLDIV2;

// Enable PLL
SET_BIT(RCC->CR, RCC_CR_PLLON);
while ((RCC->CR & RCC_CR_PLLRDY) != RCC_CR_PLLRDY) {}

// Switch system clock to PLL output
MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL);
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL) {}
}
else
{
// Disable PLL if not required
CLEAR_BIT(RCC->CR, RCC_CR_PLLON);
while ((RCC->CR & RCC_CR_PLLRDY) == RCC_CR_PLLRDY) {}
}

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

// Update global system clock variable
SystemCoreClock = sys_clock;
}
rich.g.williams
Senior
Oct 29, 2026 6:06 AM UTC

​@Marc1 

Hi Marc,

Just noticed your reply.

I'm concerned by this:-


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

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


Waiting for a PLL to fully stop is not a concept I recognise. What I have done is wait for them to start, it takes time. I have observed on scope that the PLL can attempt to start but fail to keep running and the system crashes. Is it possible one of your frequency/PLL switching events is marginal as regards the PLL locking again. Also if the CPU stops/crashes it is reset by a watchdog?  do you have a watchdog timer running. This would replace the 0x00000081 with 0x00. The reference may say that the PLL wait will time out but that’s no use if the clock and SYSCLOCK has stopped?

My feeling is that your code is very well written but also very complex, so many clocks, switches PLL waits, VOS changes etc, and its occuring on an ongoing basis. As a totality it might have something unsafe in there. If for example you are expecting the same PLL settings to settle despite having different clock input frequencies it might have become marginal, then the PLL tries to lock, fails and then dies, system freezes and watchdog resets it??  

Overall it seems like a complexity I would really prefer greatly to avoid in my code.

Please have a look at this thread   HAL_RCC_OscConfig timeout

I quote from it “Just as explanation the PLL is like a bell being hit, it rings and settles and then, once running and settled, hardware sets a PLL OK flag”

  

Bill's Notes
Marc1
Marc1Author
Associate III
Oct 29, 2026 11:48 AM UTC

Thanks for your reply.
The error does not occur during transition between PLL ON and PLL OFF.
A watchdog reset would reset the whole device, not just one register.

TDK
Oct 29, 2026 3:01 PM UTC

​@Marc1 Did you ever try the watchpoint?

Something is changing it. You have to find out what. It’s not changing on its own. Looking at PLL settings or whatever else is being suggested here is just adding noise to the conversation.

If watchpoint doesn’t work, consider adding an interrupt that checks it to see when it happens and track down from there.

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

Thank you for your message. I absolutely agree with you. We are working on that. The problem is that the affected devices are spread around the world and we need to get them here first. Meanwhile we are using our DEBUG firmware, which the customers can install themselves. All the information I have comes from the output of our DEBUG firmware.

I will keep you up-to-date.

gbm
Super User
Oct 29, 2026 5:34 PM UTC

​@Marc1 As you wrote above “ After waking up, TIM_CR1_CEN TIM2 is set again.”

What I suspect from the very beginning of this thread - that might be the cause. Show the exact code you use. Make sure there are no bitwise logical operations applied to the CR1 register.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
rich.g.williams
Senior
Oct 30, 2026 11:03 AM UTC

​@Marc1 

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

Think about this not as the device only, think about the environment around power, reset sequence outside spec can all cause strange things.

Power supply sequence as power is reapplied. If you could put sleeping waking etc into say a 1Hz loop and check on scope what power rails are doing - that they all obey spec, that reset is always clean, that there is no significant noise.

There is a possibility that PCB components eg capacitor tolerance could explain the 5%. problem PCB. Also consider residue left under IC packages causing problems, some countries have high humidity and or hot weather. Also unused GPIO pins especially Timer.

I had another look at your clock stepping code, it looks good. It's quite a clever way to do this.

Can you share some outline info about PCB complexity, size of your code?

You mentioned that you are testing TIM2_CR1 and fixing it from flash, no comment, : )

If I was working on this with access to the PCB and code I could solve this but hard to justify much work through ST forum. PM me if you are really stuck.

Good luck.

 

Bill's Notes