STM32duino RTC
Arduino library for managing the RTC peripheral on STM32 boards.
Loading...
Searching...
No Matches
STM32RTC.h
Go to the documentation of this file.
1
36
37#ifndef __STM32_RTC_H
38#define __STM32_RTC_H
39
40#include "Arduino.h"
41#if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION <= 0x020C0000)
42 #error "This library is not compatible with core version used. Please update the core."
43#endif
44#include "rtc.h"
45// Check if RTC HAL is enabled
46#if (!defined(HAL_RTC_MODULE_ENABLED) && (!defined(USE_HAL_RTC_MODULE) || \
47 (defined(USE_HAL_RTC_MODULE) && (USE_HAL_RTC_MODULE == 0))))
48#error "HAL RTC module is not enabled."
49#endif
50#include <time.h>
51
55#define STM32_RTC_VERSION_MAJOR (0x02U)
56#define STM32_RTC_VERSION_MINOR (0x00U)
57#define STM32_RTC_VERSION_PATCH (0x00U)
58/*
59 * Extra label for development:
60 * 0: official release
61 * [1-9]: release candidate
62 * F[0-9]: development
63 */
64#define STM32_RTC_VERSION_EXTRA (0x00U)
65#define STM32_RTC_VERSION ((STM32_RTC_VERSION_MAJOR << 24U)\
66 |(STM32_RTC_VERSION_MINOR << 16U)\
67 |(STM32_RTC_VERSION_PATCH << 8U )\
68 |(STM32_RTC_VERSION_EXTRA))
69
70
71#if defined(RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048) || defined(RCC_RTC_WDG_SUBG_LPAWUR_LCD_LCSC_CLKSOURCE_DIV512)
72#define IS_CLOCK_SOURCE(SRC) (((SRC) == STM32RTC::LSI_CLOCK) || ((SRC) == STM32RTC::LSE_CLOCK) ||\
73 ((SRC) == STM32RTC::HSI_CLOCK))
74#else
75#define IS_CLOCK_SOURCE(SRC) (((SRC) == STM32RTC::LSI_CLOCK) || ((SRC) == STM32RTC::LSE_CLOCK) ||\
76 ((SRC) == STM32RTC::HSE_CLOCK))
77#endif /* RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048 || RCC_RTC_WDG_SUBG_LPAWUR_LCD_LCSC_CLKSOURCE_DIV512 */
78#define IS_HOUR_FORMAT(FMT) (((FMT) == STM32RTC::HOUR_12) || ((FMT) == STM32RTC::HOUR_24))
79
85class STM32RTC {
86 public:
87
89 enum Hour_Format : uint8_t {
90 HOUR_12 = HOUR_FORMAT_12,
91 HOUR_24 = HOUR_FORMAT_24
92 };
93
95 enum AM_PM : uint8_t {
96 AM = HOUR_AM,
97 PM = HOUR_PM
98 };
99
101 enum Binary_Mode : uint8_t {
102 MODE_BCD = MODE_BINARY_NONE,
103 MODE_BIN = MODE_BINARY_ONLY,
104 MODE_MIX = MODE_BINARY_MIX
105 };
106
108 enum Alarm_Match : uint8_t {
109 MATCH_OFF = OFF_MSK, // Never
110 MATCH_SUBSEC = SUBSEC_MSK, // Every Subsecond
111 MATCH_SS = SS_MSK, // Every Minute
112 MATCH_MMSS = SS_MSK | MM_MSK, // Every Hour
113 MATCH_HHMMSS = SS_MSK | MM_MSK | HH_MSK, // Every Day
114 MATCH_DHHMMSS = SS_MSK | MM_MSK | HH_MSK | D_MSK, // Every Month
115 /* NOTE: STM32 RTC can't assign a month or a year to an alarm. Those enum
116 are kept for compatibility but are ignored inside enableAlarm(). */
117 MATCH_MMDDHHMMSS = SS_MSK | MM_MSK | HH_MSK | D_MSK | M_MSK,
118 MATCH_YYMMDDHHMMSS = SS_MSK | MM_MSK | HH_MSK | D_MSK | M_MSK | Y_MSK
119 };
120
122 enum Source_Clock : uint8_t {
125#if defined(RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048) || defined(RCC_RTC_WDG_SUBG_LPAWUR_LCD_LCSC_CLKSOURCE_DIV512)
126 HSI_CLOCK = ::HSI_CLOCK,
127#else
129#endif /* RCC_RTC_WDG_BLEWKUP_CLKSOURCE_HSI64M_DIV2048 || RCC_RTC_WDG_SUBG_LPAWUR_LCD_LCSC_CLKSOURCE_DIV512 */
130 };
131
133 enum Alarm : uint32_t {
135#if defined(ALARM_B_AVAILABLE)
136 ALARM_B = ::ALARM_B
137#endif
138 };
139
145 {
146 static STM32RTC instance; // Guaranteed to be destroyed.
147 // Instantiated on first use.
148 return instance;
149 }
150
151 STM32RTC(STM32RTC const &) = delete;
152 void operator=(STM32RTC const &) = delete;
153
159 void begin(bool resetTime, Hour_Format format = HOUR_24);
164 void begin(Hour_Format format = HOUR_24);
165
167 void end(void);
168
169#if !defined(USE_HALV2_DRIVER)
170 // Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA). Use at your own risk.
175 RTC_HandleTypeDef *getHandle(void)
176 {
177 return RTC_GetHandle();
178 }
179#endif
194 void setClockSource(Source_Clock source, uint32_t predivA = (PREDIVA_MAX + 1), uint32_t predivS = (PREDIVS_MAX + 1));
200 void getPrediv(uint32_t *predivA, uint32_t *predivS);
208 void setPrediv(uint32_t predivA, uint32_t predivS);
209
220
232
238 void attachInterrupt(voidFuncPtrParam callback, Alarm name);
245 void attachInterrupt(voidFuncPtrParam callback, void *data = nullptr, Alarm name = ALARM_A);
251
252#ifdef ONESECOND_IRQn
253 // Other mcu than stm32F1 will use the WakeUp feature to interrupt each second.
258 void attachSecondsInterrupt(voidFuncPtrParam callback);
260 void detachSecondsInterrupt(void);
261
262#endif /* ONESECOND_IRQn */
263#ifdef STM32WLxx
264 // STM32WLxx has a dedicated IRQ
270 void attachSubSecondsUnderflowInterrupt(voidFuncPtrParam callback);
272 void detachSubSecondsUnderflowInterrupt(void);
273#endif /* STM32WLxx */
274 // Kept for compatibility: use STM32LowPower library.
277
278 /* Get Functions */
279
284 uint32_t getSubSeconds(void);
289 uint8_t getSeconds(void);
294 uint8_t getMinutes(void);
300 uint8_t getHours(AM_PM *period = nullptr);
309 void getTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *subSeconds, AM_PM *period = nullptr);
310
315 uint8_t getWeekDay(void);
320 uint8_t getDay(void);
325 uint8_t getMonth(void);
330 uint8_t getYear(void);
338 void getDate(uint8_t *weekDay, uint8_t *day, uint8_t *month, uint8_t *year);
339
363 uint8_t getAlarmHours(Alarm name);
370 uint8_t getAlarmHours(AM_PM *period = nullptr, Alarm name = ALARM_A);
371
377 uint8_t getAlarmDay(Alarm name = ALARM_A);
378
379 // Kept for compatibility with Arduino RTCZero library.
384 uint8_t getAlarmMonth(void);
389 uint8_t getAlarmYear(void);
390
391 /* Set Functions */
392
397 void setSubSeconds(uint32_t subSeconds);
402 void setSeconds(uint8_t seconds);
407 void setMinutes(uint8_t minutes);
413 void setHours(uint8_t hours, AM_PM period = AM);
422 void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds = 1000, AM_PM period = AM);
423
428 void setWeekDay(uint8_t weekDay);
433 void setDay(uint8_t day);
438 void setMonth(uint8_t month);
443 void setYear(uint8_t year);
450 void setDate(uint8_t day, uint8_t month, uint8_t year);
458 void setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year);
459
465 void setAlarmSubSeconds(uint32_t subSeconds, Alarm name = ALARM_A);
471 void setAlarmSeconds(uint8_t seconds, Alarm name = ALARM_A);
477 void setAlarmMinutes(uint8_t minutes, Alarm name = ALARM_A);
483 void setAlarmHours(uint8_t hours, Alarm name);
490 void setAlarmHours(uint8_t hours, AM_PM period = AM, Alarm name = ALARM_A);
496 void setAlarmTime(uint32_t subSeconds, Alarm name = ALARM_A);
504 void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, Alarm name);
513 void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, Alarm name);
523 void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds = 0, AM_PM period = AM, Alarm name = ALARM_A);
524
530 void setAlarmDay(uint8_t day, Alarm name = ALARM_A);
531
532 // Kept for compatibility with Arduino RTCZero library.
537 void setAlarmMonth(uint8_t month);
542 void setAlarmYear(uint8_t year);
550 void setAlarmDate(uint8_t day, uint8_t month, uint8_t year, Alarm name = ALARM_A);
551
552 /* Epoch Functions */
553
559 time_t getEpoch(uint32_t *subSeconds = nullptr);
564 time_t getY2kEpoch(void);
570 void setEpoch(time_t ts, uint32_t subSeconds = 0);
575 void setY2kEpoch(time_t ts);
581 time_t getAlarmEpoch(Alarm name);
588 time_t getAlarmEpoch(uint32_t *subSeconds = nullptr, Alarm name = ALARM_A);
595 void setAlarmEpoch(time_t ts, Alarm_Match match, Alarm name);
603 void setAlarmEpoch(time_t ts, Alarm_Match match = MATCH_DHHMMSS, uint32_t subSeconds = 0, Alarm name = ALARM_A);
604
609 bool isConfigured(void)
610 {
611 return RTC_IsConfigured();
612 }
613
618 {
619 return (_format == HOUR_24);
620 }
621
631 bool isTimeSet(void)
632 {
633 return _timeSet;
634 }
635
636 friend class STM32LowPower;
637
638 private:
639 STM32RTC(void): _mode(MODE_BCD), _clockSource(LSI_CLOCK)
640 {
641 setClockSource(_clockSource);
642 }
643
644 static bool _timeSet;
645
646 Hour_Format _format;
647 Binary_Mode _mode;
648 AM_PM _hoursPeriod;
649 uint8_t _hours;
650 uint8_t _minutes;
651 uint8_t _seconds;
652 uint32_t _subSeconds;
653 uint8_t _year;
654 uint8_t _month;
655 uint8_t _day;
656 uint8_t _wday;
657
658 /* ALARM A */
659 uint8_t _alarmDay;
660 uint8_t _alarmHours;
661 uint8_t _alarmMinutes;
662 uint8_t _alarmSeconds;
663 uint32_t _alarmSubSeconds;
664 AM_PM _alarmPeriod;
665 Alarm_Match _alarmMatch;
666
667#if defined(ALARM_B_AVAILABLE)
668 /* ALARM B */
669 uint8_t _alarmBDay;
670 uint8_t _alarmBHours;
671 uint8_t _alarmBMinutes;
672 uint8_t _alarmBSeconds;
673 uint32_t _alarmBSubSeconds;
674 AM_PM _alarmBPeriod;
675 Alarm_Match _alarmBMatch;
676#endif
677
678 Source_Clock _clockSource;
679
680 void configForLowPower(Source_Clock source);
681
682 void syncTime(void);
683 void syncDate(void);
684 void syncAlarmTime(Alarm name = ALARM_A);
685
686};
687
688#endif // __STM32_RTC_H
void attachInterrupt(voidFuncPtrParam callback, void *data=nullptr, Alarm name=ALARM_A)
Attaches a callback and user data to an RTC alarm.
uint8_t getAlarmDay(Alarm name=ALARM_A)
Returns the alarm day of the month.
void setAlarmDay(uint8_t day, Alarm name=ALARM_A)
Sets the alarm day of the month.
void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds=0, AM_PM period=AM, Alarm name=ALARM_A)
Sets an alarm time.
void setAlarmDate(uint8_t day, uint8_t month, uint8_t year, Alarm name=ALARM_A)
Sets the alarm day; the month and year are ignored.
void detachInterrupt(Alarm name=ALARM_A)
Detaches the callback from an RTC alarm.
void getTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *subSeconds, AM_PM *period=nullptr)
Reads the current time.
uint32_t getSubSeconds(void)
Returns the current subsecond value in milliseconds.
void disableAlarm(Alarm name=ALARM_A)
Disables an RTC alarm.
bool isAlarmEnabled(Alarm name=ALARM_A)
Reports whether the selected alarm is enabled.
void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds=1000, AM_PM period=AM)
Sets the current time.
void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, Alarm name)
Sets an alarm time and subsecond value.
void setBinaryMode(Binary_Mode mode)
Selects BCD, binary, or mixed RTC operation.
Binary_Mode
Selects BCD, binary, or combined RTC mode.
Definition STM32RTC.h:101
@ MODE_BIN
Definition STM32RTC.h:103
@ MODE_BCD
Definition STM32RTC.h:102
@ MODE_MIX
Definition STM32RTC.h:104
Alarm
Identifies an RTC alarm.
Definition STM32RTC.h:133
@ ALARM_A
Definition STM32RTC.h:134
void setWeekDay(uint8_t weekDay)
Sets the current weekday.
uint8_t getDay(void)
Returns the current day of the month.
void setAlarmHours(uint8_t hours, AM_PM period=AM, Alarm name=ALARM_A)
Sets the alarm hours value and optional AM/PM period.
uint8_t getYear(void)
Returns the current year value.
void setAlarmSubSeconds(uint32_t subSeconds, Alarm name=ALARM_A)
Sets the alarm subsecond value in milliseconds.
void setAlarmEpoch(time_t ts, Alarm_Match match=MATCH_DHHMMSS, uint32_t subSeconds=0, Alarm name=ALARM_A)
Configures an alarm from an epoch timestamp.
void setAlarmSeconds(uint8_t seconds, Alarm name=ALARM_A)
Sets the alarm seconds value.
bool isFormat_24hour(void)
Reports whether the current hour format is 24-hour.
Definition STM32RTC.h:617
bool isConfigured(void)
Reports whether the RTC peripheral is configured.
Definition STM32RTC.h:609
uint8_t getWeekDay(void)
Returns the current weekday.
uint8_t getAlarmYear(void)
Returns zero; STM32 RTC alarms do not match a year.
void setClockSource(Source_Clock source, uint32_t predivA=(PREDIVA_MAX+1), uint32_t predivS=(PREDIVS_MAX+1))
Selects the RTC clock source and its prescalers.
uint8_t getSeconds(void)
Returns the current seconds value.
uint8_t getAlarmMonth(void)
Returns zero; STM32 RTC alarms do not match a month.
void operator=(STM32RTC const &)=delete
void setSubSeconds(uint32_t subSeconds)
Sets the current subsecond value in milliseconds.
uint32_t getAlarmSubSeconds(Alarm name=ALARM_A)
Returns the alarm subsecond value in milliseconds.
void setYear(uint8_t year)
Sets the current year value.
uint8_t getHours(AM_PM *period=nullptr)
Returns the current hours value.
void setAlarmYear(uint8_t year)
Compatibility method; the year is ignored by STM32 RTC alarms.
void setHours(uint8_t hours, AM_PM period=AM)
Sets the current hour value and optional AM/PM period.
RTC_HandleTypeDef * getHandle(void)
Returns the STM32 HAL RTC handle.
Definition STM32RTC.h:175
void setSeconds(uint8_t seconds)
Sets the current seconds value.
void setAlarmEpoch(time_t ts, Alarm_Match match, Alarm name)
Configures an alarm from an epoch timestamp.
uint8_t getMinutes(void)
Returns the current minutes value.
uint8_t getAlarmHours(AM_PM *period=nullptr, Alarm name=ALARM_A)
Returns the alarm hours value and optionally its AM/PM period.
time_t getEpoch(uint32_t *subSeconds=nullptr)
Returns the current Unix epoch time.
Source_Clock
Selects the input clock source for the RTC.
Definition STM32RTC.h:122
@ HSE_CLOCK
Definition STM32RTC.h:128
@ LSI_CLOCK
Definition STM32RTC.h:123
@ LSE_CLOCK
Definition STM32RTC.h:124
Source_Clock getClockSource(void)
Returns the selected RTC input clock source.
friend class STM32LowPower
Definition STM32RTC.h:636
void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, Alarm name)
Sets an alarm time without a subsecond value.
void setMonth(uint8_t month)
Sets the current month.
void setMinutes(uint8_t minutes)
Sets the current minutes value.
uint8_t getAlarmHours(Alarm name)
Returns the alarm hours value.
time_t getAlarmEpoch(Alarm name)
Returns the configured alarm's epoch timestamp.
void setDay(uint8_t day)
Sets the current day of the month.
void setAlarmHours(uint8_t hours, Alarm name)
Sets the alarm hours value.
void getDate(uint8_t *weekDay, uint8_t *day, uint8_t *month, uint8_t *year)
Reads the current date.
static STM32RTC & getInstance()
Returns the singleton RTC controller.
Definition STM32RTC.h:144
void setAlarmMinutes(uint8_t minutes, Alarm name=ALARM_A)
Sets the alarm minutes value.
void getPrediv(uint32_t *predivA, uint32_t *predivS)
Reads the configured or calculated RTC prescalers.
void begin(Hour_Format format=HOUR_24)
Initializes the RTC without resetting its current time.
void setEpoch(time_t ts, uint32_t subSeconds=0)
Sets the RTC from a Unix epoch timestamp.
AM_PM
Selects the AM or PM period in 12-hour format.
Definition STM32RTC.h:95
@ PM
Definition STM32RTC.h:97
@ AM
Definition STM32RTC.h:96
void begin(bool resetTime, Hour_Format format=HOUR_24)
Initializes the RTC and optionally resets its time.
void setPrediv(uint32_t predivA, uint32_t predivS)
Sets the RTC prescalers.
Alarm_Match
Selects which RTC fields must match for an alarm.
Definition STM32RTC.h:108
@ MATCH_DHHMMSS
Definition STM32RTC.h:114
@ MATCH_YYMMDDHHMMSS
Definition STM32RTC.h:118
@ MATCH_SS
Definition STM32RTC.h:111
@ MATCH_HHMMSS
Definition STM32RTC.h:113
@ MATCH_SUBSEC
Definition STM32RTC.h:110
@ MATCH_OFF
Definition STM32RTC.h:109
@ MATCH_MMDDHHMMSS
Definition STM32RTC.h:117
@ MATCH_MMSS
Definition STM32RTC.h:112
void setAlarmMonth(uint8_t month)
Compatibility method; the month is ignored by STM32 RTC alarms.
Binary_Mode getBinaryMode(void)
Returns the current RTC operating mode.
STM32RTC(STM32RTC const &)=delete
void end(void)
Deinitializes the RTC and clears registered callbacks.
void setDate(uint8_t day, uint8_t month, uint8_t year)
Sets the current date without changing the weekday.
uint8_t getAlarmMinutes(Alarm name=ALARM_A)
Returns the alarm minutes value.
uint8_t getAlarmSeconds(Alarm name=ALARM_A)
Returns the alarm seconds value.
void setY2kEpoch(time_t ts)
Sets the RTC from seconds since the Y2K epoch.
void setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year)
Sets the current weekday and date.
bool isTimeSet(void)
Reports whether the library has set the current time.
Definition STM32RTC.h:631
void attachInterrupt(voidFuncPtrParam callback, Alarm name)
Attaches a callback to an RTC alarm.
Hour_Format
Selects the RTC hour display format.
Definition STM32RTC.h:89
@ HOUR_24
Definition STM32RTC.h:91
@ HOUR_12
Definition STM32RTC.h:90
void standbyMode()
Compatibility method; use the STM32LowPower library for standby mode.
time_t getAlarmEpoch(uint32_t *subSeconds=nullptr, Alarm name=ALARM_A)
Returns the configured alarm's epoch timestamp.
void enableAlarm(Alarm_Match match, Alarm name=ALARM_A)
Enables an RTC alarm with the requested match fields.
uint8_t getMonth(void)
Returns the current month.
void setAlarmTime(uint32_t subSeconds, Alarm name=ALARM_A)
Sets an alarm using a subsecond value.
time_t getY2kEpoch(void)
Returns the current time as seconds since the Y2K epoch.