Skip to content

Start alarm by subSeconds: Avoid overflow during conversion from ms to ticks. #113

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 1 commit into from
Nov 27, 2024
Merged
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
11 changes: 4 additions & 7 deletions src/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -947,14 +947,11 @@ void RTC_StartAlarm64(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes,
if ((initMode == MODE_BINARY_ONLY) || (initMode == MODE_BINARY_MIX)) {
/* We have an SubSecond alarm to set in RTC_BINARY_MIX or RTC_BINARY_ONLY mode */
/* The subsecond in ms is converted in ticks unit 1 tick is 1000 / fqce_apre
* It keeps the subsecond accuracy on 64 bits if needed
* For the conversion, we keep the accuracy on 64 bits, since otherwise we might
* have an overflow even though the conversion result still fits in 32 bits.
*/
if (subSeconds > (uint64_t)UINT32_MAX) {
uint64_t tmp = (subSeconds * (uint64_t)(predivSync + 1)) / (uint64_t)1000;
RTC_AlarmStructure.AlarmTime.SubSeconds = (uint32_t)UINT32_MAX - (uint32_t)tmp;
} else {
RTC_AlarmStructure.AlarmTime.SubSeconds = (uint32_t)((uint32_t)UINT32_MAX - (uint32_t)(subSeconds * (predivSync + 1)) / 1000);
}
uint64_t tmp = (subSeconds * (uint64_t)(predivSync + 1)) / (uint64_t)1000;
RTC_AlarmStructure.AlarmTime.SubSeconds = (uint32_t)UINT32_MAX - (uint32_t)tmp;
} else
#endif /* RTC_ICSR_BIN */
{
Expand Down