Skip to content

Commit ee39147

Browse files
committed
feat: add a parameter to set binary mode
This is valid when the RTC_BINARY_MIX mode exists in the RTC (bitfield in the RTC ICSR register) Set the RTC mode through a setBinaryMode function to be called before begin. Signed-off-by: Francois Ramu <[email protected]> Co-authored-by: Frederic Pillon <[email protected]>
1 parent ce8d415 commit ee39147

File tree

4 files changed

+84
-7
lines changed

4 files changed

+84
-7
lines changed

src/STM32RTC.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void STM32RTC::begin(bool resetTime, Hour_Format format)
6363

6464
_format = format;
6565
reinit = RTC_init((format == HOUR_12) ? HOUR_FORMAT_12 : HOUR_FORMAT_24,
66+
(_mode == MODE_MIX) ? ::MODE_BINARY_MIX : ((_mode == MODE_BIN) ? ::MODE_BINARY_ONLY : ::MODE_BINARY_NONE),
6667
(_clockSource == LSE_CLOCK) ? ::LSE_CLOCK :
6768
(_clockSource == HSE_CLOCK) ? ::HSE_CLOCK : ::LSI_CLOCK
6869
, resetTime);
@@ -136,6 +137,26 @@ void STM32RTC::setClockSource(Source_Clock source, uint32_t predivA, uint32_t pr
136137
RTC_setPrediv(predivA, predivS);
137138
}
138139

140+
/**
141+
* @brief get the Binary Mode.
142+
* @retval mode: MODE_BCD, MODE_BIN or MODE_MIX
143+
*/
144+
STM32RTC::Binary_Mode STM32RTC::getBinaryMode(void)
145+
{
146+
return _mode;
147+
}
148+
149+
/**
150+
* @brief set the Binary Mode. By default MODE_BCD is selected. This
151+
* method must be called before begin().
152+
* @param mode: the RTC mode: MODE_BCD, MODE_BIN or MODE_MIX
153+
* @retval None
154+
*/
155+
void STM32RTC::setBinaryMode(Binary_Mode mode)
156+
{
157+
_mode = mode;
158+
}
159+
139160
/**
140161
* @brief get user (a)synchronous prescaler values if set else computed
141162
* ones for the current clock source.

src/STM32RTC.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ class STM32RTC {
8585
PM = HOUR_PM
8686
};
8787

88+
enum Binary_Mode : uint8_t {
89+
MODE_BCD = MODE_BINARY_NONE, /* not used */
90+
MODE_BIN = MODE_BINARY_ONLY,
91+
MODE_MIX = MODE_BINARY_MIX
92+
};
93+
8894
enum Alarm_Match : uint8_t {
8995
MATCH_OFF = OFF_MSK, // Never
9096
MATCH_SS = SS_MSK, // Every Minute
@@ -130,6 +136,9 @@ class STM32RTC {
130136
void getPrediv(uint32_t *predivA, uint32_t *predivS);
131137
void setPrediv(uint32_t predivA, uint32_t predivS);
132138

139+
Binary_Mode getBinaryMode(void);
140+
void setBinaryMode(Binary_Mode mode);
141+
133142
void enableAlarm(Alarm_Match match, Alarm name = ALARM_A);
134143
void disableAlarm(Alarm name = ALARM_A);
135144

@@ -227,11 +236,12 @@ class STM32RTC {
227236
friend class STM32LowPower;
228237

229238
private:
230-
STM32RTC(void): _clockSource(LSI_CLOCK) {}
239+
STM32RTC(void): _mode(MODE_BCD), _clockSource(LSI_CLOCK) {}
231240

232241
static bool _timeSet;
233242

234243
Hour_Format _format;
244+
Binary_Mode _mode;
235245
AM_PM _hoursPeriod;
236246
uint8_t _hours;
237247
uint8_t _minutes;

src/rtc.c

+45-5
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,16 @@ static uint32_t prediv = RTC_AUTO_1_SECOND;
7878
#endif /* !STM32F1xx */
7979

8080
static hourFormat_t initFormat = HOUR_FORMAT_12;
81+
static binaryMode_t initMode = MODE_BINARY_NONE;
8182

8283
/* Private function prototypes -----------------------------------------------*/
8384
static void RTC_initClock(sourceClock_t source);
8485
#if !defined(STM32F1xx)
8586
static void RTC_computePrediv(uint32_t *asynch, uint32_t *synch);
8687
#endif /* !STM32F1xx */
88+
#if defined(RTC_BINARY_NONE)
89+
static void RTC_BinaryConf(binaryMode_t mode);
90+
#endif
8791

8892
static inline int _log2(int x)
8993
{
@@ -333,17 +337,49 @@ static void RTC_computePrediv(uint32_t *asynch, uint32_t *synch)
333337
}
334338
#endif /* !STM32F1xx */
335339

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+
336371
/**
337372
* @brief RTC Initialization
338373
* This function configures the RTC time and calendar. By default, the
339374
* RTC is set to the 1st January 2001
340375
* Note: year 2000 is invalid as it is the hardware reset value and doesn't raise INITS flag
341376
* @param format: enable the RTC in 12 or 24 hours mode
377+
* @param mode: enable the RTC in BCD or Mix or Binary mode
342378
* @param source: RTC clock source: LSE, LSI or HSE
343379
* @param reset: force RTC reset, even if previously configured
344380
* @retval True if RTC is reinitialized, else false
345381
*/
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)
347383
{
348384
bool reinit = false;
349385
hourAM_PM_t period = HOUR_AM, alarmPeriod = HOUR_AM;
@@ -361,6 +397,7 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
361397
uint32_t sync;
362398

363399
initFormat = format;
400+
initMode = mode;
364401
RtcHandle.Instance = RTC;
365402

366403
/* 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)
403440
#if defined(RTC_OUTPUT_REMAP_NONE)
404441
RtcHandle.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
405442
#endif /* RTC_OUTPUT_REMAP_NONE */
406-
#if defined(RTC_BINARY_NONE)
407-
RtcHandle.Init.BinMode = RTC_BINARY_NONE;
408-
#endif
409443
// Init RTC clock
410444
RTC_initClock(source);
411445
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
413450

414451
HAL_RTC_Init(&RtcHandle);
415452
// Default: saturday 1st of January 2001
@@ -488,6 +525,9 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
488525
RTC_SetDate(RtcHandle.DateToUpdate.Year, RtcHandle.DateToUpdate.Month,
489526
RtcHandle.DateToUpdate.Date, RtcHandle.DateToUpdate.WeekDay);
490527
#endif // STM32F1xx
528+
#if defined(RTC_BINARY_NONE)
529+
RTC_BinaryConf(mode);
530+
#endif /* RTC_BINARY_NONE */
491531
}
492532
}
493533

src/rtc.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ typedef enum {
5555
HOUR_FORMAT_24
5656
} hourFormat_t;
5757

58+
typedef enum {
59+
MODE_BINARY_NONE, /* BCD only */
60+
MODE_BINARY_ONLY,
61+
MODE_BINARY_MIX
62+
} binaryMode_t;
63+
5864
typedef enum {
5965
HOUR_AM,
6066
HOUR_PM
@@ -169,7 +175,7 @@ void RTC_SetClockSource(sourceClock_t source);
169175
void RTC_getPrediv(uint32_t *asynch, uint32_t *synch);
170176
void RTC_setPrediv(uint32_t asynch, uint32_t synch);
171177

172-
bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset);
178+
bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool reset);
173179
void RTC_DeInit(void);
174180
bool RTC_IsConfigured(void);
175181

0 commit comments

Comments
 (0)