Skip to content

Commit 0126b71

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 3095095 commit 0126b71

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);
@@ -131,6 +132,26 @@ void STM32RTC::setClockSource(Source_Clock source)
131132
}
132133
}
133134

135+
/**
136+
* @brief get the Binary Mode.
137+
* @retval mode: MODE_BCD, MODE_BIN or MODE_MIX
138+
*/
139+
STM32RTC::Binary_Mode STM32RTC::getBinaryMode(void)
140+
{
141+
return _mode;
142+
}
143+
144+
/**
145+
* @brief set the Binary Mode. By default MODE_BCD is selected. This
146+
* method must be called before begin().
147+
* @param mode: the RTC mode: MODE_BCD, MODE_BIN or MODE_MIX
148+
* @retval None
149+
*/
150+
void STM32RTC::setBinaryMode(Binary_Mode mode)
151+
{
152+
_mode = mode;
153+
}
154+
134155
/**
135156
* @brief get user (a)synchronous prescaler values if set else computed
136157
* 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
@@ -128,6 +134,9 @@ class STM32RTC {
128134
Source_Clock getClockSource(void);
129135
void setClockSource(Source_Clock source);
130136

137+
Binary_Mode getBinaryMode(void);
138+
void setBinaryMode(Binary_Mode mode);
139+
131140
void enableAlarm(Alarm_Match match, Alarm name = ALARM_A);
132141
void disableAlarm(Alarm name = ALARM_A);
133142

@@ -228,11 +237,12 @@ class STM32RTC {
228237
friend class STM32LowPower;
229238

230239
private:
231-
STM32RTC(void): _clockSource(LSI_CLOCK) {}
240+
STM32RTC(void): _mode(MODE_BCD), _clockSource(LSI_CLOCK) {}
232241

233242
static bool _timeSet;
234243

235244
Hour_Format _format;
245+
Binary_Mode _mode;
236246
AM_PM _hoursPeriod;
237247
uint8_t _hours;
238248
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
{
@@ -332,17 +336,49 @@ static void RTC_computePrediv(uint32_t *asynch, uint32_t *synch)
332336
}
333337
#endif /* !STM32F1xx */
334338

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

362398
initFormat = format;
399+
initMode = mode;
363400
RtcHandle.Instance = RTC;
364401

365402
/* 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)
402439
#if defined(RTC_OUTPUT_REMAP_NONE)
403440
RtcHandle.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
404441
#endif /* RTC_OUTPUT_REMAP_NONE */
405-
#if defined(RTC_BINARY_NONE)
406-
RtcHandle.Init.BinMode = RTC_BINARY_NONE;
407-
#endif
408442
// Init RTC clock
409443
RTC_initClock(source);
410444
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
412449

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

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
@@ -168,7 +174,7 @@ void RTC_SetClockSource(sourceClock_t source);
168174
void RTC_getPrediv(uint32_t *asynch, uint32_t *synch);
169175
void RTC_setPrediv(uint32_t asynch, uint32_t synch);
170176

171-
bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset);
177+
bool RTC_init(hourFormat_t format, binaryMode_t mode, sourceClock_t source, bool reset);
172178
void RTC_DeInit(void);
173179
bool RTC_IsConfigured(void);
174180

0 commit comments

Comments
 (0)