cancel
Showing results for 
Search instead for 
Did you mean: 

RTC Initialization Failure When Running Application from External Flash on NUCLEO-H7S3L8

QuocBui
Associate III

Environment:

  • Hardware: NUCLEO-H7S3L8
  • Software:
    • Bootloader: Loaded on internal flash
    • Application: Loaded on external flash

Hi Support Team,

We are currently facing an issue with the RTC module, where it fails to initialize and set up when running the application code from external flash.

Below are the details of the issue and the debugging steps we have attempted:

  1. We found that the RTC initialization fails due to a timeout error and gets stuck in a loop inside the function RTC_EnterInitMode(RTC_HandleTypeDef *hrtc) in the stm32h7rsxx_hal_rtc.c file.

    /* Wait till RTC is in INIT state and if timeout is reached, exit */
    while ((READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT))
    {
        if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
        {
            /* New check to avoid false timeout detection in case of preemption */
            if (READ_BIT(RTC->ICSR, RTC_ICSR_INITF) == 0U)
            {
                status = HAL_TIMEOUT;
    
                /* Change RTC state */
                hrtc->State = HAL_RTC_STATE_TIMEOUT;
            }
            else
            {
                break;
            }
        }
    }
    
    
  2. We attempted to initialize the RTC module in both the bootloader and the application code. Interestingly, the RTC initializes successfully in the bootloader but fails in the application.

  3. Based on similar issues discussed in the ST forums, we tried adding HAL_PWR_EnableBkUpAccess(); after the SystemClock_Config() function, but this did not resolve the issue.

    Related discussions:
  4. System Clock Configuration - SystemClock_Config() (Application - External Flash Loader):

    void SystemClock_Config(void)
    {
        RCC_OscInitTypeDef RCC_OscInitStruct = {0};
        RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    
        /* Configure the main internal regulator output voltage */
        if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE0) != HAL_OK)
        {
            Error_Handler();
        }
    
        /* Configure LSE Drive Capability */
        HAL_PWR_EnableBkUpAccess();
        __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
    
        /* Initialize the RCC Oscillators */
        RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
        RCC_OscInitStruct.LSEState = RCC_LSE_ON;
        RCC_OscInitStruct.HSIState = RCC_HSI_ON;
        RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
        RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
        RCC_OscInitStruct.LSIState = RCC_LSI_ON;
        RCC_OscInitStruct.PLL1.PLLState = RCC_PLL_ON;
        RCC_OscInitStruct.PLL1.PLLSource = RCC_PLLSOURCE_HSI;
        RCC_OscInitStruct.PLL1.PLLM = 4;
        RCC_OscInitStruct.PLL1.PLLN = 37;
        RCC_OscInitStruct.PLL1.PLLP = 1;
        RCC_OscInitStruct.PLL1.PLLQ = 2;
        RCC_OscInitStruct.PLL1.PLLR = 2;
        RCC_OscInitStruct.PLL1.PLLS = 2;
        RCC_OscInitStruct.PLL1.PLLT = 2;
        RCC_OscInitStruct.PLL1.PLLFractional = 4096;
        RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_ON;
        RCC_OscInitStruct.PLL2.PLLSource = RCC_PLLSOURCE_HSI;
        RCC_OscInitStruct.PLL2.PLLM = 4;
        RCC_OscInitStruct.PLL2.PLLN = 25;
        RCC_OscInitStruct.PLL2.PLLP = 2;
        RCC_OscInitStruct.PLL2.PLLQ = 2;
        RCC_OscInitStruct.PLL2.PLLR = 2;
        RCC_OscInitStruct.PLL2.PLLS = 2;
        RCC_OscInitStruct.PLL2.PLLT = 2;
        RCC_OscInitStruct.PLL2.PLLFractional = 0;
        RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;
        if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
        {
            Error_Handler();
        }
    
        /* Initialize CPU, AHB, and APB clocks */
        RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
                                      RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 |
                                      RCC_CLOCKTYPE_PCLK4 | RCC_CLOCKTYPE_PCLK5;
        RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
        RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
        RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
        RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
        RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
        RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
        RCC_ClkInitStruct.APB5CLKDivider = RCC_APB5_DIV2;
    
        if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
        {
            Error_Handler();
        }
    }
    
    

Additional Debugging Attempts:

  1. We tried configuring the RTC to use both LSE and LSI clock sources, but neither resolved the issue.

  2. We verified the clock ready bits, and they appear to be correctly configured:

    uint32_t BDCR_LSE = READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY);
    uint32_t BDCR_HSE = READ_BIT(RCC->CR, RCC_CR_HSERDY);
    uint32_t CR_HSI = READ_BIT(RCC->CR, RCC_CR_HSIRDY);
    
    

Could you please help us identify why the RTC module fails to initialize when running from external flash? Any insights or suggestions would be greatly appreciated.

Thank you in advance for your support.

0 REPLIES 0