Issue with the RTC REF_IN on STM32F439NIHx
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);
}