Skip to content

Verification that prescaler values are configured should be added #71

Closed
@jan019

Description

@jan019

RTC should contain an assertion checking if the prescaler values are configured or not. Or check if configured and if not, call the function to compute the values.

This portion of code

STM32RTC/src/rtc.c

Lines 382 to 398 in 95b7514

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;
#endif /* RTC_OUTPUT_REMAP_NONE */
RTC_getPrediv((int8_t *) & (RtcHandle.Init.AsynchPrediv), (int16_t *) & (RtcHandle.Init.SynchPrediv));
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;
}
is executed only when the device was previously unpowered -> RTC is not initialized. For the case when RTC is not initialized the function
RTC_getPrediv((int8_t *) & (RtcHandle.Init.AsynchPrediv), (int16_t *) & (RtcHandle.Init.SynchPrediv));
is called and prescaler values are computed.

In case only a soft reset is performed, the RTC remains initialized, the above mentioned code is not executed and prescaler values are not calculated. Therefore they remain set to -1. Then when setting and RTC alarm with subseconds enabled, the following issue occurs:

This leads to reserved bits in the register being written to 1 and generally incorrect behaviour. This is not easy to debug and just a note in the readme that the function setPrediv() must be called before begin() can easly be missed.

The examples also don't call this function, which makes the issue even more hidden. Maybe some boards ensure that invalid values are not written to registers and this issue is not observed on them.

Metadata

Metadata

Assignees

Labels

bug 🐛Something isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions