Skip to content

Rtc mix in millisec API is using subsecond expressed in milliseconds #95

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rtc with getTime and getAlarm depends on MIX mode
When the RTC is running in MIX mode (bianry and calendar),
the ssub-second register is a 32-bit value which must not be converted
in ms

Signed-off-by: Francois Ramu <[email protected]>
  • Loading branch information
FRASTM committed Sep 5, 2023
commit 57c9ce74d3479a01001c930e1b5f75fb4bb0616c
29 changes: 18 additions & 11 deletions src/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,14 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
// In case of RTC source clock change, Backup Domain is reset by RTC_initClock()
// Save current config before call to RTC_initClock()
RTC_GetDate(&years, &month, &days, &weekDay);
RTC_GetTime(mode, &hours, &minutes, &seconds, &subSeconds, &period);
RTC_GetTime(&hours, &minutes, &seconds, &subSeconds, &period);

if (isAlarmASet) {
RTC_GetAlarm(mode, ALARM_A, &alarmDay, &alarmHours, &alarmMinutes, &alarmSeconds, &alarmSubseconds, &alarmPeriod, &alarmMask);
RTC_GetAlarm(ALARM_A, &alarmDay, &alarmHours, &alarmMinutes, &alarmSeconds, &alarmSubseconds, &alarmPeriod, &alarmMask);
}
#ifdef RTC_ALARM_B
if (isAlarmBSet) {
RTC_GetAlarm(mode, ALARM_B, &alarmBDay, &alarmBHours, &alarmBMinutes, &alarmBSeconds, &alarmBSubseconds, &alarmBPeriod, &alarmBMask);
RTC_GetAlarm(ALARM_B, &alarmBDay, &alarmBHours, &alarmBMinutes, &alarmBSeconds, &alarmBSubseconds, &alarmBPeriod, &alarmBMask);
}
#endif
// Init RTC clock
Expand Down Expand Up @@ -583,14 +584,14 @@ bool RTC_IsConfigured(void)
* @param hours: 0-12 or 0-23. Depends on the format used.
* @param minutes: 0-59
* @param seconds: 0-59
* @param subSeconds: 0-999
* @param subSeconds: 0-999 (not used)
* @param period: select HOUR_AM or HOUR_PM period in case RTC is set in 12 hours mode. Else ignored.
* @retval None
*/
void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, hourAM_PM_t period)
{
RTC_TimeTypeDef RTC_TimeStruct;
UNUSED(subSeconds);
UNUSED(subSeconds); /* not used (read-only register) */
/* Ignore time AM PM configuration if in 24 hours format */
if (initFormat == HOUR_FORMAT_24) {
period = HOUR_AM;
Expand Down Expand Up @@ -656,7 +657,11 @@ void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *s
}
#if defined(RTC_SSR_SS)
if (subSeconds != NULL) {
*subSeconds = ((predivSync - RTC_TimeStruct.SubSeconds) * 1000) / (predivSync + 1);
if (initMode == MODE_BINARY_MIX) {
*subSeconds = UINT32_MAX - RTC_TimeStruct.SubSeconds;
} else {
*subSeconds = ((predivSync - RTC_TimeStruct.SubSeconds) * 1000) / (predivSync + 1);
}
}
#else
UNUSED(subSeconds);
Expand Down Expand Up @@ -811,21 +816,19 @@ void RTC_StartAlarm(alarm_t name, uint8_t day, uint8_t hours, uint8_t minutes, u
#if defined(RTC_ALRMASSR_SSCLR)
RTC_AlarmStructure.BinaryAutoClr = RTC_ALARMSUBSECONDBIN_AUTOCLR_NO;
#endif /* RTC_ALRMASSR_SSCLR */
RTC_AlarmStructure.AlarmMask = RTC_ALARMMASK_NONE;
RTC_AlarmStructure.AlarmMask = RTC_ALARMMASK_ALL;

#ifdef RTC_ALARM_B
if (name == ALARM_B) {
/* Expecting RTC_ALARMSUBSECONDBINMASK_NONE for the subsecond mask on ALARM B */
RTC_AlarmStructure.AlarmMask = RTC_ALARMMASK_ALL;
RTC_AlarmStructure.AlarmSubSecondMask = mask << RTC_ALRMBSSR_MASKSS_Pos;
} else
#endif
{
/* Expecting RTC_ALARMSUBSECONDBINMASK_NONE for the subsecond mask on ALARM A */
RTC_AlarmStructure.AlarmMask = RTC_ALARMMASK_ALL;
RTC_AlarmStructure.AlarmSubSecondMask = mask << RTC_ALRMASSR_MASKSS_Pos;
}
/* Special case for ALARM B configuration for stm32WL Lorawan */
/* Special case for ALARM B configuration when using subsecond reg. in RTC Mix mode */
RTC_AlarmStructure.AlarmTime.SubSeconds = subSeconds;
}

Expand Down Expand Up @@ -922,7 +925,11 @@ void RTC_GetAlarm(alarm_t name, uint8_t *day, uint8_t *hours, uint8_t *minutes,
}
#if defined(RTC_SSR_SS)
if (subSeconds != NULL) {
*subSeconds = ((predivSync - RTC_AlarmStructure.AlarmTime.SubSeconds) * 1000) / (predivSync + 1);
if (initMode == MODE_BINARY_MIX) {
*subSeconds = UINT32_MAX - RTC_AlarmStructure.AlarmTime.SubSeconds;
} else {
*subSeconds = ((predivSync - RTC_AlarmStructure.AlarmTime.SubSeconds) * 1000) / (predivSync + 1);
}
}
#else
UNUSED(subSeconds);
Expand Down
3 changes: 2 additions & 1 deletion src/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ typedef enum {
are kept for compatibility but are ignored inside enableAlarm(). */
M_MSK = 16,
Y_MSK = 32,
SUBSEC_MSK = 64
SUBSEC_MSK = 48,
ALL_MSK = 0xFF
} alarmMask_t;

typedef enum {
Expand Down