bertuletti.stefano

Hardware RTC subseconds resolution

Discussion created by bertuletti.stefano on Feb 16, 2018
Latest reply on Feb 19, 2018 by dhenry

Hi, i've a problem while reading subsecond of an hardware RTC of both STM32F4 and L4.

 

I've created a timer which generates an interrupt every 5ms. In the handler of this timer i read the RTC TIME with subsecond, but the value that i read is not 5ms, but it varied between 3-10ms.

 

***************************************************************************************

this is the configuration of the timer (clock timer =48MHz):

      htim16.Instance = TIM16;
      htim16.Init.Prescaler = 47999;
      htim16.Init.CounterMode = TIM_COUNTERMODE_UP;
      htim16.Init.Period = 4;
      htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
      htim16.Init.RepetitionCounter = 0;
      if (HAL_TIM_Base_Init(&htim16) != HAL_OK)
      {
            Error_Handler();
      }

      /* Peripheral clock enable */
      __HAL_RCC_TIM16_CLK_ENABLE();
      /* Peripheral interrupt init */
      HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, 0, 0);
      HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);

************************************************************************************

************************************************************************************

While this is the configuration of the RTC:

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;

************************************************************************************

 

In the interrupt handler of the timer i only read the rtc time and date:

RTC_TimeTypeDef time;
RTC_DateTypeDef day;

HAL_RTC_GetTime(&hrtc,&time,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc,&day,RTC_FORMAT_BIN);

struct tm t;
time_t t_of_day;

t.tm_year = day.Year+100; // Year: 2017 -> 117
t.tm_mon = day.Month-1; // Month: January -> 0
t.tm_mday = day.Date;
t.tm_hour = time.Hours;
t.tm_min = time.Minutes;
t.tm_sec = time.Seconds;
t.tm_isdst = 0;
t_of_day = mktime(&t);

uint32_t subsecond=1000-(time.SubSeconds * 1000 / 1023);

 

 

 

The strange thing is that if i read the value of the systick (1ms resolution) using the function HAL_GET_TICK() the delay between each interrupt is correctly eqaul to 10.

Outcomes