2024-09-13 12:56 PM
Hi.
I'm trying to configure a wake-up alarm (Alarm A) to wake up (for a test) after 5 minutes, being time 0 when I turn it on, but I can't even make it work the first alarm because it keeps waking up every 30 seconds or so.
I'm using the U585.
Sometimes the alarm A flag becomes 1, and it works, but it's rare.
Here is the code related to that:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
__HAL_DBGMCU_FREEZE_IWDG();
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* Configure the System Power */
SystemPower_Config();
/* USER CODE BEGIN SysInit */
wakeup_reason = get_wakeup_reason();
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_GPDMA1_Init();
MX_DCACHE1_Init();
MX_ICACHE_Init();
MX_USART1_UART_Init();
MX_IWDG_Init();
MX_TIM5_Init();
MX_RTC_Init();
MX_I2C2_Init();
MX_ADC1_Init();
MX_SPI1_Init();
MX_TIM1_Init();
MX_SAI1_Init();
MX_TIM7_Init();
MX_TIM15_Init();
MX_TIM16_Init();
/* USER CODE BEGIN 2 */
__HAL_RCC_PWR_CLK_ENABLE(); // I don't know if this should be here, elsewhere or even not called
if (__HAL_PWR_GET_FLAG(PWR_FLAG_SBF) != RESET)
{
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SBF);
if (__HAL_RTC_ALARM_GET_FLAG(&hrtc, RTC_FLAG_ALRAF) != RESET)
{
flagWakeUpScheduling = true;
__HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRAF);
}
}
custom_main();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* USER CODE END 3 */
}
uint8_t get_wakeup_reason()
{
if (__HAL_RCC_GET_FLAG(RCC_FLAG_BORRST))
{
__HAL_RCC_CLEAR_RESET_FLAGS();
}
if (__HAL_PWR_GET_FLAG(PWR_WAKEUP_FLAG1) ||
__HAL_PWR_GET_FLAG(PWR_WAKEUP_FLAG7))
{
__HAL_PWR_CLEAR_FLAG(PWR_WAKEUP_FLAG1);
__HAL_PWR_CLEAR_FLAG(PWR_WAKEUP_FLAG7);
}
MX_GPIO_Init();
GPIO_InitTypeDef GPIO_InitStruct = { 0 };
// enter_sleep(); // commented here so the other wake ups won't make it sleep again
return 0;
}
/* RTC init function */
void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_PrivilegeStateTypeDef privilegeState = {0};
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
RTC_AlarmTypeDef sAlarm = {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;
hrtc.Init.BinMode = RTC_BINARY_NONE;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
privilegeState.rtcPrivilegeFull = RTC_PRIVILEGE_FULL_NO;
privilegeState.backupRegisterPrivZone = RTC_PRIVILEGE_BKUP_ZONE_NONE;
privilegeState.backupRegisterStartZone2 = RTC_BKP_DR0;
privilegeState.backupRegisterStartZone3 = RTC_BKP_DR0;
if (HAL_RTCEx_PrivilegeModeSet(&hrtc, &privilegeState) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN Check_RTC_BKUP */
// UNUSED(sTime);
// UNUSED(sDate);
// UNUSED(sAlarm);
/* USER CODE END Check_RTC_BKUP */
/** Initialize RTC and set the Time and Date
*/
sTime.Hours = 0;
sTime.Minutes = 0;
sTime.Seconds = 0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 1;
sDate.Year = 0;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
/** Enable the Alarm A
*/
sAlarm.AlarmTime.Hours = 0;
sAlarm.AlarmTime.Minutes = 5;
sAlarm.AlarmTime.Seconds = 0;
sAlarm.AlarmTime.SubSeconds = 0;
sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = 1;
sAlarm.Alarm = RTC_ALARM_A;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
/** Enable the Alarm B
*//*
sAlarm.Alarm = RTC_ALARM_B;
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
{
Error_Handler();
}
*/
// Enable the WakeUp
// I'm not sure if this next line should be commented or not; both ways don't work
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 */
}
void custom_main(void)
{
// HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1); // is this needed?
HAL_PWR_EnableBkUpAccess();
DWT_Delay_Init();
// WatchDog Refresh
HAL_IWDG_Refresh(&hiwdg);
while (1)
{
if (__HAL_RTC_ALARM_GET_FLAG(&hrtc, RTC_FLAG_ALRAF) != RESET)
{
flagWakeUpScheduling = true; // it never gets to here
__HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRAF);
}
if (flagWakeUpScheduling)
{
flagWakeUpScheduling = false;
}
}
I know I have a part that is repeated twice, but knowing that *sometimes* the RTC_FLAG_ALRAF is true, I tried catching it in other moments.
Thanks in advance.