Skip to main content
WXiny.1
Associate II
March 4, 2020
Question

Is there any possible that users can change parameters generated by STM32 CubeIDE and use their own constants to maintain code?

  • March 4, 2020
  • 7 replies
  • 2359 views

I noticed that some documents about STM32CubeMX mentioned that, but I don't know how to use it with STM32 CubeIDE.

This topic has been closed for replies.

7 replies

chaaalyy
Senior II
March 4, 2020

I´m not sure, if i get the point here... But as i read that question, there would be an easy way to "override" ANY codegenerator (CubeMX, TouchGFX and so on):

a) place a define in usercode-section at top of main ( like " #define USEOWNPARAMETERS ")

b) just above the generated Code (of course also in usercode-section !) you place " #ifndef USEOWNPARAMETERS { "

c) at the other end in the next usercode-section after the generated code: }

#endif

#ifdef USEOWNPARAMETERS {

own_parameter_1(do something); //maybe copied and modified from generated code...

own_parameter_2(do something);

}

That way, a generator can generate, whatever it want´s to do, you just have to ensure, not to miss something. You can copy/paste the generated code just to the part with "own_parameter.....", modify however you want to and it´s save from being overwritten ;)

/Charly

WXiny.1
WXiny.1Author
Associate II
March 5, 2020

Thanks for your reply.

You do offer a solution to avoid code generator changing user's code, but that also means users need to maintain two codes. Actually what I need is a method to use code generator and user cosntants together, which means users can protect codes generated by IDE from code generator after they change codes. But if users need to add some new peripherals they can still get a template with IDE.

I thought this is a widely request, but I cannot foud it in any document.

chaaalyy
Senior II
March 5, 2020

As far, as i know, just the sections between " /* USER CODE BEGIN xxxxxx */ " and " /* USER CODE END xxxxxx */ " are protected against being changed by the generators. So the method i use, is as above: It´s the trick to "encapsulate" the generated code with Brackets in these user sections, depending on what is defined. Then i take the generated code, copy it into the user section and there it can be safely modified.

Of course: If you change (or add) something with the generator, what is needed by your application, it will be placed into the part, "owned" by the generator and you also have to just copy that into your own section after being generated.

That way, the generators can still work properly and modify, whatever they want. Their code is just ignored by the compiler... Your own code stays untouched but of course needs to be maintained by yourself then ;)

If the original files get too long and too confusing that way... just outsource your own code into new files and call just the according functions in the "original" user sections.