Skip to content

Commit 3a29b0e

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 <francois.ramu@st.com> Co-authored-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 23a1d03 commit 3a29b0e

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

src/STM32RTC.cpp

Lines changed: 21 additions & 0 deletions
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

Lines changed: 11 additions & 1 deletion
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,14 +236,15 @@ 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
setClockSource(_clockSource);
233242
}
234243

235244
static bool _timeSet;
236245

237246
Hour_Format _format;
247+
Binary_Mode _mode;
238248
AM_PM _hoursPeriod;
239249
uint8_t _hours;
240250
uint8_t _minutes;

src/rtc.c

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,16 @@ static uint32_t predivAsync = RTC_AUTO_1_SECOND;
7979
#endif /* !STM32F1xx */
8080

8181
static hourFormat_t initFormat = HOUR_FORMAT_12;
82+
static binaryMode_t initMode = MODE_BINARY_NONE;
8283

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

8993
static inline int _log2(int x)
9094
{
@@ -334,17 +338,49 @@ static void RTC_computePrediv(uint32_t *asynch, uint32_t *synch)
334338
}
335339
#endif /* !STM32F1xx */
336340

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

362398
initFormat = format;
363-
399+
initMode = mode;
364400
/* Ensure all RtcHandle properly set */
365401
RtcHandle.Instance = RTC;
366402
#if defined(STM32F1xx)
@@ -409,7 +445,10 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
409445
// Init RTC clock
410446
RTC_initClock(source);
411447
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
413452

414453
HAL_RTC_Init(&RtcHandle);
415454
// Default: saturday 1st of January 2001
@@ -471,6 +510,9 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
471510
#else
472511
RTC_getPrediv(&(RtcHandle.Init.AsynchPrediv), &(RtcHandle.Init.SynchPrediv));
473512
#endif
513+
#if defined(RTC_BINARY_NONE)
514+
RTC_BinaryConf(mode);
515+
#endif /* RTC_BINARY_NONE */
474516
HAL_RTC_Init(&RtcHandle);
475517
// Restore config
476518
RTC_SetTime(hours, minutes, seconds, subSeconds, period);
@@ -493,6 +535,9 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
493535
#else
494536
RTC_getPrediv(&(RtcHandle.Init.AsynchPrediv), &(RtcHandle.Init.SynchPrediv));
495537
#endif
538+
#if defined(RTC_BINARY_NONE)
539+
RTC_BinaryConf(mode);
540+
#endif /* RTC_BINARY_NONE */
496541
#if defined(STM32F1xx)
497542
memcpy(&RtcHandle.DateToUpdate, &BackupDate, 4);
498543
/* Update date automatically by calling HAL_RTC_GetDate */

src/rtc.h

Lines changed: 7 additions & 1 deletion
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(bool reset_cb);
174180
bool RTC_IsConfigured(void);
175181

0 commit comments

Comments
 (0)