Skip to content

Commit 1c66643

Browse files
Francois RAMUfpistm
Francois RAMU
authored andcommitted
stm32RTC change the binary Mode in case the RTC is already init
When the RTC is already initialized, but RTC BIN mode must be changed this is done by writing the RTC ICSR register on BIN[] and BCDU[] bit-field with a new `RTC_SetBinaryConf()` function. Signed-off-by: Francois Ramu <[email protected]>
1 parent ce1122e commit 1c66643

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/rtc.c

+42-2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static void RTC_computePrediv(uint32_t *asynch, uint32_t *synch);
9393
#endif /* !STM32F1xx */
9494
#if defined(RTC_BINARY_NONE)
9595
static void RTC_BinaryConf(binaryMode_t mode);
96+
static void RTC_SetBinaryConf(void);
9697
#endif
9798

9899
static inline int _log2(int x)
@@ -383,6 +384,34 @@ static void RTC_BinaryConf(binaryMode_t mode)
383384
}
384385
}
385386
}
387+
388+
/*
389+
* Function to check if the RTC ICSR register must be updated with new bit
390+
* field BIN and BCDU. To be called just after the RTC_BinaryConf
391+
* Map the LL RTC bin mode to the corresponding RtcHandle.Init.BinMode values
392+
* assuming the LL_RTC_BINARY_xxx is identical to RTC_BINARY_xxx (RTC_ICSR_BIN_xxx)
393+
* Idem for the LL_RTC_BINARY_MIX_BCDU_n and RTC_BINARY_MIX_BCDU_n
394+
*/
395+
#if (RTC_BINARY_MIX != LL_RTC_BINARY_MIX)
396+
#error "RTC_BINARY_MIX and LL_RTC_BINARY_MIX do not match"
397+
#endif
398+
#if (RTC_BINARY_MIX_BCDU_7 != LL_RTC_BINARY_MIX_BCDU_7)
399+
#error "RTC_BINARY_MIX_BCDU_n and LL_RTC_BINARY_MIX_BCDU_n do not match"
400+
#endif
401+
static void RTC_SetBinaryConf(void)
402+
{
403+
if (LL_RTC_GetBinaryMode(RtcHandle.Instance) != RtcHandle.Init.BinMode) {
404+
LL_RTC_DisableWriteProtection(RtcHandle.Instance);
405+
LL_RTC_EnableInitMode(RtcHandle.Instance);
406+
407+
LL_RTC_SetBinaryMode(RtcHandle.Instance, RtcHandle.Init.BinMode);
408+
if (RtcHandle.Init.BinMode == RTC_BINARY_MIX) {
409+
LL_RTC_SetBinMixBCDU(RtcHandle.Instance, RtcHandle.Init.BinMixBcdU);
410+
}
411+
LL_RTC_ExitInitMode(RtcHandle.Instance);
412+
LL_RTC_EnableWriteProtection(RtcHandle.Instance);
413+
}
414+
}
386415
#endif /* RTC_BINARY_NONE */
387416

388417
/**
@@ -463,6 +492,10 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
463492
RTC_initClock(source);
464493
RTC_getPrediv(&(RtcHandle.Init.AsynchPrediv), &(RtcHandle.Init.SynchPrediv));
465494
#if defined(RTC_BINARY_NONE)
495+
/*
496+
* If RTC BIN mode changed, calling the HAL_RTC_Init will
497+
* force the update of the BIN register in the RTC_ICSR
498+
*/
466499
RTC_BinaryConf(mode);
467500
#endif /* RTC_BINARY_NONE */
468501
#endif // STM32F1xx
@@ -527,11 +560,11 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
527560
#else
528561
RTC_getPrediv(&(RtcHandle.Init.AsynchPrediv), &(RtcHandle.Init.SynchPrediv));
529562
#endif
563+
#if defined(RTC_BINARY_NONE)
530564
/*
531-
* TODO: RTC is already initialized, but RTC BIN mode is changed
565+
* If RTC BIN mode changed, calling the HAL_RTC_Init will
532566
* force the update of the BIN register in the RTC_ICSR
533567
*/
534-
#if defined(RTC_BINARY_NONE)
535568
RTC_BinaryConf(mode);
536569
#endif /* RTC_BINARY_NONE */
537570
HAL_RTC_Init(&RtcHandle);
@@ -558,6 +591,13 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
558591
#endif
559592
#if defined(RTC_BINARY_NONE)
560593
RTC_BinaryConf(mode);
594+
/*
595+
* RTC is already initialized, but RTC BIN mode is changed :
596+
* force the update of the BIN register and BCDu in the RTC_ICSR
597+
* by the RTC_SetBinaryConf function
598+
*/
599+
RTC_SetBinaryConf();
600+
561601
#endif /* RTC_BINARY_NONE */
562602
#if defined(STM32F1xx)
563603
memcpy(&RtcHandle.DateToUpdate, &BackupDate, 4);

0 commit comments

Comments
 (0)