From 7d38f818878cbae676f2545710d5ee88512cbe44 Mon Sep 17 00:00:00 2001 From: fpr Date: Thu, 26 Oct 2017 16:59:51 +0200 Subject: [PATCH 01/16] Add library properties Signed-off-by: fpr --- library.properties | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 library.properties diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..9c7fd60 --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=STM32duino RTC +version=1.0.0 +author=Wi6Labs +maintainer=stm32duino +sentence=Allows to use the RTC functionalities. For STM32 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 From 0da46b5de71ae8b1402b41681c9d063b71a6bc69 Mon Sep 17 00:00:00 2001 From: fpr Date: Thu, 26 Oct 2017 17:07:10 +0200 Subject: [PATCH 02/16] Add syntax coloring map Signed-off-by: fpr --- keywords.txt | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 keywords.txt diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..68a8368 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,75 @@ +####################################### +# 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 + +####################################### +# Constants (LITERAL1) +####################################### +HOUR_12 LITERAL1 +HOUR_24 LITERAL1 +HOUR_AM LITERAL1 +HOUR_PM LITERAL1 +RTC_LSE_CLOCK LITERAL1 +RTC_LSI_CLOCK LITERAL1 +RTC_HSE_CLOCK LITERAL1 From 42236438d91c76dd7f5d058bfb31d129c8683c5c Mon Sep 17 00:00:00 2001 From: fpr Date: Thu, 26 Oct 2017 17:10:38 +0200 Subject: [PATCH 03/16] Add source files Signed-off-by: fpr --- src/STM32RTC.cpp | 855 +++++++++++++++++++++++++++++++++++++++++++++++ src/STM32RTC.h | 197 +++++++++++ 2 files changed, 1052 insertions(+) create mode 100644 src/STM32RTC.cpp create mode 100644 src/STM32RTC.h diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp new file mode 100644 index 0000000..abb5f8c --- /dev/null +++ b/src/STM32RTC.cpp @@ -0,0 +1,855 @@ +/** + ****************************************************************************** + * @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: HOUR_12 or HOUR_24(default) + * @retval None + */ +void STM32RTC::begin(bool resetTime, RTCHourFormats_t format) +{ + if(resetTime == true) { + _configured = false; + } + begin(format); +} + +/** + * @brief initializes the RTC + * @param format: hour format: HOUR_12 or HOUR_24(default) + * @retval None + */ +void STM32RTC::begin(RTCHourFormats_t format) +{ + if(_configured == false) { + RTC_init((hourFormat_t)format, (sourceClock_t)_clockSource); + } + + _hoursFormat = HOUR12_AM; + _hours = 0; + _minutes = 0; + _seconds = 0; + _subSeconds = 0; + _year = 0; + _month = 0; + _date = 0; + _day = 0; + _alarmDate = 0; + _alarmHours = 0; + _alarmMinutes = 0; + _alarmSeconds = 0; + _alarmSubSeconds = 0; + _alarmFormat = HOUR12_AM; + + _configured = true; +} + +/** + * @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_sourceClock_t source) +{ + if(IS_CLOCK_SOURCE(source)) { + _clockSource = source; + } +} + +/** + * @brief enable the RTC alarm. + * @param match: Alarm_Match configuration + * @retval None + */ +void STM32RTC::enableAlarm(Alarm_Match match) +{ + Hour12_AM_PM_t format; + uint8_t date, hours, minutes, seconds; + uint32_t subSeconds; + + if(_configured) { + RTC_GetTime(&hours, &minutes, &seconds, &subSeconds, (hourAM_PM_t*)&format); + date = getDay(); + + switch (match) { + case MATCH_OFF: + RTC_StopAlarm(); + break; + case MATCH_YYMMDDHHMMSS://kept for compatibility + case MATCH_MMDDHHMMSS: //kept for compatibility + case MATCH_DHHMMSS: + date = _alarmDate; + case MATCH_HHMMSS: + hours = _alarmHours; + format = _alarmFormat; + case MATCH_MMSS: + minutes = _alarmMinutes; + case MATCH_SS: + seconds = _alarmSeconds; + RTC_StartAlarm(date, hours, minutes, seconds, subSeconds, (hourAM_PM_t)format); + 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) +{ + if(_configured) { + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + } + return _subSeconds; +} + +/** + * @brief get RTC seconds. + * @retval return the current seconds from the RTC. + */ +uint8_t STM32RTC::getSeconds(void) +{ + if(_configured) { + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + } + return _seconds; +} + +/** + * @brief get RTC minutes. + * @retval return the current minutes from the RTC. + */ +uint8_t STM32RTC::getMinutes(void) +{ + if(_configured) { + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + } + return _minutes; +} + +/** + * @brief get RTC hours. + * @retval return the current hours from the RTC. + */ +uint8_t STM32RTC::getHours(void) +{ + if(_configured) { + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + } + return _hours; +} + +/** + * @brief get RTC hours. + * @param format: 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(Hour12_AM_PM_t *format) +{ + if(_configured) { + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + + if(format != NULL) { + *format = _hoursFormat; + } + } + return _hours; +} + +/** + * @brief get RTC week day. + * @retval return the current week day from the RTC. + */ +uint8_t STM32RTC::getWeekDay(void) +{ + if(_configured) { + RTC_GetDate(&_year, &_month, &_date, &_day); + } + return _day; +} + +/** + * @brief get RTC date. + * @retval return the current date from the RTC. + */ +uint8_t STM32RTC::getDay(void) +{ + if(_configured) { + RTC_GetDate(&_year, &_month, &_date, &_day); + } + return _date; +} + +/** + * @brief get RTC month. + * @retval return the current month from the RTC. + */ +uint8_t STM32RTC::getMonth(void) +{ + if(_configured) { + RTC_GetDate(&_year, &_month, &_date, &_day); + } + return _month; +} + +/** + * @brief get RTC year. + * @retval return the current year from the RTC. + */ +uint8_t STM32RTC::getYear(void) +{ + if(_configured) { + RTC_GetDate(&_year, &_month, &_date, &_day); + } + return _year; +} + +/** + * @brief get RTC alarm subsecond. + * @retval return the current alarm subsecond. + */ +uint32_t STM32RTC::getAlarmSubSeconds(void) +{ + if(_configured) { + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, (hourAM_PM_t*)&_alarmFormat); + } + return _alarmSubSeconds; +} + +/** + * @brief get RTC alarm second. + * @retval return the current alarm second. + */ +uint8_t STM32RTC::getAlarmSeconds(void) +{ + if(_configured) { + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, (hourAM_PM_t*)&_alarmFormat); + } + return _alarmSeconds; +} + +/** + * @brief get RTC alarm minute. + * @retval return the current alarm minute. + */ +uint8_t STM32RTC::getAlarmMinutes(void) +{ + if(_configured) { + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, (hourAM_PM_t*)&_alarmFormat); + } + return _alarmMinutes; +} + +/** + * @brief get RTC alarm hour. + * @retval return the current alarm hour. + */ +uint8_t STM32RTC::getAlarmHours(void) +{ + if(_configured) { + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, (hourAM_PM_t*)&_alarmFormat); + } + return _alarmHours; +} + +/** + * @brief get RTC alarm hour. + * @param format: pointer to the current hour format: AM or PM + * @retval return the current alarm hour. + */ +uint8_t STM32RTC::getAlarmHours(Hour12_AM_PM_t *format) +{ + if(_configured) { + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, (hourAM_PM_t*)&_alarmFormat); + + if(format != NULL) { + *format = _alarmFormat; + } + } + return _alarmHours; +} + +/** + * @brief get RTC alarm date. + * @retval return the current alarm date. + */ +uint8_t STM32RTC::getAlarmDay(void) +{ + if(_configured) { + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, (hourAM_PM_t*)&_alarmFormat); + } + return _alarmDate; +} + +/** + * @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) { + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + if(subSeconds < 1000) { + _subSeconds = subSeconds; + } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (hourAM_PM_t)_hoursFormat); + } +} + +/** + * @brief set RTC seconds. + * @param seconds: 0-59 + * @retval none + */ +void STM32RTC::setSeconds(uint8_t seconds) +{ + if (_configured) { + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + if(seconds < 60) { + _seconds = seconds; + } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (hourAM_PM_t)_hoursFormat); + } +} + +/** + * @brief set RTC minutes. + * @param minutes: 0-59 + * @retval none + */ +void STM32RTC::setMinutes(uint8_t minutes) +{ + if (_configured) { + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + if(minutes < 60) { + _minutes = minutes; + } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (hourAM_PM_t)_hoursFormat); + } +} + +/** + * @brief set RTC hours. + * @param hours: 0-23 + * @retval none + */ +void STM32RTC::setHours(uint8_t hours) +{ + if (_configured) { + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + if(hours < 24) { + _hours = hours; + } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (hourAM_PM_t)_hoursFormat); + } +} + +/** + * @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, Hour12_AM_PM_t format) +{ + if (_configured) { + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + if(hours < 24) { + _hours = hours; + } + _hoursFormat = format; + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (hourAM_PM_t)_hoursFormat); + } +} + +/** + * @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, Hour12_AM_PM_t format) +{ + if (_configured) { + setSubSeconds(subSeconds); + setSeconds(seconds); + setMinutes(minutes); + setHours(hours, format); + } +} + +/** + * @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, &_date, &_day); + if((weekDay >= 1) && (weekDay <= 7)) { + _day = weekDay; + } + RTC_SetDate(_year, _month, _date, _day); + } +} + +/** + * @brief set RTC date. + * @param date: 1-31 + * @retval none + */ +void STM32RTC::setDay(uint8_t day) +{ + if (_configured) { + RTC_GetDate(&_year, &_month, &_date, &_day); + if((day >= 1) && (day <= 31)) { + _date = day; + } + RTC_SetDate(_year, _month, _date, _day); + } +} + +/** + * @brief set RTC month. + * @param month: 1-12 + * @retval none + */ +void STM32RTC::setMonth(uint8_t month) +{ + if (_configured) { + RTC_GetDate(&_year, &_month, &_date, &_day); + if((month >= 1) && (month <= 12)) { + _month = month; + } + RTC_SetDate(_year, _month, _date, _day); + } +} + +/** + * @brief set RTC year. + * @param year: 0-99 + * @retval none + */ +void STM32RTC::setYear(uint8_t year) +{ + if (_configured) { + RTC_GetDate(&_year, &_month, &_date, &_day); + if(year < 100) { + _year = year; + } + RTC_SetDate(_year, _month, _date, _day); + } +} + +/** + * @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, Hour12_AM_PM_t format) +{ + if (_configured) { + if(hours < 24) { + _alarmHours = hours; + } + _alarmFormat = format; + } +} + +/** + * @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, Hour12_AM_PM_t format) +{ + if (_configured) { + setAlarmHours(hours, format); + setAlarmMinutes(minutes); + setAlarmSeconds(seconds); + } +} + +/** + * @brief set RTC alarm date. + * @param day: 1-31 + * @retval none + */ +void STM32RTC::setAlarmDay(uint8_t day) +{ + if (_configured) { + if((day >= 1) && (day <= 31)) { + _alarmDate = 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; + + if(_configured) { + RTC_GetDate(&_year, &_month, &_date, &_day); + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + } + + tm.tm_isdst = -1; + tm.tm_yday = 0; + tm.tm_wday = _day - 1; + tm.tm_year = _year + EPOCH_TIME_YEAR_OFF; + tm.tm_mon = _month - 1; + tm.tm_mday = _date; + 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) +{ + 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_DHHMMSS); + } +} + +/** + * @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; + _date = tmp->tm_mday; + _day = tmp->tm_wday + 1; + _hours = tmp->tm_hour; + _minutes = tmp->tm_min; + _seconds = tmp->tm_sec; + + RTC_SetDate(_year, _month, _date, _day); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (hourAM_PM_t)_hoursFormat); + } +} + +/** + * @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); + } +} diff --git a/src/STM32RTC.h b/src/STM32RTC.h new file mode 100644 index 0000000..b0feb50 --- /dev/null +++ b/src/STM32RTC.h @@ -0,0 +1,197 @@ +/** + ****************************************************************************** + * @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 *); + +// Hour format: 12 or 24 hours +typedef enum { + HOUR_12, + HOUR_24 +} RTCHourFormats_t; + +//Time AM/PM definition +typedef enum { + HOUR12_AM, + HOUR12_PM +} Hour12_AM_PM_t; + +// Clock source selection +typedef enum { + RTC_LSI_CLOCK, + RTC_LSE_CLOCK, + RTC_HSE_CLOCK +} RTC_sourceClock_t; + +#define IS_CLOCK_SOURCE(SRC) (((SRC) == RTC_LSI_CLOCK) || ((SRC) == RTC_LSE_CLOCK) ||\ + ((SRC) == RTC_HSE_CLOCK)) + +class STM32RTC { +public: + + enum Alarm_Match: uint8_t + { + MATCH_OFF = 0, // Never + MATCH_SS = 1, // Every Minute + MATCH_MMSS = 2, // Every Hour + MATCH_HHMMSS = 3, // Every Day + MATCH_DHHMMSS = 4, // 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 = 5, + MATCH_YYMMDDHHMMSS = 6 + }; + + STM32RTC(); + + void begin(bool resetTime, RTCHourFormats_t format = HOUR_24); + void begin(RTCHourFormats_t format = HOUR_24); + + void setClockSource(RTC_sourceClock_t 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(void); + uint8_t getHours(Hour12_AM_PM_t *format); + + 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(void); + uint8_t getAlarmHours(Hour12_AM_PM_t *format); + + 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, Hour12_AM_PM_t format); + 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, Hour12_AM_PM_t format); + + 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, Hour12_AM_PM_t format); + void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds); + void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, Hour12_AM_PM_t format); + + 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); + + bool isConfigured(void) { + return _configured; + } + +private: + static bool _configured; + + Hour12_AM_PM_t _hoursFormat; + uint8_t _hours; + uint8_t _minutes; + uint8_t _seconds; + uint32_t _subSeconds; + uint8_t _year; + uint8_t _month; + uint8_t _date; + uint8_t _day; + + uint8_t _alarmDate; + uint8_t _alarmHours; + uint8_t _alarmMinutes; + uint8_t _alarmSeconds; + uint32_t _alarmSubSeconds; + Hour12_AM_PM_t _alarmFormat; + + RTC_sourceClock_t _clockSource; +}; + +#endif // __STM32_RTC_H From 78990ed2314baa9d6a4fd6fa59e9ba86a905c4df Mon Sep 17 00:00:00 2001 From: fpr Date: Thu, 26 Oct 2017 17:10:51 +0200 Subject: [PATCH 04/16] Add examples Signed-off-by: fpr --- examples/Epoch/Epoch.ino | 84 +++++++++++++ .../RTCClockSelection/RTCClockSelection.ino | 112 ++++++++++++++++++ examples/SimpleRTC/SimpleRTC.ino | 107 +++++++++++++++++ examples/simpleRTCAlarm/simpleRTCAlarm.ino | 77 ++++++++++++ 4 files changed, 380 insertions(+) create mode 100644 examples/Epoch/Epoch.ino create mode 100644 examples/RTCClockSelection/RTCClockSelection.ino create mode 100644 examples/SimpleRTC/SimpleRTC.ino create mode 100644 examples/simpleRTCAlarm/simpleRTCAlarm.ino 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..ac45bfe --- /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(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!"); +} From d28089d34b5f5f90482a906dca22671fdffc65ee Mon Sep 17 00:00:00 2001 From: fpr Date: Tue, 12 Dec 2017 14:00:43 +0100 Subject: [PATCH 05/16] Update readme Signed-off-by: fpr --- README.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/README.md b/README.md index 0905bc8..34aacbf 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,49 @@ # 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(RTCHourFormats_t format)`** + +_RTC clock source_ +* **`void setClockSource(sourceClock_t source)`** : this function must be called before `begin()` + +_SubSeconds management_ +* **`uint32_t getSubSeconds(void)`** +* **`void setSubSeconds(uint32_t subSeconds)`** + +_Hour format (AM or PM)_ +* **`uint8_t getHours(Hour12_AM_PM_t *format)`** +* **`void setHours(uint8_t hours, Hour12_AM_PM_t format)`** +* **`void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, Hour12_AM_PM_t format)`** +* **`void setAlarmHours(uint8_t hours, Hour12_AM_PM_t format)`** +* **`uint8_t getAlarmHours(Hour12_AM_PM_t *format)`** +* **`void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, Hour12_AM_PM_t format)`** + +_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 From 64e4f8c22fe429217682d2cd4d418ad61ad7ef79 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Fri, 6 Apr 2018 15:28:38 +0200 Subject: [PATCH 06/16] Add STM32RTC::end() method Signed-off-by: Frederic.Pillon --- src/STM32RTC.cpp | 13 +++++++++++++ src/STM32RTC.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index abb5f8c..53ff310 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -95,6 +95,19 @@ void STM32RTC::begin(RTCHourFormats_t format) _configured = true; } +/** + * @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(). diff --git a/src/STM32RTC.h b/src/STM32RTC.h index b0feb50..2424aee 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -92,6 +92,8 @@ class STM32RTC { void begin(bool resetTime, RTCHourFormats_t format = HOUR_24); void begin(RTCHourFormats_t format = HOUR_24); + void end(void); + void setClockSource(RTC_sourceClock_t source); void enableAlarm(Alarm_Match match); From 6d25f63da10f881a351e408b1898f4f16cefbe9f Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Fri, 6 Apr 2018 16:31:03 +0200 Subject: [PATCH 07/16] Add IS_HOUR_FORMAT macro Signed-off-by: Frederic.Pillon --- src/STM32RTC.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/STM32RTC.h b/src/STM32RTC.h index 2424aee..88cf2f0 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -69,6 +69,7 @@ typedef enum { #define IS_CLOCK_SOURCE(SRC) (((SRC) == RTC_LSI_CLOCK) || ((SRC) == RTC_LSE_CLOCK) ||\ ((SRC) == RTC_HSE_CLOCK)) +#define IS_HOUR_FORMAT(FMT) (((FMT) == HOUR_12) || ((FMT) == HOUR_24)) class STM32RTC { public: From b10b9a4e6dc28247124e0b7b8ea7b6c0512ca60e Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Mon, 16 Apr 2018 10:34:19 +0200 Subject: [PATCH 08/16] Use scoped enumerations Unified enumerations definitions and hardened the code. Signed-off-by: Frederic.Pillon --- README.md | 16 +-- .../RTCClockSelection/RTCClockSelection.ino | 2 +- src/STM32RTC.cpp | 136 +++++++++++------- src/STM32RTC.h | 68 ++++----- 4 files changed, 129 insertions(+), 93 deletions(-) diff --git a/README.md b/README.md index 34aacbf..5e77d7f 100644 --- a/README.md +++ b/README.md @@ -18,22 +18,22 @@ The following functions are not supported: The following functions have been added to support specific STM32 RTC features: _RTC hours mode (12 or 24)_ -* **`void begin(RTCHourFormats_t format)`** +* **`void begin(RTC_Hour_Format format)`** _RTC clock source_ -* **`void setClockSource(sourceClock_t source)`** : this function must be called before `begin()` +* **`void setClockSource(RTC_Source_Clock source)`** : this function must be called before `begin()` _SubSeconds management_ * **`uint32_t getSubSeconds(void)`** * **`void setSubSeconds(uint32_t subSeconds)`** _Hour format (AM or PM)_ -* **`uint8_t getHours(Hour12_AM_PM_t *format)`** -* **`void setHours(uint8_t hours, Hour12_AM_PM_t format)`** -* **`void setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, Hour12_AM_PM_t format)`** -* **`void setAlarmHours(uint8_t hours, Hour12_AM_PM_t format)`** -* **`uint8_t getAlarmHours(Hour12_AM_PM_t *format)`** -* **`void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, Hour12_AM_PM_t format)`** +* **`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)`** diff --git a/examples/RTCClockSelection/RTCClockSelection.ino b/examples/RTCClockSelection/RTCClockSelection.ino index ac45bfe..57a007b 100644 --- a/examples/RTCClockSelection/RTCClockSelection.ino +++ b/examples/RTCClockSelection/RTCClockSelection.ino @@ -60,7 +60,7 @@ void setup() // Select RTC clock source: RTC_LSI_CLOCK, RTC_LSE_CLOCK or RTC_HSE_CLOCK. // By default the LSI is selected as source. - rtc.setClockSource(RTC_LSE_CLOCK); + rtc.setClockSource(STM32RTC::RTC_LSE_CLOCK); rtc.begin(); // initialize RTC 24H format diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index 53ff310..190c27c 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -54,10 +54,10 @@ STM32RTC::STM32RTC(void): _clockSource(RTC_LSI_CLOCK) /** * @brief initializes the RTC * @param resetTime: if true reconfigures the RTC - * @param format: hour format: HOUR_12 or HOUR_24(default) + * @param format: hour format: RTC_HOUR_12 or RTC_HOUR_24(default) * @retval None */ -void STM32RTC::begin(bool resetTime, RTCHourFormats_t format) +void STM32RTC::begin(bool resetTime, RTC_Hour_Format format) { if(resetTime == true) { _configured = false; @@ -67,16 +67,18 @@ void STM32RTC::begin(bool resetTime, RTCHourFormats_t format) /** * @brief initializes the RTC - * @param format: hour format: HOUR_12 or HOUR_24(default) + * @param format: hour format: RTC_HOUR_12 or RTC_HOUR_24(default) * @retval None */ -void STM32RTC::begin(RTCHourFormats_t format) +void STM32RTC::begin(RTC_Hour_Format format) { if(_configured == false) { - RTC_init((hourFormat_t)format, (sourceClock_t)_clockSource); + 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); } - _hoursFormat = HOUR12_AM; + _hoursPeriod = RTC_AM; _hours = 0; _minutes = 0; _seconds = 0; @@ -90,7 +92,7 @@ void STM32RTC::begin(RTCHourFormats_t format) _alarmMinutes = 0; _alarmSeconds = 0; _alarmSubSeconds = 0; - _alarmFormat = HOUR12_AM; + _alarmPeriod = RTC_AM; _configured = true; } @@ -114,7 +116,7 @@ void STM32RTC::end(void) * @param source: clock source: RTC_LSI_CLOCK, RTC_LSE_CLOCK or RTC_HSE_CLOCK * @retval None */ -void STM32RTC::setClockSource(RTC_sourceClock_t source) +void STM32RTC::setClockSource(RTC_Source_Clock source) { if(IS_CLOCK_SOURCE(source)) { _clockSource = source; @@ -128,14 +130,13 @@ void STM32RTC::setClockSource(RTC_sourceClock_t source) */ void STM32RTC::enableAlarm(Alarm_Match match) { - Hour12_AM_PM_t format; + hourAM_PM_t period; uint8_t date, hours, minutes, seconds; uint32_t subSeconds; if(_configured) { - RTC_GetTime(&hours, &minutes, &seconds, &subSeconds, (hourAM_PM_t*)&format); + RTC_GetTime(&hours, &minutes, &seconds, &subSeconds, &period); date = getDay(); - switch (match) { case MATCH_OFF: RTC_StopAlarm(); @@ -146,12 +147,12 @@ void STM32RTC::enableAlarm(Alarm_Match match) date = _alarmDate; case MATCH_HHMMSS: hours = _alarmHours; - format = _alarmFormat; + period = (_alarmPeriod == RTC_AM)? AM: PM; case MATCH_MMSS: minutes = _alarmMinutes; case MATCH_SS: seconds = _alarmSeconds; - RTC_StartAlarm(date, hours, minutes, seconds, subSeconds, (hourAM_PM_t)format); + RTC_StartAlarm(date, hours, minutes, seconds, subSeconds, period); break; default: break; @@ -206,7 +207,9 @@ void STM32RTC::standbyMode(void) uint32_t STM32RTC::getSubSeconds(void) { if(_configured) { - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + hourAM_PM_t p; + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; } return _subSeconds; } @@ -218,7 +221,9 @@ uint32_t STM32RTC::getSubSeconds(void) uint8_t STM32RTC::getSeconds(void) { if(_configured) { - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + hourAM_PM_t p; + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; } return _seconds; } @@ -230,7 +235,9 @@ uint8_t STM32RTC::getSeconds(void) uint8_t STM32RTC::getMinutes(void) { if(_configured) { - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + hourAM_PM_t p; + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; } return _minutes; } @@ -242,7 +249,9 @@ uint8_t STM32RTC::getMinutes(void) uint8_t STM32RTC::getHours(void) { if(_configured) { - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + hourAM_PM_t p; + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; } return _hours; } @@ -252,13 +261,15 @@ uint8_t STM32RTC::getHours(void) * @param format: 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(Hour12_AM_PM_t *format) +uint8_t STM32RTC::getHours(RTC_AM_PM *period) { if(_configured) { - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + hourAM_PM_t p; + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; - if(format != NULL) { - *format = _hoursFormat; + if(period != NULL) { + *period = _hoursPeriod; } } return _hours; @@ -319,7 +330,9 @@ uint8_t STM32RTC::getYear(void) uint32_t STM32RTC::getAlarmSubSeconds(void) { if(_configured) { - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, (hourAM_PM_t*)&_alarmFormat); + hourAM_PM_t p; + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); + _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; } return _alarmSubSeconds; } @@ -331,7 +344,9 @@ uint32_t STM32RTC::getAlarmSubSeconds(void) uint8_t STM32RTC::getAlarmSeconds(void) { if(_configured) { - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, (hourAM_PM_t*)&_alarmFormat); + hourAM_PM_t p; + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); + _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; } return _alarmSeconds; } @@ -343,7 +358,9 @@ uint8_t STM32RTC::getAlarmSeconds(void) uint8_t STM32RTC::getAlarmMinutes(void) { if(_configured) { - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, (hourAM_PM_t*)&_alarmFormat); + hourAM_PM_t p; + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); + _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; } return _alarmMinutes; } @@ -355,7 +372,9 @@ uint8_t STM32RTC::getAlarmMinutes(void) uint8_t STM32RTC::getAlarmHours(void) { if(_configured) { - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, (hourAM_PM_t*)&_alarmFormat); + hourAM_PM_t p; + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); + _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; } return _alarmHours; } @@ -365,13 +384,15 @@ uint8_t STM32RTC::getAlarmHours(void) * @param format: pointer to the current hour format: AM or PM * @retval return the current alarm hour. */ -uint8_t STM32RTC::getAlarmHours(Hour12_AM_PM_t *format) +uint8_t STM32RTC::getAlarmHours(RTC_AM_PM *period) { if(_configured) { - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, (hourAM_PM_t*)&_alarmFormat); + hourAM_PM_t p; + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); + _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; - if(format != NULL) { - *format = _alarmFormat; + if(period != NULL) { + *period = _alarmPeriod; } } return _alarmHours; @@ -384,7 +405,9 @@ uint8_t STM32RTC::getAlarmHours(Hour12_AM_PM_t *format) uint8_t STM32RTC::getAlarmDay(void) { if(_configured) { - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, (hourAM_PM_t*)&_alarmFormat); + hourAM_PM_t p; + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); + _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; } return _alarmDate; } @@ -423,11 +446,14 @@ uint8_t STM32RTC::getAlarmYear(void) void STM32RTC::setSubSeconds(uint32_t subSeconds) { if (_configured) { - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + hourAM_PM_t p; + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; + if(subSeconds < 1000) { _subSeconds = subSeconds; } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (hourAM_PM_t)_hoursFormat); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, p); } } @@ -439,11 +465,13 @@ void STM32RTC::setSubSeconds(uint32_t subSeconds) void STM32RTC::setSeconds(uint8_t seconds) { if (_configured) { - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + hourAM_PM_t p; + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; if(seconds < 60) { _seconds = seconds; } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (hourAM_PM_t)_hoursFormat); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, p); } } @@ -455,11 +483,13 @@ void STM32RTC::setSeconds(uint8_t seconds) void STM32RTC::setMinutes(uint8_t minutes) { if (_configured) { - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + hourAM_PM_t p; + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; if(minutes < 60) { _minutes = minutes; } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (hourAM_PM_t)_hoursFormat); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, p); } } @@ -471,11 +501,13 @@ void STM32RTC::setMinutes(uint8_t minutes) void STM32RTC::setHours(uint8_t hours) { if (_configured) { - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + hourAM_PM_t p; + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; if(hours < 24) { _hours = hours; } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (hourAM_PM_t)_hoursFormat); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, p); } } @@ -485,15 +517,17 @@ void STM32RTC::setHours(uint8_t hours) * @param hours format: AM or PM * @retval none */ -void STM32RTC::setHours(uint8_t hours, Hour12_AM_PM_t format) +void STM32RTC::setHours(uint8_t hours, RTC_AM_PM period) { if (_configured) { - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + hourAM_PM_t p; + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; if(hours < 24) { _hours = hours; } - _hoursFormat = format; - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (hourAM_PM_t)_hoursFormat); + _hoursPeriod = period; + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, p); } } @@ -521,13 +555,13 @@ void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds) * @param hour format: AM or PM * @retval none */ -void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, Hour12_AM_PM_t format) +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, format); + setHours(hours, period); } } @@ -677,13 +711,13 @@ void STM32RTC::setAlarmHours(uint8_t hours) * @param hour format: AM or PM * @retval none */ -void STM32RTC::setAlarmHours(uint8_t hours, Hour12_AM_PM_t format) +void STM32RTC::setAlarmHours(uint8_t hours, RTC_AM_PM period) { if (_configured) { if(hours < 24) { _alarmHours = hours; } - _alarmFormat = format; + _alarmPeriod = period; } } @@ -711,10 +745,10 @@ void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds) * @param hour format: AM or PM * @retval none */ -void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, Hour12_AM_PM_t format) +void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, RTC_AM_PM period) { if (_configured) { - setAlarmHours(hours, format); + setAlarmHours(hours, period); setAlarmMinutes(minutes); setAlarmSeconds(seconds); } @@ -781,8 +815,10 @@ uint32_t STM32RTC::getEpoch(void) struct tm tm; if(_configured) { + hourAM_PM_t p; RTC_GetDate(&_year, &_month, &_date, &_day); - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, (hourAM_PM_t*)&_hoursFormat); + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; } tm.tm_isdst = -1; @@ -852,7 +888,7 @@ void STM32RTC::setEpoch(uint32_t ts) _seconds = tmp->tm_sec; RTC_SetDate(_year, _month, _date, _day); - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (hourAM_PM_t)_hoursFormat); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); } } diff --git a/src/STM32RTC.h b/src/STM32RTC.h index 88cf2f0..199bcad 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -48,32 +48,25 @@ typedef void(*voidFuncPtr)(void *); -// Hour format: 12 or 24 hours -typedef enum { - HOUR_12, - HOUR_24 -} RTCHourFormats_t; - -//Time AM/PM definition -typedef enum { - HOUR12_AM, - HOUR12_PM -} Hour12_AM_PM_t; - -// Clock source selection -typedef enum { - RTC_LSI_CLOCK, - RTC_LSE_CLOCK, - RTC_HSE_CLOCK -} RTC_sourceClock_t; - -#define IS_CLOCK_SOURCE(SRC) (((SRC) == RTC_LSI_CLOCK) || ((SRC) == RTC_LSE_CLOCK) ||\ - ((SRC) == RTC_HSE_CLOCK)) -#define IS_HOUR_FORMAT(FMT) (((FMT) == HOUR_12) || ((FMT) == HOUR_24)) +#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 = 0, // Never @@ -88,14 +81,21 @@ class STM32RTC { MATCH_YYMMDDHHMMSS = 6 }; + 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, RTCHourFormats_t format = HOUR_24); - void begin(RTCHourFormats_t format = HOUR_24); + 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_sourceClock_t source); + void setClockSource(RTC_Source_Clock source); void enableAlarm(Alarm_Match match); void disableAlarm(void); @@ -112,7 +112,7 @@ class STM32RTC { uint8_t getSeconds(void); uint8_t getMinutes(void); uint8_t getHours(void); - uint8_t getHours(Hour12_AM_PM_t *format); + uint8_t getHours(RTC_AM_PM *period); uint8_t getWeekDay(void); uint8_t getDay(void); @@ -123,7 +123,7 @@ class STM32RTC { uint8_t getAlarmSeconds(void); uint8_t getAlarmMinutes(void); uint8_t getAlarmHours(void); - uint8_t getAlarmHours(Hour12_AM_PM_t *format); + uint8_t getAlarmHours(RTC_AM_PM *period); uint8_t getAlarmDay(void); @@ -137,9 +137,9 @@ class STM32RTC { void setSeconds(uint8_t seconds); void setMinutes(uint8_t minutes); void setHours(uint8_t hours); - void setHours(uint8_t hours, Hour12_AM_PM_t format); + 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, Hour12_AM_PM_t format); + 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); @@ -151,9 +151,9 @@ class STM32RTC { void setAlarmSeconds(uint8_t seconds); void setAlarmMinutes(uint8_t minutes); void setAlarmHours(uint8_t hours); - void setAlarmHours(uint8_t hours, Hour12_AM_PM_t format); + 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, Hour12_AM_PM_t format); + void setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, RTC_AM_PM period); void setAlarmDay(uint8_t day); @@ -177,7 +177,7 @@ class STM32RTC { private: static bool _configured; - Hour12_AM_PM_t _hoursFormat; + RTC_AM_PM _hoursPeriod; uint8_t _hours; uint8_t _minutes; uint8_t _seconds; @@ -192,9 +192,9 @@ class STM32RTC { uint8_t _alarmMinutes; uint8_t _alarmSeconds; uint32_t _alarmSubSeconds; - Hour12_AM_PM_t _alarmFormat; + RTC_AM_PM _alarmPeriod; - RTC_sourceClock_t _clockSource; + RTC_Source_Clock _clockSource; }; #endif // __STM32_RTC_H From 9e60e5865456f3a208bf32a9530434888f6027f9 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Mon, 16 Apr 2018 17:22:29 +0200 Subject: [PATCH 09/16] Remove getHours(void)/getAlarmHours(void) Use same methods with its default parameters value. * uint8_t getHours(RTC_AM_PM *period = NULL); * uint8_t getAlarmHours(RTC_AM_PM *period = NULL); Signed-off-by: Frederic.Pillon --- src/STM32RTC.cpp | 34 ++++------------------------------ src/STM32RTC.h | 6 ++---- 2 files changed, 6 insertions(+), 34 deletions(-) diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index 190c27c..80342ff 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -244,21 +244,8 @@ uint8_t STM32RTC::getMinutes(void) /** * @brief get RTC hours. - * @retval return the current hours from the RTC. - */ -uint8_t STM32RTC::getHours(void) -{ - if(_configured) { - hourAM_PM_t p; - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); - _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; - } - return _hours; -} - -/** - * @brief get RTC hours. - * @param format: pointer to the current hour format set in the RTC: AM or PM + * @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) @@ -367,21 +354,8 @@ uint8_t STM32RTC::getAlarmMinutes(void) /** * @brief get RTC alarm hour. - * @retval return the current alarm hour. - */ -uint8_t STM32RTC::getAlarmHours(void) -{ - if(_configured) { - hourAM_PM_t p; - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); - _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; - } - return _alarmHours; -} - -/** - * @brief get RTC alarm hour. - * @param format: pointer to the current hour format: AM or PM + * @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) diff --git a/src/STM32RTC.h b/src/STM32RTC.h index 199bcad..cbbdff6 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -111,8 +111,7 @@ class STM32RTC { uint32_t getSubSeconds(void); uint8_t getSeconds(void); uint8_t getMinutes(void); - uint8_t getHours(void); - uint8_t getHours(RTC_AM_PM *period); + uint8_t getHours(RTC_AM_PM *period = NULL); uint8_t getWeekDay(void); uint8_t getDay(void); @@ -122,8 +121,7 @@ class STM32RTC { uint32_t getAlarmSubSeconds(void); uint8_t getAlarmSeconds(void); uint8_t getAlarmMinutes(void); - uint8_t getAlarmHours(void); - uint8_t getAlarmHours(RTC_AM_PM *period); + uint8_t getAlarmHours(RTC_AM_PM *period = NULL); uint8_t getAlarmDay(void); From 6ef59ff810b776f82a298e8d43a32caa83efc81c Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Mon, 16 Apr 2018 17:42:40 +0200 Subject: [PATCH 10/16] Add synchronization methods Add syncTime, syncDate and syncAlarmTime to factorize code. Signed-off-by: Frederic.Pillon --- src/STM32RTC.cpp | 158 ++++++++++++++++++++--------------------------- src/STM32RTC.h | 4 ++ 2 files changed, 71 insertions(+), 91 deletions(-) diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index 80342ff..ce756c1 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -206,11 +206,7 @@ void STM32RTC::standbyMode(void) */ uint32_t STM32RTC::getSubSeconds(void) { - if(_configured) { - hourAM_PM_t p; - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); - _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; - } + syncTime(); return _subSeconds; } @@ -220,11 +216,7 @@ uint32_t STM32RTC::getSubSeconds(void) */ uint8_t STM32RTC::getSeconds(void) { - if(_configured) { - hourAM_PM_t p; - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); - _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; - } + syncTime(); return _seconds; } @@ -234,11 +226,7 @@ uint8_t STM32RTC::getSeconds(void) */ uint8_t STM32RTC::getMinutes(void) { - if(_configured) { - hourAM_PM_t p; - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); - _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; - } + syncTime(); return _minutes; } @@ -250,14 +238,9 @@ uint8_t STM32RTC::getMinutes(void) */ uint8_t STM32RTC::getHours(RTC_AM_PM *period) { - if(_configured) { - hourAM_PM_t p; - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); - _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; - - if(period != NULL) { - *period = _hoursPeriod; - } + syncTime(); + if(period != NULL) { + *period = _hoursPeriod; } return _hours; } @@ -268,9 +251,7 @@ uint8_t STM32RTC::getHours(RTC_AM_PM *period) */ uint8_t STM32RTC::getWeekDay(void) { - if(_configured) { - RTC_GetDate(&_year, &_month, &_date, &_day); - } + syncDate(); return _day; } @@ -280,9 +261,7 @@ uint8_t STM32RTC::getWeekDay(void) */ uint8_t STM32RTC::getDay(void) { - if(_configured) { - RTC_GetDate(&_year, &_month, &_date, &_day); - } + syncDate(); return _date; } @@ -292,9 +271,7 @@ uint8_t STM32RTC::getDay(void) */ uint8_t STM32RTC::getMonth(void) { - if(_configured) { - RTC_GetDate(&_year, &_month, &_date, &_day); - } + syncDate(); return _month; } @@ -304,9 +281,7 @@ uint8_t STM32RTC::getMonth(void) */ uint8_t STM32RTC::getYear(void) { - if(_configured) { - RTC_GetDate(&_year, &_month, &_date, &_day); - } + syncDate(); return _year; } @@ -316,11 +291,7 @@ uint8_t STM32RTC::getYear(void) */ uint32_t STM32RTC::getAlarmSubSeconds(void) { - if(_configured) { - hourAM_PM_t p; - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); - _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; - } + syncAlarmTime(); return _alarmSubSeconds; } @@ -330,11 +301,7 @@ uint32_t STM32RTC::getAlarmSubSeconds(void) */ uint8_t STM32RTC::getAlarmSeconds(void) { - if(_configured) { - hourAM_PM_t p; - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); - _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; - } + syncAlarmTime(); return _alarmSeconds; } @@ -344,11 +311,7 @@ uint8_t STM32RTC::getAlarmSeconds(void) */ uint8_t STM32RTC::getAlarmMinutes(void) { - if(_configured) { - hourAM_PM_t p; - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); - _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; - } + syncAlarmTime(); return _alarmMinutes; } @@ -360,14 +323,9 @@ uint8_t STM32RTC::getAlarmMinutes(void) */ uint8_t STM32RTC::getAlarmHours(RTC_AM_PM *period) { - if(_configured) { - hourAM_PM_t p; - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); - _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; - - if(period != NULL) { - *period = _alarmPeriod; - } + syncAlarmTime(); + if(period != NULL) { + *period = _alarmPeriod; } return _alarmHours; } @@ -378,11 +336,7 @@ uint8_t STM32RTC::getAlarmHours(RTC_AM_PM *period) */ uint8_t STM32RTC::getAlarmDay(void) { - if(_configured) { - hourAM_PM_t p; - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); - _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; - } + syncAlarmTime(); return _alarmDate; } @@ -420,14 +374,11 @@ uint8_t STM32RTC::getAlarmYear(void) void STM32RTC::setSubSeconds(uint32_t subSeconds) { if (_configured) { - hourAM_PM_t p; - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); - _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; - + syncTime(); if(subSeconds < 1000) { - _subSeconds = subSeconds; + _subSeconds = subSeconds; } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, p); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); } } @@ -439,13 +390,11 @@ void STM32RTC::setSubSeconds(uint32_t subSeconds) void STM32RTC::setSeconds(uint8_t seconds) { if (_configured) { - hourAM_PM_t p; - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); - _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; + syncTime(); if(seconds < 60) { _seconds = seconds; } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, p); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); } } @@ -457,13 +406,11 @@ void STM32RTC::setSeconds(uint8_t seconds) void STM32RTC::setMinutes(uint8_t minutes) { if (_configured) { - hourAM_PM_t p; - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); - _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; + syncTime(); if(minutes < 60) { _minutes = minutes; } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, p); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); } } @@ -475,13 +422,11 @@ void STM32RTC::setMinutes(uint8_t minutes) void STM32RTC::setHours(uint8_t hours) { if (_configured) { - hourAM_PM_t p; - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); - _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; + syncTime(); if(hours < 24) { _hours = hours; } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, p); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); } } @@ -494,14 +439,12 @@ void STM32RTC::setHours(uint8_t hours) void STM32RTC::setHours(uint8_t hours, RTC_AM_PM period) { if (_configured) { - hourAM_PM_t p; - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); - _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; + syncTime(); if(hours < 24) { _hours = hours; } _hoursPeriod = period; - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, p); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); } } @@ -788,12 +731,8 @@ uint32_t STM32RTC::getEpoch(void) { struct tm tm; - if(_configured) { - hourAM_PM_t p; - RTC_GetDate(&_year, &_month, &_date, &_day); - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); - _hoursPeriod = (p == AM)? RTC_AM : RTC_PM; - } + syncDate(); + syncTime(); tm.tm_isdst = -1; tm.tm_yday = 0; @@ -876,3 +815,40 @@ void STM32RTC::setY2kEpoch(uint32_t ts) 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, &_date, &_day); + } +} + +/** + * @brief synchronise the alarm time from the current RTC one + * @param none + */ +void STM32RTC::syncAlarmTime(void) +{ + if(_configured) { + hourAM_PM_t p = AM; + RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); + _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; + } +} diff --git a/src/STM32RTC.h b/src/STM32RTC.h index cbbdff6..7393344 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -193,6 +193,10 @@ class STM32RTC { RTC_AM_PM _alarmPeriod; RTC_Source_Clock _clockSource; + + void syncTime(void); + void syncDate(void); + void syncAlarmTime(void); }; #endif // __STM32_RTC_H From 35cfbcc3e601b965ca13cf484c9b0c7970411eed Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Mon, 16 Apr 2018 17:51:55 +0200 Subject: [PATCH 11/16] Init properly members during begin() Signed-off-by: Frederic.Pillon --- src/STM32RTC.cpp | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index ce756c1..68d65a8 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -76,25 +76,22 @@ void STM32RTC::begin(RTC_Hour_Format format) 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 + _alarmDate = _date; + _alarmHours = _hours; + _alarmMinutes = _minutes; + _alarmSeconds = _seconds; + _alarmSubSeconds = _subSeconds; + _alarmPeriod = _hoursPeriod; + } else { + syncTime(); + syncDate(); + syncAlarmTime(); } - - _hoursPeriod = RTC_AM; - _hours = 0; - _minutes = 0; - _seconds = 0; - _subSeconds = 0; - _year = 0; - _month = 0; - _date = 0; - _day = 0; - _alarmDate = 0; - _alarmHours = 0; - _alarmMinutes = 0; - _alarmSeconds = 0; - _alarmSubSeconds = 0; - _alarmPeriod = RTC_AM; - - _configured = true; } /** From 8e0b14deee2af201ae120c6bb1d12673b2a232db Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Wed, 18 Apr 2018 11:23:47 +0200 Subject: [PATCH 12/16] Rename members to avoid any confusion _day -> _wday is a week day (1 to 7) _date -> _day is the day (1 to 31) _alarmDate -> _alarmDay is the alarm day (1 to 31) Signed-off-by: Frederic.Pillon --- src/STM32RTC.cpp | 66 ++++++++++++++++++++++++------------------------ src/STM32RTC.h | 4 +-- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index 68d65a8..03bee56 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -81,7 +81,7 @@ void STM32RTC::begin(RTC_Hour_Format format) syncTime(); syncDate(); // Use current time to init alarm members - _alarmDate = _date; + _alarmDay = _day; _alarmHours = _hours; _alarmMinutes = _minutes; _alarmSeconds = _seconds; @@ -128,12 +128,12 @@ void STM32RTC::setClockSource(RTC_Source_Clock source) void STM32RTC::enableAlarm(Alarm_Match match) { hourAM_PM_t period; - uint8_t date, hours, minutes, seconds; + uint8_t day, hours, minutes, seconds; uint32_t subSeconds; if(_configured) { RTC_GetTime(&hours, &minutes, &seconds, &subSeconds, &period); - date = getDay(); + day = getDay(); switch (match) { case MATCH_OFF: RTC_StopAlarm(); @@ -141,7 +141,7 @@ void STM32RTC::enableAlarm(Alarm_Match match) case MATCH_YYMMDDHHMMSS://kept for compatibility case MATCH_MMDDHHMMSS: //kept for compatibility case MATCH_DHHMMSS: - date = _alarmDate; + day = _alarmDay; case MATCH_HHMMSS: hours = _alarmHours; period = (_alarmPeriod == RTC_AM)? AM: PM; @@ -149,7 +149,7 @@ void STM32RTC::enableAlarm(Alarm_Match match) minutes = _alarmMinutes; case MATCH_SS: seconds = _alarmSeconds; - RTC_StartAlarm(date, hours, minutes, seconds, subSeconds, period); + RTC_StartAlarm(day, hours, minutes, seconds, subSeconds, period); break; default: break; @@ -249,17 +249,17 @@ uint8_t STM32RTC::getHours(RTC_AM_PM *period) uint8_t STM32RTC::getWeekDay(void) { syncDate(); - return _day; + return _wday; } /** - * @brief get RTC date. - * @retval return the current date from the RTC. + * @brief get RTC day. + * @retval return the current day from the RTC. */ uint8_t STM32RTC::getDay(void) { syncDate(); - return _date; + return _day; } /** @@ -328,13 +328,13 @@ uint8_t STM32RTC::getAlarmHours(RTC_AM_PM *period) } /** - * @brief get RTC alarm date. - * @retval return the current alarm date. + * @brief get RTC alarm day. + * @retval return the current alarm day. */ uint8_t STM32RTC::getAlarmDay(void) { syncAlarmTime(); - return _alarmDate; + return _alarmDay; } /** @@ -487,27 +487,27 @@ void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t void STM32RTC::setWeekDay(uint8_t weekDay) { if (_configured) { - RTC_GetDate(&_year, &_month, &_date, &_day); + RTC_GetDate(&_year, &_month, &_day, &_wday); if((weekDay >= 1) && (weekDay <= 7)) { - _day = weekDay; + _wday = weekDay; } - RTC_SetDate(_year, _month, _date, _day); + RTC_SetDate(_year, _month, _day, _wday); } } /** - * @brief set RTC date. - * @param date: 1-31 + * @brief set RTC day. + * @param day: 1-31 * @retval none */ void STM32RTC::setDay(uint8_t day) { if (_configured) { - RTC_GetDate(&_year, &_month, &_date, &_day); + RTC_GetDate(&_year, &_month, &_day, &_wday); if((day >= 1) && (day <= 31)) { - _date = day; + _day = day; } - RTC_SetDate(_year, _month, _date, _day); + RTC_SetDate(_year, _month, _day, _wday); } } @@ -519,11 +519,11 @@ void STM32RTC::setDay(uint8_t day) void STM32RTC::setMonth(uint8_t month) { if (_configured) { - RTC_GetDate(&_year, &_month, &_date, &_day); + RTC_GetDate(&_year, &_month, &_day, &_wday); if((month >= 1) && (month <= 12)) { _month = month; } - RTC_SetDate(_year, _month, _date, _day); + RTC_SetDate(_year, _month, _day, _wday); } } @@ -535,11 +535,11 @@ void STM32RTC::setMonth(uint8_t month) void STM32RTC::setYear(uint8_t year) { if (_configured) { - RTC_GetDate(&_year, &_month, &_date, &_day); + RTC_GetDate(&_year, &_month, &_day, &_wday); if(year < 100) { _year = year; } - RTC_SetDate(_year, _month, _date, _day); + RTC_SetDate(_year, _month, _day, _wday); } } @@ -669,7 +669,7 @@ void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, RTC } /** - * @brief set RTC alarm date. + * @brief set RTC alarm day. * @param day: 1-31 * @retval none */ @@ -677,7 +677,7 @@ void STM32RTC::setAlarmDay(uint8_t day) { if (_configured) { if((day >= 1) && (day <= 31)) { - _alarmDate = day; + _alarmDay = day; } } } @@ -733,10 +733,10 @@ uint32_t STM32RTC::getEpoch(void) tm.tm_isdst = -1; tm.tm_yday = 0; - tm.tm_wday = _day - 1; + tm.tm_wday = _wday - 1; tm.tm_year = _year + EPOCH_TIME_YEAR_OFF; tm.tm_mon = _month - 1; - tm.tm_mday = _date; + tm.tm_mday = _day; tm.tm_hour = _hours; tm.tm_min = _minutes; tm.tm_sec = _seconds; @@ -791,13 +791,13 @@ void STM32RTC::setEpoch(uint32_t ts) _year = tmp->tm_year - EPOCH_TIME_YEAR_OFF; _month = tmp->tm_mon + 1; - _date = tmp->tm_mday; - _day = tmp->tm_wday + 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, _date, _day); + RTC_SetDate(_year, _month, _day, _wday); RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM); } } @@ -833,7 +833,7 @@ void STM32RTC::syncTime(void) void STM32RTC::syncDate(void) { if(_configured) { - RTC_GetDate(&_year, &_month, &_date, &_day); + RTC_GetDate(&_year, &_month, &_day, &_wday); } } @@ -845,7 +845,7 @@ void STM32RTC::syncAlarmTime(void) { if(_configured) { hourAM_PM_t p = AM; - RTC_GetAlarm(&_alarmDate, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); + RTC_GetAlarm(&_alarmDay, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); _alarmPeriod = (p == AM)? RTC_AM : RTC_PM; } } diff --git a/src/STM32RTC.h b/src/STM32RTC.h index 7393344..0ddc302 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -182,10 +182,10 @@ class STM32RTC { uint32_t _subSeconds; uint8_t _year; uint8_t _month; - uint8_t _date; uint8_t _day; + uint8_t _wday; - uint8_t _alarmDate; + uint8_t _alarmDay; uint8_t _alarmHours; uint8_t _alarmMinutes; uint8_t _alarmSeconds; From ce8571b6d52b29e7d6375c96c55a9672b7e69b3b Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Wed, 18 Apr 2018 11:26:24 +0200 Subject: [PATCH 13/16] Fix alarm match parameters usage Before only MATCH_DHHMMSS was done. Signed-off-by: Frederic.Pillon --- src/STM32RTC.cpp | 38 +++++++++++++++++++++++--------------- src/STM32RTC.h | 19 +++++++++---------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index 03bee56..fb038aa 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -127,13 +127,8 @@ void STM32RTC::setClockSource(RTC_Source_Clock source) */ void STM32RTC::enableAlarm(Alarm_Match match) { - hourAM_PM_t period; - uint8_t day, hours, minutes, seconds; - uint32_t subSeconds; - if(_configured) { - RTC_GetTime(&hours, &minutes, &seconds, &subSeconds, &period); - day = getDay(); + _alarmMatch = match; switch (match) { case MATCH_OFF: RTC_StopAlarm(); @@ -141,15 +136,12 @@ void STM32RTC::enableAlarm(Alarm_Match match) case MATCH_YYMMDDHHMMSS://kept for compatibility case MATCH_MMDDHHMMSS: //kept for compatibility case MATCH_DHHMMSS: - day = _alarmDay; case MATCH_HHMMSS: - hours = _alarmHours; - period = (_alarmPeriod == RTC_AM)? AM: PM; case MATCH_MMSS: - minutes = _alarmMinutes; case MATCH_SS: - seconds = _alarmSeconds; - RTC_StartAlarm(day, hours, minutes, seconds, subSeconds, period); + RTC_StartAlarm(_alarmDay, _alarmHours, _alarmMinutes, _alarmSeconds, + _alarmSubSeconds, (_alarmPeriod == RTC_AM)? AM: PM, + static_cast(_alarmMatch)); break; default: break; @@ -757,7 +749,7 @@ uint32_t STM32RTC::getY2kEpoch(void) * @brief set RTC alarm from epoch time * @param epoch time in seconds */ -void STM32RTC::setAlarmEpoch(uint32_t ts) +void STM32RTC::setAlarmEpoch(uint32_t ts, Alarm_Match match) { if (_configured) { if (ts < EPOCH_TIME_OFF) { @@ -771,7 +763,7 @@ void STM32RTC::setAlarmEpoch(uint32_t ts) setAlarmHours(tmp->tm_hour); setAlarmMinutes(tmp->tm_min); setAlarmSeconds(tmp->tm_sec); - enableAlarm(MATCH_DHHMMSS); + enableAlarm(match); } } @@ -845,7 +837,23 @@ void STM32RTC::syncAlarmTime(void) { if(_configured) { hourAM_PM_t p = AM; - RTC_GetAlarm(&_alarmDay, &_alarmHours, &_alarmMinutes, &_alarmSeconds, &_alarmSubSeconds, &p); + 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 index 0ddc302..bbed2e1 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -69,17 +69,15 @@ class STM32RTC { enum Alarm_Match: uint8_t { - MATCH_OFF = 0, // Never - MATCH_SS = 1, // Every Minute - MATCH_MMSS = 2, // Every Hour - MATCH_HHMMSS = 3, // Every Day - MATCH_DHHMMSS = 4, // Every Month - + 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 = 5, - MATCH_YYMMDDHHMMSS = 6 - }; + 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 { @@ -166,7 +164,7 @@ class STM32RTC { uint32_t getY2kEpoch(void); void setEpoch(uint32_t ts); void setY2kEpoch(uint32_t ts); - void setAlarmEpoch(uint32_t ts); + void setAlarmEpoch(uint32_t ts, Alarm_Match match = MATCH_DHHMMSS); bool isConfigured(void) { return _configured; @@ -191,6 +189,7 @@ class STM32RTC { uint8_t _alarmSeconds; uint32_t _alarmSubSeconds; RTC_AM_PM _alarmPeriod; + Alarm_Match _alarmMatch; RTC_Source_Clock _clockSource; From f54aca965bdf10c76db6d2bbe25c29cc7a05d64e Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Thu, 19 Apr 2018 17:32:52 +0200 Subject: [PATCH 14/16] Add get/set prediv methods Signed-off-by: Frederic.Pillon --- src/STM32RTC.cpp | 28 ++++++++++++++++++++++++++++ src/STM32RTC.h | 3 +++ 2 files changed, 31 insertions(+) diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index fb038aa..c6f28df 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -117,9 +117,37 @@ 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 diff --git a/src/STM32RTC.h b/src/STM32RTC.h index bbed2e1..929b937 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -166,6 +166,9 @@ class STM32RTC { 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; } From 5fb10e5ba550cdc6a709873048744f6e2056bd20 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Tue, 24 Apr 2018 09:28:08 +0200 Subject: [PATCH 15/16] Update keywords.txt Added: isConfigured getPrediv setPrediv IS_CLOCK_SOURCE IS_HOUR_FORMAT MATCH_OFF MATCH_SS MATCH_MMSS MATCH_HHMMSS MATCH_DHHMMSS MATCH_MMDDHHMMSS MATCH_YYMMDDHHMMSS Updated enum value: RTC_HOUR_12 RTC_HOUR_24 RTC_AM RTC_PM Signed-off-by: Frederic.Pillon --- keywords.txt | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/keywords.txt b/keywords.txt index 68a8368..63ad5b6 100644 --- a/keywords.txt +++ b/keywords.txt @@ -62,14 +62,28 @@ attachInterrupt KEYWORD2 detachInterrupt KEYWORD2 setClockSource KEYWORD2 +isConfigured KEYWORD2 + +getPrediv KEYWORD2 +setPrediv KEYWORD2 + +IS_CLOCK_SOURCE KEYWORD2 +IS_HOUR_FORMAT KEYWORD2 ####################################### # Constants (LITERAL1) ####################################### -HOUR_12 LITERAL1 -HOUR_24 LITERAL1 -HOUR_AM LITERAL1 -HOUR_PM 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 From fdb541d3daf8507c25ecd85afad03c75ffc08208 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Tue, 24 Apr 2018 13:56:01 +0200 Subject: [PATCH 16/16] Update library.properties and README.md Signed-off-by: Frederic.Pillon --- README.md | 6 +++++- library.properties | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5e77d7f..defe1f6 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,11 @@ _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()` +* **`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)`** diff --git a/library.properties b/library.properties index 9c7fd60..346866d 100644 --- a/library.properties +++ b/library.properties @@ -1,8 +1,8 @@ name=STM32duino RTC version=1.0.0 -author=Wi6Labs +author=STMicroelectronics, Wi6Labs maintainer=stm32duino -sentence=Allows to use the RTC functionalities. For STM32 boards. +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