Skip to content

Commit fac8ab6

Browse files
authored
feat(stm32F1): set/get prescaler div interface change (#56)
change the set/get prescaler divider Interface in case of stm32F1 The stm32F1 prescaler is asynchronous and mapped on a 32 bits param. Signed-off-by: Francois Ramu <[email protected]>
1 parent f2bfa27 commit fac8ab6

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

src/STM32RTC.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ void STM32RTC::setClockSource(Source_Clock source)
133133
}
134134
}
135135

136+
#if defined(STM32F1xx)
137+
/**
138+
* @brief get user asynchronous prescaler value for the current clock source.
139+
* @param predivA: pointer to the current Asynchronous prescaler value
140+
* @param dummy : not used (kept for compatibility reason)
141+
* @retval None
142+
*/
143+
void STM32RTC::getPrediv(uint32_t *predivA, int16_t *dummy)
144+
{
145+
UNUSED(dummy);
146+
RTC_getPrediv(predivA);
147+
}
148+
#else
136149
/**
137150
* @brief get user (a)synchronous prescaler values if set else computed
138151
* ones for the current clock source.
@@ -146,7 +159,22 @@ void STM32RTC::getPrediv(int8_t *predivA, int16_t *predivS)
146159
RTC_getPrediv(predivA, predivS);
147160
}
148161
}
162+
#endif /* STM32F1xx */
149163

164+
#if defined(STM32F1xx)
165+
/**
166+
* @brief set user asynchronous prescalers value.
167+
* @note This method must be called before begin().
168+
* @param predivA: Asynchronous prescaler value. Reset value: RTC_AUTO_1_SECOND
169+
* @param dummy : not used (kept for compatibility reason)
170+
* @retval None
171+
*/
172+
void STM32RTC::setPrediv(uint32_t predivA, int16_t dummy)
173+
{
174+
UNUSED(dummy);
175+
RTC_setPrediv(predivA);
176+
}
177+
#else
150178
/**
151179
* @brief set user (a)synchronous prescalers value.
152180
* @note This method must be called before begin().
@@ -158,6 +186,7 @@ void STM32RTC::setPrediv(int8_t predivA, int16_t predivS)
158186
{
159187
RTC_setPrediv(predivA, predivS);
160188
}
189+
#endif /* STM32F1xx */
161190

162191
/**
163192
* @brief enable the RTC alarm.

src/STM32RTC.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,13 @@ class STM32RTC {
189189
void setY2kEpoch(uint32_t ts);
190190
void setAlarmEpoch(uint32_t ts, Alarm_Match match = MATCH_DHHMMSS, uint32_t subSeconds = 0);
191191

192+
#if defined(STM32F1xx)
193+
void getPrediv(uint32_t *predivA, int16_t *dummy = nullptr);
194+
void setPrediv(uint32_t predivA, int16_t dummy = 0);
195+
#else
192196
void getPrediv(int8_t *predivA, int16_t *predivS);
193197
void setPrediv(int8_t predivA, int16_t predivS);
194-
198+
#endif /* STM32F1xx */
195199
bool isConfigured(void)
196200
{
197201
return _configured;

src/rtc.c

+31-10
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ static uint8_t HSEDiv = 0;
6666
static uint8_t predivSync_bits = 0xFF;
6767
static int8_t predivAsync = -1;
6868
static int16_t predivSync = -1;
69+
#else
70+
static uint32_t prediv = RTC_AUTO_1_SECOND;
6971
#endif /* !STM32F1xx */
7072

7173
static hourFormat_t initFormat = HOUR_FORMAT_12;
@@ -203,6 +205,20 @@ static void RTC_initClock(sourceClock_t source)
203205
__HAL_RCC_RTC_ENABLE();
204206
}
205207

