From d84d83028f059772ed604b19eed12f379a2f7d82 Mon Sep 17 00:00:00 2001 From: Francois RAMU Date: Mon, 26 Feb 2024 15:57:48 +0100 Subject: [PATCH] stm32RTC change the binary Mode in case the RTC is already init RTC is already initialized, but RTC BIN mode must be changed Change the RTC_BinaryConf function to update the RTC ICSR register on BIN[] and BCDU[] bit-field Signed-off-by: Francois RAMU --- src/rtc.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/rtc.c b/src/rtc.c index 73c911f..de1b779 100644 --- a/src/rtc.c +++ b/src/rtc.c @@ -93,6 +93,7 @@ static void RTC_computePrediv(uint32_t *asynch, uint32_t *synch); #endif /* !STM32F1xx */ #if defined(RTC_BINARY_NONE) static void RTC_BinaryConf(binaryMode_t mode); +static void RTC_SetBinaryConf(void); #endif static inline int _log2(int x) @@ -383,6 +384,34 @@ static void RTC_BinaryConf(binaryMode_t mode) } } } + + /* + * Function to check if the RTC ICSR register must be updated with new bit + * field BIN and BCDU. To be called just after the RTC_SetBinaryConf + * Map the LL RTC bin mode to the corresponding RtcHandle.Init.BinMode values + * assuming the LL_RTC_BINARY_xxx is identical to RTC_BINARY_xxx (RTC_ICSR_BIN_xxx) + * Idem for the LL_RTC_BINARY_MIX_BCDU_n and RTC_BINARY_MIX_BCDU_n + */ +#if (RTC_BINARY_MIX != LL_RTC_BINARY_MIX) +#error "RTC_BINARY_MIX and LL_RTC_BINARY_MIX do not match" +#endif +#if (RTC_BINARY_MIX_BCDU_7 != LL_RTC_BINARY_MIX_BCDU_7) +#error "RTC_BINARY_MIX_BCDU_n and LL_RTC_BINARY_MIX_BCDU_n do not match" +#endif +static void RTC_SetBinaryConf(void) +{ + if (LL_RTC_GetBinaryMode(RtcHandle.Instance) != RtcHandle.Init.BinMode) { + LL_RTC_DisableWriteProtection(RtcHandle.Instance); + LL_RTC_EnableInitMode(RtcHandle.Instance); + + LL_RTC_SetBinaryMode(RtcHandle.Instance, RtcHandle.Init.BinMode); + if (RtcHandle.Init.BinMode == RTC_BINARY_MIX) { + LL_RTC_SetBinMixBCDU(RtcHandle.Instance, RtcHandle.Init.BinMixBcdU); + } + LL_RTC_ExitInitMode(RtcHandle.Instance); + LL_RTC_EnableWriteProtection(RtcHandle.Instance); + } +} #endif /* RTC_BINARY_NONE */ /** @@ -462,6 +491,10 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool RTC_initClock(source); RTC_getPrediv(&(RtcHandle.Init.AsynchPrediv), &(RtcHandle.Init.SynchPrediv)); #if defined(RTC_BINARY_NONE) + /* + * If RTC BIN mode changed, calling the HAL_RTC_Init will + * force the update of the BIN register in the RTC_ICSR + */ RTC_BinaryConf(mode); #endif /* RTC_BINARY_NONE */ #endif // STM32F1xx @@ -526,11 +559,11 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool #else RTC_getPrediv(&(RtcHandle.Init.AsynchPrediv), &(RtcHandle.Init.SynchPrediv)); #endif +#if defined(RTC_BINARY_NONE) /* - * TODO: RTC is already initialized, but RTC BIN mode is changed + * If RTC BIN mode changed, calling the HAL_RTC_Init will * force the update of the BIN register in the RTC_ICSR */ -#if defined(RTC_BINARY_NONE) RTC_BinaryConf(mode); #endif /* RTC_BINARY_NONE */ HAL_RTC_Init(&RtcHandle); @@ -557,6 +590,13 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool #endif #if defined(RTC_BINARY_NONE) RTC_BinaryConf(mode); + /* + * RTC is already initialized, but RTC BIN mode is changed : + * force the update of the BIN register and BCDu in the RTC_ICSR + * by the RTC_SetBinaryConf function + */ + RTC_SetBinaryConf(); + #endif /* RTC_BINARY_NONE */ #if defined(STM32F1xx) memcpy(&RtcHandle.DateToUpdate, &BackupDate, 4);