cancel
Showing results for 
Search instead for 
Did you mean: 

Getting RTC value

Selim Sagir
Associate III

Hello, I am reading ADC data with b-l072z-lrwan1 and send another kit. I need to add this datas to timestamp for compare different sensor datas. So I am trying to read RTC time value. I have called this function from "hw_rtc.c" ;

uint32_t CalendarValue1 = HW_RTC_GetTimerValue();

code to my main loop and read RTC timer in ticks. I am reading numbers like;

[17:31:44:155] 1732371278 �?�
[17:31:44:155] 1732371279 �?�
[17:31:44:155] 1732371281 �?�
[17:31:44:155] 1732371282 �?�
[17:31:44:155] 1732371284 �?�
[17:31:44:155] 1732371285 �?�
[17:31:44:161] 1732371286 �?�
[17:31:44:161] 1732371288 �?�
[17:31:44:161] 1732371289 �?�
[17:31:44:161] 1732371291 �?�
[17:31:44:163] 1732371292 �?�
[17:31:44:163] 1732371294 �?�
[17:31:44:166] 1732371295 �?�
[17:31:44:170] 1732371296 �?�
[17:31:44:170] 1732371298 �?�
[17:31:44:170] 1732371299 �?�
[17:31:44:172] 1732371301 �?�
[17:31:44:172] 1732371302 �?�
[17:31:44:173] 1732371303 �?�
[17:31:44:175] 1732371305 �?�
[17:31:44:177] 1732371306 �?�
[17:31:44:177] 1732371308 �?�
[17:31:44:178] 1732371309 �?�
[17:31:44:181] 1732371311 �?�
[17:31:44:181] 1732371312 �?�
[17:31:44:183] 1732371313 �?�
[17:31:44:184] 1732371315 �?�
[17:31:44:185] 1732371316 �?�
[17:31:44:188] 1732371318 �?�
[17:31:44:188] 1732371319 �?�
[17:31:44:189] 1732371320 �?�
[17:31:44:191] 1732371322 �?�
[17:31:44:192] 1732371323 �?�
[17:31:44:195] 1732371325 �?�
[17:31:44:195] 1732371326 �?�
[17:31:44:198] 1732371328 �?�
[17:31:44:198] 1732371329 �?�
[17:31:44:200] 1732371330 �?�
[17:31:44:202] 1732371332 �?�
[17:31:44:202] 1732371333 �?�
[17:31:44:203] 1732371335 �?�
[17:31:44:205] 1732371336 �?�
[17:31:44:206] 1732371337 �?�
[17:31:44:207] 1732371339 �?�
[17:31:44:209] 1732371340 �?�
[17:31:44:210] 1732371342 �?�
[17:31:44:212] 1732371343 �?�
[17:31:44:213] 1732371345 �?�
[17:31:44:214] 1732371346 �?�
[17:31:44:216] 1732371347 �?�
[17:31:44:217] 1732371349 �?�
[17:31:44:220] 1732371350 �?�
[17:31:44:220] 1732371352 �?�
[17:31:44:221] 1732371353 �?�
[17:31:44:224] 1732371354 �?�
[17:31:44:227] 1732371356 �?�
[17:31:44:227] 1732371357 �?�
[17:31:44:229] 1732371359 �?�
[17:31:44:229] 1732371360 �?�
[17:31:44:231] 1732371362 �?�
[17:31:44:231] 1732371363 �?�
[17:31:44:233] 1732371364 �?�
[17:31:44:235] 1732371366 �?�
[17:31:44:235] 1732371367 �?�
[17:31:44:237] 1732371369 �?�
[17:31:44:238] 1732371370 �?�
[17:31:44:240] 1732371371 �?�
[17:31:44:241] 1732371373 �?�
[17:31:44:244] 1732371374 �?�
[17:31:44:244] 1732371376 �?�
[17:31:44:246] 1732371377 �?�
[17:31:44:246] 1732371379 �?�
[17:31:44:248] 1732371380 �?�
[17:31:44:250] 1732371381 �?�
[17:31:44:251] 1732371383 �?�
[17:31:44:253] 1732371384 �?�
[17:31:44:253] 1732371386 �?�
[17:31:44:255] 1732371387 �?�
[17:31:44:257] 1732371388 �?�
[17:31:44:258] 1732371390 �?�
[17:31:44:261] 1732371391 �?�
[17:31:44:261] 1732371393 �?�
[17:31:44:264] 1732371394 �?�
[17:31:44:264] 1732371396 �?�
[17:31:44:265] 1732371397 �?�
[17:31:44:267] 1732371398 �?�
[17:31:44:267] 1732371400 �?�
[17:31:44:268] 1732371401 �?�
[17:31:44:270] 1732371403 �?�
[17:31:44:272] 1732371404 �?�
[17:31:44:272] 1732371405 �?�
[17:31:44:275] 1732371407 �?�
[17:31:44:277] 1732371408 �?�
[17:31:44:278] 1732371410 �?�
[17:31:44:278] 1732371411 �?�

Time values come from CuteCom, calendarValues like 1732....... Then i converted this value to normal date but it is wrong when i compare with RTC registers.

/*!
 * @brief Get the RTC timer value
 * @param none
 * @retval RTC Timer value in ticks
 */
