title: stm32l4 RTC HAL not working
body: <p>I'm having a strange behavior with the RTC on a stm32L476 with FreeRTOS.</p>
  
  <p>It only reads the first time in RUN mode, RTC is working, because from run to run it saves the value of the internal register and is going up.</p>
  
  <p>Also if I do DEBUG when I put breakpoint at stm32l4xx_hal_rtc.c at line 583:</p>
  
  <pre class="lang-c prettyprint-override"><code>tmpreg = (uint32_t)(hrtc-&gt;Instance-&gt;TR &amp; RTC_TR_RESERVED_MASK);    
  *breakpoint* sTime-&gt;Hours = (uint8_t)((tmpreg &amp; (RTC_TR_HT | RTC_TR_HU)) &gt;&gt; 16);
  </code></pre>
  
  <p>I can see the tmpreg and TR register how they update, and then when I click jump to next breakpoint witch is the same I saw the display updated.</p>
  
  <p>So why it's not working when normal RUN?</p>
  
  <p>Init code (cube MX generated):</p>
  
  <pre class="lang-c prettyprint-override"><code>void MX_RTC_Init(void)
  {
    RTC_TimeTypeDef sTime;
    RTC_DateTypeDef sDate;
  
      /**Initialize RTC Only 
      */
    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;
    if (HAL_RTC_Init(&amp;hrtc) != HAL_OK)
    {
      _Error_Handler(__FILE__, __LINE__);
    }
  
      /**Initialize RTC and set the Time and Date 
      */
    if(HAL_RTCEx_BKUPRead(&amp;hrtc, RTC_BKP_DR0) != 0x32F2){
    sTime.Hours = 0;
    sTime.Minutes = 0;
    sTime.Seconds = 0;
    sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
    sTime.StoreOperation = RTC_STOREOPERATION_RESET;
    if (HAL_RTC_SetTime(&amp;hrtc, &amp;sTime, RTC_FORMAT_BIN) != HAL_OK)
    {
      _Error_Handler(__FILE__, __LINE__);
    }
  
    sDate.WeekDay = RTC_WEEKDAY_MONDAY;
    sDate.Month = RTC_MONTH_JANUARY;
    sDate.Date = 1;
    sDate.Year = 0;
  
    if (HAL_RTC_SetDate(&amp;hrtc, &amp;sDate, RTC_FORMAT_BIN) != HAL_OK)
    {
      _Error_Handler(__FILE__, __LINE__);
    }
  
      HAL_RTCEx_BKUPWrite(&amp;hrtc,RTC_BKP_DR0,0x32F2);
    }
  
  }
  
  void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
  {
  
    if(rtcHandle-&gt;Instance==RTC)
    {
    /* USER CODE BEGIN RTC_MspInit 0 */
  
    /* USER CODE END RTC_MspInit 0 */
      /* RTC clock enable */
      __HAL_RCC_RTC_ENABLE();
    /* USER CODE BEGIN RTC_MspInit 1 */
  
    /* USER CODE END RTC_MspInit 1 */
    }
  }
  </code></pre>
  
  <p>task where clock is readed and printed all this task and functions are at the same menu.c:</p>
  
  <pre class="lang-c prettyprint-override"><code>void MenuTask(void const *argument){
      for(;;){
  
          /*
           * Menus
           */
          DrawMenu();
  
          osDelay(100);
      }
  }
  
  void DrawMenu(){
      switch(menuTaskStatus){
  
  /* Not important code */
  
      case MENU_INFO:
          menuInfoBar();
  
          break;
  
      }
  }
  </code></pre>
  
  <p>I print on the LCD a bar with the clock in the middle</p>
  
  <pre class="lang-c prettyprint-override"><code>void menuInfoBar(){
  
      //Clock
      CheckClock();
      if(updateNeeded.Clock){
          DrawClock();
          updateNeeded.Clock = 0;
      }
  
  }
  </code></pre>
  
  <p>Here is the problematic part, as you can see I have tried a wait for synchro but also didn't work. I have some doubts of how does this syncro and RTC reading works.</p>
  
  <pre class="lang-c prettyprint-override"><code>void CheckClock(){
      RTC_TimeTypeDef timeVar;
      //  HAL_GPIO_TogglePin(LEDR_GPIO_Port, LEDR_Pin);
  //  if(HAL_RTC_WaitForSynchro(&amp;hrtc) == HAL_OK){
          while(HAL_RTC_GetTime(&amp;hrtc,&amp;timeVar,RTC_FORMAT_BIN)!= HAL_OK);
          if(timeVar.Seconds != timeVarAnt.Seconds){
              timeVarAnt.Minutes = timeVar.Minutes;
              timeVarAnt.Hours = timeVar.Hours;
              timeVarAnt.Seconds = timeVar.Seconds;
              updateNeeded.Clock = 1;
          }
  //  }
  }
  </code></pre>
  
  <p>Here I only draw the clock on my display</p>
  
  <pre class="lang-c prettyprint-override"><code>void DrawClock(){
      DISP_locate(49,0);
      sprintf((char *)stringBuffer,"%02d:%02d:%02d",(int)timeVarAnt.Hours,(int)timeVarAnt.Minutes,(int)timeVarAnt.Seconds);
      DISP_puts((char *)stringBuffer);
  }
  </code></pre>
  
  <p>It's possible I can't read the RTC fast as 100ms?
  some one could explain to me why is needed a syncronitzation? datasheet explains that if the clock is 7 time faster is ok, I'm using an 80Mhz APB1 clock</p>
  
  <p>some tutorials and examples I've found the do the exact same I do, but they read on the main loop with osDelay() of many values. Is a problem using freeRTOS and reading from a task?</p>
  
  <p>time has nothing to do I've tried with 5s delay and also don't works</p>
  
  <p>Thanks</p>