4

I'm having a strange behavior with the RTC on a stm32L476 with FreeRTOS.

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.

Also if I do DEBUG when I put breakpoint at stm32l4xx_hal_rtc.c at line 583:

tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);    
*breakpoint* sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);

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.

So why it's not working when normal RUN?

Init code (cube MX generated):

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(&hrtc) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Initialize RTC and set the Time and Date 
    */
  if(HAL_RTCEx_BKUPRead(&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(&hrtc, &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(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
  }

}

void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
{

  if(rtcHandle->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 */
  }
}

task where clock is readed and printed all this task and functions are at the same menu.c:

void MenuTask(void const *argument){
    for(;;){

        /*
         * Menus
         */
        DrawMenu();

        osDelay(100);
    }
}

void DrawMenu(){
    switch(menuTaskStatus){

/* Not important code */

    case MENU_INFO:
        menuInfoBar();

        break;

    }
}

I print on the LCD a bar with the clock in the middle

void menuInfoBar(){

    //Clock
    CheckClock();
    if(updateNeeded.Clock){
        DrawClock();
        updateNeeded.Clock = 0;
    }

}

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.

void CheckClock(){
    RTC_TimeTypeDef timeVar;
    //  HAL_GPIO_TogglePin(LEDR_GPIO_Port, LEDR_Pin);
//  if(HAL_RTC_WaitForSynchro(&hrtc) == HAL_OK){
        while(HAL_RTC_GetTime(&hrtc,&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;
        }
//  }
}

Here I only draw the clock on my display

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);
}

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

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?

time has nothing to do I've tried with 5s delay and also don't works

Thanks

7
  • Why don't you use registers - just this HAL bloatware? Commented Oct 31, 2017 at 23:46
  • I'm a hardware specialist, I want to waste the less time possible in software. But after all this nonsense problems I'm getting convinced that HAL libraries are far away of been the smart solution ST would like.
    – taquionbcn
    Commented Nov 1, 2017 at 7:19
  • You cant use uC without its peripheral knowledge. Using HAL does not change it, it is just impossible. And at the end of the day you need to learn uC & very poor documented, full of bugs, glitches and strange HAL library. So you are gong to waste a lots of time. Have you noticed that here there are almost no questions from people programming the bare metal way, only HAL Commented Nov 1, 2017 at 9:24
  • @PeterJ_01 Using a HAL is obviously good practice, as it does indeed allow you to port the code to a different platform without having to rewrite anything but the driver. This is the main reason why properly designed MCU programs use a HAL. As for the specific case of ST, I don't think they quite know what a HAL is, because indeed they use it like you describe, as some misguided attempt to hide away hardware-related programming, rather than to encapsulate device-specific functionality. But just because ST are bad at program design, it doesn't mean that all HAL are bad.
    – Lundin
    Commented Nov 1, 2017 at 10:24
  • What does this question have to do with C11 specifically? If you just have a generic C question which is not about C11 features, just tag the question C. Unless a specific standard version is mentioned when tagging a question as C, we assume the current standard, which happens to be C11. See stackoverflow.com/tags/c/info
    – Lundin
    Commented Nov 1, 2017 at 10:27

4 Answers 4

19

A little late may be but I ran into the same problem and it turns out that the HAL_GetTime(..) function has a quirk. HAL_RTC_GetDate() must be called after it to unlock the values.

You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values * in the higher-order calendar shadow registers to ensure consistency between the time and date values. * Reading RTC current time locks the values in calendar shadow registers until current date is read.

This is written as a note in their documentation as well as in the source code of the RTC HAL driver.

Personally, I believe that ST's guys should've made a HAL_RTC_GetTimeAndDate() and spare us falling in this trap.

1
  • thanks john, I missed where is explained but dosen't matter now, the function is working OK as I write down at the solution. And works well because it is in a firmware already working on a device in production.
    – taquionbcn
    Commented May 16, 2018 at 10:03
0
struct 
{
    unsigned TR;
}*rcc = (void *)0x56456546;

typedef struct
{
    unsigned    su  :4;
    unsigned    st  :2;
    unsigned        :1;
    unsigned    mu  :4;
    unsigned    mt  :2;
    unsigned        :1;
    unsigned    hu  :4;
    unsigned    ht  :2;
    unsigned    pm  :1;
    unsigned        :9
}RCC_TR_T;


typedef struct
{
    uint8_t seconds, minutes, hours, pm;
}TIME_T;


TIME_T *GetTime(TIME_T *time)
{
    RCC_TR_T *tr = (RCC_TR_T *)&rcc -> TR;

    time -> hours = tr -> hu + tr -> ht * 10;
    time -> minutes = tr -> mu + tr -> mt * 10;
    time -> seconds = tr -> su + tr -> st * 10;
    time -> pm = tr -> pm;
    return time;
}
1
  • 1
    useless, and don't answers my question as John Wick did. This is another workaround more fancy and complicated but does the same or not, I will not try.
    – taquionbcn
    Commented May 16, 2018 at 10:28
0

This Answer don't answers why the ST "hal" don't works, but it solves what I need, that is using RTC.

This is my new CheckClock function:

void CheckClock(){

    uint32_t tmpreg = (uint32_t) hrtc.Instance->TR;

    /* Fill the structure fields with the read parameters */
    timeVar.Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
    timeVar.Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
    timeVar.Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));

    if(timeVar.Seconds != timeVarAnt.Seconds){
        HAL_GPIO_TogglePin(LEDR_GPIO_Port, LEDR_Pin);
        timeVarAnt.Minutes = timeVar.Minutes;
        timeVarAnt.Hours = timeVar.Hours;
        timeVarAnt.Seconds = timeVar.Seconds;
        updateNeeded.Clock = 1;
    }
}

