cancel
Showing results for 
Search instead for 
Did you mean: 

RTC Calendar and Standby mode

Posted on November 25, 2015 at 19:51

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,themicrocontroller goes to sleep

repeatedly

for 10 seconds, during this time everything works as expected, thecalendariscontinuouslyupdated. 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:


void
CLOCK_SystemClockConfig(
void
) {

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_ClkInitTypeDef RCC_ClkInitStruct;


__PWR_CLK_ENABLE();


__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);


RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE;

RCC_OscInitStruct.LSEState = RCC_LSE_ON;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSICalibrationValue = 16;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

HAL_RCC_OscConfig(&RCC_OscInitStruct);


RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV8;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);


HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);


HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

}

Then we configure the RTC: Set the RTC calendar clock to 1Hz


GLOBAL 
void
RTC_Init() {


/**Initialize RTC and set the Time and Date

*/

hrtc.Instance = RTC;

hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

hrtc.Init.AsynchPrediv = 127;

hrtc.Init.SynchPrediv = 255;

hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

HAL_RTC_Init(&hrtc);


// Read Time / Date directly

HAL_RTCEx_EnableBypassShadow(&hrtc);

}


void
HAL_RTC_MspInit(){

RCC_PeriphCLKInitTypeDef PeriphClkInit;

PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;

PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;

PeriphClkInit.LCDClockSelection = RCC_RTCCLKSOURCE_LSE;

HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);


/*##-2- Enable the RTC peripheral Clock ####################################*/

/* Enable RTC Clock */

__HAL_RCC_RTC_ENABLE();


/*##-3- Configure the NVIC for RTC Wakeup ###################################*/

HAL_NVIC_SetPriority(RTC_WKUP_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);

}

Then we set some low power settings:


/* Enable Ultra low power mode */

HAL_PWREx_EnableUltraLowPower();


/* Enable the fast wake up from Ultra low power mode */

HAL_PWREx_EnableFastWakeUp();


/* Check and handle if the system was resumed from StandBy mode */

if
(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET) {

/* Clear Standby flag */

__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);

// Clear WUTF FLAG in ISR Register of RTC

RTC_ClearWakeupFlag();

}

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


RTC_SetSystemWakeupClock() {

HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);


/* Clear all related wakeup flags */

__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);


/* Re-enable all used wakeup sources*/

/* ## Setting the Wake up time */

// Example for 100ms sleep

HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 204, RTC_WAKEUPCLOCK_RTCCLK_DIV16);


/* Enter the Standby mode */

HAL_PWR_EnterSTANDBYMode();

while
(1) {

}

}

Do you have any ideas? Thanks
1 REPLY 1
Posted on November 27, 2015 at 09:16

Does no one have any clue? I am really desperate