cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX does not split main.c into individual device files properly - Bug

matt-crc
Associate III

Originally I had the project initialize all devices in main.c.  Since the file was starting to get large, i went to the Code Generator in CubeMX and selected:

mattcrc_0-1741323072344.png

The option to "Keep User Code when re-generating" just doesn't work reliably.  It seems it did not keep the "User Code" for the FMC device, and possibly the RTC device.... I now have to check each and every device manually.

Original FMC init code:

 

 

/* FMC initialization function */
static void MX_FMC_Init(void)
{

  /* USER CODE BEGIN FMC_Init 0 */

  /* USER CODE END FMC_Init 0 */

  FMC_SDRAM_TimingTypeDef SdramTiming = {0};

  /* USER CODE BEGIN FMC_Init 1 */

  /* USER CODE END FMC_Init 1 */

  /** Perform the SDRAM1 memory initialization sequence
  */
  hsdram1.Instance = FMC_SDRAM_DEVICE;
  /* hsdram1.Init */
  hsdram1.Init.SDBank = FMC_SDRAM_BANK1;
  hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_9;
  hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_13;
  hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_32;
  hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
  hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_2;
  hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
  hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
  hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_ENABLE;
  hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_2;
  /* SdramTiming */
  SdramTiming.LoadToActiveDelay = 2;
  SdramTiming.ExitSelfRefreshDelay = 7;
  SdramTiming.SelfRefreshTime = 5;
  SdramTiming.RowCycleDelay = 6;
  SdramTiming.WriteRecoveryTime = 3;
  SdramTiming.RPDelay = 2;
  SdramTiming.RCDDelay = 2;

  if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
  {
    Error_Handler( );
  }

  /* USER CODE BEGIN FMC_Init 2 */

	FMC_SDRAM_CommandTypeDef Command;
	/* Step 1 and Step 2 already done in HAL_SDRAM_Init() */
	/* Step 3: Configure a clock configuration enable command */
	Command.CommandMode = FMC_SDRAM_CMD_CLK_ENABLE; /* Set MODE bits to "001" */
	Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1; /* configure the Target Bank bits */
	Command.AutoRefreshNumber = 1;
	Command.ModeRegisterDefinition = 0;
	if (HAL_SDRAM_SendCommand(&hsdram1, &Command, 0xfff) != HAL_OK) {
		printf("Step 3 error\r\n");
	}
	/* Step 4: Insert 100 us minimum delay - Min HAL Delay is 1ms */
	HAL_Delay(1);
	/* Step 5: Configure a PALL (precharge all) command */
	Command.CommandMode = FMC_SDRAM_CMD_PALL; /* Set MODE bits to "010" */
	if (HAL_SDRAM_SendCommand(&hsdram1, &Command, 0xfff) != HAL_OK) {
		printf("Step 5 error\r\n");
	}
	/* Step 6: Configure an Auto Refresh command */
	Command.CommandMode = FMC_SDRAM_CMD_AUTOREFRESH_MODE; /* Set MODE bits to "011" */
	Command.AutoRefreshNumber = 2;
	if (HAL_SDRAM_SendCommand(&hsdram1, &Command, 0xfff) != HAL_OK) {
		printf("Step 6 error\r\n");
	}
	/* Step 7: Program the external memory mode register */
	Command.CommandMode = FMC_SDRAM_CMD_LOAD_MODE;/*set the MODE bits to "100" */
	Command.ModeRegisterDefinition = (uint32_t) 0 | 0 << 3 | 2 << 4 | 0 << 7
			| 1 << 9;
	if (HAL_SDRAM_SendCommand(&hsdram1, &Command, 0xfff) != HAL_OK) {
		printf("Step 7 error\r\n");
	}
	/* Step 8: Set the refresh rate counter - refer to section SDRAM refresh timer register in RM0455 */
	/* Set the device refresh rate
	 * COUNT = [(SDRAM self refresh time / number of row) x  SDRAM CLK] – 20
	 = [(64ms/4096) * 100MHz] - 20 = 1562.5 - 20 ~ 1542 */
	/* (64ms/8192) * 100MHz) - 20 = 781.25 - 20 = 761 */
	if (HAL_SDRAM_ProgramRefreshRate(&hsdram1, 761) != HAL_OK) {
		printf("Step 8 error\r\n");
	}

  /* USER CODE END FMC_Init 2 */
}

 

 

What CubeMX put in the fmc.c file is:

 

 

/* FMC initialization function */
void MX_FMC_Init(void)
{
  /* USER CODE BEGIN FMC_Init 0 */

  /* USER CODE END FMC_Init 0 */

  FMC_SDRAM_TimingTypeDef SdramTiming = {0};

  /* USER CODE BEGIN FMC_Init 1 */

  /* USER CODE END FMC_Init 1 */

  /** Perform the SDRAM1 memory initialization sequence
  */
  hsdram1.Instance = FMC_SDRAM_DEVICE;
  /* hsdram1.Init */
  hsdram1.Init.SDBank = FMC_SDRAM_BANK1;
  hsdram1.Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_9;
  hsdram1.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_13;
  hsdram1.Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_32;
  hsdram1.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
  hsdram1.Init.CASLatency = FMC_SDRAM_CAS_LATENCY_2;
  hsdram1.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
  hsdram1.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
  hsdram1.Init.ReadBurst = FMC_SDRAM_RBURST_ENABLE;
  hsdram1.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_2;
  /* SdramTiming */
  SdramTiming.LoadToActiveDelay = 2;
  SdramTiming.ExitSelfRefreshDelay = 7;
  SdramTiming.SelfRefreshTime = 5;
  SdramTiming.RowCycleDelay = 6;
  SdramTiming.WriteRecoveryTime = 3;
  SdramTiming.RPDelay = 2;
  SdramTiming.RCDDelay = 2;

  if (HAL_SDRAM_Init(&hsdram1, &SdramTiming) != HAL_OK)
  {
    Error_Handler( );
  }

  /* USER CODE BEGIN FMC_Init 2 */

  /* USER CODE END FMC_Init 2 */
}

 

 

 

Some "User Code" was put in the new files, in other places it just dropped the user code.

 

3 REPLIES 3
Ghofrane GSOURI
ST Employee

Hello @matt-crc 

First let me thank you for posting.

I would appreciate it if you could provide your IOC so that I can further investigate.

I will be waiting for your feedback.

THX

Ghofrane

Hi @Ghofrane GSOURI 

See attached

Hello @matt-crc 

We appreciate you bringing this to our attention.

Issue has been raised to dev team for fix.

Internal number ticket 204844 

THX

Ghofrane