How to change C-coder settings to create static variables in the "Model step function"?

4 views (last 30 days)
I'm using the Simulink C-Coder to generate my model in C and use it in a micro controller. When I run the generated C-code I get a "stack overflow", and this is because of the many variables in /*Model step function*/. Is there a way to change the coder settings, so that the variables in the step function are declared as static variables?
This is how the code is generated:
/* Model step function */
void Battery_state_algorithms_v1_step(void)
{
/* local block i/o variables */
real32_T rtb_Z1[180];
real32_T rtb_Z2[180];
real32_T rtb_Z3[180];
.
.
.
}
This is how I would like to have it:
/* Model step function */
void Battery_state_algorithms_v1_step(void)
{
/* local block i/o variables */
static real32_T rtb_Z1[180];
static real32_T rtb_Z2[180];
static real32_T rtb_Z3[180];
.
.
.
}
Thanks!

Answers (2)

TAB
TAB on 29 Jan 2013
There is no configuration option available to change the storage class specifier of variables in generated step function.
Instead you can try to minimize the generation of function local variables using optimization setting.
  1. Goto Configuration Parameters -> Optimization -> Signals and Parameters
  2. Disable the option Enable local block outputs
In this way, all the intermidiate data will be generated as global variables whenever possible.

Enrique Martí
Enrique Martí on 21 Oct 2016
You can directly place a limit on stack size using *Configuration Parameters -> Optimization -> Signals and Parameters -> Maximum stack size (in bytes). See this link.
If you prefer to do it from command, take your simulink config structure and change it this way ( taken from here):
<cfg_structure>.set_param('MaxStackSize', '512');

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!