cancel
Showing results for 
Search instead for 
Did you mean: 

My STM32L0 RTC stops in standby mode

Kevin Bain
Associate II
Posted on September 27, 2017 at 05:09

Hi, I have a small custom board using an STM32L0xxx.

The RTC is running as expected in run mode.

When I enter Standby mode the RTC appears to stop.

The MCU is in standby most of the time, it wakes up for about 2 to 4 ms, checks some digital and analogs and goes back to standby for about 600ms.

What I am seeing is the RTC time freeze during standby, the RTC is working in run and the MCU wakes up OK.

Currently I am using the LSI oscillator for the RTC.

Im not sure whether I have an issue with the RTC or with the Standyby config??

Here is my code entering Standby:

 __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);

 HAL_RTCEx_DeactivateWakeUpTimer(&__g_rtc);

 HAL_RTCEx_SetWakeUpTimer_IT(&__g_rtc, sleepTime, RTC_WAKEUPCLOCK_RTCCLK_DIV16); //0x0610

   /* Clear all related wakeup flags */

 __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);

 HAL_PWR_EnterSTANDBYMode();

Here is the clock Init: sorry its a bit ugle looking, all of this is called with each wake up.

void sys_clock_init(int funct)  // funct 0 = full init, 1 = ??

{

    RCC_ClkInitTypeDef clk;

    RCC_OscInitTypeDef osc;

    __HAL_RCC_PWR_CLK_ENABLE();

    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    // Enable HSI Oscillator and activate PLL with HSI as source.

    osc.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;

    osc.HSEState = RCC_HSE_OFF;

    osc.HSIState = RCC_HSI_ON;

    osc.LSIState = RCC_LSI_ON;

    osc.PLL.PLLState = RCC_PLL_ON;

    osc.PLL.PLLSource = RCC_PLLSOURCE_HSI;

    osc.PLL.PLLMUL = RCC_PLLMUL_4;

    osc.PLL.PLLDIV = RCC_PLLDIV_2;

    osc.HSICalibrationValue = 0x10;

    if (HAL_RCC_OscConfig(&osc) != HAL_OK) {

        sys_error();

    }

    // Select PLL as system clock source and configure dividers.

    clk.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK |

                    RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;

    clk.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

    clk.AHBCLKDivider = RCC_SYSCLK_DIV1;

    clk.APB1CLKDivider = RCC_HCLK_DIV1;

    clk.APB2CLKDivider = RCC_HCLK_DIV1;

    if (HAL_RCC_ClockConfig(&clk, FLASH_LATENCY_1) != HAL_OK) {

        sys_error();

    }

    // Required for HAL_Delay to work correctly

    __HAL_RCC_TIM2_CLK_ENABLE();

    __TIM2_CLK_ENABLE();

    // move stuff here that is not required in init after sleep

    if (funct == 0) {

    }

}

// Initialize low power management.

void sys_lp_init(void)

{

}

// Initialize the LSE clock which is used by LPUART.

void sys_clock_lse_init(void)

{

    RCC_OscInitTypeDef osc;

    osc.OscillatorType = RCC_OSCILLATORTYPE_LSE;

    osc.PLL.PLLState = RCC_PLL_NONE;

    osc.LSEState = RCC_LSE_ON;

    if (HAL_RCC_OscConfig(&osc) != HAL_OK) {

        sys_error();

    }

}

// Initialize the LSI clock which is used by LPTIM and RTC.

void sys_clock_lsi_init(void)

{

    RCC_OscInitTypeDef osc;

    osc.OscillatorType = RCC_OSCILLATORTYPE_LSI;

    osc.PLL.PLLState = RCC_PLL_NONE;

    osc.LSIState = RCC_LSI_ON;

    osc.LSEState = RCC_LSI_OFF;

    if (HAL_RCC_OscConfig(&osc) != HAL_OK) {

        sys_error();

    }

}

// Initializes the RTC based on the LSI.

// Notes: The LSI **must** be initialized prior to this.

void sys_rtc_init(void)

{

    // Configure LSI as RTC clock source

    RCC_PeriphCLKInitTypeDef pclk;

    pclk.PeriphClockSelection = RCC_PERIPHCLK_RTC;

    pclk.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;

    if (HAL_RCCEx_PeriphCLKConfig(&pclk) != HAL_OK) {

        sys_error();

    }

    // Enable the RTC clock

    __HAL_RCC_RTC_ENABLE();

    // Enable NVIC for RTC alarm

    HAL_NVIC_SetPriority(RTC_IRQn, 0x0, 0);

    HAL_NVIC_EnableIRQ(RTC_IRQn);

}

If any one can help that would be great.

Thanks

Kevin

6 REPLIES 6
Mary Solomo
Associate II

Hello, I have the same problem. I'm using STM32F076 and after wakeup from standby, the RTC time is stuck to the time before it entered the standby mode. Why is the RTC time frozen in this case?

Probably because the LSI isn't in the low power domain. ​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi Clive. According to the Reference Manual, the RTC clock and calendar still runs even in Standby mode given that LSI is used.

From the Manual:

"The RTC remains active when the RTC clock source is LSE or LSI. RTC alarm, RTC

tamper event, RTC timestamp event, and RTC Wakeup cause the device to exit the

Standby mode."

In my understanding, the RTC should still continue running, and it was able to alarmon the designated time so it should be running. Is my understanding correct?

From RM0377:

In Standby mode, the following features can be selected by programming individual controlbits:

...

  • Real-time clock (RTC): this is configured by the RTCEN bit in the RCC_CSR register
  • Internal RC oscillator (LSI RC): this is configured by the LSION bit in the RCC_CSRregister.

Are you sure both bits are set and the RTC is configured?

Mary Solomo
Associate II

Hello, don't mind this question. Actually, the RTC time isn't freezing after standby. I was printing the "Set alarm time" and then "wake up time" and wondered why they are equal. Of course, they will be equal because the wake up time (after standby) should be equal to the alarm set.

Mark Shoe
Senior

This happens when you have a wake time below 1 second. Just like the second is always written to the same value. The dirty solution was to count the number of wakes and adjust the rtc. Not accurate its off 10 munites a day.