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