Main Content

Generate Reusable Code from Stateflow Atomic Subcharts

Generate Reusable Code for Linked Atomic Subcharts

To specify code generation parameters for linked atomic subcharts from the same library:

  1. Open the library model that contains your atomic subchart.

  2. Unlock the library.

  3. Right-click the library chart and select Block Parameters.

  4. In the dialog box, specify the following parameters:

    1. On the Main tab, select parameter Treat as atomic unit.

    2. On the Code Generation tab, set parameter Function packaging to Reusable function.

    3. Set File name options to User specified.

    4. For File name, enter the name of the file without an extension.

    5. Click OK to apply the changes.

  5. (OPTIONAL) Customize the generated function names for atomic subcharts:

    1. Set model configuration parameter System target file to ert.tlc.

    2. For model configuration parameter Subsystem methods, specify the format of the function names using a combination of the following naming rule tokens:

      • $R — root model name

      • $F — type of interface function for the atomic subchart

      • $N — block name

      • $H — subsystem index

      • $M — name-mangling text

    3. Click OK to apply the changes.

When you generate code for your model, a separate file stores the code for linked atomic subcharts from the same library.

Generate Reusable Code for Unlinked Atomic Subcharts

To specify code generation parameters for an unlinked atomic subchart:

  1. In your chart, right-click the atomic subchart and select Properties.

  2. In the dialog box, specify the following properties:

    1. Set property Code generation function packaging to Reusable function.

    2. Set Code generation file name options to User specified.

    3. For Code generation file name, enter the name of the file without extension.

    4. Click OK to apply the changes.

  3. (OPTIONAL) Customize the generated function names for atomic subcharts:

    1. Set model configuration parameter System target file to ert.tlc.

    2. For model configuration parameter Subsystem methods, specify the format of the function names using a combination of the following naming rule tokens:

      • $R — root model name

      • $F — type of interface function for the atomic subchart

      • $N — block name

      • $H — subsystem index

      • $M — name-mangling text

    3. Click OK to apply the changes.

When you generate code for your model, a separate file stores the code for the atomic subchart. For more information, see Generate Code from Atomic Subcharts.

Generate Reusable Code for Unit Testing

Convert a State to an Atomic Subchart

To convert state A to an atomic subchart, right-click the state and select Group & Subchart > Atomic Subchart. State A changes to an atomic subchart:

Set Up a Standalone C File for the Atomic Subchart

  1. Open the properties dialog box for A.

  2. Set property Code generation function packaging to Reusable function.

  3. Set Code generation file name options to User specified.

  4. For Code generation file name, enter saturator as the name of the file.

  5. Click OK.

Set Up the Code Generation Report

  1. Set model configuration parameter System target file to ert.tlc.

  2. Select model configuration parameters Create code generation report and Open report automatically.

  3. Select parameters Code-to-model and Model-to-code.

  4. Click Apply.

Customize the Generated Function Names

  1. Set model configuration parameter Subsystem methods to the format scheme $R$N$M$F, where:

    • $R is the root model name.

    • $N is the block name.

    • $M is the mangle token.

    • $F is the type of interface function for the atomic subchart.

    For more information, see Subsystem methods.

  2. Click Apply.

Generate Code for Only the Atomic Subchart

To generate code for your model, press Ctrl+B. In the code generation report that appears, you see a separate file that contains the generated code for the atomic subchart.

To inspect the code for saturator.c, click the hyperlink in the report to see the following code:

Line 28 shows that the during function generated for the atomic subchart has the name ex_reuse_states_A_during. This name follows the format scheme $R$N$M$F specified for Subsystem methods:

  • $R is the root model name, ex_reuse_states.

  • $N is the block name, A.

  • $M is the mangle token, which is empty.

  • $F is the type of interface function for the atomic subchart, during.

Note

The line numbers shown can differ from the numbers that appear in your code generation report.

File Packaging for Atomic Subsystems (Code Only)

This model shows how to partition an atomic subsystem into its own source file. Once a subsystem is specified as atomic, on the Subsystem Block Parameter dialog, on the Code Generation tab, you can specify how the subsystem is represented in the generated code using the Function packaging parameter:

  • Inline: an Inlined function

  • Function: function with I/O passed as global data

  • Reusable function: function with I/O passed as function arguments

  • Auto: Simulink Coder optimizes based on context

With the Function and Reusable function options, you can specify a function and file name with Function name and File name, respectively. In the model, rtwdemo_filepart, SS1 subsystem is configured as Reusable function with function name myfun and file name myfile.

open_system('rtwdemo_filepart')

This capability only partitions the code for a subsystem. The data is declared and owned by the parent model. For full system encapsulation (i.e., data and functions), use Model Reference.

bdclose('rtwdemo_filepart');

Related Topics