Configure Code Generation for Model Entry-Point Functions

What Is an Entry-Point Function?

An entry point is a location in code where a transfer of program control (execution) occurs. The main function (main()) is the entry point to a C/C++ program and is called when the application starts executing. Calls to other functions, for example from the main function, provide entry points to function code. Program control is transferred to the called function. The function code executes, and when finished, returns control to the main or other calling function.

When producing code for a model, the code generator defines a set of entry-point functions that you can call to execute the generated code. You can call the generated functions from external code or from a version of the generated main function that you modify.

The Code Interface Report section of the code generation report lists the entry-point functions that the code generator produces for a model. For more information, see Analyze the Generated Code Interface.

Categories and Types of Generated Entry-Point Functions

The type of entry-point functions that the code generator produces for a model and the calling interface for the functions depend on whether the model is:

  • Rate-based or is an export-function model

  • Configured for reusable, multi-instance code generation

Depending on the style and configuration of a model, the code generator produces one or more of these entry-point functions.

Function CategoryFunctionDescription
Initialize/Terminatemodel_initialize

Initialization code for a model. At the start of the application code, call the function once. Do not use this function to reset the real-time model data structure (rtM).

model_terminate

Code for turning off a system. For ERT-based models, you can suppress generation of this function by clearing the model configuration parameter Terminate function required (set by default).

Executionmodel_step

For blocks in a rate-based model, output and update code. If you clear the model configuration parameter Single output/update function (Simulink Coder) (selected by default), instead of producing a model_step function, the code generator produces functions model_output and model_update.

model_function-nameFor an exported-function model, the exported function for a subsystem.
function-nameFor an exported-function model, the exported function for a Simulink Function block.
model_reset

If the model includes a Reset Function block, reset code generated. To reset conditions or state, call the function from the application code.

ref-modelFor a reference model, output and update code.
isr_numinterrupt-number_vecinterrupt-vector-offsetFor an Async Interrupt block, interrupt service routine (ISR) code.
Shared utilityfunction-nameFor shared utility functions, output code.

Configure Whether Entry-Point Functions Are Reusable

By default, for top models, the code generator produces code that is not reusable or reentrant. Entry-point functions have a void-void interface. Code communicates with other code by directly accessing global data structures that reside in shared memory.

If your application requires reusable, multi-instance entry-point function code, you can configure the code generator to call each function (instance) with unique data. In this case, the code is reentrant.

You configure whether entry-point functions are reusable with the model configuration parameter Code interface packaging (Simulink Coder) and related parameters. The parameter settings that you choose depend on factors such as configuration selections for the system target file, programming language, and argument interface.

Default Configurations for Single-Instance C Entry-Point Functions

By default, for GRT- and ERT-based system target files, the code generator produces single-instance C entry-point functions. The generated code:

  • Creates an execution function without arguments (void-void).

  • Allocates memory statically (at compile time) for model data structures.

The default model configuration parameter settings for configuring single-instance entry-point function code are:

Generate Reusable, Multi-Instance C Entry-Point Functions

You can configure the code generator to produce reusable entry-point functions in C for either a GRT- or ERT-based system target file. However, the function interfaces that the code generator produces by default varies. Assuming that model configuration parameter Language (Simulink Coder) is set to C and Code interface packaging (Simulink Coder) is set to Reusable function, the code generator produces this entry-point function code for each system target file scenario.

System Target FileInterface
GRT-based
  • Reusable, multi-instance C entry-point functions that are reentrant.

  • Packs values of model root-level Inport blocks and Output blocks into the real-time model data structure. Passes that structure to the execution function as an argument by reference.

  • Allocates memory dynamically at runtime for the data of a model instance. Allocates the memory by calling a function, such as malloc.

ERT-based
  • Reusable, multi-instance C entry-point functions that are reentrant.

  • Passes the value of each model root-level Inport block and Outport block to the execution function as a separate argument.

  • Allocates memory statically for model data structures.

If you are using an ERT-based system target file and want to generate reusable, multi-instance C entry-point functions that are reentrant, consider:

  • Using dynamic memory allocation to initialize model data structures. Select Use dynamic memory allocation for model initialization.

  • Packing values of model root-level Inport blocks into a structure, packing values of root-level Outport blocks into a second structure, and passing the structures to the execution function as arguments by reference. Set Pass root-level I/O as to Structure reference.

  • Packing values of model root-level Inport blocks and Output blocks into the real-time model data structure and passing that structure to the execution function as an argument by reference. Set Pass root-level I/O as to Part of model data structure.

Generate Reusable, Multi-Instance C++ Entry-Point Functions

The C++ class interface encapsulates model data as class properties and entry-point functions as class methods. That interface is available for use with ERT-based system target files. To use the interface, set Language (Simulink Coder) to C++ and set Code interface packaging (Simulink Coder) to C++ class. You can:

  • Preview and customize the C++ class interface by clicking Configure C++ Class Interface. Customization means that you can generate code for integration with external code and verify that the code complies with coding standards.

  • Configure the visibility of class inheritance by specifying whether to generate the block parameter structure as a public, private, or protected data member. Set Parameter visibility to public, private, or protected.

  • Generate C++ interface code for model block parameters that meet code execution speed or tunability requirements. The code can be noninlined or inlined access methods. Set Parameter access to Method or Inlined method.

  • Generate C++ interface code for model root-level Inport and Outport blockset that meets code execution speed, data tunability, or data packaging requirements. The code can be one of these types of access methods.

    For Access Method TypeSet External I/O access To
    NoninlinedMethod
    InlinedInlined method
    Noninlined structure-basedStructure-based method
    Inlined structured-basedInlined structure-based method
  • Configure the visibility of class inheritance by specifying whether to generate the root-level I/O structure as a public, private, or protected data member. Set External I/O visibility to public, private, or protected.

Configure Generated Entry-Point Function Declarations

Depending on application requirements, such as integration or compliance with code standards, you might need to configure how the code generator produces declarations for entry-point functions. Embedded Coder® provides multiple configuration options.

Configure Placement of Entry-Point Functions in Memory

If your application requires that you configure the placement of entry-point functions in memory, for example, to optimize the generated code for specific hardware, you can apply a default memory section to a category of functions across a model. In the Code Mappings editor, map a function category to a function customization template that is defined to use a specific memory section. See Code Mappings Editor.

How to Interface with Generated Entry-Point Functions

  1. After generating code for a model, use the Code view to review the generated entry-point functions and, if applicable, variables representing external input and output ports.

  2. Add #include statements to your external code that include the generated header files that declare the model entry-point functions.

  3. Add an #include statement that includes the generated file rtwtypes.h. This file provides type definitions, #define statements, and enumerations.

  4. Initialize target-specific data structures and hardware, such as ADCs or DACs.

  5. If applicable, initialize data for each instance of a reusable model.

  6. If applicable, write input data to generated variables that represent model Inport blocks.

  7. Call the generated entry-point functions or set up use of the rt_OneStep function.

  8. If applicable, read data from generated variables that represent model Outport blocks.

For more information, see Deploy Generated Standalone Executable Programs To Target Hardware.

Related Topics