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 (Embedded Coder).

  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.

Related Topics