Configure Block States for C Code Generation - MATLAB & Simulink (original) (raw)

Block states, such as the state of a Unit Delay block, retain a state value between execution cycles of an algorithm. When configuring a model for code generation, you can configure state data to:

For code generation, examples show how to configure a block state for model ConfigurationInterface. You can configure code mappings by using the Code Mappings Editor – C or code mappings programming interface (coder.mapping.api.CodeMapping).

Choose Code Configuration Options for States

Based on your code generation requirements, decide how to represent state data. By default, states in a model appear in generated code as fields of a global data structure named_`model`__DW. If you do not configure customizations, the code generator determines whether to eliminate or change the representation of states in generated code for optimization purposes. If you configure customizations, decide:

Other considerations include whether to:

For a list of interface requirements that are relevant to states with corresponding storage classes and storage class properties, see Choose Storage Class and Storage Class Properties for Data Stores.

State requirements for example model ConfigurationInterface are:

For this example, you set the default representation of the state in the generated code as a global variable with the static type qualifier. Then, you configure the state of the Unit Delay block to use the default storage class and a unique code identifier that includes the required prefix dtemp_. The code identifier capability enables you to specify code generation identifiers without having to modify the model design.

Configure Default Code Generation Settings for States

A default code generation setting for states can reduce the effort of preparing a model for code generation, especially if a model has a significant number of states that you want to gain access to while the generated code executes. Choose configuration settings once, and the code generator applies those settings to states across the model. Simulink® stores the default configuration as part of the model.

Consider configuring default code generation settings for model states if your model uses multiple states that do not have unique requirements or uses a shared Embedded Coder Dictionary.

This example shows how to use the Code Mappings Editor – C to configure default settings for states. Use the Code Mappings editor to set the default storage class for states in model ConfigurationInterface to FileScope. With that storage class setting, the code generator represents state data in the generated code as global variables that have the static type qualifier.

  1. Open model ConfigurationInterface.
    openExample("ConfigurationInterface")
    Simulink model to use for learning how to configure model states for code generation.
  2. Open the Embedded Coder app.
  3. In the C Code tab, select > .
  4. In the Code Mappings editor, under Signals, select categorySignals, states, and internal data. Set the default storage class to FileScope.
    Code Mappings editor with Data Defaults tab selected, Signals tree node expanded, and storage class for Signals, states, and internal data set to FileScope.
  5. Save the model.

Configure Code Generation Settings for Individual States

You can configure individual states for code generation. For example, if a model has two states that have unique code generation requirements, configure the states individually. Or, if you configure default settings for states, you can override those settings for specific states.

If your model meets at least one of these criteria, consider configuring code generation settings for states individually:

This example shows how to use the Code Mappings editor to apply your default storage class setting to the Unit Delay block state X in model ConfigurationInterface.

The example also shows how to configure a code identifier for that state. You can specify code generation identifiers, for example for integration, without modifying the model design.

  1. If you have not already done so, complete the steps in Configure Default Code Generation Settings for States.
  2. In the Code Mappings editor, click the Signals/States tab. Expand States. The storage class for the state is set toAuto, which means that the code generator might eliminate or change the representation of relevant code for optimization purposes. If optimizations are not possible, the code generator applies the model default configuration. For this example, the model default configuration specifies storage class FileScope.
    • To avoid optimizations and force the code generator to use the default configuration, set the storage class to Model default.
    • To override the default configuration, specify the storage class that meets the code generation requirements for that state.
  3. In the Code Mappings editor, select state X. Set the storage class to Model default: FileScope.
  4. Configure the code identifier for the state with a name that includes the prefixdstate_. In the Code Mappings editor, select stateX. Click the Icon to configure additional code mapping properties icon and set the storage class propertyIdentifier to dstate_X.
    Code Mappings editor with Signals/States tab selected, States tree node expanded, and storage class for state X set to Model default: FileScope. Mapping Inspector shows Identifier property for state X set to dstate_X.
  5. Save the model.
  6. Generate and view the code. For example, in ConfigurationInterface.c, find the data definitions for the state data.
    Find where the state data is used in the step entry-point function.
    .
    .
    .
    if (mode) {
    output = (real_T)mp_K1 * dout_Table1;
    } else {
    output = dstate_X;
    }
    .
    .
    .
    dstate_X = dout_Table2;
    }

Configure Code Generation Settings for States Programmatically

To automate configuration of states for code generation, use the programming interface for code mappings. For example, when creating custom block libraries or as part of creating an application test environment, use the programming interface to automate data configuration.

