Skip to content

Commit 1cfcfcd

Browse files
committed
Support auto RTC calibration
1 parent a9fdc93 commit 1cfcfcd

File tree

2 files changed

+59
-6
lines changed
  • cores/arduino/stm32
  • libraries/SrcWrapper/src/stm32

2 files changed

+59
-6
lines changed

cores/arduino/stm32/rtc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ void RTC_SetClockSource(sourceClock_t source);
150150
void RTC_getPrediv(int8_t *asynch, int16_t *synch);
151151
void RTC_setPrediv(int8_t asynch, int16_t synch);
152152

153+
uint32_t RTC_getCalib(); // sakinit
154+
void RTC_setCalib(uint32_t minusPulsesValue); // sakinit
155+
156+
uint32_t RTC_getDefaultClockFrequency(); //sakinit
157+
153158
void RTC_init(hourFormat_t format, sourceClock_t source, bool reset);
154159
void RTC_DeInit(void);
155160
bool RTC_IsTimeSet(void);

libraries/SrcWrapper/src/stm32/rtc.c

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ static void *callbackUserData = NULL;
6262

6363
static sourceClock_t clkSrc = LSI_CLOCK;
6464
static uint8_t HSEDiv = 0;
65-
#if !defined(STM32F1xx)
65+
6666
/* Custom user values */
6767
static int8_t userPredivAsync = -1;
6868
static int16_t userPredivSync = -1;
69-
#endif /* !STM32F1xx */
7069

7170
static hourFormat_t initFormat = HOUR_FORMAT_12;
7271

@@ -214,8 +213,10 @@ void RTC_setPrediv(int8_t asynch, int16_t synch)
214213
userPredivSync = synch;
215214
}
216215
#else
217-
UNUSED(asynch);
218-
UNUSED(synch);
216+
if (asynch >= -1) {
217+
userPredivAsync = asynch;
218+
userPredivSync = synch;
219+
}
219220
#endif /* !STM32F1xx */
220221
}
221222

@@ -238,8 +239,10 @@ void RTC_getPrediv(int8_t *asynch, int16_t *synch)
238239
}
239240
}
240241
#else
241-
UNUSED(asynch);
242-
UNUSED(synch);
242+
if ((asynch != NULL) && (synch != NULL)) {
243+
*asynch = userPredivAsync;
244+
*synch = userPredivSync;
245+
}
243246
#endif /* !STM32F1xx */
244247
}
245248

@@ -297,6 +300,45 @@ static void RTC_computePrediv(int8_t *asynch, int16_t *synch)
297300
}
298301
#endif /* !STM32F1xx */
299302

303+
/**
304+
* @brief RTC Calibration
305+
* This function calibrates the RTC (See AN2604 Application note).
306+
* By default the calibration is set to 0.
307+
* @note Call after RTC_Init() as the backup domain might get reset
308+
* when RTC_Init() or RTC_initClock() get called.
309+
* @param minusPulsesValue: specifies the RTC Clock Calibration value
310+
* @retval None
311+
*/
312+
void RTC_setCalib(uint32_t minusPulsesValue) // sakinit
313+
{
314+
#ifdef RTC_CALR_CALM
315+
const uint32_t Calib_Mask = RTC_CALR_CALM;
316+
#elif defined(STM32F1xx)
317+
const uint32_t Calib_Mask = 0x7FU;
318+
#endif
319+
HAL_RTCEx_SetSmoothCalib(&RtcHandle, 0, 0, minusPulsesValue & Calib_Mask);
320+
}
321+
322+
/**
323+
* @brief Returns the rtc peripheral clock frequency
324+
* @note Returns 0 if rtc peripheral clock is unknown (e.g. clock is not configured)
325+
* @retval Frequency in Hz (0: means that no available frequency for the peripheral)
326+
*/
327+
uint32_t RTC_getDefaultClockFrequency() //sakinit
328+
{
329+
return HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_RTC);
330+
}
331+
332+
/**
333+
* @brief Return RTC Calibration value
334+
* This function returns the calibration value.
335+
* @retval the RTC Clock Calibration value
336+
*/
337+
uint32_t RTC_getCalib() // sakinit
338+
{
339+
return LL_RTC_CAL_GetCoarseDigital(BKP);
340+
}
341+
300342
/**
301343
* @brief RTC Initialization
302344
* This function configures the RTC time and calendar. By default, the
@@ -318,8 +360,14 @@ void RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
318360
RtcHandle.Instance = RTC;
319361

320362
#if defined(STM32F1xx)
363+
if (userPredivAsync > -1) {
364+
/* Use the user specified value for the prescaler. */
365+
userPredivAsync &= 0x0F; // RTC_PRLH size 4 bits
366+
RtcHandle.Init.AsynchPrediv = (((uint32_t)userPredivAsync) << 16) + (uint32_t)userPredivSync;
367+
} else {
321368
/* Let HAL calculate the prescaler */
322369
RtcHandle.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
370+
}
323371
RtcHandle.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
324372
UNUSED(format);
325373
#else

0 commit comments

Comments
 (0)