cancel
Showing results for 
Search instead for 
Did you mean: 

Bug found and solved inside HAL_RTCEx_RTCIRQHandler(RTC_HandleTypeDef *hrtc)

VRami.1
Associate III

Hi all, how are you?. I had that what I think is bug after enable the seconds interrupt by using the macro

  __HAL_RTC_ALARM_ENABLE_IT(&hrtc,RTC_IT_SEC);

in my main() function, then I had to put the function

  CLEAR_BIT(RTC->CRL,RTC_CRL_CNF);

after the __HAL_RTC_ALARM_ENABLE_IT() macro because I realized that the above macro doesn't exited from configuration mode for the RTC.

Then after I was able to enable the seconds interrupt I found what I think is another bug inside HAL_RTCEx_RTCIRQHandler(RTC_HandleTypeDef *hrtc) starting in the line 379 of the file stm32f1xx_hal_rtc_ex.c, the content of the function is the following

void HAL_RTCEx_RTCIRQHandler(RTC_HandleTypeDef *hrtc)
{
  if (__HAL_RTC_SECOND_GET_IT_SOURCE(hrtc, RTC_IT_SEC))
  {
    /* Get the status of the Interrupt */
    if (__HAL_RTC_SECOND_GET_FLAG(hrtc, RTC_FLAG_SEC))
    {
      /* Check if Overrun occurred */
      if (__HAL_RTC_SECOND_GET_FLAG(hrtc, RTC_FLAG_OW))
      {
        /* Second error callback */
        HAL_RTCEx_RTCEventErrorCallback(hrtc);
 
        /* Clear flag Second */
        __HAL_RTC_OVERFLOW_CLEAR_FLAG(hrtc, RTC_FLAG_OW);
 
        /* Change RTC state */
        hrtc->State = HAL_RTC_STATE_ERROR;
      }
      else
      {
        /* Second callback */
        HAL_RTCEx_RTCEventCallback(hrtc);
 
        /* Change RTC state */
        hrtc->State = HAL_RTC_STATE_READY;
      }
 
      /* Clear flag Second */
      __HAL_RTC_SECOND_CLEAR_FLAG(hrtc, RTC_FLAG_SEC);
    }
  }
}

The bug is in the line 408 of the same file, in that line is the following macro

 __HAL_RTC_SECOND_CLEAR_FLAG(hrtc, RTC_FLAG_SEC);

it had the same behavior as before, the macro enters into configuration mode and never exits from it, the problem was solved the problem by using again the function

  CLEAR_BIT(RTC->CRL,RTC_CRL_CNF);

after the macro __HAL_RTC_SECOND_CLEAR_FLAG(hrtc, RTC_FLAG_SEC) to exit the configuration mode for the RTC.

I beleive the problem here is that those macros makes that the microcontroller enters into configuration mode for the RTC but after the macro is executed then the microcontroller never exits the configuration for the RTC, I mean, it doesn't clear the CNF bit of the RTC_CRL register unless the the bit is explicitly cleared by the user, because of that the RTC never updates the time count and consecuently it doesn't increment the user time.

The above allowed me to use the seconds interrupt in conjunction with the normal count of the RTC without one feature blocking the other.

Finally I can tell that it seems like a strange behavior to me but happent and after those changes this part of my code is working so I'm publishing it in case that others have the same problem.

0 REPLIES 0