MATLAB: Embedded Coder – Simulink.Parameter declaration

autoclasscoderembeddedEmbedded Coderexternoptimizationsettingsstorage

When I generate autocode for the using the TI C2000 toolchain, all Simulink.Parameter variables in the model are declared as 'extern' in the model header file. I.E. if I have a variable x, when i generate code, the header file will have a line that says something like extern int x; No matter what datastorage method I use, the end result is that the variable is some kind of extern (Simulink Global puts the variable into a structure, and then the structure has an extern declaration).
Because we are using Model Based Development to create code for aerospace applications, any global declarations are not allowed. How do I get Simulink to generate code where Simulink.Parameter values are created inside the model's function (i.e. so the variables are not accessible by other functions).

Best Answer

My understanding of the issue is that in your Simulink model, all parameters are treated as extern variables when the code is generated for the model. You would like to get rid of extern variables and instead wish to see those parameters treated as local variables.
The way to make the variable local is to set the storage class to "auto". However, upon doing so, the Embedded Coder's optimization settings must be taken care to make sure that local variables are generated whenever possible. To do so, in the configuration parameters dialog box for the model (press CTRL + E in the model), go to "All Parameters" tab, and search for the following optimization settings for code generations:
  • Signal storage reuse
  • Reuse block outputs
  • Enable local block outputs
  • Eliminate superfluous local variables (expression folding)
Please uncheck each of those settings if they are checked, and you should be able to see global variables for parameters replaced with local variables wherever possible with the updated parameter settings.
In addition, I would like to confirm that you are talking about variables representing block parameter values, and not the signals feeding in and out of the blocks. Variables representing signals are almost always of type "extern", with rare instances as seen below:
Here is a reference for a detailed explanation of how storage classes affect variables.