208+
#if defined(STM32F1xx)
209+
/**
210+
* @brief set user asynchronous prescaler value.
211+
* @note use RTC_AUTO_1_SECOND to reset value
212+
* @param asynch: asynchronous prescaler value in range 0 - PREDIVA_MAX
213+
* @retval None
214+
*/
215+
void RTC_setPrediv(uint32_t asynch)
216+
{
217+
/* set the prescaler for a stm32F1 (value is hold by one param) */
218+
prediv = asynch;
219+
LL_RTC_SetAsynchPrescaler(RTC, asynch);
220+
}
221+
#else
206222
/**
207223
* @brief set user (a)synchronous prescaler values.
208224
* @note use -1 to reset value and use computed ones
@@ -212,7 +228,6 @@ static void RTC_initClock(sourceClock_t source)
212228
*/
213229
void RTC_setPrediv(int8_t asynch, int16_t synch)
214230
{
215-
#if !defined(STM32F1xx)
216231
if ((asynch >= -1) && ((uint32_t)asynch <= PREDIVA_MAX) && \
217232
(synch >= -1) && ((uint32_t)synch <= PREDIVS_MAX)) {
218233
predivAsync = asynch;
@@ -221,12 +236,22 @@ void RTC_setPrediv(int8_t asynch, int16_t synch)
221236
RTC_computePrediv(&predivAsync, &predivSync);
222237
}
223238
predivSync_bits = (uint8_t)_log2(predivSync) + 1;
224-
#else
225-
UNUSED(asynch);
226-
UNUSED(synch);
227-
#endif /* !STM32F1xx */
228239
}
240+
#endif /* STM32F1xx */
229241

242+
#if defined(STM32F1xx)
243+
/**
244+
* @brief get user asynchronous prescaler value for the current clock source.
245+
* @param asynch: pointer where return asynchronous prescaler value.
246+
* @retval None
247+
*/
248+
void RTC_getPrediv(uint32_t *asynch)
249+
{
250+
/* get the prescaler for a stm32F1 (value is hold by one param) */
251+
prediv = LL_RTC_GetDivider(RTC);
252+
*asynch = prediv;
253+
}
254+
#else
230255
/**
231256
* @brief get user (a)synchronous prescaler values if set else computed ones
232257
* for the current clock source.
@@ -236,7 +261,6 @@ void RTC_setPrediv(int8_t asynch, int16_t synch)
236261
*/
237262
void RTC_getPrediv(int8_t *asynch, int16_t *synch)
238263
{
239-
#if !defined(STM32F1xx)
240264
if ((predivAsync == -1) || (predivSync == -1)) {
241265
RTC_computePrediv(&predivAsync, &predivSync);
242266
}
@@ -245,11 +269,8 @@ void RTC_getPrediv(int8_t *asynch, int16_t *synch)
245269
*synch = predivSync;
246270
}
247271
predivSync_bits = (uint8_t)_log2(predivSync) + 1;
248-
#else
249-
UNUSED(asynch);
250-
UNUSED(synch);
251-
#endif /* !STM32F1xx */
252272
}
273+
#endif /* STM32F1xx */
253274

254275
#if !defined(STM32F1xx)
255276
/**

src/rtc.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ typedef void(*voidCallbackPtr)(void *);
9494
#endif
9595
#define PREDIVA_MAX (RTC_PRER_PREDIV_A >> RTC_PRER_PREDIV_A_Pos)
9696
#define PREDIVS_MAX (RTC_PRER_PREDIV_S >> RTC_PRER_PREDIV_S_Pos)
97+
#else
98+
/* for stm32F1 the MAX value is combining PREDIV low & high registers */
99+
#define PREDIVA_MAX 0xFFFFFU
97100
#endif /* !STM32F1xx */
98101

99102
#if defined(STM32F0xx) || defined(STM32L0xx) || \
@@ -135,9 +138,13 @@ static uint32_t RTC_getSource(void) {
135138
/* Exported macro ------------------------------------------------------------*/
136139
/* Exported functions ------------------------------------------------------- */
137140
void RTC_SetClockSource(sourceClock_t source);
138-
141+
#if defined(STM32F1xx)
142+
void RTC_getPrediv(uint32_t *asynch);
143+
void RTC_setPrediv(uint32_t asynch);
144+
#else
139145
void RTC_getPrediv(int8_t *asynch, int16_t *synch);
140146
void RTC_setPrediv(int8_t asynch, int16_t synch);
147+
#endif /* STM32F1xx */
141148

142149
void RTC_init(hourFormat_t format, sourceClock_t source, bool reset);
143150
void RTC_DeInit(void);

0 commit comments

Comments
 (0)