@@ -79,12 +79,16 @@ static uint32_t predivAsync = RTC_AUTO_1_SECOND;
7979#endif /* !STM32F1xx */
8080
8181static hourFormat_t initFormat = HOUR_FORMAT_12 ;
82+ static binaryMode_t initMode = MODE_BINARY_NONE ;
8283
8384/* Private function prototypes -----------------------------------------------*/
8485static void RTC_initClock (sourceClock_t source );
8586#if !defined(STM32F1xx )
8687static void RTC_computePrediv (uint32_t * asynch , uint32_t * synch );
8788#endif /* !STM32F1xx */
89+ #if defined(RTC_BINARY_NONE )
90+ static void RTC_BinaryConf (binaryMode_t mode );
91+ #endif
8892
8993static inline int _log2 (int x )
9094{
@@ -334,17 +338,49 @@ static void RTC_computePrediv(uint32_t *asynch, uint32_t *synch)
334338}
335339#endif /* !STM32F1xx */
336340
341+ #if defined(RTC_BINARY_NONE )
342+ static void RTC_BinaryConf (binaryMode_t mode )
343+ {
344+ RtcHandle .Init .BinMode = (mode == MODE_BINARY_MIX ) ? RTC_BINARY_MIX : ((mode == MODE_BINARY_ONLY ) ? RTC_BINARY_ONLY : RTC_BINARY_NONE );
345+ if (RtcHandle .Init .BinMode == RTC_BINARY_MIX ) {
346+ /* Configure the 1s BCD calendar increment */
347+
348+ uint32_t inc = 1 / (1.0 / ((float )clkVal / (float )(predivAsync + 1.0 )));
349+ if (inc <= 256 ) {
350+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_0 ;
351+ } else if (inc < (256 << 1 )) {
352+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_1 ;
353+ } else if (inc < (256 << 2 )) {
354+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_2 ;
355+ } else if (inc < (256 << 3 )) {
356+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_3 ;
357+ } else if (inc < (256 << 4 )) {
358+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_4 ;
359+ } else if (inc < (256 << 5 )) {
360+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_5 ;
361+ } else if (inc < (256 << 6 )) {
362+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_6 ;
363+ } else if (inc < (256 << 7 )) {
364+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_7 ;
365+ } else {
366+ Error_Handler ();
367+ }
368+ }
369+ }
370+ #endif /* RTC_BINARY_NONE */
371+
337372/**
338373 * @brief RTC Initialization
339374 * This function configures the RTC time and calendar. By default, the
340375 * RTC is set to the 1st January 2001
341376 * Note: year 2000 is invalid as it is the hardware reset value and doesn't raise INITS flag
342377 * @param format: enable the RTC in 12 or 24 hours mode
378+ * @param mode: enable the RTC in BCD or Mix or Binary mode
343379 * @param source: RTC clock source: LSE, LSI or HSE
344380 * @param reset: force RTC reset, even if previously configured
345381 * @retval True if RTC is reinitialized, else false
346382 */
347- bool RTC_init (hourFormat_t format , sourceClock_t source , bool reset )
383+ bool RTC_init (hourFormat_t format , binaryMode_t mode , sourceClock_t source , bool reset )
348384{
349385 bool reinit = false;
350386 hourAM_PM_t period = HOUR_AM , alarmPeriod = HOUR_AM ;
@@ -360,7 +396,7 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
360396#endif
361397
362398 initFormat = format ;
363-
399+ initMode = mode ;
364400 /* Ensure all RtcHandle properly set */
365401 RtcHandle .Instance = RTC ;
366402#if defined(STM32F1xx )
@@ -409,7 +445,10 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
409445 // Init RTC clock
410446 RTC_initClock (source );
411447 RTC_getPrediv (& (RtcHandle .Init .AsynchPrediv ), & (RtcHandle .Init .SynchPrediv ));
412- #endif // STM32F1xx
448+ #if defined(RTC_BINARY_NONE )
449+ RTC_BinaryConf (mode );
450+ #endif /* RTC_BINARY_NONE */
451+ #endif // STM32F1xx
413452
414453 HAL_RTC_Init (& RtcHandle );
415454 // Default: saturday 1st of January 2001
@@ -471,6 +510,9 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
471510#else
472511 RTC_getPrediv (& (RtcHandle .Init .AsynchPrediv ), & (RtcHandle .Init .SynchPrediv ));
473512#endif
513+ #if defined(RTC_BINARY_NONE )
514+ RTC_BinaryConf (mode );
515+ #endif /* RTC_BINARY_NONE */
474516 HAL_RTC_Init (& RtcHandle );
475517 // Restore config
476518 RTC_SetTime (hours , minutes , seconds , subSeconds , period );
@@ -493,6 +535,9 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
493535#else
494536 RTC_getPrediv (& (RtcHandle .Init .AsynchPrediv ), & (RtcHandle .Init .SynchPrediv ));
495537#endif
538+ #if defined(RTC_BINARY_NONE )
539+ RTC_BinaryConf (mode );
540+ #endif /* RTC_BINARY_NONE */
496541#if defined(STM32F1xx )
497542 memcpy (& RtcHandle .DateToUpdate , & BackupDate , 4 );
498543 /* Update date automatically by calling HAL_RTC_GetDate */
0 commit comments