AnsweredAssumed Answered

RTC Calendar and Standby mode

Question asked by Seppli on Nov 25, 2015
Latest reply on Nov 27, 2015 by Seppli
Hi everybody

We are using a STM32L152 microcontroller and we are regularly entering standby mode. Moreover, we are also using the RTC as a calendar powered by an external clock (LSE). In one case, the microcontroller goes to sleep repeatedly for 10 seconds, during this time everything works as expected, the calendar is continuously updated. But as soon as we change the sleep duration to 100ms, the calendar is not updated properly anymore. Basically, it does not continue to count, it just holds the same timestamp.


First I initialises the clocks as follows:

01.void CLOCK_SystemClockConfig(void) {
02.  RCC_OscInitTypeDef RCC_OscInitStruct;
03.  RCC_ClkInitTypeDef RCC_ClkInitStruct;
04. 
05.  __PWR_CLK_ENABLE();
06. 
07.  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
08. 
09.  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE;
10.  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
11.  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
12.  RCC_OscInitStruct.HSICalibrationValue = 16;
13.  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
14.  HAL_RCC_OscConfig(&RCC_OscInitStruct);
15. 
16.  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK;
17.  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
18.  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV8;
19.  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
20.  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
21.  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
22. 
23.  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
24. 
25.  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
26.}

Then we configure the RTC:

Set the RTC calendar clock to 1Hz
01.GLOBAL void RTC_Init() {
02. 
03.  /**Initialize RTC and set the Time and Date
04.   */
05.  hrtc.Instance = RTC;
06.  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
07.  hrtc.Init.AsynchPrediv = 127;
08.  hrtc.Init.SynchPrediv = 255;
09.  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
10.  HAL_RTC_Init(&hrtc);
11. 
12.  // Read Time / Date directly
13.  HAL_RTCEx_EnableBypassShadow(&hrtc);
14.}


01.void HAL_RTC_MspInit(){
02.    RCC_PeriphCLKInitTypeDef PeriphClkInit;
03.    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
04.    PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
05.    PeriphClkInit.LCDClockSelection = RCC_RTCCLKSOURCE_LSE;
06.    HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
07. 
08.    /*##-2- Enable the RTC peripheral Clock ####################################*/
09.    /* Enable RTC Clock */
10.    __HAL_RCC_RTC_ENABLE();
11. 
12.    /*##-3- Configure the NVIC for RTC Wakeup ###################################*/
13.    HAL_NVIC_SetPriority(RTC_WKUP_IRQn, 0, 0);
14.    HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
15.  }


Then we set some low power settings:

01./* Enable Ultra low power mode */
02.HAL_PWREx_EnableUltraLowPower();
03. 
04./* Enable the fast wake up from Ultra low power mode */
05.HAL_PWREx_EnableFastWakeUp();
06. 
07./* Check and handle if the system was resumed from StandBy mode */
08.if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET) {
09.  /* Clear Standby flag */
10.  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
11.  // Clear WUTF FLAG in ISR Register of RTC
12.  RTC_ClearWakeupFlag();
13.}



In the end, after doing some work we enter Standby mode:

01.RTC_SetSystemWakeupClock() {
02.  HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
03. 
04.  /* Clear all related wakeup flags */
05.  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
06. 
07.  /* Re-enable all used wakeup sources*/
08.  /* ## Setting the Wake up time */
09.  // Example for 100ms sleep
10.  HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 204, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
11. 
12.  /* Enter the Standby mode */
13.  HAL_PWR_EnterSTANDBYMode();
14.  while (1) {
15.  }
16.}


Do you have any ideas?

Thanks

Outcomes