Main Content

Reduce Global Variables in Nonreusable Subsystem Functions

Global variables can increase memory requirements and reduce execution speed. To reduce global RAM for a nonreusable subsystem, you can generate a function interface that passes data through arguments instead of global variables. The Subsystem block parameter Function interface provides this option. To compare the outputs for the Function interface options, generate a function for a subsystem with a void-void interface, and then generate a function with arguments.

Generate void-void Function

By default, when you configure a Subsystem block as a nonreusable function, it generates a void-void interface.

  1. Open the example model RollAxisAutopilot.

    openExample('RollAxisAutopilot');

  2. Right-click the subsystem RollAngleReference. From the list select Block Parameter (Subsystem).

  3. In the Block Parameters dialog box, confirm that the Treat as atomic unit check box is selected.

  4. Click the Code Generation tab and set the Code interface packaging parameter to Nonreusable function.

  5. The Function interface parameter is already set to void_void.

  6. Click Apply and OK.

  7. Repeat steps 2–6, for the other subsystems HeadingMode and BasicRollMode.

  8. Generate code and the static code metrics report for RollAxisAutopilot. This model is configured to generate a code generation report and to open the report automatically. For more information, see Generate Static Code Metrics Report for Simulink Model.

In the code generation report, in RollAxisAutopilot.c, the generated code for subsystem RollAngleReference contains a void-void function definition:

 static void RollAngleReference(void)
 {
  ... 
 }
In the static code metrics report, navigate to Global Variables. With the void_void option, the number of bytes for global variables is 47.

Next, generate the same function with the Allow arguments (Optimized) option to compare the results.

Generate Function with Arguments

To reduce global RAM, improve ROM usage and execution speed, generate a function that allows arguments:

  1. Open the Subsystem Block Parameter dialog box for RollAngleReference.

  2. Click the Code Generation tab. Set the Function interface parameter to Allow arguments (Optimized).

  3. Click Apply and OK.

  4. Repeat steps 2 and 3, for the other subsystems HeadingMode and BasicRollMode.

  5. Generate code and the static code metrics report for RollAxisAutopilot.

In the code generation report, in RollAxisAutopilot.c, the generated code for subsystem RollAngleReference now has arguments:

static real32_T RollAngleReference(real32_T rtu_Phi,... 
                                   boolean_T rtu_AP_Eng,...
                                   real32_T rtu_Turn_Knob)
 {
 ... 
 }
In the static code metrics report, navigate to Global Variables. With the Allow arguments option set, the total number of bytes for global variables is now 39 bytes.

In some cases, when generating optimized code, the code generator might not generate a function that has arguments. To generate a predictable function interface that has arguments, set Function interface to Allow arguments (Match graphical interface).

Related Topics