Generate Reusable Code

An atomic subchart is a graphical object in Stateflow® that helps you to create standalone subcomponents in a Stateflow chart. For more information, see Create Reusable Subcomponents by Using Atomic Subcharts.

Creating reuseable generated code from atomic subcharts is useful for testing individual parts of your Stateflow chart. When you generate code for your chart, a separate file stores the code for the atomic subchart, which allows for unit testing. For more information, see Generate Reusable Code.

How to 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 Treat as atomic unit.

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

    3. Set File name options to User specified.

    4. For File name, enter the name of the file with no 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 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.

How to 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 parameters:

    1. Set 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 with no 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 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.

Generate Reusable Code for Unit Testing

To unit test a Stateflow chart within a Simulink® model, you must first break the chart down into smaller standalone components by using atomic subcharts. An atomic subchart is a graphical object that helps you to create standalone subcomponents in a Stateflow chart. Once your chart is broken down into the individual units, you can then generate code for these units that you can test.

To test your Stateflow charts without generating code from atomic subcharts, see Test Harnesses (Simulink Test).

Assume that you have the following model, and the chart has two states:

Suppose that you want to generate reusable code so that you can perform unit testing on state A. You can convert that part of the chart to an atomic subchart and then specify a separate file to store the generated code.

Convert a State to an Atomic Subchart

To convert state A to an atomic subchart, in the Modeling tab, click Atomic Subchart. State A changes to an atomic subchart:

Specify Code Generation Parameters

Set Up a Standalone C File for the Atomic Subchart

  1. Open the state properties dialog box for A.

  2. Set 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 parameter Create code generation report.

    This step automatically selects Open report automatically and Code-to-model.

  3. Select parameter 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 (Simulink Coder).

  2. Click Apply.

Generate Separate Code for an 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 31 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