AnsweredAssumed Answered

Slow RTC

Question asked by rincon.sergio on Jan 19, 2016
Latest reply on Jan 19, 2016 by Clive One
Hi all,

I'm using the RTC to send data every few minutes, but after a while I saw that the shipments are carried out each longer than scheduled but ntp number sent by my stm were correct as I wanted.
Then I compared the measures of the RTC with HAL_GetTick() measured . For a 10 minutes RCT alarm  I get 65583 milliseconds. My RTC is almost 10% slower.

Does anyone know this may be due? Im using Lsi.

<code>
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_ADC;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
       Error_Handler();
    }
/*------------------------------------------------------------------------*/
 
void MX_RTC_Init(void)
{
        /**  Initialize RTC  **/
      RtcHandle.Instance = RTC;
      RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
      RtcHandle.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
      if(HAL_RTC_Init(&RtcHandle) != HAL_OK)
       {
         /* Initialization Error */
         Error_Handler();
       }
}
 
void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
 
  if(hrtc->Instance==RTC)
  {
  /* USER CODE BEGIN RTC_MspInit 0 */
 
  /* USER CODE END RTC_MspInit 0 */
 
    /* Enable BKP CLK enable for backup registers */
    __HAL_RCC_BKP_CLK_ENABLE();
 
    HAL_PWR_EnableBkUpAccess();
    /* Peripheral clock enable */
    __HAL_RCC_RTC_ENABLE();
  /* USER CODE BEGIN RTC_MspInit 1 */
    HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
  /* USER CODE END RTC_MspInit 1 */
  }
 
}
static void RTC_TimeConfigNTP(void)
{
 
    RTC_DateTypeDef  sdatestructure_set = {0};
    RTC_TimeTypeDef  stimestructure = {0};
 
    SNTP_Timestamp_t ntpTime;
    NetTime_t extractedTime;
 
    sntp_NTPGetTime(&ntpTime, 0);
    sntp_ExtractNTPTime(&ntpTime, &extractedTime);
 
 
  /*##-1- Configure the Date #################################################*/
 
  sdatestructure_set.Year = extractedTime.year % 100;   //Introducimos solo los dos ultimos numeros
  sdatestructure_set.Month = extractedTime.mon;
  sdatestructure_set.Date = extractedTime.mday;
 
  if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure_set,RTC_FORMAT_BIN) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
 
  /*##-2- Configure the Time #################################################*/
 
  stimestructure.Hours = extractedTime.hour;
  stimestructure.Minutes = extractedTime.min;
  stimestructure.Seconds = extractedTime.sec;
  if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,RTC_FORMAT_BIN) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
}
 
static void RTC_AlarmConfigNTP(void)
{
 
    uint8_t hoursAlarm = 0;
    uint8_t minutesAlarm = 1;
    uint8_t secondsAlarm = 0;
    RTC_TimeTypeDef  stimestructure = {0};
    RTC_AlarmTypeDef salarmstructure = {{0}, 0};
    RTC_AlarmTypeDef sprealarmstructure = {{0}, 1};
 
    if(sprealarmstructure.AlarmTime.Hours== 0 && sprealarmstructure.AlarmTime.Minutes == 0 && sprealarmstructure.AlarmTime.Seconds==0 ){
        HAL_RTC_GetTime(&RtcHandle, &stimestructure, RTC_FORMAT_BIN);
        sprealarmstructure.Alarm = RTC_ALARM_A;
        sprealarmstructure.AlarmTime.Hours = stimestructure.Hours;
        sprealarmstructure.AlarmTime.Minutes = stimestructure.Minutes;
        sprealarmstructure.AlarmTime.Seconds = stimestructure.Seconds;
    }
 
    //HAL_RTC_GetTime(&RtcHandle, &stimestructure, RTC_FORMAT_BIN);
    //HAL_RTC_GetDate(&RtcHandle, &sdatestructure_set, RTC_FORMAT_BCD);
 
 
  /*##-3- Configure the RTC Alarm peripheral #################################*/
 
  salarmstructure.Alarm = RTC_ALARM_A;
  salarmstructure.AlarmTime.Hours = (sprealarmstructure.AlarmTime.Hours+((sprealarmstructure.AlarmTime.Minutes+((sprealarmstructure.AlarmTime.Seconds
                                              +secondsAlarm)/60)+minutesAlarm)/60)+hoursAlarm)%24;
  salarmstructure.AlarmTime.Minutes = (sprealarmstructure.AlarmTime.Minutes+((sprealarmstructure.AlarmTime.Seconds
                                              +secondsAlarm)/60)+minutesAlarm)%60;
  salarmstructure.AlarmTime.Seconds = (sprealarmstructure.AlarmTime.Seconds+secondsAlarm)%60;
 
  if(HAL_RTC_SetAlarm_IT(&RtcHandle,&salarmstructure,RTC_FORMAT_BIN) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
}

Sorry for such a precarious code.

Outcomes