cancel
Showing results for 
Search instead for 
Did you mean: 

Changing the HSI clock speed before and after entering the sleep mode

abdelrahman-kadah
Associate

Hello, 

We are working on low-power product using STM32G030K8 MCU that has maximum HSI 16 MHz. 

We are trying to decrease the power consumption using several ways. 

One of them is to decrease the HSI clock speed before entering the sleep mode and then increase it again after exiting the sleep mode by changing the HSI clock divider as attached, but the application doesn't work well after that.

Does the code have any problems that make the application doesn't work well?

If it has, what is the correct way to change the clock speed at runtime before entering the sleep mode and after exiting?

 

 

void EnterSleepMode(void)
{
__HAL_RCC_HSI_CONFIG(RCC_HSI_DIV8);

__HAL_TIM_SET_PRESCALER(&htim1, 1);
__HAL_TIM_SET_PRESCALER(&htim3, 1);

HAL_SuspendTick();

HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);

HAL_ResumeTick();

__HAL_RCC_HSI_CONFIG(RCC_HSI_DIV1);

__HAL_TIM_SET_PRESCALER(&htim1, 15);
__HAL_TIM_SET_PRESCALER(&htim3, 15);
}

 

 

2 REPLIES 2
nouirakh
ST Employee

Hello @abdelrahman-kadah 

When you change the clock speed, peripherals that depend on the clock (like timers in your case) need to be reconfigured accordingly. Also, changing the clock configuration can cause timing issues if not done correctly. So, you need to ensure that the system clock source is stable before and after changing the clock divider.
Can you try this code to verify if there is a timing issue:

void EnterSleepMode(void)
{
    // Temporarily disable interrupts to prevent any timing issues during the clock change.
    __disable_irq();

    // Change HSI clock divider to reduce clock speed
    __HAL_RCC_HSI_CONFIG(RCC_HSI_DIV8);

    // Reconfigure timers with new prescaler values
    __HAL_TIM_SET_PRESCALER(&htim1, 1);
    __HAL_TIM_SET_PRESCALER(&htim3, 1);

    // Suspend SysTick interrupt to prevent it from waking up the MCU
    HAL_SuspendTick();

    // Enter sleep mode
    HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);

    // Resume SysTick interrupt
    HAL_ResumeTick();

    // Restore HSI clock divider to original speed
    __HAL_RCC_HSI_CONFIG(RCC_HSI_DIV1);

    // Reconfigure timers with original prescaler values
    __HAL_TIM_SET_PRESCALER(&htim1, 15);
    __HAL_TIM_SET_PRESCALER(&htim3, 15);

    // Enable interrupts
    __enable_irq();
}

Finlay, it could be an issue of Clock Stability so you need to be sure that the clock source is stable before and after changing the divider. You may need to add delays or check for clock stability.


Could you please check the points I mentioned above?

abdelrahman-kadah
Associate

I tried the suggested the points and still doesn't work.