This example shows how to use the programming interface to configure states for modelConfigurationInterface. Set the default representation of states in the generated code as global variables that have the static type qualifier. Then, configure state X of the Unit Delay block to use the default storage class and unique code identifier that includes the required prefixdstate_.

  1. Open the example model.
    openExample("ConfigurationInterface")
  2. Create object cm by calling functioncoder.mapping.api.get. The object stores the code generation configuration for data and functions for modelConfigurationInterface.
    cm = coder.mapping.api.get("ConfigurationInterface");
  3. Configure default settings for states by calling functionsetDataDefault. For the arguments, specify these values:
    • The object returned by coder.mapping.api.get
    • InternalData for the default category
    • Property name StorageClass with property value FileScope
      setDataDefault(cm,"InternalData","StorageClass","FileScope");
  4. Verify your default configuration for states. Issue a call togetDataDefault that specifies the object returned bycoder.mapping.api.get and category InternalData. Specify the third argument as property StorageClass.
    getDataDefault(cm,"InternalData","StorageClass")
  5. Apply the default configuration for states to state X.
    By default, Simulink sets the storage class for individual states toAuto. The code generator:
    • Determines whether to eliminate the data from the generated code for optimization purposes.
    • If retaining the data, determines how to efficiently represent the data in the generated code, taking into account default configuration settings.
      To control the configuration for a state, call functionsetState.
      Issue a call to setState that specifies:
    • Object returned by coder.mapping.api.get
    • State name X
    • Default storage class previously set for states by using propertyStorageClass and property value Model default.
    • Property Identifier and property valuedstate_X
      setState(cm,"X","StorageClass","Model default","Identifier","dstate_X");
  6. Verify your configuration changes by calling function getState. Specify the object returned by coder.mapping.api.get, name of the state, and property StorageClass orIdentifier.
    getState(cm,"X","StorageClass")
    getState(cm,"X","Identifier")
  7. Save the model.
  8. Generate and view the code. For example, in ConfigurationInterface.c, find the data definitions for state data.
    Find where the state data is used in the step entry-point function.
    .
    .
    .
    if (mode) {
    output = (real_T)mp_K1 * dout_Table1;
    } else {
    output = dstate_X;
    }
    .
    .
    .
    dstate_X = dout_Table2;
    }

Choose Storage Class and Storage Class Properties for States

Depending on your code generation requirements, choose from these storage classes to configure code generation for block states. The list of storage classes is defined in the coder dictionary.

Tip

In the default mappings, states are configured in the section Signals, states, and internal data.

Requirements Storage Class for Default Mappings Storage Class for Individual Mappings
For data elements that cannot be optimized, represent data as a field of a standard data structure. Default
Enable optimizations, potentially generating more efficient code. Auto
Prevent optimizations from eliminating storage for a data element and use the default mapping for the data element category. Model Default
When using a shared coder dictionary, select the dictionary default for data elements that you do not want the code generator to optimize. Dictionary Default
Generate standalone global variables. Generated code contains the variable declaration and definition. ExportedGlobal ExportedGlobal
Generate standalone global variables or global variable pointers. Generated code contains the variable or pointer declaration. Your external code provides the definition. ImportedExtern, ImportedExternPointer ImportedExtern, ImportedExternPointer
Generate standalone global variables with the volatile qualifier. Volatile (See Const, Volatile, and ConstVolatile) Volatile (See Const, Volatile, and ConstVolatile)
Generate standalone global variables. You can specify the external files that contain the variable declaration and definition. ExportToFile ExportToFile
Generate standalone global variables. You can specify the external file that contains the variable declaration. Your external code provides the definition. ImportFromFile ImportFromFile
Generate standalone variables with file scope. FileScope (Local and shared local data store mappings only) FileScope (Local and shared local data store mappings only)
Generate standalone variables whose scope is defined by their usage. Attempt to minimize the use of global scope by using variables localized to a function or file where possible. Localizable Localizable
Generate variables as fields of a structure. Struct
Generate variables as fields of a structure that stores Boolean, fixed-point, or integer data in named bitfields. Bitfield
Generate variables that you access through a function call. GetSet GetSet
Generate standalone global variables that enable buffer reuse. Reusable
Generate variables for single-instance data and generate structures for multi-instance data. MultiInstance MultiInstance

The list of available storage classes might include other project-specific storage classes defined in an Embedded Coder Dictionary. If you have special requirements that are not met by the listed storage classes and you have Embedded Coder software, you can define a storage class. See Define Service Interfaces, Storage Classes, Memory Sections, and Function Templates for Software Architecture.

For an individual state, use the Identifier storage class property to configure a name for the variable representing the state in the generated code. If you leave the Identifier property blank, the code generator uses theState name parameter of the block that has the state. If this parameter is empty, the code generator uses the name of the block.

With Embedded Coder, depending on the storage class that you choose, you can also configure these properties.

Property Description Storage Classes
DefinitionFile Source definition file that contains definitions for global data, which is read by the state and external code ExportToFile and Volatile
GetFunction Data element that appears in the generated code as a call to a specifiedget function GetSet
HeaderFile Source header file that contains declarations for global data, which is read by the state and external code ExportToFile, GetSet,ImportFromFile, and Volatile
Memory Section (default state configuration only) Memory section that contains data read by the state Default
Owner A component in the model hierarchy where the code generator places a global data definition instead of placing it in the top component of the hierarchy. To use this property, you must set the model configuration parameter Use owner from data object for data definition placement.. See Control Placement of Global Data Definitions and Declarations in Generated Files. ExportToFile and Volatile
PreserveDimensions Flag that indicates whether to declare multidimensional data as multidimensional arrays in the generated code. To use this flag, you must set the model configuration parameter Array layout to Row-major. See Preserve Dimensions of Multidimensional Arrays in Generated Code. ExportToFile, FileScope,GetSet, ImportFromFile,Localizable and Volatile
SetFunction Data element that appears in the generated code as a call to a specifiedset function GetSet
StructName Name for a structure in the generated code for state BitField and Struct

See Also

Code Mappings Editor – C | coder.mapping.api.CodeMapping

Topics