Skip to content

Commit

Permalink
chore: set clock source and (a)synchronous prescalers together
Browse files Browse the repository at this point in the history
(a)synchronous prescalers values depend on the RTC clock source.

Signed-off-by: Frederic Pillon <[email protected]>
  • Loading branch information
fpistm committed Sep 15, 2023
1 parent 9d07ee5 commit fb5b8ab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ _RTC hours mode (12 or 24)_

_RTC clock source_
* **`Source_Clock getClockSource(void)`** : get current clock source.
* **`void setClockSource(Source_Clock source)`** : this function must be called before `begin()`.
* **`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))_**

_RTC Asynchronous and Synchronous prescaler_
* **`void getPrediv(uint32_t *predivA, uint32_t *predivS)`** : get (a)synchronous prescaler values if set else computed ones for the current clock source.
Expand Down
15 changes: 10 additions & 5 deletions src/STM32RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,23 @@ STM32RTC::Source_Clock STM32RTC::getClockSource(void)
}

/**
* @brief set the RTC clock source. By default LSI clock is selected. This
* method must be called before begin().
* @brief set the RTC clock source and user (a)synchronous prescalers values.
* @note By default LSI clock is selected. This method must be called before begin().
* @param source: clock source: LSI_CLOCK, LSE_CLOCK or HSE_CLOCK
* @param predivA: Asynchronous prescaler value.
* @note Reset value: RTC_AUTO_1_SECOND for STM32F1xx series, else (PREDIVA_MAX + 1)
* @param predivS: Synchronous prescaler value.
* @note Reset value: (PREDIVS_MAX + 1), not used for STM32F1xx series.
* @retval None
*/
void STM32RTC::setClockSource(Source_Clock source)
void STM32RTC::setClockSource(Source_Clock source, uint32_t predivA, uint32_t predivS)
{
if (IS_CLOCK_SOURCE(source)) {
_clockSource = source;
RTC_SetClockSource((_clockSource == LSE_CLOCK) ? ::LSE_CLOCK :
(_clockSource == HSE_CLOCK) ? ::HSE_CLOCK : ::LSI_CLOCK);
}
RTC_setPrediv(predivA, predivS);
}

/**
Expand All @@ -151,7 +156,7 @@ void STM32RTC::getPrediv(uint32_t *predivA, uint32_t *predivS)
}

/**
* @brief set user (a)synchronous prescalers value.
* @brief set user (a)synchronous prescalers values.
* @note This method must be called before begin().
* @param predivA: Asynchronous prescaler value.
* @note Reset value: RTC_AUTO_1_SECOND for STM32F1xx series, else (PREDIVA_MAX + 1)
Expand All @@ -161,7 +166,7 @@ void STM32RTC::getPrediv(uint32_t *predivA, uint32_t *predivS)
*/
void STM32RTC::setPrediv(uint32_t predivA, uint32_t predivS)
{
RTC_setPrediv(predivA, predivS);
setClockSource(_clockSource, predivA, predivS);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/STM32RTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ class STM32RTC {
void end(void);

Source_Clock getClockSource(void);
void setClockSource(Source_Clock source);
void setClockSource(Source_Clock source, uint32_t predivA = (PREDIVS_MAX + 1), uint32_t predivS = (PREDIVS_MAX + 1));
void getPrediv(uint32_t *predivA, uint32_t *predivS);
void setPrediv(uint32_t predivA, uint32_t predivS);

void enableAlarm(Alarm_Match match, Alarm name = ALARM_A);
void disableAlarm(Alarm name = ALARM_A);
Expand Down Expand Up @@ -212,9 +214,6 @@ class STM32RTC {
void setAlarmEpoch(time_t ts, Alarm_Match match, Alarm name);
void setAlarmEpoch(time_t ts, Alarm_Match match = MATCH_DHHMMSS, uint32_t subSeconds = 0, Alarm name = ALARM_A);

void getPrediv(uint32_t *predivA, uint32_t *predivS);
void setPrediv(uint32_t predivA, uint32_t predivS);

bool isConfigured(void)
{
return RTC_IsConfigured();
Expand Down

0 comments on commit fb5b8ab

Please sign in to comment.