STM32F103 Multiple wakeup sources detection. Unable to read RTC_CRL[ALRF] flag after reset from STBY
Hello,
In this thread, I would like to clarify a point regarding how to read the RTC_CLR[ALRF] flag. I am using the STM32F103C8T6 microcontroller and the STM32Cube Firmware (HAL) version 1.8.5. This flag indicates that the alarm has been activated.
My intention is to determine the reason for the system reset, as I have configured two ways to exit the STANDBY mode before enabling it:
1. Through the wakeup pin.
2. Through the activation of the alarm.
To achieve this, I am trying to read the RTC_CRL[ALRF] bit after returning from STANDBY mode and executing the functions HAL_Init(); and SystemClock_Config(); The result I obtain is somewhat curious. When the debugger is connected (Stlink V2), the flag is correctly activated after the system reset. However, when I disconnect the debugger and perform logging through UART, the bit never gets activated, even though it is the alarm that causes the system reset.

I have created my own function to read the register following the instructions I´ve found on the reference manual and on AN2629, and it must be executed before the function MX_RTC_Init(); generated automatically by the STMCUBE IDE. This is because MX_RTC_Init includes the HAL_RTC_Init function, which clears the ALRF flag during RTC initialization.


#define RTC_REG (0x40002800)
RTC_TypeDef *Rtc = (RTC_TypeDef *) RTC_REG;
static char message_buffer[30];
static uint8_t ALRF_Get_Flag (uint8_t *alrf){
uint32_t tickstart = 0U;
uint8_t ret_val = HAL_OK;
/* Clear RSF flag */
CLEAR_BIT(Rtc->CRL, RTC_FLAG_RSF);
tickstart = HAL_GetTick();
/* Wait the registers to be synchronised */
while (READ_BIT(Rtc->CRL, RTC_CRL_RSF) != (RTC_CRL_RSF))
{
if ((HAL_GetTick() - tickstart) > 2000)
{
ret_val = HAL_TIMEOUT;
}
}
*alrf = (uint8_t)(READ_BIT(Rtc->CRL, RTC_CRL_ALRF) == (RTC_CRL_ALRF)? 1:0);
return ret_val;
}
int main(void)
{
/* USER CODE BEGIN 1 */
uint8_t flag_check;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* Configure the system Power */
SystemPower_Config();
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C2_Init();
MX_USART1_UART_Init();
ALRF_Get_Flag(&flag_check);
if(flag_check == 1){
(void)snprintf (message_buffer, (size_t)50, "RTC_WKPU - RTC_CLR[ALRF]: %d\r\n", flag_check);
HAL_UART_Transmit(&huart1, (uint8_t *)message_buffer, strlen (message_buffer), HAL_MAX_DELAY);
}else{
(void)snprintf (message_buffer, (size_t)50, "PIN WKPU - RTC_CLR[ALRF]: %d\r\n", flag_check);
HAL_UART_Transmit(&huart1, (uint8_t *)message_buffer, strlen (message_buffer), HAL_MAX_DELAY);
}
/*...*/
}My solution to this problem is as follows:
After a system reset following a return from STANDBY mode due to a rising edge on pin PA0 or the activation of the RTC alarm, we can read the PWR_FLAG_SB flag using the macro: __HAL_PWR_GET_FLAG(PWR_FLAG_SB). This way, I can confirm that a reset has occurred. To determine the cause of the reset, I read the current time and compare it with the time I had set for the alarm (previously saved in backup registers or flash memory). If both times are equal, the reset reason is the RTC; otherwise, it's the PA0 pin. This solution works, but in my opinion, it may not be the best one.
My questions are as follows:
1. Does anyone in this community or who works at ST Microelectronics know if it is possible to read the RTC_CRL[ALRF] flag the way I am attempting to do it?
2. Is there a way to achieve this using only the HAL firmware STM32Cube FWF1 V1.8.5?
3. Why do I only see the flag active when I am in DEBUG mode and not when the software is running normally?
If anyone finds this post interesting and knows the solution to my questions, I would appreciate it if you could leave a comment.
Thank you in advance!
