initializationstm32auto-generatestm32cubeide

How to reorder the peripheral initialisation code that CubeID automatically generates


I'm using STM32CubeIDE v1.12.1(lasted version), the IDE provided by STM. Recently I started a new project, and it's a product that uses LCDs.

I'm adding external SDRAM and connecting it to the FMC and using it with the LTDC. While adding other peripherals first and implementing the LCD-related device driver last, we encountered an issue where the device kept getting into a Hard-Fault on power-up.

While analysing the cause, I found that the part of the device initialisation code that the IDE auto-generates is as follows.

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_QUADSPI_Init();
  MX_SPI1_Init();
  MX_I2C1_Init();
  MX_DMA2D_Init();
  MX_I2C2_Init();
  MX_RTC_Init();
  MX_USART1_UART_Init();
  MX_USART6_UART_Init();
  MX_LTDC_Init();
  MX_FMC_Init();
  MX_SPI6_Init();
  MX_SDMMC1_SD_Init();
  MX_FATFS_Init();
  /* USER CODE BEGIN 2 */

I suspected that the LCD's initialisation code was too far behind (specifically the FMC initialisation function) and was causing the problem, so I modified the sequence as follows.

  MX_GPIO_Init();
  MX_DMA_Init();
  MX_FMC_Init();
  MX_QUADSPI_Init();
  MX_SPI1_Init();
  MX_SPI6_Init();
  MX_I2C1_Init();
  MX_I2C2_Init();
  MX_USART1_UART_Init();
  MX_USART6_UART_Init();
  MX_LTDC_Init();
  MX_DMA2D_Init();
  MX_SDMMC1_SD_Init();
  MX_FATFS_Init();
  MX_RTC_Init();

The problem has since disappeared! The cause is unknown, but our best guess is that a critical error occurred during the initialisation sequence.

The problem is that each time CubeID generates the code automatically, the order of the initialisation code is reverted. I've temporarily disabled the auto-generated code using the preprocessor as follows, but this is a temporary measure.

    /**
     *          MX_FMC_Init() positioning problem
     *          this function must be immediately after MX_DMA_Init().
     */
    MX_GPIO_Init();
    MX_DMA_Init();
    MX_FMC_Init();
    MX_QUADSPI_Init();
    MX_SPI1_Init();
    MX_SPI6_Init();
    MX_I2C1_Init();
    MX_I2C2_Init();
    MX_USART1_UART_Init();
    MX_USART6_UART_Init();
    MX_LTDC_Init();
    MX_DMA2D_Init();
    MX_SDMMC1_SD_Init();
    MX_FATFS_Init();
    MX_RTC_Init();
#if 0
    /* USER CODE END SysInit */
    
    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_DMA_Init();
    MX_QUADSPI_Init();
    MX_SPI1_Init();
    MX_I2C1_Init();
    MX_DMA2D_Init();
    MX_I2C2_Init();
    MX_RTC_Init();
    MX_USART1_UART_Init();
    MX_USART6_UART_Init();
    MX_LTDC_Init();
    MX_FMC_Init();
    MX_SPI6_Init();
    MX_SDMMC1_SD_Init();
    MX_FATFS_Init();
    /* USER CODE BEGIN 2 */
#endif

Is there any way for me to specify the order of the initialisation code that is automatically generated by CubeIDE?


Solution

  • Yes, there is a way.

    In STM32CubIDE, open your .ioc file, click on the Project Manager tab at the top, then click on the Advanced Settings box on the left hand side.

    This will give you a list of Generated Function Calls. Each one has a Rank associated with it, which determines the order that they are called in.

    Allocate the Rank fields as needed to ensure your peripherals are initialised in the order that you need.