HAL_Delay() doesn't synch with RTC on STM32H7A3
I'm rewriting some of my old LL code to use HAL and having trouble getting the RTC to read time correctly. I have a simple test project where I'm calling HAL_RTC_GetTime() and HAL_RTC_GetDate() before and after a call to HAL_Delay() with the expectation that the difference in the 2 RTC calls will be something close the the HAL_Delay but that isn't happening. I running on a custom SOM we've developed that uses a STM32H7A3LIH6Q with a 24 MHz HSE crystal and a 32.767 kHz LSE crystal.
Here's my code:
#include "main.h"
#include "rtc.h"
#include "gpio.h"
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_RTC_Init();
/* USER CODE BEGIN 2 */
RTC_TimeTypeDef newTime1;
RTC_DateTypeDef newDate1;
RTC_TimeTypeDef newTime2;
RTC_DateTypeDef newDate2;
//Delay to get some non-zero value in newTime and newDate
HAL_Delay(4500);
//Initialize newTime1 and 2, newDate1 and 2
HAL_RTC_GetTime(&hrtc, &newTime2, RTC_FORMAT_BCD);
HAL_RTC_GetDate(&hrtc, &newDate2, RTC_FORMAT_BCD);
HAL_RTC_GetTime(&hrtc, &newTime1, RTC_FORMAT_BCD);
HAL_RTC_GetDate(&hrtc, &newDate1, RTC_FORMAT_BCD);
uint32_t delay = 1500;
for (int i = 0; i < 6; i++)
{
HAL_GPIO_TogglePin(LEDsom_GPIO_Port, LEDsom_Pin);
HAL_Delay(delay);
}
HAL_RTC_GetTime(&hrtc, &newTime2, RTC_FORMAT_BCD);
HAL_RTC_GetDate(&hrtc, &newDate2, RTC_FORMAT_BCD);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/*AXI clock gating */
RCC->CKGAENR = 0xFFFFFFFF;
/** Supply configuration update enable
*/
HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
/** Configure LSE Drive Capability
*/
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 3;
RCC_OscInitStruct.PLL.PLLN = 70;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
|RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
Error_Handler();
}
}
I would l have expected to see something like 4 in the newTime1.Seconds field. The blinking LED for loop runs 6 times with a 1500 mSec delay so a total of 9 seconds which I've verified with a stop watch. So I would have expected to see something like 13 or 14 second in the New Time2.Seconds field. But here's what shows up in the Variables window.

As a test, I modified the hrtc.Init.SynchPrediv from the default 255 to 127 and see newTime1.Seconds = 4 and newTime2.Seconds=19. But 255 is supposed to be the correct value for a 32.767 kHz LSE crystal according to the Reference Manual.
I started by creating a CubeMX project with nothing but the RTC enabled like this:

The clock configuration is this:

Any ideas on what I'm doing wrong and how to fix it will be greatly appreciated.
Thanks
