Skip to content

Commit 86ab4b7

Browse files
authored
Merge pull request #68 from ABOSTM/RTC_ALARM_LOWPOWER
fix: in configForLowPower(), begin() should be call unconditionnally
2 parents b468393 + 033c7eb commit 86ab4b7

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

src/STM32RTC.cpp

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -926,41 +926,43 @@ void STM32RTC::configForLowPower(Source_Clock source)
926926
#ifdef __HAL_RCC_RTCAPB_CLKAM_ENABLE
927927
__HAL_RCC_RTCAPB_CLKAM_ENABLE();
928928
#endif
929-
if (!RTC_IsConfigured()) {
929+
930+
begin();
931+
932+
if (_clockSource != source) {
933+
// Save current config
934+
AM_PM period, alarmPeriod = _alarmPeriod;
935+
uint32_t subSeconds;
936+
uint8_t seconds, minutes, hours, weekDay, day, month, years;
937+
uint8_t alarmSeconds, alarmMinutes, alarmHours, alarmDay;
938+
Alarm_Match alarmMatch = _alarmMatch;
939+
940+
alarmDay = _alarmDay;
941+
alarmHours = _alarmHours;
942+
alarmMinutes = _alarmMinutes;
943+
alarmSeconds = _alarmSeconds;
944+
945+
getDate(&weekDay, &day, &month, &years);
946+
getTime(&seconds, &minutes, &hours, &subSeconds, &period);
947+
948+
end();
930949
_clockSource = source;
931950
// Enable RTC
932-
begin();
933-
} else {
934-
if (_clockSource != source) {
935-
// Save current config
936-
AM_PM period, alarmPeriod = _alarmPeriod;
937-
uint32_t subSeconds;
938-
uint8_t seconds, minutes, hours, weekDay, day, month, years;
939-
uint8_t alarmSeconds, alarmMinutes, alarmHours, alarmDay;
940-
Alarm_Match alarmMatch = _alarmMatch;
941-
942-
alarmDay = _alarmDay;
943-
alarmHours = _alarmHours;
944-
alarmMinutes = _alarmMinutes;
945-
alarmSeconds = _alarmSeconds;
946-
947-
getDate(&weekDay, &day, &month, &years);
948-
getTime(&seconds, &minutes, &hours, &subSeconds, &period);
949-
950-
end();
951-
_clockSource = source;
952-
// Enable RTC
953-
begin(period);
954-
// Restore config
955-
setTime(seconds, minutes, hours, subSeconds, period);
956-
setDate(weekDay, day, month, years);
957-
setAlarmTime(alarmHours, alarmMinutes, alarmSeconds, alarmPeriod);
958-
setAlarmDay(alarmDay);
959-
if (RTC_IsAlarmSet()) {
960-
enableAlarm(alarmMatch);
961-
}
951+
begin(_format);
952+
// Restore config
953+
setTime(seconds, minutes, hours, subSeconds, period);
954+
setDate(weekDay, day, month, years);
955+
setAlarmTime(alarmHours, alarmMinutes, alarmSeconds, alarmPeriod);
956+
setAlarmDay(alarmDay);
957+
if (RTC_IsAlarmSet()) {
958+
enableAlarm(alarmMatch);
962959
}
963960
}
961+
962+
if (!isTimeSet()) {
963+
// Set arbitrary time for Lowpower; if not already set
964+
setTime(12, 0, 0, 0, AM);
965+
}
964966
#endif
965967
}
966968

src/rtc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
355355

356356
if (reset) {
357357
resetBackupDomain();
358+
RTC_initClock(source);
358359
}
359360

360361
#if defined(STM32F1xx)
@@ -402,9 +403,6 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
402403
HAL_RTCEx_EnableBypassShadow(&RtcHandle);
403404
#endif
404405

405-
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO);
406-
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
407-
408406
return reinit;
409407
}
410408

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

655653
/* Set RTC_Alarm */
656654
HAL_RTC_SetAlarm_IT(&RtcHandle, &RTC_AlarmStructure, RTC_FORMAT_BIN);
655+
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, RTC_IRQ_PRIO, RTC_IRQ_SUBPRIO);
656+
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
657657
}
658658
}
659659

0 commit comments

Comments
 (0)