cancel
Showing results for 
Search instead for 
Did you mean: 

RTC on STM32H745

DMast.1
Associate II

Hi,

We are using a STM32H745 in our project. Over the past few days, I've been wasting a lot of time figuring out why RTC has a major error ... it always runs slower than it should. To be sure we tried to used the crystal and the capacities present in the ST demo board (STM32H747 disco) bu the problem still remain.

In the event of a power failure, the RTC remains powered through a supercap.

The waveform on the crystal pin seems to be there, a sine wave of 500mVpp @ 36.768KHz .... below is the code used for initialization:

HalErr_tError HalRtc_Init(void)
{
   HalErr_tError     zError = HalErr_eNoError;
 
   /* Configure RTC prescaler and RTC data registers */
   /* RTC configured as follows:
       - Hour Format    = Format 24
       - Asynch Prediv  = Value according to source clock
       - Synch Prediv   = Value according to source clock
       - OutPut         = Output Disable
       - OutPutPolarity = High Polarity
       - OutPutType     = Open Drain */
   gRtcHandle.Instance = RTC;
 
   gRtcHandle.Init.HourFormat     = RTC_HOURFORMAT_24;
   gRtcHandle.Init.AsynchPrediv   = RTC_ASYNCH_PREDIV;
   gRtcHandle.Init.SynchPrediv    = RTC_SYNCH_PREDIV;
   gRtcHandle.Init.OutPut         = RTC_OUTPUT_DISABLE;
   gRtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
   gRtcHandle.Init.OutPutType     = RTC_OUTPUT_TYPE_OPENDRAIN;
   if (HAL_RTC_Init(&gRtcHandle) != HAL_OK)
   {
      /* Initialization Error */
      zError = HalErr_eInternal;
   }
 
   /** Enable the reference Clock input */
   if (HAL_RTCEx_SetRefClock(&gRtcHandle) != HAL_OK)
   {
      zError = HalErr_eInternal;
   }
 
   return zError;
}
void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
{
   RCC_OscInitTypeDef        RCC_OscInitStruct;
   RCC_PeriphCLKInitTypeDef  PeriphClkInitStruct;
 
   /*##-1- Enables access to the backup domain ######*/
   /* To enable access on RTC registers */
   HAL_PWR_EnableBkUpAccess();
 
   /*##-2- Configure LSE/LSI as RTC clock source ###############################*/
   RCC_OscInitStruct.OscillatorType =  RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
   RCC_OscInitStruct.LSEState = RCC_LSE_ON;
   RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
   if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
   {
      while(1){};
   }
 
   PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
   if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
   {
      while(1){};
   }
   /* Configures the External Low Speed oscillator (LSE) drive capability */
   __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_HIGH);
}

Any idea to fix this issue?

Thanks in advance,

DM

6 REPLIES 6

> it always runs slower than it should

Roughly half a second per device reset? Avoid RTC initialization when it's already initialized. Search this forum.

JW

DMast.1
Associate II

The RTC is initialize only on startup.....I leave the board on during knight and morning after the RTC time is few minues later.

DM

No sleep/wakeup either, possibly going through some Cube initialization?

2 minutes during 12 hours is equivalent of 10 seconds per hour.

Try an absolutely minimal setup, *nothing* more than initializing RTC, run it during one hour and check, 10 seconds delay should be visible.

Make sure the RTC runs from LSE, by reading out the respective RCC/RTC registers content; post them here.

JW

DMast.1
Associate II

Good morning JW,

attached the RTC register status after initialization.

Now I'm modifing my project to get and test a minimal setup.

Thanks,

DM

DMast.1
Associate II

Good monrning JW,

in the last test the RTC losed about 5sec in 24Hour of test...so it seems to be better.....but is it possible to achieve a better precision?

I see that with an LSE of 36768 with 12pF of suggest resonance capacitance the RTC is very slow even if I change the value of HAL_RCC_LSEDRIVE_CONFIG(..)

function's parameters. Now I'm using RCC_LSEDRIVE_LOW value with 1,8pF with this cristal CC7V-T1A 32.768KHZ.

The RTC seems to goes well only if the cristal capacitance is less....about 2pF...even if this value is not suggest by cristal manifacturer.

thanks,

DM

> even if this value is not suggest by cristal manifacturer.

What the crystal manufacturer suggests as loading capacitance is not equal to the capacitance of capacitors you should place next to the crystal. Don't forget, that those two capacitors are effectively in series, and also that there are significant parasitic capacitances on your board (the pin's parasitic capacitance, plus the tracks). Read AN2867.

Unfortunately, those parasitics are hard to determine. Also, as you've mentioned, the oscillator "drive" does influence the frequency, too. Also, there may be noise sources on the board which may influence the oscillator, so it's not a simple task to arrive at a precise timing. 5 sec / 24 hours is roughly 60ppm, which is not stellar, but also not quite bad.

If your oscillator is stable even if not precise, you can fine-tune it digitally, read RTC smooth digital calibration subchapter of RTC chapter in RM.

JW