diff --git a/README.md b/README.md index 0905bc8..defe1f6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,53 @@ # STM32RTC A RTC library for STM32. + +# API + +This library is based on the Arduino RTCZero library. +The library allows to take control of the internal RTC of the STM32 boards. + +The following functions are not supported: + +* **`void standbyMode()`**: use the STM32 Low Power library instead. +* **`uint8_t getAlarmMonth()`**: month not supported by STM32 RTC architecture. +* **`uint8_t getAlarmYear()`**: year not supported by STM32 RTC architecture. +* **`void setAlarmMonth(uint8_t month)`**: month not supported by STM32 RTC architecture. +* **`void setAlarmYear(uint8_t year)`**: year not supported by STM32 RTC architecture. +* **`void setAlarmDate(uint8_t day, uint8_t month, uint8_t year)`**: month and year not supported by STM32 RTC architecture. + +The following functions have been added to support specific STM32 RTC features: + +_RTC hours mode (12 or 24)_ +* **`void begin(RTC_Hour_Format format)`** + +_RTC clock source_ +* **`void setClockSource(RTC_Source_Clock source)`** : this function must be called before `begin()`. + +_RTC Asynchronous and Synchronous prescaler_ +* **`void getPrediv(int8_t *predivA, int16_t *predivS)`** : get user (a)synchronous prescaler values if set else computed ones for the current clock source. +* **`void setPrediv(int8_t predivA, int16_t predivS)`** : set user (a)synchronous prescaler values. This function must be called before `begin()`. Use -1 to reset value and use computed ones. + +_SubSeconds management_ +* **`uint32_t getSubSeconds(void)`** +* **`void setSubSeconds(uint32_t subSeconds)`** + +_Hour format (AM or PM)_ +* **`uint8_t getHours(RTC_AM_PM *period)`** +* **`void setHours(uint8_t hours, RTC_AM_PM period)`** +* **`void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, RTC_AM_PM period)`** +* **`void setAlarmHours(uint8_t hours, RTC_AM_PM period)`** +* **`uint8_t getAlarmHours(RTC_AM_PM *period)`** +* **`void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, RTC_AM_PM period)`** + +_Week day configuration_ +* **`uint8_t getWeekDay(void)`** +* **`void setWeekDay(uint8_t weekDay)`** +* **`void setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year)`** + +Refer to the Arduino RTC documentation for the other functions +http://arduino.cc/en/Reference/RTC + +## Source + +Source files available at: +https://github.com/stm32duino/STM32RTC diff --git a/examples/Epoch/Epoch.ino b/examples/Epoch/Epoch.ino new file mode 100644 index 0000000..23890d6 --- /dev/null +++ b/examples/Epoch/Epoch.ino @@ -0,0 +1,84 @@ +/** + ****************************************************************************** + * @file Epoch.ino + * @author WI6LABS + * @version V1.0.0 + * @date 12-December-2017 + * @brief RTC epoch example + * + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2017 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#include + +/* Create a rtc object */ +STM32RTC rtc; + +void setup() { + Serial.begin(9600); + + rtc.begin(); // initialize RTC 24H format + + rtc.setEpoch(1451606400); // Jan 1, 2016 +} + +void loop() { + Serial.print("Unix time = "); + Serial.println(rtc.getEpoch()); + + Serial.print("Seconds since Jan 1 2000 = "); + Serial.println(rtc.getY2kEpoch()); + + // Print date... + Serial.print(rtc.getDay()); + Serial.print("/"); + Serial.print(rtc.getMonth()); + Serial.print("/"); + Serial.print(rtc.getYear()); + Serial.print("\t"); + + // ...and time + print2digits(rtc.getHours()); + Serial.print(":"); + print2digits(rtc.getMinutes()); + Serial.print(":"); + print2digits(rtc.getSeconds()); + + Serial.println(); + + delay(1000); +} + +void print2digits(int number) { + if (number < 10) { + Serial.print("0"); + } + Serial.print(number); +} diff --git a/examples/RTCClockSelection/RTCClockSelection.ino b/examples/RTCClockSelection/RTCClockSelection.ino new file mode 100644 index 0000000..57a007b --- /dev/null +++ b/examples/RTCClockSelection/RTCClockSelection.ino @@ -0,0 +1,112 @@ +/** + ****************************************************************************** + * @file RTCClockSelection.ino + * @author WI6LABS + * @version V1.0.0 + * @date 15-March-2018 + * @brief RTC clock selection: LSI, LSE or HSE. Refer to board datasheet to + * know available clock. + * + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2018 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#include + +/* Create a rtc object */ +STM32RTC rtc; + +/* Change these values to set the current initial time */ +const byte seconds = 0; +const byte minutes = 0; +const byte hours = 16; + +/* Change these values to set the current initial date */ +/* Monday 15th June 2015 */ +const byte weekDay = 1; +const byte day = 15; +const byte month = 6; +const byte year = 15; + +void setup() +{ + Serial.begin(9600); + + // Select RTC clock source: RTC_LSI_CLOCK, RTC_LSE_CLOCK or RTC_HSE_CLOCK. + // By default the LSI is selected as source. + rtc.setClockSource(STM32RTC::RTC_LSE_CLOCK); + + rtc.begin(); // initialize RTC 24H format + + // Set the time + rtc.setHours(hours); + rtc.setMinutes(minutes); + rtc.setSeconds(seconds); + + // Set the date + rtc.setWeekDay(weekDay); + rtc.setDay(day); + rtc.setMonth(month); + rtc.setYear(year); + + // you can use also + //rtc.setTime(hours, minutes, seconds); + //rtc.setDate(weekDay, day, month, year); +} + +void loop() +{ + // Print date... + print2digits(rtc.getDay()); + Serial.print("/"); + print2digits(rtc.getMonth()); + Serial.print("/"); + print2digits(rtc.getYear()); + Serial.print(" "); + + // ...and time + print2digits(rtc.getHours()); + Serial.print(":"); + print2digits(rtc.getMinutes()); + Serial.print(":"); + print2digits(rtc.getSeconds()); + + Serial.println(); + + delay(1000); +} + + + +void print2digits(int number) { + if (number < 10) { + Serial.print("0"); // print a 0 before if the number is < than 10 + } + Serial.print(number); +} diff --git a/examples/SimpleRTC/SimpleRTC.ino b/examples/SimpleRTC/SimpleRTC.ino new file mode 100644 index 0000000..823ffdf --- /dev/null +++ b/examples/SimpleRTC/SimpleRTC.ino @@ -0,0 +1,107 @@ +/** + ****************************************************************************** + * @file SimpleRTC.ino + * @author WI6LABS + * @version V1.0.0 + * @date 12-December-2017 + * @brief Simple RTC example. + * + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2017 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#include + +/* Create a rtc object */ +STM32RTC rtc; + +/* Change these values to set the current initial time */ +const byte seconds = 0; +const byte minutes = 0; +const byte hours = 16; + +/* Change these values to set the current initial date */ +/* Monday 15th June 2015 */ +const byte weekDay = 1; +const byte day = 15; +const byte month = 6; +const byte year = 15; + +void setup() +{ + Serial.begin(9600); + + rtc.begin(); // initialize RTC 24H format + + // Set the time + rtc.setHours(hours); + rtc.setMinutes(minutes); + rtc.setSeconds(seconds); + + // Set the date + rtc.setWeekDay(weekDay); + rtc.setDay(day); + rtc.setMonth(month); + rtc.setYear(year); + + // you can use also + //rtc.setTime(hours, minutes, seconds); + //rtc.setDate(weekDay, day, month, year); +} + +void loop() +{ + // Print date... + print2digits(rtc.getDay()); + Serial.print("/"); + print2digits(rtc.getMonth()); + Serial.print("/"); + print2digits(rtc.getYear()); + Serial.print(" "); + + // ...and time + print2digits(rtc.getHours()); + Serial.print(":"); + print2digits(rtc.getMinutes()); + Serial.print(":"); + print2digits(rtc.getSeconds()); + + Serial.println(); + + delay(1000); +} + + + +void print2digits(int number) { + if (number < 10) { + Serial.print("0"); // print a 0 before if the number is < than 10 + } + Serial.print(number); +} diff --git a/examples/simpleRTCAlarm/simpleRTCAlarm.ino b/examples/simpleRTCAlarm/simpleRTCAlarm.ino new file mode 100644 index 0000000..ba4d5b9 --- /dev/null +++ b/examples/simpleRTCAlarm/simpleRTCAlarm.ino @@ -0,0 +1,77 @@ +/** + ****************************************************************************** + * @file simpleRTCAlarm.ino + * @author WI6LABS + * @version V1.0.0 + * @date 12-December-2017 + * @brief Simple RTC with alarm example. + * + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2017 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#include + +/* Create a rtc object */ +STM32RTC rtc; + +/* Change these values to set the current initial time */ +const byte seconds = 0; +const byte minutes = 0; +const byte hours = 16; + +/* Change these values to set the current initial date */ +const byte day = 25; +const byte month = 9; +const byte year = 15; + +void setup() +{ + Serial.begin(9600); + + rtc.begin(); // initialize RTC 24H format + + rtc.setTime(hours, minutes, seconds); + rtc.setDate(day, month, year); + + rtc.attachInterrupt(alarmMatch); + rtc.setAlarmDay(day); + rtc.setAlarmTime(16, 0, 10); + rtc.enableAlarm(rtc.MATCH_DHHMMSS); +} + +void loop() +{ + +} + +void alarmMatch(void *data) +{ + Serial.println("Alarm Match!"); +} diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..63ad5b6 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,89 @@ +####################################### +# Syntax Coloring Map For RTC +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +STM32RTC KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +getWeekDay KEYWORD2 +getDay KEYWORD2 +getMonth KEYWORD2 +getYear KEYWORD2 +getHours KEYWORD2 +getMinutes KEYWORD2 +getSeconds KEYWORD2 +getSubSeconds KEYWORD2 + +setWeekDay KEYWORD2 +setDay KEYWORD2 +setMonth KEYWORD2 +setYear KEYWORD2 +setHours KEYWORD2 +setMinutes KEYWORD2 +setSeconds KEYWORD2 +setSubSeconds KEYWORD2 +setDate KEYWORD2 +setTime KEYWORD2 + +getEpoch KEYWORD2 +getY2kEpoch KEYWORD2 +setEpoch KEYWORD2 +setY2kEpoch KEYWORD2 +setAlarmEpoch KEYWORD2 + +getAlarmDay KEYWORD2 +getAlarmHours KEYWORD2 +getAlarmMinutes KEYWORD2 +getAlarmSeconds KEYWORD2 +getAlarmSubSeconds KEYWORD2 + +setAlarmSeconds KEYWORD2 +setAlarmMinutes KEYWORD2 +setAlarmHours KEYWORD2 +setAlarmHours KEYWORD2 +setAlarmTime KEYWORD2 +setAlarmTime KEYWORD2 +setAlarmDay KEYWORD2 +setAlarmMonth KEYWORD2 +setAlarmYear KEYWORD2 +setAlarmDate KEYWORD2 + +enableAlarm KEYWORD2 +disableAlarm KEYWORD2 + +attachInterrupt KEYWORD2 +detachInterrupt KEYWORD2 + +setClockSource KEYWORD2 +isConfigured KEYWORD2 + +getPrediv KEYWORD2 +setPrediv KEYWORD2 + +IS_CLOCK_SOURCE KEYWORD2 +IS_HOUR_FORMAT KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### +MATCH_OFF LITERAL1 +MATCH_SS LITERAL1 +MATCH_MMSS LITERAL1 +MATCH_HHMMSS LITERAL1 +MATCH_DHHMMSS LITERAL1 +MATCH_MMDDHHMMSS LITERAL1 +MATCH_YYMMDDHHMMSS LITERAL1 +RTC_HOUR_12 LITERAL1 +RTC_HOUR_24 LITERAL1 +RTC_AM LITERAL1 +RTC_PM LITERAL1 +RTC_LSE_CLOCK LITERAL1 +RTC_LSI_CLOCK LITERAL1 +RTC_HSE_CLOCK LITERAL1 diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..346866d --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=STM32duino RTC +version=1.0.0 +author=STMicroelectronics, Wi6Labs +maintainer=stm32duino +sentence=Allows to use the RTC functionalities of STM32 based boards. +paragraph=With this library you can use the RTC peripheral in order to program actions related to date and time. +category=Timing +url=https://github.com/stm32duino/STM32RTC.git +architectures=stm32 diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp new file mode 100644 index 0000000..c6f28df --- /dev/null +++ b/src/STM32RTC.cpp @@ -0,0 +1,887 @@ +/** + ****************************************************************************** + * @file STM32RTC.cpp + * @author WI6LABS + * @version V1.0.0 + * @date 12-December-2017 + * @brief Provides a RTC interface for Arduino + * + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2017 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#include + +#include "STM32RTC.h" + +#define EPOCH_TIME_OFF 946684800 // This is 1st January 2000, 00:00:00 in epoch time +#define EPOCH_TIME_YEAR_OFF 100 // years since 1900 + +// Initialize static variable +bool STM32RTC::_configured = false; + +STM32RTC::STM32RTC(void): _clockSource(RTC_LSI_CLOCK) +{ + +} + +/** + * @brief initializes the RTC + * @param resetTime: if true reconfigures the RTC + * @param format: hour format: RTC_HOUR_12 or RTC_HOUR_24(default) + * @retval None + */ +void STM32RTC::begin(bool resetTime, RTC_Hour_Format format) +{ + if(resetTime == true) { + _configured = false; + } + begin(format); +} + +/** + * @brief initializes the RTC + * @param format: hour format: RTC_HOUR_12 or RTC_HOUR_24(default) + * @retval None + */ +void STM32RTC::begin(RTC_Hour_Format format) +{ + if(_configured == false) { + RTC_init((format == RTC_HOUR_12)? HOUR_FORMAT_12: HOUR_FORMAT_24, + (_clockSource == RTC_LSE_CLOCK)? LSE_CLOCK: + (_clockSource == RTC_HSE_CLOCK)? HSE_CLOCK : LSI_CLOCK); + // Must be set before call of sync methods + _configured = true; + syncTime(); + syncDate(); + // Use current time to init alarm members + _alarmDay = _day; + _alarmHours = _hours; + _alarmMinutes = _minutes; + _alarmSeconds = _seconds; + _alarmSubSeconds = _subSeconds; + _alarmPeriod = _hoursPeriod; + } else { + syncTime(); + syncDate(); + syncAlarmTime(); + } +} + +/** + * @brief Deinitialize and stop the RTC + * @param None + * @retval None + */ +void STM32RTC::end(void) +{ + if(_configured == true) { + RTC_DeInit(); + _configured = false; + } +} + +/** + * @brief set the RTC clock source. By default LSI clock is selected. This + * method must be called before begin(). + * @param source: clock source: RTC_LSI_CLOCK, RTC_LSE_CLOCK or RTC_HSE_CLOCK + * @retval None + */ +void STM32RTC::setClockSource(RTC_Source_Clock source) +{ + if(IS_CLOCK_SOURCE(source)) { + _clockSource = source; + RTC_SetClockSource((_clockSource == RTC_LSE_CLOCK)? LSE_CLOCK: + (_clockSource == RTC_HSE_CLOCK)? HSE_CLOCK : LSI_CLOCK); + } +} + +/** + * @brief get user (a)synchronous prescaler values if set else computed + * ones for the current clock source. + * @param predivA: pointer to the current Asynchronous prescaler value + * @param predivS: pointer to the current Synchronous prescaler value + * @retval None + */ +void STM32RTC::getPrediv(int8_t *predivA, int16_t *predivS) +{ + if((predivA != NULL) && (predivS != NULL)) { + RTC_getPrediv(predivA, predivS); + } +} + +/** + * @brief set user (a)synchronous prescalers value. + * @note This method must be called before begin(). + * @param predivA: Asynchronous prescaler value. Reset value: -1 + * @param predivS: Synchronous prescaler value. Reset value: -1 + * @retval None + */ +void STM32RTC::setPrediv(int8_t predivA, int16_t predivS) +{ + RTC_setPrediv(predivA, predivS); +} + +/** + * @brief enable the RTC alarm. + * @param match: Alarm_Match configuration + * @retval None + */ +void STM32RTC::enableAlarm(Alarm_Match match) +{ + if(_configured) { + _alarmMatch = match; + switch (match) { + case MATCH_OFF: + RTC_StopAlarm(); + break; + case MATCH_YYMMDDHHMMSS://kept for compatibility + case MATCH_MMDDHHMMSS: //kept for compatibility + case MATCH_DHHMMSS: + case MATCH_HHMMSS: + case MATCH_MMSS: + case MATCH_SS: + RTC_StartAlarm(_alarmDay, _alarmHours, _alarmMinutes, _alarmSeconds, + _alarmSubSeconds, (_alarmPeriod == RTC_AM)? AM: PM, + static_cast(_alarmMatch)); + break; + default: + break; + } + } +} + +/** + * @brief disable the RTC alarm. + * @retval None + */ +void STM32RTC::disableAlarm(void) +{ + if(_configured) { + RTC_StopAlarm(); + } +} + +/** + * @brief attach a callback to the RTC alarm interrupt. + * @param callback: pointer to the callback + * @retval None + */ +void STM32RTC::attachInterrupt(voidFuncPtr callback, void *data) +{ + attachAlarmCallback(callback, data); +} + +/** + * @brief detach the RTC alarm callback. + * @retval None + */ +void STM32RTC::detachInterrupt(void) +{ + detachAlarmCallback(); +} + +// Kept for compatibility. Use STM32LowPower library. +void STM32RTC::standbyMode(void) +{ + +} + +/* + * Get Functions + */ + +/** + * @brief get RTC subseconds. + * @retval return the current subseconds from the RTC. + */ +uint32_t STM32RTC::getSubSeconds(void) +{ + syncTime(); + return _subSeconds; +} + +/** + * @brief get RTC seconds. + * @retval return the current seconds from the RTC. + */ +uint8_t STM32RTC::getSeconds(void) +{ + syncTime(); + return _seconds; +} + +/** + * @brief get RTC minutes. + * @retval return the current minutes from the RTC. + */ +uint8_t STM32RTC::getMinutes(void) +{ + syncTime(); + return _minutes; +} + +/** + * @brief get RTC hours. + * @param format: optional (default: NULL) + * pointer to the current hour format set in the RTC: AM or PM + * @retval return the current hours from the RTC. + */ +uint8_t STM32RTC::getHours(RTC_AM_PM *period) +{ + syncTime(); + if(period != NULL) { + *period = _hoursPeriod; + } + return _hours; +} + +/** + * @brief get RTC week day. + * @retval return the current week day from the RTC. + */ +uint8_t STM32RTC::getWeekDay(void) +{ + syncDate(); + return _wday; +} + +/** + * @brief get RTC day. + * @retval return the current day from the RTC. + */ +uint8_t STM32RTC::getDay(void) +{ + syncDate(); + return _day; +} + +/** + * @brief get RTC month. + * @retval return the current month from the RTC. + */ +uint8_t STM32RTC::getMonth(void) +{ + syncDate(); + return _month; +} + +/** + * @brief get RTC year. + * @retval return the current year from the RTC. + */ +uint8_t STM32RTC::getYear(void) +{ + syncDate(); + return _year; +} + +/** + * @brief get RTC alarm subsecond. + * @retval return the current alarm subsecond. + */ +uint32_t STM32RTC::getAlarmSubSeconds(void) +{ + syncAlarmTime(); + return _alarmSubSeconds; +} + +/** + * @brief get RTC alarm second. + * @retval return the current alarm second. + */ +uint8_t STM32RTC::getAlarmSeconds(void) +{ + syncAlarmTime(); + return _alarmSeconds; +} + +/** + * @brief get RTC alarm minute. + * @retval return the current alarm minute. + */ +uint8_t STM32RTC::getAlarmMinutes(void) +{ + syncAlarmTime(); + return _alarmMinutes; +} + +/** + * @brief get RTC alarm hour. + * @param format: optional (default: NULL) + * pointer to the current hour format set in the RTC: AM or PM + * @retval return the current alarm hour. + */ +uint8_t STM32RTC::getAlarmHours(RTC_AM_PM *period) +{ + syncAlarmTime(); + if(period != NULL) { + *period = _alarmPeriod; + } + return _alarmHours; +} + +/** + * @brief get RTC alarm day. + * @retval return the current alarm day. + */ +uint8_t STM32RTC::getAlarmDay(void) +{ + syncAlarmTime(); + return _alarmDay; +} + +/** + * @brief get RTC alarm month. + * @NOTE This function is kept for compatibility but the STM32 RTC + * can't assign a month to an alarm. See board datasheet. + * @retval always returns 0 + */ +uint8_t STM32RTC::getAlarmMonth(void) +{ + return 0; +} + +/** + * @brief get RTC alarm year. + * @NOTE This function is kept for compatibility but the STM32 RTC + * can't assign a year to an alarm. See board datasheet. + * @retval always returns 0 + */ +uint8_t STM32RTC::getAlarmYear(void) +{ + return 0; +} + +/* + * Set Functions + */ + +/** + * @brief set RTC subseconds. + * @param subseconds: 0-999 + * @retval none + */ +void STM32RTC::setSubSeconds(uint32_t subSeconds) +{ + if (_configured) { + syncTime(); + if(subSeconds < 1000) { + _subSeconds = subSeconds; + } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); + } +} + +/** + * @brief set RTC seconds. + * @param seconds: 0-59 + * @retval none + */ +void STM32RTC::setSeconds(uint8_t seconds) +{ + if (_configured) { + syncTime(); + if(seconds < 60) { + _seconds = seconds; + } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); + } +} + +/** + * @brief set RTC minutes. + * @param minutes: 0-59 + * @retval none + */ +void STM32RTC::setMinutes(uint8_t minutes) +{ + if (_configured) { + syncTime(); + if(minutes < 60) { + _minutes = minutes; + } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); + } +} + +/** + * @brief set RTC hours. + * @param hours: 0-23 + * @retval none + */ +void STM32RTC::setHours(uint8_t hours) +{ + if (_configured) { + syncTime(); + if(hours < 24) { + _hours = hours; + } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); + } +} + +/** + * @brief set RTC hours. + * @param hours: 0-23 or 0-12 + * @param hours format: AM or PM + * @retval none + */ +void STM32RTC::setHours(uint8_t hours, RTC_AM_PM period) +{ + if (_configured) { + syncTime(); + if(hours < 24) { + _hours = hours; + } + _hoursPeriod = period; + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); + } +} + +/** + * @brief set RTC time. + * @param hours: 0-23 + * @param minutes: 0-59 + * @param seconds: 0-59 + * @retval none + */ +void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds) +{ + if (_configured) { + setSeconds(seconds); + setMinutes(minutes); + setHours(hours); + } +} + +/** + * @brief set RTC time. + * @param hours: 0-23 or 0-12 + * @param minutes: 0-59 + * @param seconds: 0-59 + * @param hour format: AM or PM + * @retval none + */ +void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, RTC_AM_PM period) +{ + if (_configured) { + setSubSeconds(subSeconds); + setSeconds(seconds); + setMinutes(minutes); + setHours(hours, period); + } +} + +/** + * @brief set RTC week day. + * @param week day: 1-7 (Monday first) + * @retval none + */ +void STM32RTC::setWeekDay(uint8_t weekDay) +{ + if (_configured) { + RTC_GetDate(&_year, &_month, &_day, &_wday); + if((weekDay >= 1) && (weekDay <= 7)) { + _wday = weekDay; + } + RTC_SetDate(_year, _month, _day, _wday); + } +} + +/** + * @brief set RTC day. + * @param day: 1-31 + * @retval none + */ +void STM32RTC::setDay(uint8_t day) +{ + if (_configured) { + RTC_GetDate(&_year, &_month, &_day, &_wday); + if((day >= 1) && (day <= 31)) { + _day = day; + } + RTC_SetDate(_year, _month, _day, _wday); + } +} + +/** + * @brief set RTC month. + * @param month: 1-12 + * @retval none + */ +void STM32RTC::setMonth(uint8_t month) +{ + if (_configured) { + RTC_GetDate(&_year, &_month, &_day, &_wday); + if((month >= 1) && (month <= 12)) { + _month = month; + } + RTC_SetDate(_year, _month, _day, _wday); + } +} + +/** + * @brief set RTC year. + * @param year: 0-99 + * @retval none + */ +void STM32RTC::setYear(uint8_t year) +{ + if (_configured) { + RTC_GetDate(&_year, &_month, &_day, &_wday); + if(year < 100) { + _year = year; + } + RTC_SetDate(_year, _month, _day, _wday); + } +} + +/** + * @brief set RTC calendar. + * @param day: 1-31 + * @param month: 1-12 + * @param year: 0-99 + * @retval none + */ +void STM32RTC::setDate(uint8_t day, uint8_t month, uint8_t year) +{ + if (_configured) { + setDay(day); + setMonth(month); + setYear(year); + } +} + +/** + * @brief set RTC calendar. + * @param weekDay: 1-7 (Monday first) + * @param day: 1-31 + * @param month: 1-12 + * @param year: 0-99 + * @retval none + */ +void STM32RTC::setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year) +{ + if (_configured) { + setWeekDay(weekDay); + setDay(day); + setMonth(month); + setYear(year); + } +} + +/** + * @brief set RTC alarm second. + * @param seconds: 0-59 + * @retval none + */ +void STM32RTC::setAlarmSeconds(uint8_t seconds) +{ + if (_configured) { + if(seconds < 60) { + _alarmSeconds = seconds; + } + } +} + +/** + * @brief set RTC alarm minute. + * @param minutes: 0-59 + * @retval none + */ +void STM32RTC::setAlarmMinutes(uint8_t minutes) +{ + if (_configured) { + if(minutes < 60) { + _alarmMinutes = minutes; + } + } +} + +/** + * @brief set RTC alarm hour. + * @param hour: 0-23 + * @retval none + */ +void STM32RTC::setAlarmHours(uint8_t hours) +{ + if (_configured) { + if(hours < 24) { + _alarmHours = hours; + } + } +} + +/** + * @brief set RTC alarm hour. + * @param hour: 0-23 or 0-12 + * @param hour format: AM or PM + * @retval none + */ +void STM32RTC::setAlarmHours(uint8_t hours, RTC_AM_PM period) +{ + if (_configured) { + if(hours < 24) { + _alarmHours = hours; + } + _alarmPeriod = period; + } +} + +/** + * @brief set RTC alarm time. + * @param hours: 0-23 + * @param minutes: 0-59 + * @param seconds: 0-59 + * @retval none + */ +void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds) +{ + if (_configured) { + setAlarmHours(hours); + setAlarmMinutes(minutes); + setAlarmSeconds(seconds); + } +} + +/** + * @brief set RTC alarm time. + * @param hours: 0-23 + * @param minutes: 0-59 + * @param seconds: 0-59 + * @param hour format: AM or PM + * @retval none + */ +void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, RTC_AM_PM period) +{ + if (_configured) { + setAlarmHours(hours, period); + setAlarmMinutes(minutes); + setAlarmSeconds(seconds); + } +} + +/** + * @brief set RTC alarm day. + * @param day: 1-31 + * @retval none + */ +void STM32RTC::setAlarmDay(uint8_t day) +{ + if (_configured) { + if((day >= 1) && (day <= 31)) { + _alarmDay = day; + } + } +} + +/** + * @brief set RTC alarm month. + * @NOTE This function is kept for compatibility but the STM32 RTC + * can't assign a month to an alarm. See board datasheet. + * @param month is ignored. + */ +void STM32RTC::setAlarmMonth(uint8_t month) +{ + UNUSED(month); +} + +/** + * @brief set RTC alarm year. + * @NOTE This function is kept for compatibility but the STM32 RTC + * can't assign a year to an alarm. See board datasheet. + * @param year is ignored. + */ +void STM32RTC::setAlarmYear(uint8_t year) +{ + UNUSED(year); +} + +/** + * @brief set RTC alarm date. + * @NOTE Parameters month and year are ingored because the STM32 RTC can't + * assign a month or year to an alarm. See board datasheet. + * @param day: 1-31 + * @param month is ignored + * @param year is ignored + */ +void STM32RTC::setAlarmDate(uint8_t day, uint8_t month, uint8_t year) +{ + UNUSED(month); + UNUSED(year); + + setAlarmDay(day); +} + +/** + * @brief get epoch time + * @retval epoch time in seconds + */ +uint32_t STM32RTC::getEpoch(void) +{ + struct tm tm; + + syncDate(); + syncTime(); + + tm.tm_isdst = -1; + tm.tm_yday = 0; + tm.tm_wday = _wday - 1; + tm.tm_year = _year + EPOCH_TIME_YEAR_OFF; + tm.tm_mon = _month - 1; + tm.tm_mday = _day; + tm.tm_hour = _hours; + tm.tm_min = _minutes; + tm.tm_sec = _seconds; + + return mktime(&tm); +} + +/** + * @brief get epoch time since 1st January 2000, 00:00:00 + * @retval epoch time in seconds + */ +uint32_t STM32RTC::getY2kEpoch(void) +{ + return (getEpoch() - EPOCH_TIME_OFF); +} + +/** + * @brief set RTC alarm from epoch time + * @param epoch time in seconds + */ +void STM32RTC::setAlarmEpoch(uint32_t ts, Alarm_Match match) +{ + if (_configured) { + if (ts < EPOCH_TIME_OFF) { + ts = EPOCH_TIME_OFF; + } + + time_t t = ts; + struct tm* tmp = gmtime(&t); + + setAlarmDay(tmp->tm_mday); + setAlarmHours(tmp->tm_hour); + setAlarmMinutes(tmp->tm_min); + setAlarmSeconds(tmp->tm_sec); + enableAlarm(match); + } +} + +/** + * @brief set RTC time from epoch time + * @param epoch time in seconds + */ +void STM32RTC::setEpoch(uint32_t ts) +{ + if (_configured) { + if (ts < EPOCH_TIME_OFF) { + ts = EPOCH_TIME_OFF; + } + + time_t t = ts; + struct tm* tmp = gmtime(&t); + + _year = tmp->tm_year - EPOCH_TIME_YEAR_OFF; + _month = tmp->tm_mon + 1; + _day = tmp->tm_mday; + _wday = tmp->tm_wday + 1; + _hours = tmp->tm_hour; + _minutes = tmp->tm_min; + _seconds = tmp->tm_sec; + + RTC_SetDate(_year, _month, _day, _wday); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); + } +} + +/** + * @brief set RTC time from epoch time since 1st January 2000, 00:00:00 + * @param epoch time in seconds + */ +void STM32RTC::setY2kEpoch(uint32_t ts) +{ + if (_configured) { + setEpoch(ts + EPOCH_TIME_OFF); + } +} + +/** + * @brief synchronise the time from the current RTC one + * @param none + */ +void STM32RTC::syncTime(void) +{ + if(_configured) { + hourAM_PM_t p = AM; + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; + } +} + +/** + * @brief synchronise the time from the current RTC one + * @param none + */ +void STM32RTC::syncDate(void) +{ + if(_configured) { + RTC_GetDate(&_year, &_month, &_day, &_wday); + } +} + +/** + * @brief synchronise the alarm time from the current RTC one + * @param none + */ +void STM32RTC::syncAlarmTime(void) +{ + if(_configured) { + hourAM_PM_t p = AM; + uint8_t match; + RTC_GetAlarm(&_alarmDay, &_alarmHours, &_alarmMinutes, &_alarmSeconds, + &_alarmSubSeconds, &p, &match); + _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; + switch (static_cast(match)) { + case MATCH_OFF: + case MATCH_YYMMDDHHMMSS://kept for compatibility + case MATCH_MMDDHHMMSS: //kept for compatibility + case MATCH_DHHMMSS: + case MATCH_HHMMSS: + case MATCH_MMSS: + case MATCH_SS: + _alarmMatch = static_cast(match); + break; + default: + _alarmMatch = MATCH_OFF; + break; + } + } +} diff --git a/src/STM32RTC.h b/src/STM32RTC.h new file mode 100644 index 0000000..929b937 --- /dev/null +++ b/src/STM32RTC.h @@ -0,0 +1,204 @@ +/** + ****************************************************************************** + * @file STM32RTC.h + * @author WI6LABS + * @version V1.0.0 + * @date 12-December-2017 + * @brief Provides a RTC interface for Arduino + * + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2017 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#ifndef __STM32_RTC_H +#define __STM32_RTC_H + +#include "Arduino.h" + +// Check if RTC HAL enable in variants/board_name/stm32yzxx_hal_conf.h +#ifndef HAL_RTC_MODULE_ENABLED +#error "RTC configuration is missing. Check flag HAL_RTC_MODULE_ENABLED in variants/board_name/stm32yzxx_hal_conf.h" +#endif + +typedef void(*voidFuncPtr)(void *); + +#define IS_CLOCK_SOURCE(SRC) (((SRC) == STM32RTC::RTC_LSI_CLOCK) || ((SRC) == STM32RTC::RTC_LSE_CLOCK) ||\ + ((SRC) == STM32RTC::RTC_HSE_CLOCK)) +#define IS_HOUR_FORMAT(FMT) (((FMT) == STM32RTC::RTC_HOUR_12) || ((FMT) == STM32RTC::RTC_HOUR_24)) + +class STM32RTC { +public: + + enum RTC_Hour_Format : uint8_t + { + RTC_HOUR_12 = HOUR_FORMAT_12, + RTC_HOUR_24 = HOUR_FORMAT_24 + }; + + enum RTC_AM_PM : uint8_t + { + RTC_AM = AM, + RTC_PM = PM + }; + + enum Alarm_Match: uint8_t + { + MATCH_OFF = OFF_MSK, // Never + MATCH_SS = SS_MSK, // Every Minute + MATCH_MMSS = SS_MSK | MM_MSK, // Every Hour + MATCH_HHMMSS = SS_MSK | MM_MSK | HH_MSK, // Every Day + MATCH_DHHMMSS = SS_MSK | MM_MSK | HH_MSK | D_MSK, // Every Month + /* NOTE: STM32 RTC can't assign a month or a year to an alarm. Those enum + are kept for compatibility but are ignored inside enableAlarm(). */ + MATCH_MMDDHHMMSS = SS_MSK | MM_MSK | HH_MSK | D_MSK | M_MSK, + MATCH_YYMMDDHHMMSS = SS_MSK | MM_MSK | HH_MSK | D_MSK | M_MSK | Y_MSK }; + + enum RTC_Source_Clock: uint8_t + { + RTC_LSI_CLOCK = LSI_CLOCK, + RTC_LSE_CLOCK = LSE_CLOCK, + RTC_HSE_CLOCK = HSE_CLOCK + }; + + STM32RTC(); + + void begin(bool resetTime, RTC_Hour_Format format = RTC_HOUR_24); + void begin(RTC_Hour_Format format = RTC_HOUR_24); + + void end(void); + + void setClockSource(RTC_Source_Clock source); + + void enableAlarm(Alarm_Match match); + void disableAlarm(void); + + void attachInterrupt(voidFuncPtr callback, void *data = NULL); + void detachInterrupt(void); + + // Kept for compatibility: use STM32LowPower library. + void standbyMode(); + + /* Get Functions */ + + uint32_t getSubSeconds(void); + uint8_t getSeconds(void); + uint8_t getMinutes(void); + uint8_t getHours(RTC_AM_PM *period = NULL); + + uint8_t getWeekDay(void); + uint8_t getDay(void); + uint8_t getMonth(void); + uint8_t getYear(void); + + uint32_t getAlarmSubSeconds(void); + uint8_t getAlarmSeconds(void); + uint8_t getAlarmMinutes(void); + uint8_t getAlarmHours(RTC_AM_PM *period = NULL); + + uint8_t getAlarmDay(void); + + // Kept for compatibility with Arduino RTCZero library. + uint8_t getAlarmMonth(); + uint8_t getAlarmYear(); + + /* Set Functions */ + + void setSubSeconds(uint32_t subSeconds); + void setSeconds(uint8_t seconds); + void setMinutes(uint8_t minutes); + void setHours(uint8_t hours); + void setHours(uint8_t hours, RTC_AM_PM period); + void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds); + void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, RTC_AM_PM period); + + void setWeekDay(uint8_t weekDay); + void setDay(uint8_t day); + void setMonth(uint8_t month); + void setYear(uint8_t year); + void setDate(uint8_t day, uint8_t month, uint8_t year); + void setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year); + + void setAlarmSeconds(uint8_t seconds); + void setAlarmMinutes(uint8_t minutes); + void setAlarmHours(uint8_t hours); + void setAlarmHours(uint8_t hours, RTC_AM_PM period); + void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds); + void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, RTC_AM_PM period); + + void setAlarmDay(uint8_t day); + + // Kept for compatibility with Arduino RTCZero library. + void setAlarmMonth(uint8_t month); + void setAlarmYear(uint8_t year); + void setAlarmDate(uint8_t day, uint8_t month, uint8_t year); + + /* Epoch Functions */ + + uint32_t getEpoch(void); + uint32_t getY2kEpoch(void); + void setEpoch(uint32_t ts); + void setY2kEpoch(uint32_t ts); + void setAlarmEpoch(uint32_t ts, Alarm_Match match = MATCH_DHHMMSS); + + void getPrediv(int8_t *predivA, int16_t *predivS); + void setPrediv(int8_t predivA, int16_t predivS); + + bool isConfigured(void) { + return _configured; + } + +private: + static bool _configured; + + RTC_AM_PM _hoursPeriod; + uint8_t _hours; + uint8_t _minutes; + uint8_t _seconds; + uint32_t _subSeconds; + uint8_t _year; + uint8_t _month; + uint8_t _day; + uint8_t _wday; + + uint8_t _alarmDay; + uint8_t _alarmHours; + uint8_t _alarmMinutes; + uint8_t _alarmSeconds; + uint32_t _alarmSubSeconds; + RTC_AM_PM _alarmPeriod; + Alarm_Match _alarmMatch; + + RTC_Source_Clock _clockSource; + + void syncTime(void); + void syncDate(void); + void syncAlarmTime(void); +}; + +#endif // __STM32_RTC_H