Skip to content

Commit 23a1d03

Browse files
committed
chore: set clock source and (a)synchronous prescalers together
(a)synchronous prescalers values depend on the RTC clock source. Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 9d07ee5 commit 23a1d03

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ _RTC hours mode (12 or 24)_
3131

3232
_RTC clock source_
3333
* **`Source_Clock getClockSource(void)`** : get current clock source.
34-
* **`void setClockSource(Source_Clock source)`** : this function must be called before `begin()`.
34+
* **`void setClockSource(Source_Clock source, uint32_t predivA, uint32_t predivS)`** : set the clock source (`LSI_CLOCK`, `LSE_CLOCK` or `HSE_CLOCK`) and (a)synchronous prescaler values. This function must be called before `begin()`. Use `(PREDIVA_MAX + 1)` and `(PREDIVS_MAX +1)` to reset value and use computed ones. Those values have to match the following conditions: **_1Hz = RTC CLK source / ((predivA + 1) * (predivS + 1))_**
3535

3636
_RTC Asynchronous and Synchronous prescaler_
3737
* **`void getPrediv(uint32_t *predivA, uint32_t *predivS)`** : get (a)synchronous prescaler values if set else computed ones for the current clock source.

src/STM32RTC.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,23 @@ STM32RTC::Source_Clock STM32RTC::getClockSource(void)
117117
}
118118

119119
/**
120-
* @brief set the RTC clock source. By default LSI clock is selected. This
121-
* method must be called before begin().
120+
* @brief set the RTC clock source and user (a)synchronous prescalers values.
121+
* @note By default LSI clock is selected. This method must be called before begin().
122122
* @param source: clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK
123+
* @param predivA: Asynchronous prescaler value.
124+
* @note Reset value: RTC_AUTO_1_SECOND for STM32F1xx series, else (PREDIVA_MAX + 1)
125+
* @param predivS: Synchronous prescaler value.
126+
* @note Reset value: (PREDIVS_MAX + 1), not used for STM32F1xx series.
123127
* @retval None
124128
*/
125-
void STM32RTC::setClockSource(Source_Clock source)
129+
void STM32RTC::setClockSource(Source_Clock source, uint32_t predivA, uint32_t predivS)
126130
{
127131
if (IS_CLOCK_SOURCE(source)) {
128132
_clockSource = source;
129133
RTC_SetClockSource((_clockSource == LSE_CLOCK) ? ::LSE_CLOCK :
130134
(_clockSource == HSE_CLOCK) ? ::HSE_CLOCK : ::LSI_CLOCK);
131135
}
136+
RTC_setPrediv(predivA, predivS);
132137
}
133138

134139
/**
@@ -151,7 +156,7 @@ void STM32RTC::getPrediv(uint32_t *predivA, uint32_t *predivS)
151156
}
152157

153158
/**
154-
* @brief set user (a)synchronous prescalers value.
159+
* @brief set user (a)synchronous prescalers values.
155160
* @note This method must be called before begin().
156161
* @param predivA: Asynchronous prescaler value.
157162
* @note Reset value: RTC_AUTO_1_SECOND for STM32F1xx series, else (PREDIVA_MAX + 1)
@@ -161,7 +166,7 @@ void STM32RTC::getPrediv(uint32_t *predivA, uint32_t *predivS)
161166
*/
162167
void STM32RTC::setPrediv(uint32_t predivA, uint32_t predivS)
163168
{
164-
RTC_setPrediv(predivA, predivS);
169+
setClockSource(_clockSource, predivA, predivS);
165170
}
166171

167172
/**

src/STM32RTC.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ class STM32RTC {
126126
void end(void);
127127

128128
Source_Clock getClockSource(void);
129-
void setClockSource(Source_Clock source);
129+
void setClockSource(Source_Clock source, uint32_t predivA = (PREDIVA_MAX + 1), uint32_t predivS = (PREDIVS_MAX + 1));
130+
void getPrediv(uint32_t *predivA, uint32_t *predivS);
131+
void setPrediv(uint32_t predivA, uint32_t predivS);
130132

131133
void enableAlarm(Alarm_Match match, Alarm name = ALARM_A);
132134
void disableAlarm(Alarm name = ALARM_A);
@@ -212,9 +214,6 @@ class STM32RTC {
212214
void setAlarmEpoch(time_t ts, Alarm_Match match, Alarm name);
213215
void setAlarmEpoch(time_t ts, Alarm_Match match = MATCH_DHHMMSS, uint32_t subSeconds = 0, Alarm name = ALARM_A);
214216

215-
void getPrediv(uint32_t *predivA, uint32_t *predivS);
216-
void setPrediv(uint32_t predivA, uint32_t predivS);
217-
218217
bool isConfigured(void)
219218
{
220219
return RTC_IsConfigured();

0 commit comments

Comments
 (0)