Home

Awesome

STM32RTC

A RTC library for STM32.

Requirement

API

This library is based on the Arduino RTCZero library. The library allows to take control of the internal RTC of the STM32 boards.

Singleton design pattern is used to ensure that only one STM32RTC instance is instantiated:

/* Get the rtc object */
STM32RTC& rtc = STM32RTC::getInstance();

The following functions are not supported:

The following functions have been added to support specific STM32 RTC features:

RTC hours mode (12 or 24)

RTC clock source

RTC Asynchronous and Synchronous prescaler

SubSeconds management

Hour format (AM or PM)

Week day configuration

Time and date configuration (added for convenience)

Since STM32RTC version higher than 1.0.3

SubSeconds alarm management

Important note:

Library version management

STM32 RTC library version is based on Semantic Versioning 2.0.0 (https://semver.org/)

This will ease some dependencies:

* `STM32_RTC_VERSION_MAJOR` -> major version
* `STM32_RTC_VERSION_MINOR` -> minor version
* `STM32_RTC_VERSION_PATCH` -> patch version
* `STM32_RTC_VERSION_EXTRA` -> Extra label
 with:
  - 0: official release
  - [1-9]: release candidate
  - F[0-9]: development

* `STM32_RTC_VERSION` --> Full version number

STM32_RTC_VERSION can de used to handle some API change:

#if defined(STM32_RTC_VERSION) && (STM32_RTC_VERSION  >= 0x01010000)
  rtc.setAlarmTime(alarmHours, alarmMinutes, alarmSeconds, 123);
#else
  rtc.setAlarmTime(alarmHours, alarmMinutes, alarmSeconds);
#endif

Since STM32RTC version higher than 1.1.1

One-Second interrupt

STM32 RTC includes a one-second interrupt for generating a periodic interrupt signal.

Date retention for stm32F1xx

STM32 RTC includes date save/retrieve mechanism for the stm32F1xx mcu, that do not have a date counter.

The content is stored in BackUp memory which is kept during Reset and powered by Vbat when the Vdd is off.

Since STM32 Core version > 1.5.0

Reset time management

By default, if a time is set it will not be reset after a reboot.

Using begin(true) or begin(true, HOUR_24) will reset the RTC registers.

To know if a time has already been set use:

  if (!rtc.isTimeSet()) {
    // Set the time
    rtc.setHours(hours);
    rtc.setMinutes(minutes);
    rtc.setSeconds(seconds);
  }

Since STM32RTC version higher than 1.3.4

Second alarm (Alarm B)

Some STM32 RTC have a second alarm named RTC_ALARM_B. It is possible to use it thanks all alarm API with an extra argument:

    rtc.attachInterrupt(myCallback, &myCallbackdata, STM32RTC::ALARM_B);
    rtc.setAlarmDay(day, STM32RTC::ALARM_B);
    rtc.setAlarmTime(hours, minutes, seconds + 5, 567, STM32RTC::ALARM_B);
    rtc.enableAlarm(rtc.MATCH_DHHMMSS, STM32RTC::ALARM_B);

Since STM32RTC version higher than 1.3.7

Get the RTC handle

binary and mix modes

Some STM32 RTC have a binary mode with 32-bit free-running counter in addition to their BCD mode for calendar (for example stm32wl55). Combined with BCD this is the MIX mode. Only using the binary counter is the BIN mode. Three RTC functional modes are available:

Any API using the Subsecond parameter is expressed in milliseconds whatever the RTC input clock. This parameter is [0..999] in MIX or BCD mode and [0..0xFFFFFFFF] in BIN mode. In BIN only mode, time and date registers are not used by the RTC. Thus the getEpoch function is only to be called to get the subsecond [0..0xFFFFFFFF] (returned time_t is not valid). The setAlarmEpoch only uses the sub-second [0..0xFFFFFFFF] (time_t value is useless).

SubSeconds underflow

Only dor STM32WLxx. Manage interrupt (SSRU) when SubSeconds register underflow. Used by STM32LoRaWAN.

Refer to the Arduino RTC documentation for the other functions http://arduino.cc/en/Reference/RTC

Since STM32RTC version higher than 1.4.0

IsFormat_24Hour

Returns True if the current Hour Format is HOUR_24 else false if Hour format is HOUR_12

Source

Source files available at: https://github.com/stm32duino/STM32RTC