diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index 4e33ef7..82817b3 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -42,43 +42,40 @@ #define EPOCH_TIME_YEAR_OFF 100 // years since 1900 // Initialize static variable -bool STM32RTC::_configured = false; -bool STM32RTC::_reset = false; +bool STM32RTC::_timeSet = false; /** * @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, Hour_Format format) +void STM32RTC::begin(Hour_Format format) { - _reset = resetTime; - if (resetTime == true) { - _configured = false; - _alarmEnabled = false; - } - begin(format); + begin(false, format); } /** * @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(Hour_Format format) +void STM32RTC::begin(bool resetTime, Hour_Format format) { - if (_configured == false) { - _format = format; - RTC_init((format == HOUR_12) ? HOUR_FORMAT_12 : HOUR_FORMAT_24, - (_clockSource == LSE_CLOCK) ? ::LSE_CLOCK : - (_clockSource == HSE_CLOCK) ? ::HSE_CLOCK : ::LSI_CLOCK -#if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x01050000) - , _reset -#endif - ); - // Must be set before call of sync methods - _configured = true; + bool reinit; + + if (resetTime == true) { + _timeSet = false; + } + + _format = format; + reinit = RTC_init((format == HOUR_12) ? HOUR_FORMAT_12 : HOUR_FORMAT_24, + (_clockSource == LSE_CLOCK) ? ::LSE_CLOCK : + (_clockSource == HSE_CLOCK) ? ::HSE_CLOCK : ::LSI_CLOCK + , resetTime); + + if (reinit == true) { + _timeSet = false; syncTime(); syncDate(); // Use current time to init alarm members @@ -89,9 +86,7 @@ void STM32RTC::begin(Hour_Format format) _alarmSubSeconds = _subSeconds; _alarmPeriod = _hoursPeriod; } else { - syncTime(); - syncDate(); - syncAlarmTime(); + _timeSet = true; } } @@ -102,11 +97,8 @@ void STM32RTC::begin(Hour_Format format) */ void STM32RTC::end(void) { - if (_configured == true) { - RTC_DeInit(); - _configured = false; - _alarmEnabled = false; - } + RTC_DeInit(); + _timeSet = false; } /** @@ -195,26 +187,23 @@ void STM32RTC::setPrediv(int8_t predivA, int16_t predivS) */ void STM32RTC::enableAlarm(Alarm_Match match) { - if (_configured) { - _alarmMatch = match; - switch (match) { - case MATCH_OFF: - RTC_StopAlarm(); - break; - case MATCH_YYMMDDHHMMSS://kept for compatibility - case MATCH_MMDDHHMMSS: //kept for compatibility - case MATCH_DHHMMSS: - case MATCH_HHMMSS: - case MATCH_MMSS: - case MATCH_SS: - RTC_StartAlarm(_alarmDay, _alarmHours, _alarmMinutes, _alarmSeconds, - _alarmSubSeconds, (_alarmPeriod == AM) ? HOUR_AM : HOUR_PM, - static_cast(_alarmMatch)); - _alarmEnabled = true; - break; - default: - break; - } + _alarmMatch = match; + switch (match) { + case MATCH_OFF: + RTC_StopAlarm(); + break; + case MATCH_YYMMDDHHMMSS://kept for compatibility + case MATCH_MMDDHHMMSS: //kept for compatibility + case MATCH_DHHMMSS: + case MATCH_HHMMSS: + case MATCH_MMSS: + case MATCH_SS: + RTC_StartAlarm(_alarmDay, _alarmHours, _alarmMinutes, _alarmSeconds, + _alarmSubSeconds, (_alarmPeriod == AM) ? HOUR_AM : HOUR_PM, + static_cast(_alarmMatch)); + break; + default: + break; } } @@ -224,10 +213,7 @@ void STM32RTC::enableAlarm(Alarm_Match match) */ void STM32RTC::disableAlarm(void) { - if (_configured) { - RTC_StopAlarm(); - _alarmEnabled = false; - } + RTC_StopAlarm(); } /** @@ -508,13 +494,12 @@ uint8_t STM32RTC::getAlarmYear(void) */ void STM32RTC::setSubSeconds(uint32_t subSeconds) { - if (_configured) { - syncTime(); - if (subSeconds < 1000) { - _subSeconds = subSeconds; - } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == AM) ? HOUR_AM : HOUR_PM); + syncTime(); + if (subSeconds < 1000) { + _subSeconds = subSeconds; } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == AM) ? HOUR_AM : HOUR_PM); + _timeSet = true; } /** @@ -524,13 +509,12 @@ void STM32RTC::setSubSeconds(uint32_t subSeconds) */ void STM32RTC::setSeconds(uint8_t seconds) { - if (_configured) { - syncTime(); - if (seconds < 60) { - _seconds = seconds; - } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == AM) ? HOUR_AM : HOUR_PM); + syncTime(); + if (seconds < 60) { + _seconds = seconds; } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == AM) ? HOUR_AM : HOUR_PM); + _timeSet = true; } /** @@ -540,13 +524,12 @@ void STM32RTC::setSeconds(uint8_t seconds) */ void STM32RTC::setMinutes(uint8_t minutes) { - if (_configured) { - syncTime(); - if (minutes < 60) { - _minutes = minutes; - } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == AM) ? HOUR_AM : HOUR_PM); + syncTime(); + if (minutes < 60) { + _minutes = minutes; } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == AM) ? HOUR_AM : HOUR_PM); + _timeSet = true; } /** @@ -557,16 +540,15 @@ void STM32RTC::setMinutes(uint8_t minutes) */ void STM32RTC::setHours(uint8_t hours, AM_PM period) { - if (_configured) { - syncTime(); - if (hours < 24) { - _hours = hours; - } - if (_format == HOUR_12) { - _hoursPeriod = period; - } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == AM) ? HOUR_AM : HOUR_PM); + syncTime(); + if (hours < 24) { + _hours = hours; } + if (_format == HOUR_12) { + _hoursPeriod = period; + } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == AM) ? HOUR_AM : HOUR_PM); + _timeSet = true; } /** @@ -580,25 +562,24 @@ void STM32RTC::setHours(uint8_t hours, AM_PM period) */ void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, AM_PM period) { - if (_configured) { - syncTime(); - if (subSeconds < 1000) { - _subSeconds = subSeconds; - } - if (seconds < 60) { - _seconds = seconds; - } - if (minutes < 60) { - _minutes = minutes; - } - if (hours < 24) { - _hours = hours; - } - if (_format == HOUR_12) { - _hoursPeriod = period; - } - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == AM) ? HOUR_AM : HOUR_PM); + syncTime(); + if (subSeconds < 1000) { + _subSeconds = subSeconds; } + if (seconds < 60) { + _seconds = seconds; + } + if (minutes < 60) { + _minutes = minutes; + } + if (hours < 24) { + _hours = hours; + } + if (_format == HOUR_12) { + _hoursPeriod = period; + } + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == AM) ? HOUR_AM : HOUR_PM); + _timeSet = true; } /** @@ -608,13 +589,12 @@ void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t */ void STM32RTC::setWeekDay(uint8_t weekDay) { - if (_configured) { - syncDate(); - if ((weekDay >= 1) && (weekDay <= 7)) { - _wday = weekDay; - } - RTC_SetDate(_year, _month, _day, _wday); + syncDate(); + if ((weekDay >= 1) && (weekDay <= 7)) { + _wday = weekDay; } + RTC_SetDate(_year, _month, _day, _wday); + _timeSet = true; } /** @@ -624,13 +604,12 @@ void STM32RTC::setWeekDay(uint8_t weekDay) */ void STM32RTC::setDay(uint8_t day) { - if (_configured) { - syncDate(); - if ((day >= 1) && (day <= 31)) { - _day = day; - } - RTC_SetDate(_year, _month, _day, _wday); + syncDate(); + if ((day >= 1) && (day <= 31)) { + _day = day; } + RTC_SetDate(_year, _month, _day, _wday); + _timeSet = true; } /** @@ -640,13 +619,12 @@ void STM32RTC::setDay(uint8_t day) */ void STM32RTC::setMonth(uint8_t month) { - if (_configured) { - syncDate(); - if ((month >= 1) && (month <= 12)) { - _month = month; - } - RTC_SetDate(_year, _month, _day, _wday); + syncDate(); + if ((month >= 1) && (month <= 12)) { + _month = month; } + RTC_SetDate(_year, _month, _day, _wday); + _timeSet = true; } /** @@ -656,13 +634,12 @@ void STM32RTC::setMonth(uint8_t month) */ void STM32RTC::setYear(uint8_t year) { - if (_configured) { - syncDate(); - if (year < 100) { - _year = year; - } - RTC_SetDate(_year, _month, _day, _wday); + syncDate(); + if (year < 100) { + _year = year; } + RTC_SetDate(_year, _month, _day, _wday); + _timeSet = true; } /** @@ -674,19 +651,18 @@ void STM32RTC::setYear(uint8_t year) */ void STM32RTC::setDate(uint8_t day, uint8_t month, uint8_t year) { - if (_configured) { - syncDate(); - if ((day >= 1) && (day <= 31)) { - _day = day; - } - if ((month >= 1) && (month <= 12)) { - _month = month; - } - if (year < 100) { - _year = year; - } - RTC_SetDate(_year, _month, _day, _wday); + syncDate(); + if ((day >= 1) && (day <= 31)) { + _day = day; } + if ((month >= 1) && (month <= 12)) { + _month = month; + } + if (year < 100) { + _year = year; + } + RTC_SetDate(_year, _month, _day, _wday); + _timeSet = true; } /** @@ -699,22 +675,21 @@ void STM32RTC::setDate(uint8_t day, uint8_t month, uint8_t year) */ void STM32RTC::setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year) { - if (_configured) { - syncDate(); - if ((weekDay >= 1) && (weekDay <= 7)) { - _wday = weekDay; - } - if ((day >= 1) && (day <= 31)) { - _day = day; - } - if ((month >= 1) && (month <= 12)) { - _month = month; - } - if (year < 100) { - _year = year; - } - RTC_SetDate(_year, _month, _day, _wday); + syncDate(); + if ((weekDay >= 1) && (weekDay <= 7)) { + _wday = weekDay; + } + if ((day >= 1) && (day <= 31)) { + _day = day; } + if ((month >= 1) && (month <= 12)) { + _month = month; + } + if (year < 100) { + _year = year; + } + RTC_SetDate(_year, _month, _day, _wday); + _timeSet = true; } /** @@ -724,10 +699,8 @@ void STM32RTC::setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year */ void STM32RTC::setAlarmSubSeconds(uint32_t subSeconds) { - if (_configured) { - if (subSeconds < 1000) { - _alarmSubSeconds = subSeconds; - } + if (subSeconds < 1000) { + _alarmSubSeconds = subSeconds; } } @@ -738,10 +711,8 @@ void STM32RTC::setAlarmSubSeconds(uint32_t subSeconds) */ void STM32RTC::setAlarmSeconds(uint8_t seconds) { - if (_configured) { - if (seconds < 60) { - _alarmSeconds = seconds; - } + if (seconds < 60) { + _alarmSeconds = seconds; } } @@ -752,10 +723,8 @@ void STM32RTC::setAlarmSeconds(uint8_t seconds) */ void STM32RTC::setAlarmMinutes(uint8_t minutes) { - if (_configured) { - if (minutes < 60) { - _alarmMinutes = minutes; - } + if (minutes < 60) { + _alarmMinutes = minutes; } } @@ -767,13 +736,11 @@ void STM32RTC::setAlarmMinutes(uint8_t minutes) */ void STM32RTC::setAlarmHours(uint8_t hours, AM_PM period) { - if (_configured) { - if (hours < 24) { - _alarmHours = hours; - } - if (_format == HOUR_12) { - _alarmPeriod = period; - } + if (hours < 24) { + _alarmHours = hours; + } + if (_format == HOUR_12) { + _alarmPeriod = period; } } @@ -788,12 +755,10 @@ void STM32RTC::setAlarmHours(uint8_t hours, AM_PM period) */ void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, AM_PM period) { - if (_configured) { - setAlarmHours(hours, period); - setAlarmMinutes(minutes); - setAlarmSeconds(seconds); - setAlarmSubSeconds(subSeconds); - } + setAlarmHours(hours, period); + setAlarmMinutes(minutes); + setAlarmSeconds(seconds); + setAlarmSubSeconds(subSeconds); } /** @@ -803,10 +768,8 @@ void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uin */ void STM32RTC::setAlarmDay(uint8_t day) { - if (_configured) { - if ((day >= 1) && (day <= 31)) { - _alarmDay = day; - } + if ((day >= 1) && (day <= 31)) { + _alarmDay = day; } } @@ -897,21 +860,19 @@ uint32_t STM32RTC::getY2kEpoch(void) */ void STM32RTC::setAlarmEpoch(uint32_t ts, Alarm_Match match, uint32_t subSeconds) { - if (_configured) { - if (ts < EPOCH_TIME_OFF) { - ts = EPOCH_TIME_OFF; - } + if (ts < EPOCH_TIME_OFF) { + ts = EPOCH_TIME_OFF; + } - time_t t = ts; - struct tm *tmp = gmtime(&t); + 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); - setAlarmSubSeconds(subSeconds); - enableAlarm(match); - } + setAlarmDay(tmp->tm_mday); + setAlarmHours(tmp->tm_hour); + setAlarmMinutes(tmp->tm_min); + setAlarmSeconds(tmp->tm_sec); + setAlarmSubSeconds(subSeconds); + enableAlarm(match); } /** @@ -921,30 +882,29 @@ void STM32RTC::setAlarmEpoch(uint32_t ts, Alarm_Match match, uint32_t subSeconds */ void STM32RTC::setEpoch(uint32_t ts, uint32_t subSeconds) { - if (_configured) { - if (ts < EPOCH_TIME_OFF) { - ts = EPOCH_TIME_OFF; - } - - time_t t = ts; - struct tm *tmp = gmtime(&t); + if (ts < EPOCH_TIME_OFF) { + ts = EPOCH_TIME_OFF; + } - _year = tmp->tm_year - EPOCH_TIME_YEAR_OFF; - _month = tmp->tm_mon + 1; - _day = tmp->tm_mday; - if (tmp->tm_wday == 0) { - _wday = RTC_WEEKDAY_SUNDAY; - } else { - _wday = tmp->tm_wday; - } - _hours = tmp->tm_hour; - _minutes = tmp->tm_min; - _seconds = tmp->tm_sec; - _subSeconds = subSeconds; + time_t t = ts; + struct tm *tmp = gmtime(&t); - RTC_SetDate(_year, _month, _day, _wday); - RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == AM) ? HOUR_AM : HOUR_PM); + _year = tmp->tm_year - EPOCH_TIME_YEAR_OFF; + _month = tmp->tm_mon + 1; + _day = tmp->tm_mday; + if (tmp->tm_wday == 0) { + _wday = RTC_WEEKDAY_SUNDAY; + } else { + _wday = tmp->tm_wday; } + _hours = tmp->tm_hour; + _minutes = tmp->tm_min; + _seconds = tmp->tm_sec; + _subSeconds = subSeconds; + + RTC_SetDate(_year, _month, _day, _wday); + RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == AM) ? HOUR_AM : HOUR_PM); + _timeSet = true; } /** @@ -953,9 +913,7 @@ void STM32RTC::setEpoch(uint32_t ts, uint32_t subSeconds) */ void STM32RTC::setY2kEpoch(uint32_t ts) { - if (_configured) { - setEpoch(ts + EPOCH_TIME_OFF); - } + setEpoch(ts + EPOCH_TIME_OFF); } /** @@ -968,7 +926,7 @@ void STM32RTC::configForLowPower(Source_Clock source) #ifdef __HAL_RCC_RTCAPB_CLKAM_ENABLE __HAL_RCC_RTCAPB_CLKAM_ENABLE(); #endif - if (!_configured) { + if (!RTC_IsConfigured()) { _clockSource = source; // Enable RTC begin(); @@ -980,7 +938,6 @@ void STM32RTC::configForLowPower(Source_Clock source) uint8_t seconds, minutes, hours, weekDay, day, month, years; uint8_t alarmSeconds, alarmMinutes, alarmHours, alarmDay; Alarm_Match alarmMatch = _alarmMatch; - bool alarmEnabled = _alarmEnabled; alarmDay = _alarmDay; alarmHours = _alarmHours; @@ -999,7 +956,7 @@ void STM32RTC::configForLowPower(Source_Clock source) setDate(weekDay, day, month, years); setAlarmTime(alarmHours, alarmMinutes, alarmSeconds, alarmPeriod); setAlarmDay(alarmDay); - if (alarmEnabled) { + if (RTC_IsAlarmSet()) { enableAlarm(alarmMatch); } } @@ -1013,11 +970,9 @@ void STM32RTC::configForLowPower(Source_Clock source) */ void STM32RTC::syncTime(void) { - if (_configured) { - hourAM_PM_t p = HOUR_AM; - RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); - _hoursPeriod = (p == HOUR_AM) ? AM : PM; - } + hourAM_PM_t p = HOUR_AM; + RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p); + _hoursPeriod = (p == HOUR_AM) ? AM : PM; } /** @@ -1026,9 +981,7 @@ void STM32RTC::syncTime(void) */ void STM32RTC::syncDate(void) { - if (_configured) { - RTC_GetDate(&_year, &_month, &_day, &_wday); - } + RTC_GetDate(&_year, &_month, &_day, &_wday); } /** @@ -1037,25 +990,23 @@ void STM32RTC::syncDate(void) */ void STM32RTC::syncAlarmTime(void) { - if (_configured) { - hourAM_PM_t p = HOUR_AM; - uint8_t match; - RTC_GetAlarm(&_alarmDay, &_alarmHours, &_alarmMinutes, &_alarmSeconds, - &_alarmSubSeconds, &p, &match); - _alarmPeriod = (p == HOUR_AM) ? AM : 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; - } + hourAM_PM_t p = HOUR_AM; + uint8_t match; + RTC_GetAlarm(&_alarmDay, &_alarmHours, &_alarmMinutes, &_alarmSeconds, + &_alarmSubSeconds, &p, &match); + _alarmPeriod = (p == HOUR_AM) ? AM : 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 02602d9..c61321a 100644 --- a/src/STM32RTC.h +++ b/src/STM32RTC.h @@ -204,16 +204,16 @@ class STM32RTC { #endif /* STM32F1xx */ bool isConfigured(void) { - return _configured; + return RTC_IsConfigured(); } bool isAlarmEnabled(void) { - return _alarmEnabled; + return RTC_IsAlarmSet(); } bool isTimeSet(void) { #if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x01050000) - return RTC_IsTimeSet(); + return _timeSet; #else return false; #endif @@ -224,8 +224,7 @@ class STM32RTC { private: STM32RTC(void): _clockSource(LSI_CLOCK) {} - static bool _configured; - static bool _reset; + static bool _timeSet; Hour_Format _format; AM_PM _hoursPeriod; @@ -245,7 +244,6 @@ class STM32RTC { uint32_t _alarmSubSeconds; AM_PM _alarmPeriod; Alarm_Match _alarmMatch; - bool _alarmEnabled; Source_Clock _clockSource; diff --git a/src/rtc.c b/src/rtc.c index 8c6f3c1..d1ac970 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -35,6 +35,7 @@ */ #include "rtc.h" +#include "stm32yyxx_ll_rtc.h" #include #if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x01090000) &&\ @@ -331,64 +332,69 @@ static void RTC_computePrediv(int8_t *asynch, int16_t *synch) /** * @brief RTC Initialization * This function configures the RTC time and calendar. By default, the - * RTC is set to the 1st January 2017 0:0:0:00 + * RTC is set to the 1st January 2001 + * Note: year 2000 is invalid as it is the hardware reset value and doesn't raise INITS flag * @param format: enable the RTC in 12 or 24 hours mode - * @retval None + * @param source: RTC clock source: LSE, LSI or HSE + * @param reset: force RTC reset, even if previously configured + * @retval True if RTC is reinitialized, else false */ -void RTC_init(hourFormat_t format, sourceClock_t source, bool reset) +bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset) { - initFormat = format; + bool reinit = false; - if (reset) { - resetBackupDomain(); - } + initFormat = format; /* Init RTC clock */ RTC_initClock(source); RtcHandle.Instance = RTC; + /* Ensure backup domain is enabled before we init the RTC so we can use the backup registers for date retention on stm32f1xx boards */ + enableBackupDomain(); + + if (reset) { + resetBackupDomain(); + } + #if defined(STM32F1xx) - /* Let HAL calculate the prescaler */ - RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND; - RtcHandle.Init.OutPut = RTC_OUTPUTSOURCE_NONE; - UNUSED(format); -#else - if (format == HOUR_FORMAT_12) { - RtcHandle.Init.HourFormat = RTC_HOURFORMAT_12; + uint32_t BackupDate; + BackupDate = getBackupRegister(RTC_BKP_DATE) << 16; + BackupDate |= getBackupRegister(RTC_BKP_DATE + 1) & 0xFFFF; + if ((BackupDate == 0) || reset) { + /* Let HAL calculate the prescaler */ + RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND; + RtcHandle.Init.OutPut = RTC_OUTPUTSOURCE_NONE; + HAL_RTC_Init(&RtcHandle); + // Default: saturday 1st of January 2001 + // Note: year 2000 is invalid as it is the hardware reset value and doesn't raise INITS flag + RTC_SetDate(1, 1, 1, 6); + reinit = true; } else { - RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; + memcpy(&RtcHandle.DateToUpdate, &BackupDate, 4); + /* and fill the new RTC Date value */ + RTC_SetDate(RtcHandle.DateToUpdate.Year, RtcHandle.DateToUpdate.Month, + RtcHandle.DateToUpdate.Date, RtcHandle.DateToUpdate.WeekDay); } - RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; - RTC_getPrediv((int8_t *) & (RtcHandle.Init.AsynchPrediv), (int16_t *) & (RtcHandle.Init.SynchPrediv)); +#else + + if (!LL_RTC_IsActiveFlag_INITS(RtcHandle.Instance) || reset) { + RtcHandle.Init.HourFormat = format == HOUR_FORMAT_12 ? RTC_HOURFORMAT_12 : RTC_HOURFORMAT_24; + RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; + RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; + RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; #if defined(RTC_OUTPUT_REMAP_NONE) - RtcHandle.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE; + RtcHandle.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE; #endif /* RTC_OUTPUT_REMAP_NONE */ - RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; - RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; -#endif /* STM32F1xx */ - /* Ensure backup domain is enabled before we init the RTC so we can use the backup registers for date retention on stm32f1xx baords */ - enableBackupDomain(); + RTC_getPrediv((int8_t *) & (RtcHandle.Init.AsynchPrediv), (int16_t *) & (RtcHandle.Init.SynchPrediv)); - HAL_RTC_Init(&RtcHandle); - -#if defined(STM32F1xx) - // Copy RTC date back out of the BackUp registers - uint32_t BackupDate; - /* date from backup battery was not reset, load it */ - if (!reset) { - BackupDate = getBackupRegister(RTC_BKP_DATE) << 16; - BackupDate |= getBackupRegister(RTC_BKP_DATE + 1) & 0xFFFF; - if (BackupDate != 0) { - /* cannot force the date to be 0 but 1/1/2000 is the reset value, always */ - memcpy(&RtcHandle.DateToUpdate, &BackupDate, 4); - /* and fill the new RTC Date value */ - RTC_SetDate(RtcHandle.DateToUpdate.Year, RtcHandle.DateToUpdate.Month, - RtcHandle.DateToUpdate.Date, RtcHandle.DateToUpdate.WeekDay); - } + HAL_RTC_Init(&RtcHandle); + // Default: saturday 1st of January 2001 + // Note: year 2000 is invalid as it is the hardware reset value and doesn't raise INITS flag + RTC_SetDate(1, 1, 1, 6); + reinit = true; } - RTC_StoreDate(); #endif /* STM32F1xx */ #if defined(RTC_CR_BYPSHAD) @@ -398,6 +404,8 @@ void RTC_init(hourFormat_t format, sourceClock_t source, bool reset) HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO); HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); + + return reinit; } /** @@ -416,9 +424,16 @@ void RTC_DeInit(void) * @brief Check if time is already set * @retval True if set else false */ -bool RTC_IsTimeSet(void) +bool RTC_IsConfigured(void) { - return (getBackupRegister(RTC_BKP_INDEX) == RTC_BKP_VALUE) ? true : false; +#if defined(STM32F1xx) + uint32_t BackupDate; + BackupDate = getBackupRegister(RTC_BKP_DATE) << 16; + BackupDate |= getBackupRegister(RTC_BKP_DATE + 1) & 0xFFFF; + return (BackupDate != 0); +#else + return LL_RTC_IsActiveFlag_INITS(RtcHandle.Instance); +#endif } /** @@ -462,7 +477,6 @@ void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSe #endif /* !STM32F1xx */ HAL_RTC_SetTime(&RtcHandle, &RTC_TimeStruct, RTC_FORMAT_BIN); - setBackupRegister(RTC_BKP_INDEX, RTC_BKP_VALUE); } } @@ -534,7 +548,6 @@ void RTC_SetDate(uint8_t year, uint8_t month, uint8_t day, uint8_t wday) RTC_DateStruct.Date = day; RTC_DateStruct.WeekDay = wday; HAL_RTC_SetDate(&RtcHandle, &RTC_DateStruct, RTC_FORMAT_BIN); - setBackupRegister(RTC_BKP_INDEX, RTC_BKP_VALUE); #if defined(STM32F1xx) RTC_StoreDate(); #endif /* STM32F1xx */ @@ -658,6 +671,20 @@ void RTC_StopAlarm(void) HAL_RTC_DeactivateAlarm(&RtcHandle, RTC_ALARM_A); } +/** + * @brief Check whether RTC alarm is set + * @param None + * @retval True if Alarm is set + */ +bool RTC_IsAlarmSet(void) +{ +#if defined(STM32F1xx) + return LL_RTC_IsEnabledIT_ALR(RtcHandle.Instance); +#else + return LL_RTC_IsEnabledIT_ALRA(RtcHandle.Instance); +#endif +} + /** * @brief Get RTC alarm * @param day: 1-31 day of the month (optional could be NULL) diff --git a/src/rtc.h b/src/rtc.h index 79e7a15..a1789e6 100644 --- a/src/rtc.h +++ b/src/rtc.h @@ -178,9 +178,9 @@ void RTC_getPrediv(int8_t *asynch, int16_t *synch); void RTC_setPrediv(int8_t asynch, int16_t synch); #endif /* STM32F1xx */ -void RTC_init(hourFormat_t format, sourceClock_t source, bool reset); +bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset); void RTC_DeInit(void); -bool RTC_IsTimeSet(void); +bool RTC_IsConfigured(void); void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, hourAM_PM_t period); void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *subSeconds, hourAM_PM_t *period); @@ -190,6 +190,7 @@ void RTC_GetDate(uint8_t *year, uint8_t *month, uint8_t *day, uint8_t *wday); void RTC_StartAlarm(uint8_t day, uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, hourAM_PM_t period, uint8_t mask); void RTC_StopAlarm(void); +bool RTC_IsAlarmSet(void); void RTC_GetAlarm(uint8_t *day, uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *subSeconds, hourAM_PM_t *period, uint8_t *mask); void attachAlarmCallback(voidCallbackPtr func, void *data); void detachAlarmCallback(void);