uint32_t HW_RTC_GetTimerValue( void )
{
  RTC_TimeTypeDef RTC_TimeStruct;
  RTC_DateTypeDef RTC_DateStruct;
  
  uint32_t CalendarValue = (uint32_t) HW_RTC_GetCalendarValue(&RTC_DateStruct, &RTC_TimeStruct );
 
  return( CalendarValue );
 
}
/*!
 * @brief get current time from calendar in ticks
 * @param pointer to RTC_DateStruct
 * @param pointer to RTC_TimeStruct
 * @retval time in ticks
 */
static TimerTime_t HW_RTC_GetCalendarValue( RTC_DateTypeDef* RTC_DateStruct, RTC_TimeTypeDef* RTC_TimeStruct )
{
  TimerTime_t calendarValue = 0;
  uint32_t i = 0;
  uint32_t first_read;
  
  /* Get Time and Date*/
  HAL_RTC_GetTime( &RtcHandle, RTC_TimeStruct, RTC_FORMAT_BIN );
 
   /* make sure it is correct due to asynchronus nature of RTC*/
  do {
//    first_read = RTC_TimeStruct->SubSeconds;
	  first_read =  RTC->SSR;
    HAL_RTC_GetDate( &RtcHandle, RTC_DateStruct, RTC_FORMAT_BIN );
    HAL_RTC_GetTime( &RtcHandle, RTC_TimeStruct, RTC_FORMAT_BIN );
  }while( first_read != RTC->SSR );
//  } while (first_read != RTC_TimeStruct->SubSeconds);
 
  /* years (calc valid up to year 2099)*/
  for( i = 0; i < RTC_DateStruct->Year; i++ )
  {
    if( (i % 4) == 0 )
    {
      calendarValue += DaysInLeapYear * SecondsInDay;
    }
    else
    {
      calendarValue += DaysInYear * SecondsInDay;
    }
  }
 
  /* months (calc valid up to year 2099)*/
  if(( (RTC_DateStruct->Year % 4) == 0 ) )
  {
    for( i = 0; i < ( RTC_DateStruct->Month - 1 ); i++ )
    {
      calendarValue += DaysInMonthLeapYear[i] * SecondsInDay;
    }
  }
  else
  {
    for( i = 0;  i < ( RTC_DateStruct->Month - 1 ); i++ )
    {
      calendarValue += DaysInMonth[i] * SecondsInDay;
    }
  }
 
  /* days */
  calendarValue += ( ( uint32_t )RTC_TimeStruct->Seconds + 
                     ( ( uint32_t )RTC_TimeStruct->Minutes * SecondsInMinute ) +
                     ( ( uint32_t )RTC_TimeStruct->Hours * SecondsInHour ) + 
                     ( ( uint32_t )( RTC_DateStruct->Date * SecondsInDay ) ) );
  
  calendarValue = (calendarValue<<N_PREDIV_S) + ( PREDIV_S - RTC_TimeStruct->SubSeconds);
 
  return( calendarValue );
}

What is the wrong with this. I am just call a function from library. Actually, when the first time i think to just read time registers registers as RTC_TimeStruct->Seconds, RTC_TimeStruct->Minutes, RTC_TimeStruct->Hours, but i can not solve it.Thanks for help.

2 REPLIES 2

What is the problem? What do you observe and how is it different from what you expect to see?

JW

PS. Read RM, Reading the calendar subchapter of the RTC chapter. Note that if BYPSHAD=0, reading time locks the date register until date is read; and then it takes some time until shadow TR gets updated.

Hi JW, thanks for your reply. My time init is like that;

static void HW_RTC_SetConfig( void )
{
  RTC_TimeTypeDef RTC_TimeStruct;
  RTC_DateTypeDef RTC_DateStruct;
 
  RtcHandle.Instance = RTC;
 
  RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
  RtcHandle.Init.AsynchPrediv = PREDIV_A; /* RTC_ASYNCH_PREDIV; */
  RtcHandle.Init.SynchPrediv = PREDIV_S; /* RTC_SYNCH_PREDIV; */
  RtcHandle.Init.OutPut = RTC_OUTPUT;
  RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
 
  HAL_RTC_Init( &RtcHandle );
  
  /*Monday 1st January 2016*/
  RTC_DateStruct.Year = 16;
  RTC_DateStruct.Month = RTC_MONTH_JANUARY;
  RTC_DateStruct.Date = 1;
  RTC_DateStruct.WeekDay = RTC_WEEKDAY_MONDAY;
  HAL_RTC_SetDate(&RtcHandle , &RTC_DateStruct, RTC_FORMAT_BIN);
  
  /*at 0:0:0*/
  RTC_TimeStruct.Hours = 0;
  RTC_TimeStruct.Minutes = 0;
 
  RTC_TimeStruct.Seconds = 0;
  RTC_TimeStruct.TimeFormat = 0;
  RTC_TimeStruct.SubSeconds = 0;
  RTC_TimeStruct.StoreOperation = RTC_DAYLIGHTSAVING_NONE;
  RTC_TimeStruct.DayLightSaving = RTC_STOREOPERATION_RESET;
  
  HAL_RTC_SetTime(&RtcHandle , &RTC_TimeStruct, RTC_FORMAT_BIN);
  
 /*Enable Direct Read of the calendar registers (not through Shadow) */
  HAL_RTCEx_EnableBypassShadow(&RtcHandle);
}

I didnt change anything for first step, so i need to see that date Monday 1st January 2016 and time is 00:00:seconds,subsecond but it is not. Maybe i am converting it wrong way. I use i online website for ticks to time converter. Do you have any advice for convert it to time and date?

Thanks.

Selim.