Skip to content

Commit 27babd6

Browse files
author
Francois RAMU
committed
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 <[email protected]>
1 parent 5da16e6 commit 27babd6

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_SetBinaryConf
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
/**
@@ -462,6 +491,10 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
462491
RTC_initClock(source);
463492
RTC_getPrediv(&(RtcHandle.Init.AsynchPrediv), &(RtcHandle.Init.SynchPrediv));
464493
#if defined(RTC_BINARY_NONE)
494+
/*
495+
* If RTC BIN mode changed, calling the HAL_RTC_Init will
496+
* force the update of the BIN register in the RTC_ICSR
497+
*/
465498
RTC_BinaryConf(mode);
466499
#endif /* RTC_BINARY_NONE */
467500
#endif // STM32F1xx
@@ -526,11 +559,11 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
526559
#else
527560
RTC_getPrediv(&(RtcHandle.Init.AsynchPrediv), &(RtcHandle.Init.SynchPrediv));
528561
#endif
562+
#if defined(RTC_BINARY_NONE)
529563
/*
530-
* TODO: RTC is already initialized, but RTC BIN mode is changed
564+
* If RTC BIN mode changed, calling the HAL_RTC_Init will
531565
* force the update of the BIN register in the RTC_ICSR
532566
*/
533-
#if defined(RTC_BINARY_NONE)
534567
RTC_BinaryConf(mode);
535568
#endif /* RTC_BINARY_NONE */
536569
HAL_RTC_Init(&RtcHandle);
@@ -557,6 +590,13 @@ bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool
557590
#endif
558591
#if defined(RTC_BINARY_NONE)
559592
RTC_BinaryConf(mode);
593+
/*
594+
* RTC is already initialized, but RTC BIN mode is changed :
595+
* force the update of the BIN register and BCDu in the RTC_ICSR
596+
* by the RTC_SetBinaryConf function
597+
*/
598+
RTC_SetBinaryConf();
599+
560600
#endif /* RTC_BINARY_NONE */
561601
#if defined(STM32F1xx)
562602
memcpy(&RtcHandle.DateToUpdate, &BackupDate, 4);

0 commit comments

Comments
 (0)