Search code examples
stm32interruptalarmrational-team-concert

RTC alarm interrupt while loop


My problem is that when the rtc alarm is activated, it prints the message that is in the interrupt every 10 seconds, but it stops printing the messages that are in the while loop of the main.

I am using platformio and vs code to program the stm32f103rct6 microcontroller.

main.c

int main(void)
{
HAL_Init();
SystemClock_Config();
HardwareInit();
while (1)
{
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
myprintf("Date: %02d-%02d-%02d\\n", sDate.Date, sDate.Month, 2000 + sDate.Year);

        HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
        myprintf("Time: %02d:%02d:%02d\n", sTime.Hours, sTime.Minutes, sTime.Seconds);
    
        HAL_RTC_GetAlarm(&hrtc, &sAlarm, RTC_ALARM_A, FORMAT_BIN);
        myprintf("Alarm: %02d:%02d:%02d\n", sAlarm.AlarmTime.Hours, sAlarm.AlarmTime.Minutes, sAlarm.AlarmTime.Seconds);
    
    }

}

stm32f1xx_it.c

void RTC_Alarm_IRQHandler(void)
{
    if (__HAL_RTC_ALARM_GET_FLAG(&hrtc, RTC_IT_ALRA) != RESET)
    {
        myprintf("Alarm match\\n");
        HAL_RTC_GetAlarm(&hrtc, &sAlarm, RTC_ALARM_A, FORMAT_BIN);
        sAlarm.AlarmTime.Seconds = sAlarm.AlarmTime.Seconds + 10;
        HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, FORMAT_BIN);
        __HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_IT_ALRA);
    }
}

RTC_Init

void RTC_Init(void)
{
    __HAL_RCC_RTC_ENABLE();

    hrtc.Instance = RTC;
    hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
    hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM;
    if (HAL_RTC_Init(&hrtc) != HAL_OK)
    {
        Error_Handler();
    }

    sTime.Hours = 0x13;
    sTime.Minutes = 0x30;
    sTime.Seconds = 0x0;
    if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
    {
        Error_Handler();
    }

    sDate.WeekDay = RTC_WEEKDAY_WEDNESDAY;
    sDate.Month = RTC_MONTH_NOVEMBER;
    sDate.Date = 0x29;
    sDate.Year = 0x23;
    if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
    {
        Error_Handler();
    }

    sAlarm.Alarm = RTC_ALARM_A;
    sAlarm.AlarmTime.Hours = sTime.Hours;
    sAlarm.AlarmTime.Minutes = sTime.Minutes;
    sAlarm.AlarmTime.Seconds = sTime.Seconds + 0x10;
    if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD) != HAL_OK)
    {
        Error_Handler();
    }

    HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);

    __HAL_RTC_ALARM_ENABLE_IT(&hrtc, RTC_IT_ALRA);
    __HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_IT_ALRA);
}

The answer I get:

.
.
.
Date: 29-11-2023
Time: 13:30:09
Alarm: 13:30:10

Date: 29-11-2023
Time: 13:30:10
Alarm: 13:30:10

Alarm match
Alarm match
Alarm match
Alarm match
.
.
.

The answer I am waiting for:

.
.
.
Date: 29-11-2023
Time: 13:30:09
Alarm: 13:30:10

Date: 29-11-2023
Time: 13:30:10
Alarm: 13:30:10

Alarm match

Date: 29-11-2023
Time: 13:30:11
Alarm: 13:30:20

Date: 29-11-2023
Time: 13:30:12
Alarm: 13:30:20
.
.
.

I have tried clearing the flag and what I expect is that it will show me the message of the RTC alarm interruption but then follow the normal course until the new alarm.


Solution

  • I found the problem, by using the macro __HAL_RTC_ALARM_GET_FLAG and not using the usual function HAL_RTC_AlarmIRQHandler(&hrtc) to execute the callback function, I was missing the following line of code that is inside the previous function __HAL_RTC_ALARM_EXTI_CLEAR_FLAG().