cancel
Showing results for 
Search instead for 
Did you mean: 

using variables / constants in .ioc file from main?

SWenn.1
Senior III

Good afternoon....

Can someone tell me how to use constants that are #defined in main back in the .ioc file? I have run the CubeMX and generated the ioc file then generated main. I then do work in main and make some changes to the initialization features that CubeMx generated. Now I am finding (for other reasons) to go back into CubeMx but each time I regenerate I overwrite the #defines with values. Is there a way to just fill in the integer values with constant names in the ioc file ??? Is there a way to do variables from main backwards to the ioc file (guessing not here but wanted to ask)???

Thanks

Steve

6 REPLIES 6
TDK
Guru

You can't go from code to IOC file, only from IOC to code.

If you put your definitions within USER CODE sections, they will not be overwritten, barring CubeMX bugs. Any modifications outside of these sections are overwritten.

If you feel a post has answered your question, please click "Accept as Solution".
SWenn.1
Senior III

Not sure how I would do this.....Below is a trivial example of what I mean....The CubeMx generates the following code.....

static void MX_RTC_Init(void)
{
 
  /* USER CODE BEGIN RTC_Init 0 */
 
  /* USER CODE END RTC_Init 0 */
 
  /* USER CODE BEGIN RTC_Init 1 */
 
  /* USER CODE END RTC_Init 1 */
  /** Initialize RTC Only
  */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = CFG_RTC_ASYNC_PRELOAD;
  hrtc.Init.SynchPrediv = CFG_RTC_SYNC_PRELOAD;
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN RTC_Init 2 */
 
  /* USER CODE END RTC_Init 2 */
 
}

Suppose for example sake I want to change the line above to:

hrtc.Init.AsynchPrediv = 127;

Where do you suggest I do this??? If I were to do this in USER CODE Block 2 I would have to call the HAL_RTC_init function again. Obviously I cannot do this in block 0 or 1 or it gets overwritten with the above code???

thanks

steve

TDK
Guru

You can define a user constant and use that for the value instead of a numeric value. Maybe that's what you're looking for?

0693W00000HnhlaQAB.png 

0693W00000HnhlkQAB.png 

0693W00000HnhmOQAR.png 

But if you change the numeric value in the code, the IOC isn't updated and it'll be overwritten when you regenerate. You could change the value in the IOC and regenerate to update it.

If you feel a post has answered your question, please click "Accept as Solution".
Bob S
Principal

Some CubeMX config fields have "validation" options. If you click on a field, say the "Asynchronous Predivider Value" line under RTC, look for a little plus sign in a circle at the right hand edge of that line. Click on that "+" and select "No Check". You can then enter any value you want, including any define that you have elsewhere in the code. I use this is set the I2C slave addresses. For example, I'll have in my code:

#define FLAMMERJAMMER_I2C_ADDRESS 0x5c

then in CubeMX, I will set the "Primary Slave Address" field to "FLAMMERJAMMER_I2C_ADDRESS" instead of a hard-coded decimal or hex value.

While you CAN define that value in CubeMX, you don't have to. I usually don't use the "User Contrants" tabs as I like the ability to change the defines w/o needing to re-run the CubeMX/code generation step just to tweak some value. And having the define outside CubeMX's control also allows me to document/comment WHY that define has that particular value (for times when it isn't totally obvious).

SWenn.1
Senior III

Thank you for all the responses....I think I got it.