cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F411 RTC 32768 cal Freq only 490Hz

erastusC
Associate III

Post Edited by ST moderator to apply source code formatting to comply with the community rule:


I wonder if any one can help me.

The rtc Cal freq produced by the init code below produces on 490Hz.  The RTC clock is loosing about 55 sec in 20 minutes.

Is there a way to increase this freq without replacing the crystal?

 

 
static void MX_RTC_Init(void)

{

/* USER CODE BEGIN RTC_Init 0 */

/* USER CODE END RTC_Init 0 */



RTC_TimeTypeDef sTime = {0};

RTC_DateTypeDef sDate = {0};

/* USER CODE BEGIN RTC_Init 1 */

/* USER CODE END RTC_Init 1 */

/** Initialize RTC Only */

hrtc.Instance = RTC;

hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

hrtc.Init.AsynchPrediv = 127;

hrtc.Init.SynchPrediv = 255;

hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

if (HAL_RTC_Init(&hrtc) != HAL_OK)

{

Error_Handler();

}



/* USER CODE BEGIN Check_RTC_BKUP */



/* USER CODE END Check_RTC_BKUP */



/** Initialize RTC and set the Time and Date

*/

sTime.Hours = 18;

sTime.Minutes = 4;

sTime.Seconds = 11;

sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

sTime.StoreOperation = RTC_STOREOPERATION_RESET;

if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)

{

Error_Handler();

}

sDate.WeekDay = RTC_WEEKDAY_SATURDAY;

sDate.Month = RTC_MONTH_JANUARY;

sDate.Date = 24;

sDate.Year = 26;



if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)

{

Error_Handler();

}



/** Enable Calibrartion

*/

if (HAL_RTCEx_SetCalibrationOutPut(&hrtc, RTC_CALIBOUTPUT_512HZ) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN RTC_Init 2 */



/* USER CODE END RTC_Init 2 */



}

 

 

Thank you

11 REPLIES 11
mƎALLEm
ST Employee

Hello,

1- Please use </> button to share your code. Read How to write your question to maximize your chances to find a solution. I invite you to edit your post to be inline to this community rule.

2- This knowledge base article may help you: How to calibrate the STM32's real-time clock (RTC)

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
TDK
Super User

> Is there a way to increase this freq without replacing the crystal?

No, 55s in 20min is 5% off. That's 50000 ppm. That is way more than you can correct for in software. You'll have to fix whatever is wrong with the circuit.

If you feel a post has answered your question, please click "Accept as Solution".
Peter BENSCH
ST Employee

@erastusC 

You didn't specify which clock you are using to test the RTC. As @TDK said, your deviation is so large that it can't possibly have anything to do with a (real) 32768Hz crystal.

Let's do the math: if you multiply your 490Hz by the corresponding divisor of 64, you get 31360Hz. Then you look at the documentation of the STM32F411 (data sheet) and see that the free-running LSI has a frequency of min 17kHz, typ 32kHz and max 47kHz – do you notice anything?

Exactly – you clocked the RTC from the LSI and not from the LSE.

Regards
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
waclawek.jan
Super User

... or the LSE circuit is badly disturbed by the calib-out signal.

waclawekjan_0-1770456900579.png

JW

PS. [rant mode on] Couldn't errata be proof-read? Those references to packages are a mess.

Hi Peter,

Thanks for the info. What I understand is this configuration selects the Crystal for the RTC.

Am I doing something wrong?

The line of code according to the documentation should either increase or decease the clock.  The Freq on the Oscilloscope remains 490Hz no matter what I do to the 0x1F0 value.

/ HAL_RTCEx_SetSmoothCalib(&hrtc, RTC_SMOOTHCALIB_PERIOD_8SEC,

/  RTC_SMOOTHCALIB_PLUSPULSES_RESET, 0x1F0);

 

erastusC_1-1770460832630.png

 

 

 

 

 

 

 

+

Thank you for your response.   DO you suggest Once I do the calibration I disable PC13 pin as it might affect the crystal osc?

The way I read the errata sheet, you can't even use PC13 for calibration, as (ironically) the signal you use in the calibration process may disturb the very clock you want to calibrate.

The way I read the errata sheet, you can't even use PC13 for calibration, as (ironically) the signal you use in the calibration process may disturb the very clock you want to calibrate.

Basically, yes. Errata are all about blunders like these.

In many STM32 there are secondary RTC outputs, outside of the low-power backup/VBAT-power-domain, where you can redirect the calibration output (they sometimes have associated errata, too, but of different nature). Not in 'F411, though.

Another way to calibrate, somewhat indirectly, is to use the LSE-to-TIM5 connection to compare the LSE to a known good time source. Another option is to output LSE onto MCO and measure there.

During the weekend, I was playing with a Nucleo-F411. It has (in the version I have, which I believe is the second one, I don't have it at hand and I plan to write a longer writeup on that experiment when time allows) an on-board through-hole 32.768kHz crystal and the ground arrangement of the burden capacitors which is IMO less than optimal. Running experiments only with the LSE and PC13 (i.e. no other internal modules active, nor any other pins toggling, nor any external circuitry being active (not that there is any at the Nucleo, just wanted to stress the "quietness" of the environment)) I found out, that outputting the 512Hz calibration onto PC13 did cause increased jitter at the LSE output (as monitored through MCO), but it did not change the LSE frequency (as monitored through said TIM5, comparing to HSE fed from the STLink's MCO which has a 8MHz crystal as primary source), or at least not significantly enough so that I would observe it (short-term it stayed within 1ppm). Increasing the LSE drive to high significantly decreased that jitter, too.

I have also toggled PC13 as a GPIO output, and also set it to input and toggled it from a different pin. In both these cases the toggling was asynchronous to LSE, and it caused not only jitter on LSE, but also a changing frequency, again in short term (1s) up to +-5ppm.

What I am getting at is, that while the issue outlined in the erratum is certainly there, its severity will surely depend on the particular design of the LSE circuitry and the nature of the signal at PC13. As the frequency difference observed by OP is far far more dramatic than a few ppm, I bet that either the LSE design is such that its stability is poor even without the signal at PC13, or it's what @Peter BENSCH hinted above, that LSI is used instead of LSE as RTC clock.

Clicking in CubeMX alone sometimes does not lead to functional results. In this particular case, for example, if LSI was ever selected as RTC clock and there was no backup-domain reset of either form, then it does not matter that subsequent code is written so that it would use LSE, LSI remains to be the RTC clock source. Debugging should always start with reading out and checking the relevant registers (here, RCC_BDCR, and possibly RTC registers).

JW

Thank you for the explanation. I have never used an F411, but an interesting read nonetheless.

Definitely – register contents need to be checked, to see if the RTC clock source is indeed what it is supposed to be. Reading register contents with STM32CubeProgrammer through the SWD interface has helped me solve problems countless times.