Skip to content

fix: in configForLowPower(), begin() should be call unconditionnally #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 33 additions & 31 deletions src/STM32RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,41 +926,43 @@ void STM32RTC::configForLowPower(Source_Clock source)
#ifdef __HAL_RCC_RTCAPB_CLKAM_ENABLE
__HAL_RCC_RTCAPB_CLKAM_ENABLE();
#endif
if (!RTC_IsConfigured()) {

begin();

if (_clockSource != source) {
// Save current config
AM_PM period, alarmPeriod = _alarmPeriod;
uint32_t subSeconds;
uint8_t seconds, minutes, hours, weekDay, day, month, years;
uint8_t alarmSeconds, alarmMinutes, alarmHours, alarmDay;
Alarm_Match alarmMatch = _alarmMatch;

alarmDay = _alarmDay;
alarmHours = _alarmHours;
alarmMinutes = _alarmMinutes;
alarmSeconds = _alarmSeconds;

getDate(&weekDay, &day, &month, &years);
getTime(&seconds, &minutes, &hours, &subSeconds, &period);

end();
_clockSource = source;
// Enable RTC
begin();
} else {
if (_clockSource != source) {
// Save current config
AM_PM period, alarmPeriod = _alarmPeriod;
uint32_t subSeconds;
uint8_t seconds, minutes, hours, weekDay, day, month, years;
uint8_t alarmSeconds, alarmMinutes, alarmHours, alarmDay;
Alarm_Match alarmMatch = _alarmMatch;

alarmDay = _alarmDay;
alarmHours = _alarmHours;
alarmMinutes = _alarmMinutes;
alarmSeconds = _alarmSeconds;

getDate(&weekDay, &day, &month, &years);
getTime(&seconds, &minutes, &hours, &subSeconds, &period);

end();
_clockSource = source;
// Enable RTC
begin(period);
// Restore config
setTime(seconds, minutes, hours, subSeconds, period);
setDate(weekDay, day, month, years);
setAlarmTime(alarmHours, alarmMinutes, alarmSeconds, alarmPeriod);
setAlarmDay(alarmDay);
if (RTC_IsAlarmSet()) {
enableAlarm(alarmMatch);
}
begin(_format);
// Restore config
setTime(seconds, minutes, hours, subSeconds, period);
setDate(weekDay, day, month, years);
setAlarmTime(alarmHours, alarmMinutes, alarmSeconds, alarmPeriod);
setAlarmDay(alarmDay);
if (RTC_IsAlarmSet()) {
enableAlarm(alarmMatch);
}
}

if (!isTimeSet()) {
// Set arbitrary time for Lowpower; if not already set
setTime(12, 0, 0, 0, AM);
}
#endif
}

Expand Down
6 changes: 3 additions & 3 deletions src/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)

if (reset) {
resetBackupDomain();
RTC_initClock(source);
}

#if defined(STM32F1xx)
Expand Down Expand Up @@ -402,9 +403,6 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
HAL_RTCEx_EnableBypassShadow(&RtcHandle);
#endif

HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO);
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);

return reinit;
}

Expand Down Expand Up @@ -654,6 +652,8 @@ void RTC_StartAlarm(uint8_t day, uint8_t hours, uint8_t minutes, uint8_t seconds

/* Set RTC_Alarm */
HAL_RTC_SetAlarm_IT(&RtcHandle, &RTC_AlarmStructure, RTC_FORMAT_BIN);
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO);
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
}
}

Expand Down