Skip to content

Commit b0adb67

Browse files
committed
stm32duino RTC add a parameter to set binary or bcd or Mix mode
This is valid whe the RTC_BINARY_MIX mode exists in the RTC (bitfield in the RTC ICSR register) Set the RTC mode through a setRTCMode function to be called before begin Signed-off-by: Francois Ramu <francois.ramu@st.com>
1 parent 442ec38 commit b0adb67

File tree

4 files changed

+45
-5
lines changed

4 files changed

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

135+
/**
136+
* @brief get the RTC Mode .
137+
* @retval mode: MODE_BCD, MODE_BIN or MODE_MIX
138+
*/
139+
STM32RTC::RTC_Mode STM32RTC::getRTCMode(void)
140+
{
141+
return _mode;
142+
}
143+
144+
/**
145+
* @brief set the RTC 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::setRTCMode(RTC_Mode mode)
151+
{
152+
_mode = mode;
153+
}
154+
134155
#if defined(STM32F1xx)
135156
/**
136157
* @brief get user asynchronous prescaler value for the current clock source.

src/STM32RTC.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ class STM32RTC {
8585
PM = HOUR_PM
8686
};
8787

88+
enum RTC_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+
RTC_Mode getRTCMode(void);
138+
void setRTCMode(RTC_Mode mode);
139+
131140
void enableAlarm(Alarm_Match match, Alarm name = ALARM_A);
132141
void disableAlarm(Alarm name = ALARM_A);
133142

@@ -237,6 +246,7 @@ class STM32RTC {
237246
static bool _timeSet;
238247

239248
Hour_Format _format;
249+
RTC_Mode _mode;
240250
AM_PM _hoursPeriod;
241251
uint8_t _hours;
242252
uint8_t _minutes;

src/rtc.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static uint32_t prediv = RTC_AUTO_1_SECOND;
7777
#endif /* !STM32F1xx */
7878

7979
static hourFormat_t initFormat = HOUR_FORMAT_12;
80+
static rtcMode_t initMode = MODE_BINARY_NONE;
8081

8182
/* Private function prototypes -----------------------------------------------*/
8283
static void RTC_initClock(sourceClock_t source);
@@ -334,11 +335,12 @@ static void RTC_computePrediv(int8_t *asynch, int16_t *synch)
334335
* RTC is set to the 1st January 2001
335336
* Note: year 2000 is invalid as it is the hardware reset value and doesn't raise INITS flag
336337
* @param format: enable the RTC in 12 or 24 hours mode
338+
* @param mode: enable the RTC in BCD or Mix or Binary mode
337339
* @param source: RTC clock source: LSE, LSI or HSE
338340
* @param reset: force RTC reset, even if previously configured
339341
* @retval True if RTC is reinitialized, else false
340342
*/
341-
bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
343+
bool RTC_init(hourFormat_t format, rtcMode_t mode, sourceClock_t source, bool reset)
342344
{
343345
bool reinit = false;
344346
hourAM_PM_t period = HOUR_AM, alarmPeriod = HOUR_AM;
@@ -360,6 +362,7 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
360362
#endif
361363

362364
initFormat = format;
365+
initMode = mode;
363366
RtcHandle.Instance = RTC;
364367

365368
/* Ensure backup domain is enabled before we init the RTC so we can use the backup registers for date retention on stm32f1xx boards */
@@ -390,7 +393,7 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
390393
#else
391394
if (!LL_RTC_IsActiveFlag_INITS(RtcHandle.Instance) || reset) {
392395
// RTC needs initialization
393-
RtcHandle.Init.HourFormat = format == HOUR_FORMAT_12 ? RTC_HOURFORMAT_12 : RTC_HOURFORMAT_24;
396+
RtcHandle.Init.HourFormat = (format == HOUR_FORMAT_12) ? RTC_HOURFORMAT_12 : RTC_HOURFORMAT_24;
394397
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
395398
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
396399
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
@@ -401,8 +404,8 @@ bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
401404
RtcHandle.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
402405
#endif /* RTC_OUTPUT_REMAP_NONE */
403406
#if defined(RTC_BINARY_NONE)
404-
RtcHandle.Init.BinMode = RTC_BINARY_NONE;
405-
#endif
407+
RtcHandle.Init.BinMode = (mode == MODE_BINARY_MIX) ? RTC_BINARY_MIX : ((mode == MODE_BINARY_ONLY) ? RTC_BINARY_ONLY : RTC_BINARY_NONE);
408+
#endif /* RTC_BINARY_NONE */
406409
RTC_getPrediv((int8_t *) & (RtcHandle.Init.AsynchPrediv), (int16_t *) & (RtcHandle.Init.SynchPrediv));
407410
#endif // STM32F1xx
408411
// Init RTC clock

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 : not used */
60+
MODE_BINARY_ONLY,
61+
MODE_BINARY_MIX
62+
} rtcMode_t;
63+
5864
typedef enum {
5965
HOUR_AM,
6066
HOUR_PM
@@ -173,7 +179,7 @@ void RTC_getPrediv(int8_t *asynch, int16_t *synch);
173179
void RTC_setPrediv(int8_t asynch, int16_t synch);
174180
#endif /* STM32F1xx */
175181

176-
bool RTC_init(hourFormat_t format, sourceClock_t source, bool reset);
182+
bool RTC_init(hourFormat_t format, rtcMode_t mode, sourceClock_t source, bool reset);
177183
void RTC_DeInit(void);
178184
bool RTC_IsConfigured(void);
179185

0 commit comments

Comments
 (0)