Skip to content

stm32F1 set/get prescaler div interface change #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/STM32RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ void STM32RTC::setClockSource(Source_Clock source)
}
}

#if defined(STM32F1xx)
/**
* @brief get user asynchronous prescaler value for the current clock source.
* @param predivA: pointer to the current Asynchronous prescaler value
* @param dummy : not used (kept for compatibility reason)
* @retval None
*/
void STM32RTC::getPrediv(uint32_t *predivA, int16_t *dummy)
{
UNUSED(dummy);
RTC_getPrediv(predivA);
}
#else
/**
* @brief get user (a)synchronous prescaler values if set else computed
* ones for the current clock source.
Expand All @@ -146,7 +159,22 @@ void STM32RTC::getPrediv(int8_t *predivA, int16_t *predivS)
RTC_getPrediv(predivA, predivS);
}
}
#endif /* STM32F1xx */

#if defined(STM32F1xx)
/**
* @brief set user asynchronous prescalers value.
* @note This method must be called before begin().
* @param predivA: Asynchronous prescaler value. Reset value: RTC_AUTO_1_SECOND
* @param dummy : not used (kept for compatibility reason)
* @retval None
*/
void STM32RTC::setPrediv(uint32_t predivA, int16_t dummy)
{
UNUSED(dummy);
RTC_setPrediv(predivA);
}
#else
/**
* @brief set user (a)synchronous prescalers value.
* @note This method must be called before begin().
Expand All @@ -158,6 +186,7 @@ void STM32RTC::setPrediv(int8_t predivA, int16_t predivS)
{
RTC_setPrediv(predivA, predivS);
}
#endif /* STM32F1xx */

/**
* @brief enable the RTC alarm.
Expand Down
6 changes: 5 additions & 1 deletion src/STM32RTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,13 @@ class STM32RTC {
void setY2kEpoch(uint32_t ts);
void setAlarmEpoch(uint32_t ts, Alarm_Match match = MATCH_DHHMMSS, uint32_t subSeconds = 0);

#if defined(STM32F1xx)
void getPrediv(uint32_t *predivA, int16_t *dummy = nullptr);
void setPrediv(uint32_t predivA, int16_t dummy = 0);
#else
void getPrediv(int8_t *predivA, int16_t *predivS);
void setPrediv(int8_t predivA, int16_t predivS);

#endif /* STM32F1xx */
bool isConfigured(void)
{
return _configured;
Expand Down
41 changes: 31 additions & 10 deletions src/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ static uint8_t HSEDiv = 0;
static uint8_t predivSync_bits = 0xFF;
static int8_t predivAsync = -1;
static int16_t predivSync = -1;
#else
static uint32_t prediv = RTC_AUTO_1_SECOND;
#endif /* !STM32F1xx */

static hourFormat_t initFormat = HOUR_FORMAT_12;
Expand Down Expand Up @@ -203,6 +205,20 @@ static void RTC_initClock(sourceClock_t source)
__HAL_RCC_RTC_ENABLE();
}

#if defined(STM32F1xx)
/**
* @brief set user asynchronous prescaler value.
* @note use RTC_AUTO_1_SECOND to reset value
* @param asynch: asynchronous prescaler value in range 0 - PREDIVA_MAX
* @retval None
*/
void RTC_setPrediv(uint32_t asynch)
{
/* set the prescaler for a stm32F1 (value is hold by one param) */
prediv = asynch;
LL_RTC_SetAsynchPrescaler(RTC, asynch);
}
#else
/**
* @brief set user (a)synchronous prescaler values.
* @note use -1 to reset value and use computed ones
Expand All @@ -212,7 +228,6 @@ static void RTC_initClock(sourceClock_t source)
*/
void RTC_setPrediv(int8_t asynch, int16_t synch)
{
#if !defined(STM32F1xx)
if ((asynch >= -1) && ((uint32_t)asynch <= PREDIVA_MAX) && \
(synch >= -1) && ((uint32_t)synch <= PREDIVS_MAX)) {
predivAsync = asynch;
Expand All @@ -221,12 +236,22 @@ void RTC_setPrediv(int8_t asynch, int16_t synch)
RTC_computePrediv(&predivAsync, &predivSync);
}
predivSync_bits = (uint8_t)_log2(predivSync) + 1;
#else
UNUSED(asynch);
UNUSED(synch);
#endif /* !STM32F1xx */
}
#endif /* STM32F1xx */

#if defined(STM32F1xx)
/**
* @brief get user asynchronous prescaler value for the current clock source.
* @param asynch: pointer where return asynchronous prescaler value.
* @retval None
*/
void RTC_getPrediv(uint32_t *asynch)
{
/* get the prescaler for a stm32F1 (value is hold by one param) */
prediv = LL_RTC_GetDivider(RTC);
*asynch = prediv;
}
#else
/**
* @brief get user (a)synchronous prescaler values if set else computed ones
* for the current clock source.
Expand All @@ -236,7 +261,6 @@ void RTC_setPrediv(int8_t asynch, int16_t synch)
*/
void RTC_getPrediv(int8_t *asynch, int16_t *synch)
{
#if !defined(STM32F1xx)
if ((predivAsync == -1) || (predivSync == -1)) {
RTC_computePrediv(&predivAsync, &predivSync);
}
Expand All @@ -245,11 +269,8 @@ void RTC_getPrediv(int8_t *asynch, int16_t *synch)
*synch = predivSync;
}
predivSync_bits = (uint8_t)_log2(predivSync) + 1;
#else
UNUSED(asynch);
UNUSED(synch);
#endif /* !STM32F1xx */
}
#endif /* STM32F1xx */

#if !defined(STM32F1xx)
/**
Expand Down
9 changes: 8 additions & 1 deletion src/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ typedef void(*voidCallbackPtr)(void *);
#endif
#define PREDIVA_MAX (RTC_PRER_PREDIV_A >> RTC_PRER_PREDIV_A_Pos)
#define PREDIVS_MAX (RTC_PRER_PREDIV_S >> RTC_PRER_PREDIV_S_Pos)
#else
/* for stm32F1 the MAX value is combining PREDIV low & high registers */
#define PREDIVA_MAX 0xFFFFFU
#endif /* !STM32F1xx */

#if defined(STM32F0xx) || defined(STM32L0xx) || \
Expand Down Expand Up @@ -135,9 +138,13 @@ static uint32_t RTC_getSource(void) {
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void RTC_SetClockSource(sourceClock_t source);

#if defined(STM32F1xx)
void RTC_getPrediv(uint32_t *asynch);
void RTC_setPrediv(uint32_t asynch);
#else
void RTC_getPrediv(int8_t *asynch, int16_t *synch);
void RTC_setPrediv(int8_t asynch, int16_t synch);
#endif /* STM32F1xx */

void RTC_init(hourFormat_t format, sourceClock_t source, bool reset);
void RTC_DeInit(void);
Expand Down