@@ -78,12 +78,16 @@ static uint32_t prediv = RTC_AUTO_1_SECOND;
78
78
#endif /* !STM32F1xx */
79
79
80
80
static hourFormat_t initFormat = HOUR_FORMAT_12 ;
81
+ static binaryMode_t initMode = MODE_BINARY_NONE ;
81
82
82
83
/* Private function prototypes -----------------------------------------------*/
83
84
static void RTC_initClock (sourceClock_t source );
84
85
#if !defined(STM32F1xx )
85
86
static void RTC_computePrediv (uint32_t * asynch , uint32_t * synch );
86
87
#endif /* !STM32F1xx */
88
+ #if defined(RTC_BINARY_NONE )
89
+ static void RTC_BinaryConf (binaryMode_t mode );
90
+ #endif
87
91
88
92
static inline int _log2 (int x )
89
93
{
@@ -332,17 +336,49 @@ static void RTC_computePrediv(uint32_t *asynch, uint32_t *synch)
332
336
}
333
337
#endif /* !STM32F1xx */
334
338
339
+ #if defined(RTC_BINARY_NONE )
340
+ static void RTC_BinaryConf (binaryMode_t mode )
341
+ {
342
+ RtcHandle .Init .BinMode = (mode == MODE_BINARY_MIX ) ? RTC_BINARY_MIX : ((mode == MODE_BINARY_ONLY ) ? RTC_BINARY_ONLY : RTC_BINARY_NONE );
343
+ if (RtcHandle .Init .BinMode == RTC_BINARY_MIX ) {
344
+ /* Configure the 1s BCD calendar increment */
345
+
346
+ uint32_t inc = 1 / (1.0 / ((float )clkVal / (float )(predivAsync + 1.0 )));
347
+ if (inc <= 256 ) {
348
+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_0 ;
349
+ } else if (inc < (256 << 1 )) {
350
+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_1 ;
351
+ } else if (inc < (256 << 2 )) {
352
+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_2 ;
353
+ } else if (inc < (256 << 3 )) {
354
+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_3 ;
355
+ } else if (inc < (256 << 4 )) {
356
+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_4 ;
357
+ } else if (inc < (256 << 5 )) {
358
+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_5 ;
359
+ } else if (inc < (256 << 6 )) {
360
+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_6 ;
361
+ } else if (inc < (256 << 7 )) {
362
+ RtcHandle .Init .BinMixBcdU = RTC_BINARY_MIX_BCDU_7 ;
363
+ } else {
364
+ Error_Handler ();
365
+ }
366
+ }
367
+ }
368
+ #endif /* RTC_BINARY_NONE */
369
+
335
370
/**
336
371
* @brief RTC Initialization
337
372
* This function configures the RTC time and calendar. By default, the
338
373
* RTC is set to the 1st January 2001
339
374
* Note: year 2000 is invalid as it is the hardware reset value and doesn't raise INITS flag
340
375
* @param format: enable the RTC in 12 or 24 hours mode
376
+ * @param mode: enable the RTC in BCD or Mix or Binary mode
341
377
* @param source: RTC clock source: LSE, LSI or HSE
342
378
* @param reset: force RTC reset, even if previously configured
343
379
* @retval True if RTC is reinitialized, else false
344
380
*/
345
- bool RTC_init (hourFormat_t format , sourceClock_t source , bool reset )
381
+ bool RTC_init (hourFormat_t format , binaryMode_t mode , sourceClock_t source , bool reset )
346
382
{
347
383
bool reinit = false;
348
384
hourAM_PM_t period = HOUR_AM , alarmPeriod = HOUR_AM ;
@@ -360,6 +396,7 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
360
396
uint32_t sync ;
361
397
362
398
initFormat = format ;
399
+ initMode = mode ;
363
400
RtcHandle .Instance = RTC ;
364
401
365
402
/* Ensure backup domain is enabled before we init the RTC so we can use the backup registers for date retention on stm32f1xx boards */
@@ -402,13 +439,13 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
402
439
#if defined(RTC_OUTPUT_REMAP_NONE )
403
440
RtcHandle .Init .OutPutRemap = RTC_OUTPUT_REMAP_NONE ;
404
441
#endif /* RTC_OUTPUT_REMAP_NONE */
405
- #if defined(RTC_BINARY_NONE )
406
- RtcHandle .Init .BinMode = RTC_BINARY_NONE ;
407
- #endif
408
442
// Init RTC clock
409
443
RTC_initClock (source );
410
444
RTC_getPrediv (& (RtcHandle .Init .AsynchPrediv ), & (RtcHandle .Init .SynchPrediv ));
411
- #endif // STM32F1xx
445
+ #if defined(RTC_BINARY_NONE )
446
+ RTC_BinaryConf (mode );
447
+ #endif /* RTC_BINARY_NONE */
448
+ #endif // STM32F1xx
412
449
413
450
HAL_RTC_Init (& RtcHandle );
414
451
// Default: saturday 1st of January 2001
@@ -487,6 +524,9 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
487
524
RTC_SetDate (RtcHandle .DateToUpdate .Year , RtcHandle .DateToUpdate .Month ,
488
525
RtcHandle .DateToUpdate .Date , RtcHandle .DateToUpdate .WeekDay );
489
526
#endif // STM32F1xx
527
+ #if defined(RTC_BINARY_NONE )
528
+ RTC_BinaryConf (mode );
529
+ #endif /* RTC_BINARY_NONE */
490
530
}
491
531
}
492
532
0 commit comments