@@ -79,12 +79,16 @@ static uint32_t predivAsync = RTC_AUTO_1_SECOND;
79
79
#endif /* !STM32F1xx */
80
80
81
81
static hourFormat_t initFormat = HOUR_FORMAT_12 ;
82
+ static binaryMode_t initMode = MODE_BINARY_NONE ;
82
83
83
84
/* Private function prototypes -----------------------------------------------*/
84
85
static void RTC_initClock (sourceClock_t source );
85
86
#if !defined(STM32F1xx )
86
87
static void RTC_computePrediv (uint32_t * asynch , uint32_t * synch );
87
88
#endif /* !STM32F1xx */
89
+ #if defined(RTC_BINARY_NONE )
90
+ static void RTC_BinaryConf (binaryMode_t mode );
91
+ #endif
88
92
89
93
static inline int _log2 (int x )
90
94
{
@@ -334,17 +338,49 @@ static void RTC_computePrediv(uint32_t *asynch, uint32_t *synch)
334
338
}
335
339
#endif /* !STM32F1xx */
336
340
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
+
337
372
/**
338
373
* @brief RTC Initialization
339
374
* This function configures the RTC time and calendar. By default, the
340
375
* RTC is set to the 1st January 2001
341
376
* Note: year 2000 is invalid as it is the hardware reset value and doesn't raise INITS flag
342
377
* @param format: enable the RTC in 12 or 24 hours mode
378
+ * @param mode: enable the RTC in BCD or Mix or Binary mode
343
379
* @param source: RTC clock source: LSE, LSI or HSE
344
380
* @param reset: force RTC reset, even if previously configured
345
381
* @retval True if RTC is reinitialized, else false
346
382
*/
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 )
348
384
{
349
385
bool reinit = false;
350
386
hourAM_PM_t period = HOUR_AM , alarmPeriod = HOUR_AM ;
@@ -360,7 +396,7 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
360
396
#endif
361
397
362
398
initFormat = format ;
363
-
399
+ initMode = mode ;
364
400
/* Ensure all RtcHandle properly set */
365
401
RtcHandle .Instance = RTC ;
366
402
#if defined(STM32F1xx )
@@ -409,7 +445,10 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
409
445
// Init RTC clock
410
446
RTC_initClock (source );
411
447
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
413
452
414
453
HAL_RTC_Init (& RtcHandle );
415
454
// Default: saturday 1st of January 2001
@@ -471,6 +510,9 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
471
510
#else
472
511
RTC_getPrediv (& (RtcHandle .Init .AsynchPrediv ), & (RtcHandle .Init .SynchPrediv ));
473
512
#endif
513
+ #if defined(RTC_BINARY_NONE )
514
+ RTC_BinaryConf (mode );
515
+ #endif /* RTC_BINARY_NONE */
474
516
HAL_RTC_Init (& RtcHandle );
475
517
// Restore config
476
518
RTC_SetTime (hours , minutes , seconds , subSeconds , period );
@@ -493,6 +535,9 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
493
535
#else
494
536
RTC_getPrediv (& (RtcHandle .Init .AsynchPrediv ), & (RtcHandle .Init .SynchPrediv ));
495
537
#endif
538
+ #if defined(RTC_BINARY_NONE )
539
+ RTC_BinaryConf (mode );
540
+ #endif /* RTC_BINARY_NONE */
496
541
#if defined(STM32F1xx )
497
542
memcpy (& RtcHandle .DateToUpdate , & BackupDate , 4 );
498
543
/* Update date automatically by calling HAL_RTC_GetDate */
0 commit comments