cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F100 RTC Wake From Standby is always 1 Second Late

ISeed
Senior

Hello,

I use STM32F100RBT. I want to enter STANDBY mode and exit every second.

It works well, except for one issue: it always wakes up 1 second later.

I run the following code, where I get the current time, print it and set an alarm for the next second (this is only a test, I start from 0 seconds and check what happens in the next several prints), then I enter STANDBY.

The RTC given seconds are always incremented by 2 instead of one, as does the timestamp in the serial monitor (the RTC second progression corresponds to real time).

I tried it with other delays, like 2, 3, 4, and 5, and the RTC is always 1 second late.

Why does RTC wake the system 1 second late?

How can this be fixed?

Thanks!

void test_code()
 
{
 
	RTC_DateTypeDef sdatestructure = {0};
 
	RTC_TimeTypeDef stimestructure = {0};
 
 
 
	hrtc.Instance = RTC;
 
 
 
	HAL_RTC_GetTime(&hrtc, &stimestructure, RTC_FORMAT_BCD);
 
	HAL_RTC_GetDate(&hrtc, &sdatestructure, RTC_FORMAT_BCD);
 
 
 
	printf("wake up time: %u\n", stimestructure.Seconds);
 
 
 
	__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
 
	if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)
 
	{
 
		printf("wake\n");
 
		/* Clear Standby flag */
 
		__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
 
	}
 
	else
 
	{
 
		printf("reset\n");
 
 
 
		MX_RTC_Init();
 
	}
 
 
 
	RTC_AlarmTypeDef salarmstructure = {{0}, 0};
 
 
 
	HAL_RTC_GetTime(&hrtc, &stimestructure, RTC_FORMAT_BCD);
 
 
 
	stimestructure.Seconds += 1;
 
 
 
	salarmstructure.Alarm = RTC_ALARM_A;
 
	salarmstructure.AlarmTime.Hours = stimestructure.Hours;
 
	salarmstructure.AlarmTime.Minutes = stimestructure.Minutes;
 
	salarmstructure.AlarmTime.Seconds = stimestructure.Seconds;
 
 
 
	if(HAL_RTC_SetAlarm_IT(&hrtc,&salarmstructure,RTC_FORMAT_BCD) != HAL_OK)
 
	{
 
		/* Initialization Error */
 
		SET_CRITCAL_ERROR()
 
	}
 
 
 
	HAL_PWR_EnterSTANDBYMode();
 
}
 
 
 
static void MX_RTC_Init(void)
 
{
 
 
 
 /* USER CODE BEGIN RTC_Init 0 */
 
 
 
 /* USER CODE END RTC_Init 0 */
 
 
 
 RTC_TimeTypeDef sTime = {0};
 
 RTC_DateTypeDef DateToUpdate = {0};
 
 
 
 /* USER CODE BEGIN RTC_Init 1 */
 
 
 
 /* USER CODE END RTC_Init 1 */
 
 /** Initialize RTC Only
 
 */
 
 hrtc.Instance = RTC;
 
 
 
 hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
 
 	hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM;
 
 	if (HAL_RTC_Init(&hrtc) != HAL_OK)
 
 	{
 
 	 SET_CRITCAL_ERROR()
 
 	}
 
 
 
 	sTime.Hours = 0x0;
 
 	sTime.Minutes = 0x0;
 
 	sTime.Seconds = 0x0;
 
 
 
 	if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
 
 	{
 
 	 SET_CRITCAL_ERROR()
 
 	}
 
 	DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY;
 
 	DateToUpdate.Month = RTC_MONTH_JANUARY;
 
 	DateToUpdate.Date = 0x1;
 
 	DateToUpdate.Year = 0x0;
 
 
 
 	if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK)
 
 	{
 
 	 SET_CRITCAL_ERROR()
 
 	}
 
}
 
 
 
int main(void)
 
{
 
 HAL_Init();
 
 SystemClock_Config();
 
 MX_GPIO_Init();
 
 MX_USART1_UART_Init();
 
 MX_USART2_UART_Init();
 
 
 
 test_code();
 
}

1 REPLY 1

Seem to recall the RTC reports a second behind or something. I think this is a known/documented phenomena. Sorry haven't actively used F1 series parts for nearly a decade, and then I used the SPL.

Prior thread, just keep one topic when discussing basically the same issue/problem

https://community.st.com/s/question/0D50X0000Bddv0mSQA/exit-standby-every-second-from-rtc-on-stm32f100rbt

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