HAL Library 24- RTC for STM32Fxxx

RTC library was ported to HAL drivers for STM32Fxxx line from my previously library RTC for STM32F4xx. If features similar things as before except some functions were changed, which are already described in HAL API.


Read more about new HAL libraries


  • Support Internal or external clock source
    • PC14 and PC15 pins are used for external crystal oscillator
    • STM32F4/429 Discovery does not have RTC crystal onboard. Check board’s manual on how to set it up
  • Support wakeup interrupt
  • Support to set 2 internal alarms to trigger interrupts
  • they can also wake up STM32Fxxx from any low power mode
  • Get seconds from 01.01.1970 00:00:00
  • Get readable time from seconds from 01.01.1970 00:00:00
  • Support to save/get data in binary or BCD format
  • Support for read/write data to/from RTC backup registers
  • Support for subsecond
  • Support to write data in string format
  • Date and time are checked before saved for valid input data
  • Get days in month and year


  • HAL
  • TM
    • STM32Fxxx HAL
    • defines.h configuration file
    • attributes.h

Functions and enumerations

Full functions list can be found on HAL API section.


Projects are available on Github, download all libraries below.

TM STM32 Libraries

STM32 libraries based on STM32Fxxx HAL drivers.


Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Pingback: All STM32 HAL libraries - STM32F4 Discovery()

  • Christian Julius

    Just an addition:

    Most quarzes have a deviation from ideal 32768 ticks/s. Mine too, The RTC goes wrong for +3s a day. The easiest way is to use this function of StdPeriphLib somewhere in startup code:


    SmoothCalibPlusPulses_Reset: If the clock is too fast, the 22 is my personal ideal value to reduce clock speed by subtracting 22 ticks per 32s. (value 0 < 512)

    SmoothCalibPlusPulses_Set: If the clock is too slow, than the value is added every 32s

    The RTC_SmoothCalibPeriod_32sec can be reduced too 16 and 8, if the deviation is bigger.

    RTC is exacly in sync with the atomic clock for more than 4 days now 🙂

  • Anna Leshinsky

    I am trying to use the file “tm_stm32_rtc.c” from your library but I have many undefined variables ( image attached), all this variables is defined in different stm32f4xx_hal_XXXX.c files . all the hal peripherals files included to the project. I tried to include headers from all hal files but it doesn’t help. Maybe you have any idea?

    • Set include files. Include paths are missing I would say in your project.

      • Anna Leshinsky

        have included all path….
        Mounted the project again… without using RTC works , with it don’t
        But there is some errors with the linker (attached pic)

        Thanks for your advise…
        Will try to solve it..

        • Have no idea what IAR does.
          How can you use so ugly IDE?

          • Anna Leshinsky

            For my regret this is what I have…. The keil is more friendly?

          • Anything is more friendly than IAR.

          • Anna Leshinsky

            Will try in my spare time 🙂 Thanks

  • Anna Leshinsky

    After overcoming the IAR challenges It is working 🙂
    Just want to ask how can I get the real time ? from where the rtc synchronizes?

    • You must write time manually. I used ethernet in one project for that

      • Anna Leshinsky

        What do you mean manually? when I am starting the program to update the time ant it will be run from this point?

        • It will, butyou must somehow know current time to write it when necessary.

          • Anna Leshinsky

            Hmmm….. I am designing kind of data logging machine… there is timestamp neened to map the data sampled. Do you know how I can make it?

        • Manual means you need to write it yourself. Where you will get current time for write when you do it, this is your problem.

  • Abhinav Kumar

    Hi Majerle ,

    Thanks a lot for this RTC program.But i am facing some issues here :
    hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK; inside RTC_EnterInitMode(RTC_HandleTypeDef* hrtc) is not being performed properly as a result it returns HAL_TIMEOUT and setting of Date and Time Fails.

    The very same programs for RTC works fine which you have done using Standard Peripherals Library.

    Can you please suggest what might be the probable cause?

    (I am using STM32F4 discovery board)

  • Youssef Kamoun

    How can I get Microseconds out of this? I already implemented it and can get only Milliseconds.

  • Abhinav Kumar

    Hi Majerle ,

    I am facing a very strange problem, i am displaying the RTC time on Hyperterminal using UART.

    The time is displayed properly in debug mode(Keil)
    whereas in normal execution without debug the time displayed is always constant.

    Can you please suggest the probable cause?

    • From know informations I’m unable to give you useful info.

  • Cesar


    I have a problem when i compiling 3 erros
    ..0-HAL_DRIVERSSTM32F7xx_HAL_DriverSrcstm32f7xx_hal_pcd.c(382): error: #20: identifier “USB_OTG_DOEPINT_OTEPSPR” is undefined
    ..0-HAL_DRIVERSSTM32F7xx_HAL_DriverSrcstm32f7xx_hal_pcd.c(521): error: #20: identifier “USB_OTG_DOEPMSK_OTEPSPRM” is undefined

    ..0-HAL_DRIVERSSTM32F7xx_HAL_DriverSrcstm32f7xx_hal_pcd.c: 0 warnings, 2 errors

    “USB_OTG_DOEPINT_OTEPSPR” keil say undeclared