cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with the RTC REF_IN on STM32F439NIHx

DPitt.2
Associate II

Hi,

I'm having some trouble with trying to correct the RTC drift on a STM32F439NIHx. I've enabled the REF_IN and provided a 50Hz signal on the pin, but the RTC still drifts by around 1s per day.

I've also tested that the 50Hz signal is correctly received by changing the pin to an external interrupt and correctly generated 50 interrupts per seconds.

Am I missing something or doing something wrong in the setup:

Enabling the RTC and the ref in :

  RTC_HandleTypeDef hrtc = RTC_HandleTypeDef();
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat     = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv   = RTC_ASYNCH_PREDIV;
  hrtc.Init.SynchPrediv    = RTC_SYNCH_PREDIV;
  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();
  }
  
  if (HAL_RTCEx_EnableBypassShadow(hrtc) != HAL_OK)
  {
      Error_Handler();  
  }
 
  if (HAL_RTCEx_SetRefClock(hrtc) != HAL_OK)
  {
      Error_Handler();
  }

and the HAL_RTC_MspInit function:

void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc) {
  RCC_OscInitTypeDef RCC_OscInitStruct          = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct  = {0};
  GPIO_InitTypeDef GPIO_InitStruct              = {0};
 
  /**RTC GPIO Configuration
  PB15     ------> RTC_REFIN
  */
 
   GPIO_InitStruct.Pin = GPIO_PIN_15;
   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 
   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
   HAL_GPIO_LockPin(GPIOB, RTC_REF_IN_PIN);  // Lock Pin configuration
 
   // Configure LSE 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) {
      // Initialization Error
      return;
   }
   
   PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
 
   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
      // Initialization Error
      return;
   }
 
   // Enable RTC peripheral Clocks
   __HAL_RCC_RTC_ENABLE();
 
   // Configure the NVIC for RTC Alarm
   HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0x0F, 0);
   HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
}

1 ACCEPTED SOLUTION

Accepted Solutions

> GPIO_InitStruct.Pin = GPIO_PIN_15;

> GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

According to Datasheet, RTC_REFIN is PB15 AF0, so you want to set it to Alternate Function.

JW

View solution in original post

17 REPLIES 17

I don't have experience with the REF_IN facility, but 1s/day is around 10ppm, are you sure your 50Hz source more precise than that?

What's the raw precision of the LSE?

At any case, read out and check/post content of RTC and RCC_BDTR registers.

JW

DPitt.2
Associate II

The 50Hz source is a lot more precise than that, under 0.01ppm.

I'm not sure about the LSE precision, but given we get the 1s/day drift also when we don't set the REF_IN, probably around 10ppm.

I don't see a RCC_BDTR register, but RCC_BDCR has the value 0x00008103

The RTC registers are as follow:

RTC_CR        0x00001130
RTC_ISR       0x00000017
RTC_PRER      0x007F00FF
RTC_WURT      0X0000FFFF
RTC_CALIBR    0X00000000
RTC_ALRMAR    0XC1808080
RTC_ALRMBR    0X00000000
RTC_ALRMASSR  0X0F000000
RTC_RESERVED7 0X00002000
RTC_BKP0R     0X000032F2

with the other RTC registers at 0x00000000, or changing for TR, DR and SSR

> RTC_BKP0R 0X000032F2

Oh, that unnecessary Cube "signature".

Which makes me to ask: do you power-on/reset the device within the 24-hour period? If yes, couldn't it be this?

JW

DPitt.2
Associate II

No, though I also have that issue with the reset, in this case the drift happens without any reset on the device during the test.. (The drift is also forward, gaining 1s/day, so it can't be caused by a similar issue, since it would then lose 1s/day).

AScha.3
Chief III

>under 0.01ppm

thats 10e-8 -- so you have an atomic clock around all the time ?

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

GPS or AC Grid synchronization..​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

obvious ... but : afaik GPS makes 1 Hz output, not 50.

and grid ... now i looked: actual -240ppm away, so "under 0.01ppm" you might get in 10..20 month average, never in 24 h. https://www.netzfrequenzmessung.de/

that's why I'm puzzled...

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

The 50Hz signal I use for the REF_IN is indeed provided by an atomic clock.

> GPIO_InitStruct.Pin = GPIO_PIN_15;

> GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

According to Datasheet, RTC_REFIN is PB15 AF0, so you want to set it to Alternate Function.

JW