Thanks

EDIT 05/2018 :

void CheckClock(){

    uint32_t tmpreg = (uint32_t) hrtc.Instance->TR;
    timeVar.Hours = Bcd2ToByte((uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16));
    timeVar.Minutes =Bcd2ToByte( (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8));
    timeVar.Seconds =Bcd2ToByte( (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU)));

    if(timeVar.Seconds != timeVarAnt.Seconds){
        timeVarAnt.Minutes = timeVar.Minutes;
        timeVarAnt.Hours = timeVar.Hours;
        timeVarAnt.Seconds = timeVar.Seconds;
        IBS.updates.Clock = 1;
    }
    //  }
}
5
  • I afraid your function is 100% wrong. See my answer how to correct handle it. The values stored are not binary. Commented May 7, 2018 at 11:00
  • there is nothing to correct, this function has work since it was posted here and its working in a deployed project with a few hundred of devices and growing.
    – taquionbcn
    Commented May 16, 2018 at 10:28
  • They are BCD coded not binary. You do binary arithmetic not BCD. But of course you can choose to do it the wrong way. It is your program. Commented May 16, 2018 at 10:37
  • I knew is BCD long time ago
    – taquionbcn
    Commented May 16, 2018 at 10:38
  • never mind, the question was related to wrong use of HAL libs not number codification of RTC, Observing that there is an answer marked as solution, first you should have ask if I was saving them as BCD or binary, and then I would answered you that the BCD to binary is done in another function and I would have put the finally version of what is running as I haven't touched since December
    – taquionbcn
    Commented May 16, 2018 at 10:51
0
         //HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN); 
         char xsa[6];
         char your_time[9];
         sprintf(xsa,"%06x",(uint32_t)hrtc.Instance->TR);
         sprintf((char*)your_time,"%c%c:%c%c:%c%c%c",xsa[0],xsa[1],xsa[2],xsa[3],xsa[4],xsa[5],'\0');
1
  • Your answer must have an interpretive example along with your code, after all this question is old ...
    – Boaventura
    Commented May 29, 2021 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.