2018-12-15 08:27 AM
Hi
I have setup the RTC on the STM32L433 and the clock is running about 5 second fast in 24 hours. I am using a 32.768KHz crystal, which is directly next to the processor . I have set the
RTC->PREDIV_A to 0x 7f, and RTC->PREDIV_A to 0x ff, and selected the LSE clock, cant think what it can be, anyone help?
Thanks
Scott
2018-12-15 08:40 AM
Double check the measured frequency. Should be able to benchmark via a connected TIM, or if available on an MCO pin.
Check loading, check trim settings.
2018-12-15 09:16 AM
5 second in a day is about 60 ppm. Crystal tolerance is oftne 50 ppm plus some load capacitance mismatch and your number can be explained.
2018-12-15 09:37 AM
You can use TIM16
Example here for measuring LSI here, but can select LSE
STM32Cube_FW_L4_V1.13.0\Projects\NUCLEO-L432KC\Examples\RTC\RTC_LSI\Src\main.c
The math here is broken, destroys accuracy
/* Frequency computation */
uwLsiFreq = (uint32_t) SystemCoreClock / uwPeriodValue;
uwLsiFreq *= 8;
Use
uwLsiFreq = (SystemCoreClock * 8) / uwPeriodValue;
2018-12-16 01:26 AM
Use HAL_RTCEx_SetSmoothCalib to tune the frequency. Here is my clock set up. Note Prediv setting are different to allow sub second reading
The calibration value (Option.RTC_calibrate in my code) can be between -511 and + 512. n specifies the number of extra clock pulses to be "created" or "removed" every 2^20 real clock pulses. If my maths are correct therefore a change of +/-1 should equate to about 0.0824 seconds per day.
/* RTC init function */
static void myMX_RTC_Init(void)
{
int up=RTC_SMOOTHCALIB_PLUSPULSES_RESET;
int calibrate= -Option.RTC_Calibrate;
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
/* 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 = 7;
hrtc.Init.SynchPrediv = 4095;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
if (HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
if (HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
RtcGetTime();
if(sDate.Year<18){
/**Initialize RTC and set the Time and Date
*/
sTime.Hours = 0x0;
sTime.Minutes = 0x0;
sTime.Seconds = 0x0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 0x1;
sDate.Year = 0x0;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
if(Option.RTC_Calibrate>0){
up=RTC_SMOOTHCALIB_PLUSPULSES_SET;
calibrate=512-Option.RTC_Calibrate;
}
HAL_RTCEx_SetSmoothCalib(&hrtc, RTC_SMOOTHCALIB_PERIOD_32SEC, up, calibrate);
}
2018-12-16 08:07 AM
Thanks Clive.
I will have to download the examples, do I have to install the latest cubemx , which wil instalkl the examples?
Scott
2018-12-16 08:09 AM
Thanks Peter
I will look into this. I am only using the 32.768KHz as the external crystal, what does it calibrate it against? Wouldnt it need a known frequency to calibrate against?
Scott
2018-12-16 09:26 AM
"Wouldnt it need a known frequency to calibrate against?"
The real world :)
"the clock is running about 5 second fast in 24 hours"
You would start with a value of -5/0.0824 = -61
this converts to
HAL_RTCEx_SetSmoothCalib(&hrtc, RTC_SMOOTHCALIB_PERIOD_32SEC, RTC_SMOOTHCALIB_PLUSPULSES_RESET, 61);
I you were 5 seconds slow it would be
HAL_RTCEx_SetSmoothCalib(&hrtc, RTC_SMOOTHCALIB_PERIOD_32SEC, RTC_SMOOTHCALIB_PLUSPULSES_SET, 512-61);
2018-12-16 10:05 AM
Typically an HSE supplied source, or you measuring it via MCO pin output on a test fixture.
2018-12-16 10:06 AM
They should be in the repository if you have previously installed the L4 libraries. You can alternately pull the CubeL4 ZIP file an unpack and use that.