Main Content

Control Code Partitions for Subsystem Block

Simulink® PLC Coder™ converts subsystems to function block units according to the following rules:

  • Generates a function block for the top-level atomic subsystem for which you generate code.

  • Generates a function block for an atomic subsystem whose Function packaging parameter is set to Reusable function.

  • Inlines generated code from atomic subsystems, whose Function packaging parameter is set to Inline, into the function block that corresponds to the nearest ancestor subsystem. This nearest ancestor cannot be inlined.

For code generation from a subsystem with no inputs or outputs, you must set the Function packaging parameter of the block to Reusable function.

These topics use code generated with CODESYS Version 2.3.

Control Code Partitions Using Subsystem Block Parameters

You can partition generated code using the following Subsystem block parameters on the Code Generation tab. See the Subsystem block documentation for details.

  • Function packaging

  • Function name options

Leave the File name options set to the default, Auto.

Generating Separate Partitions and Inlining Subsystem Code

Use the Function packaging parameter to specify the code format to generate for an atomic (nonvirtual) subsystem. The Simulink PLC Coder software interprets this parameter depending on the setting that you choose:

SettingCoder Interpretation
Auto

Uses the optimal format based on the type and number of subsystem instances in the model.

Reusable function

Generates a function with arguments that allows reuse of subsystem code when a model includes multiple instances of the subsystem.

Nonreusable function

The Simulink PLC Coder does not support Nonreusable function packaging. See, Restrictions.

Inline

Inlines the subsystem unconditionally.

To open the plcdemo_hierarchical_virtual_subsystem model, enter:

openExample('plcdemo_hierarchical_virtual_subsystem');
In the plcdemo_hierarchical_virtual_subsystem, you can:

  • Inline the S1 subsystem code by setting Function packaging to Inline. This setting creates one function block for the parent with the S1 subsystem inlined.

  • Create a function block for the S2 subsystem by setting Function packaging to Reusable function or Auto. This setting creates two function blocks, one for the parent, one for S2.

Changing the Name of a Subsystem

You can use the Function name options parameter to change the name of a subsystem from the one on the block label. When the Simulink PLC Coder generates software, it uses the string you specify for this parameter as the subsystem name. For example, open the plcdemo_hierarchical_virtual_subsystem model:

openExample('plcdemo_hierarchical_virtual_subsystem');
In the plcdemo_hierarchical_virtual_subsystem model:

  1. Open the S1 subsystem block parameter dialog box.

  2. If the Treat as atomic unit check box is not yet selected, select it.

  3. Click the Code Generation tab.

  4. Set Function packaging to Reusable function.

  5. Set Function name options to User specified.

  6. In the Function name field, specify a custom name. For example, type my_own_subsystem.

  7. Save the new settings.

  8. Generate code for the parent subsystem.

  9. Observe the renamed function block.

One Function Block for Atomic Subsystems

The code for plcdemo_simple_subsystem is an example of generating code with one function block. To open the model, enter:

openExample('plcdemo_simple_subsystem');
The atomic subsystem for which you generate code does not contain other subsystems.

One Function Block for Virtual Subsystems

The plcdemo_hierarchical_virtual_subsystem example contains an atomic subsystem that has two virtual subsystems, S1 and S2, inlined. To open the model, enter:

openExample('plcdemo_hierarchical_virtual_subsystem');
A virtual subsystem does not have the Treat as atomic unit parameter selected. When you generate code for the hierarchical subsystem, the code contains only the FUNCTION_BLOCK HierarchicalSubsystem component. There are no additional function blocks for the S1 and S2 subsystems.

Multiple Function Blocks for Nonvirtual Subsystems

The plcdemo_hierarchical_subsystem example contains an atomic subsystem that has two nonvirtual subsystems, S1 and S2. To open the model, enter:

openExample('plcdemo_hierarchical_subsystem');
Virtual subsystems have the Treat as atomic unit parameter selected. When you generate code for the hierarchical subsystem, that code contains the FUNCTION_BLOCK HierarchicalSubsystem, FUNCTION_BLOCK S1, and FUNCTION_BLOCK S2 components.