cancel
Showing results for 
Search instead for 
Did you mean: 

Enabling automatic, regular wakeup with RTC timer

AHass.1
Associate II

Dear community,

I have some issues with implementing an automatic wakeup with the RTC timer on my STM32L476RG (currently on the Nucleo-Board, will be later replaced with proprietary hardware).

I generally have two wakeup sources: either the L476 is woken up by the external wake up pin 1 on PA0 (which works fine) or it is woken up on the RTC regularly. However, the RTC doesn't seem to wake up the controller, even though I (think I) implemented it as suggested in other forum entries.

I use the HAL in my code (with some sprinklers of Arduino code), below you'll find the relevant parts of it.

The timer initialization:

void rtc_init(void)
{
 
  // Initialize RTC Only
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 127;
  hrtc.Init.SynchPrediv = 255;
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  HAL_RTC_Init(&hrtc);
  enable_wakeup_timer();
}
 
void enable_wakeup_timer(void)
{
	HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, WAKE_UP_COUNTER, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
}
 
void disable_wakeup_timer(void)
{
	HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
}

I also configure the system clock as follows, of course before initializing the RTC:

void SystemClock_config(void)
{
	//RCC_OscInitTypeDef RCC_OscInitStruct = {0};
	RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
	RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
 
	HAL_PWR_EnableBkUpAccess();
	__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
 
	PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1
	                              |RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_USART3
	                              |RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_ADC;
	PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
	HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
 
	RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
	                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
	RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
	RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
	RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV16;
	RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
	RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV16;
 
	HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
 
}

I also disable the wakeup timer on start up with the above mentioned disable_wakeup_timer function. Then, I enter the standby mode with the function below, where neccessary (where I also reset the standby and wakeup flags).

void sleep_mode_enter(void)
{
	delay(LED_ON_TIME);
	led_off();
	timer_stop();
	__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
	__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
	enable_wakeup_timer();
	HAL_PWR_EnableWakeUpPin(WAKE_UP_PIN);
	HAL_PWR_EnterSTANDBYMode();
}

Any help and hints are appreciated. Thank you in advance!

1 REPLY 1
MFara.1346
Associate II

I have got exactly the same problem. Could you find a solution for it?