Electronic – STM32 RTC with HAL libraries

hal-libraryrtcstm32

I am working on stm32l053 (nucleo 64) and I want to track time with integrated RTC. Since I am quite new to stm32 development I am using CubeMX and HAL libraries.

My problem is that the time is not changing over time, the HAL_RTC_GetTime() function is always returning time that was configured at the beginning in the MX_RTC_Init() function generated by CubeMX.

Here is my code for testing:
Vars:

RTC_TimeTypeDef currTime = {0};

in while

    while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
      HAL_RTC_GetTime(&hrtc, &currTime, RTC_FORMAT_BIN);
      char timeBuff[20];
      sprintf(timeBuff,"%d,,,%d...%d\n", currTime.Hours, currTime.Minutes, currTime.Seconds);
      HAL_UART_Transmit(&huart2, timeBuff, sizeof(timeBuff), 100);
      HAL_Delay(1000);


  }

And here is the generated MX_RTC_Init() function:

static void MX_RTC_Init(void)
{

  /* USER CODE BEGIN RTC_Init 0 */

  /* USER CODE END RTC_Init 0 */

  RTC_TimeTypeDef sTime = {0};
  RTC_DateTypeDef sDate = {0};

  /* USER CODE BEGIN RTC_Init 1 */

  /* USER CODE END RTC_Init 1 */
  /** Initialize RTC Only 
  */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_12;
  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;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }

  /* USER CODE BEGIN Check_RTC_BKUP */

  /* USER CODE END Check_RTC_BKUP */

  /** Initialize RTC and set the Time and Date 
  */
  sTime.Hours = 10;
  sTime.Minutes = 15;
  sTime.Seconds = 0;
  sTime.TimeFormat = RTC_HOURFORMAT12_PM;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  sDate.WeekDay = RTC_WEEKDAY_SATURDAY;
  sDate.Month = RTC_MONTH_MAY;
  sDate.Date = 25;
  sDate.Year = 19;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
  /** Enable the WakeUp 
  */
  if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 5, RTC_WAKEUPCLOCK_CK_SPRE_16BITS) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN RTC_Init 2 */

  /* USER CODE END RTC_Init 2 */

}

Serial output is:

10,,,15...0
10,,,15...0
10,,,15...0
10,,,15...0
10,,,15...0
10,,,15...0
10,,,15...0
10,,,15...0
10,,,15...0
10,,,15...0
10,,,15...0
.
.
.

PS.
RTC WakeUp is working as expected so the RTC is counting but I guess it is not storing counts in registers.

Best Answer

The solution is to call both GetTime and GetDate at the same time, If you call only GetTime it does not update correctly:

HAL_RTC_GetTime(&hrtc, &currTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &currDate, RTC_FORMAT_BIN);

I don't know how they are connected, even more so because GetDate() is run after GetTime() and it still makes difference.