cancel
Showing results for 
Search instead for 
Did you mean: 

Regenerated Code

Fannyrsy
Associate II

Hi everyone!

I use STM32CubeIDE and the RTC of my uC. Everytime I change something on STM32CubeMX the code is regenerated and I know that I must write my personal code in the USER SECTIONS. My question is that I want to change parameters in the initialization part that is always regenerated such as Async/syn prescaler value and RTC time. Do I have to create another function like User_init() to rewrite my parameters and the two functions will coexist or is there any other way to avoid always having to modify this initialization function, which will always be regenerated?

There is the STM32CubeMX initialization and the init I want : 

my init.png

RTC init.png

 Thank you for your help !

3 REPLIES 3
AScha.3
Chief

Hi,

You can add, what you want, in the

" /*....user code begin/* >write here the init call with your parameters again ! < /*..*/

see my example here, to change rx/tx pins:

static void MX_I2S1_Init(void)
{

  /* USER CODE BEGIN I2S1_Init 0 */

  /* USER CODE END I2S1_Init 0 */

  /* USER CODE BEGIN I2S1_Init 1 */

  /* USER CODE END I2S1_Init 1 */
  hi2s1.Instance = SPI1;
  hi2s1.Init.Mode = I2S_MODE_SLAVE_TX;
  hi2s1.Init.Standard = I2S_STANDARD_PHILIPS;
  hi2s1.Init.DataFormat = I2S_DATAFORMAT_32B;
  hi2s1.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
  hi2s1.Init.AudioFreq = I2S_AUDIOFREQ_96K;
  hi2s1.Init.CPOL = I2S_CPOL_LOW;
  hi2s1.Init.FirstBit = I2S_FIRSTBIT_MSB;
  hi2s1.Init.WSInversion = I2S_WS_INVERSION_ENABLE;
  hi2s1.Init.Data24BitAlignment = I2S_DATA_24BIT_ALIGNMENT_LEFT;
  hi2s1.Init.MasterKeepIOState = I2S_MASTER_KEEP_IO_STATE_ENABLE;
  if (HAL_I2S_Init(&hi2s1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2S1_Init 2 */
  hi2s1.Instance = SPI1;
  hi2s1.Init.Mode = I2S_MODE_SLAVE_TX;
  hi2s1.Init.IOSwap = SPI_IO_SWAP_ENABLE;       // modified init -> swap rx/tx pins !!!!
  hi2s1.Init.Standard = I2S_STANDARD_PHILIPS;
  hi2s1.Init.DataFormat = I2S_DATAFORMAT_32B;
  hi2s1.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
  hi2s1.Init.AudioFreq = I2S_AUDIOFREQ_96K;
  hi2s1.Init.CPOL = I2S_CPOL_LOW;
  hi2s1.Init.FirstBit = I2S_FIRSTBIT_MSB;
  hi2s1.Init.WSInversion = I2S_WS_INVERSION_ENABLE;
  hi2s1.Init.Data24BitAlignment = I2S_DATA_24BIT_ALIGNMENT_LEFT;
  hi2s1.Init.MasterKeepIOState = I2S_MASTER_KEEP_IO_STATE_ENABLE;
  if (HAL_I2S_Init(&hi2s1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE END I2S1_Init 2 */

}

just copy + modify the "...init" + put in user code area !

 

If you feel a post has answered your question, please click "Accept as Solution".
TDK
Guru

Change the predividers in the IOC file.

TDK_0-1708438418133.png

 

You cannot modify code outside of the USER CODE sections and have it be preserved through new "generate code" calls.

If you feel a post has answered your question, please click "Accept as Solution".
SofLit
ST Employee

Hello,

Either you make this changes in CubeMx each time you have to make the change then yiu generate the code.

Or, in USER CODE you do again the init but you need to ensure you execute the Deinit in msp file that have to contain a correct De-initialization of the peripheral.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.