2023-10-02 01:31 AM - edited 2023-10-02 01:33 AM
Hello! I am using the RTC of a STM32L412 with 32kHz LSI to wake the MCU from STOP2 mode after a specified time in seconds. Now I´m having two issues:
1) I cannot program the MCU using a ST-Link V2 with current Firmware, when the STOP2 mode is active. ("Error in initializing ST-LINK device. Reason: No device found on target."). I already "lost" one device, now I´m doing a 10s HAL_Delay() in the beginning before the program starts to quickly flash new firmware on-board. Is there a workaround on this? How can I rescue my device that goes directly into STOP2 mode to flash it again?
2) Accuracy: I have tried a 20 seconds sleep in the STOP2 low-power mode. The program prints out the RTC time, and it seems that it is sleeping for slightly less than 20 seconds. Which is a weird behaviour. If time would be higher I could think of the printf() execution time etc. I calculated the wakeup-timer like this:
wakeUpCounter = wakeUpTime / (LSI_Prescaler / LSE_Frequency) = 20s / (16 / 32000Hz) = 40000
Here´s my code:
uint16_t secondsToSleep = 20;
for (uint16_t ctr=0;ctr<500;ctr++) {
printf("Current RTC time >>> %lu\r\n", (unsigned long)getEpochTimeFromRTC(hrtc));
HAL_SuspendTick();
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, (secondsToSleep * 2000), RTC_WAKEUPCLOCK_RTCCLK_DIV16,0); // Set wake-up timer
HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI); // Enter STOP 2 mode
HAL_RTCEx_DeactivateWakeUpTimer(&hrtc); // Code continues after waking up from STOP2 low-power mode
SystemClock_Config();
HAL_ResumeTick();
}
The RTC init looks like this:
static void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
/* USER CODE BEGIN RTC_Init 1 */
/* USER CODE END RTC_Init 1 */
/** 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;
hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN Check_RTC_BKUP */
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 0x0;
sTime.Minutes = 0x0;
sTime.Seconds = 0x0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 0x1;
sDate.Year = 0x0;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
Error_Handler();
}
/** Enable the WakeUp
*/
if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0, RTC_WAKEUPCLOCK_RTCCLK_DIV16, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}
2023-10-02 01:57 AM
Welcome @fieteboernsen to the ST Community,
1) in order to keep debug alive in the low power modes the DGBMCU->CR should be kept at 0x7. The IDE will configure this during connection if "Debug in low power modes" is set in the debug configuration settings.
So maybe check your debug configuration
2) Maybe instead of initializing RTC time and date to 0x0, set them to the current time and date HAL_RTC_GetTime() and HAL_RTC_GetDate(), to make sure the RTC is accurately keeping track of time and date.
Hope that helps!
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2023-10-02 03:40 AM - edited 2023-10-02 03:43 AM
Hi Sarra.S, thanks for your answer!
I changed the "Debug in low power modes" to "Enable", but it is still not working. Then I added this line to the code and now it works. My question: Does this setting affect power consumption or any other parameter in a way that I should take it out before releasing the firmware?
DBGMCU->CR |= DBGMCU_CR_DBG_STOP; // Enable Flashing/Debugging in Stop mode
The RTC time is already set to current date/time, I verified it is counting correctly. For the HAL_RTCEx_SetWakeUpTimer_IT() function I think it can only take 2^16 seconds (even though the function's input parameter is a uint32_t). I tried to add longer time periods (5400 sec => WakeUpCounter = 108000000) but it never sleeps more than ~30 seconds. Can you clarify on this?
Or is there a better solution to go to STOP2 mode for a specific time (we need a configurable interval in the range 10sec - 604800sec)?
2023-10-02 08:22 AM
Hello again,
>>Does this setting affect power consumption or any other parameter in a way that I should take it out before releasing the firmware?
Yes, enabling debugging in low-power modes increases the power consumption; you may want to test/ re-measure your consumption values.
To rescue your device that goes directly into STOP2 mode, try connecting with STM32CubeProgrammer while selecting "connect under reset".
>>I tried to add longer time periods (5400 sec => WakeUpCounter = 108000000) but it never sleeps more than ~30 seconds. Can you clarify on this?
This is because the RTC wake-up timer is a 16-bit counter, and can only count up to a maximum value of 65535.
Could you please specify exactly the wakeup time (less than 20sec), since I don't have the values of LSI prescaler,... double check your wakeup counter calculation referring to this article
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.