cancel
Showing results for 
Search instead for 
Did you mean: 

How to wake-up from Standby mode with RTC?

Junde
Senior III

Hi guys,

I want to wake up my MCU every 10 sec. And I had to write some code as follows:

 

 

 

  if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET) {
      __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
      sprintf((char*)uartBuf, "wakeup from standby\n");
      HAL_UART_Transmit(&huart1, (uint8_t*)uartBuf, strlen((char*)uartBuf), 100);
      
      HAL_RTC_GetTime(&hrtc, &rtcTime, RTC_FORMAT_BIN);
      rtcAlarm.AlarmTime.Hours = (rtcTime.Hours + (rtcTime.Minutes + (rtcTime.Seconds + 10) / 60) / 60) % 24;
      rtcAlarm.AlarmTime.Minutes = (rtcTime.Minutes + (rtcTime.Seconds + 10) / 60) % 60;
      rtcAlarm.AlarmTime.Seconds = (rtcTime.Seconds + 10) % 60;
      HAL_RTC_SetAlarm_IT(&hrtc, &rtcAlarm, RTC_FORMAT_BIN);
      
      sprintf((char*)uartBuf, "cur time:  %02d:%02d:%02d\n", rtcTime.Hours, rtcTime.Minutes, rtcTime.Seconds);
      HAL_UART_Transmit(&huart1, (uint8_t*)uartBuf, strlen((char*)uartBuf), 100);
      sprintf((char*)uartBuf, "set alarm: %02d:%02d:%02d\n", rtcAlarm.AlarmTime.Hours, rtcAlarm.AlarmTime.Minutes, rtcAlarm.AlarmTime.Seconds);
      HAL_UART_Transmit(&huart1, (uint8_t*)uartBuf, strlen((char*)uartBuf), 100);
      
      sprintf((char*)uartBuf, "Do User Tasks...\n");
      HAL_UART_Transmit(&huart1, (uint8_t*)uartBuf, strlen((char*)uartBuf), 100);
      HAL_Delay(500);

      HAL_PWR_EnterSTANDBYMode();
  } else {  
      sprintf((char*)uartBuf, "\n\nRTC init\n");
      HAL_UART_Transmit(&huart1, (uint8_t*)uartBuf, strlen((char*)uartBuf), 100);
      
      //MX_RTC_Init();
      hrtc.Instance = RTC;
      hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
      HAL_RTC_Init(&hrtc);

      rtcTime.Hours = 0;
      rtcTime.Minutes = 0;
      rtcTime.Seconds = 0;
      HAL_RTC_SetTime(&hrtc, &rtcTime, RTC_FORMAT_BIN);

      rtcAlarm.Alarm = RTC_ALARM_A;
      rtcAlarm.AlarmTime.Hours = 0;
      rtcAlarm.AlarmTime.Minutes = 0;
      rtcAlarm.AlarmTime.Seconds = 10;
      HAL_RTC_SetAlarm_IT(&hrtc, &rtcAlarm, RTC_FORMAT_BIN);

      HAL_PWR_EnterSTANDBYMode();
  }

 

 

 

The output is as below:

 

 

 

// I add the timestamp for reference
[10:45:49.103] RTC init
[10:46:00.218] wakeup from standby
[10:46:02.733] cur time:  15:53:36
[10:46:02.733] set alarm: 15:53:46
[10:46:02.733] Do User Tasks...
[10:46:02.733] wakeup from standby
[10:46:05.236] cur time:  15:53:36
[10:46:05.236] set alarm: 15:53:46
[10:46:05.236] Do User Tasks...
[10:46:05.236] wakeup from standby
[10:46:07.753] cur time:  15:53:36
[10:46:07.753] set alarm: 15:53:46
[10:46:07.753] Do User Tasks...
[10:46:07.753] wakeup from standby

 

 

 

 From the output, we can see the MCU was waked up every 2-3sec, NOT 10 sec. And the cur time is always 15:53:36, can't be added.

I am not clear why this happened, may there be something wrong with the schematic or just the code?

Junde_0-1703569141379.png

The above unexpected output is received when both the VCC3.3V and the Battery are 3.3V. (The result is the same when taking off the battery.)

I have more questions: Can the MCU work well just with the VDD power or just with the VBAT power?

Thanks for your help.

 

3 REPLIES 3
MM..1
Chief III

In firmwre resource folder you can found examples , check your code with this. STM32CubeF1/Projects/STM32F103RB-Nucleo/Examples at master · STMicroelectronics/STM32CubeF1 · GitHub

And maybe your primary issue is debug with enabled low power mode debuging = waking MCU here isnt real standart. Disable it in config and power off all power sources before next testing.

Hi @MM..1 

Thanks for your reply.

I already read all the source code about RTC and standby. And my code is changed from the examples.

Now I can be sure the RTC alarm setting is correct because it works well when I disable the standby mode.

There must be some bugs in my "standby" code or the schematic.

Try add GetDATE dummy ... hal locking issue