Configure Generated C Function Interface for Model Entry-Point Functions - MATLAB & Simulink (original) (raw)

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 Generated Data Code Interface Report (Embedded Coder).

Types of Generated Entry-Point Functions

The code generator produces entry-point functions for these types of model functions.

Type of Model Function Model Source Name Default Generated Function Name Description
Exported function (requires Embedded Coder®) ExportedFunction:slIdentifier, where slIdentifier is the name of the function-call Inport block in the model function-call-inport-block-name or signal-label (if specified) For an exported-function model, the exported function for a subsystem.
Initialize function Initialize model_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).
Partition function Partition:slIdentifier, where_slIdentifier_ is a partition that was created explicitly from a block in the model and shown in the Simulink® Schedule Editor (for example, P1), or a task name in the Concurrent Execution dialog model_step_n_, where n uniquely identifies the function generated for one of the model sample periods For a model partition, output and update code. Model configuration parameterSingle output/update function is selected (the default).
Partition update function PartitionUpdate:slIdentifier, where_slIdentifier_ is a partition that was created explicitly from a block in the model and shown in the Simulink® Schedule Editor (for example, P1), or a task name in the Concurrent Execution dialog model_output_n_ and model_update_n_, where n uniquely identifies the function generated for one of the model sample periods For a model partition, output and update code in separate functions. Model configuration parameter Single output/update function is cleared.
Periodic multitasking function Periodic:slIdentifier, where_slIdentifier_ is an annotation that corresponds to the sample time period for a periodic or continuous rate of a multitasking model (for example, D1) model_step_n_, where n uniquely identifies the function generated for one of the model sample periods For blocks in a rate-based model configured for multitasking, output and update code. The code generator produces a function for each sample period. Model configuration parameter Single output/update function is selected (the default).
Periodic multitasking update function PeriodicUpdate:slIdentifier, where_slIdentifier_ is an annotation that corresponds to the sample time period for a periodic or continuous rate of a multitasking model (for example, D1) model_output_n_ and model_update_n_, where n uniquely identifies the function generated for one of the model sample periods For blocks in a rate-based model configured for multitasking, output and update code in separate functions. The code generator produces output and update functions for each sample period. Model configuration parameter Single output/update function is cleared.
Periodic single-tasking function Periodic model_step For blocks in a rate-based model configured for single-tasking, output and update code. Model configuration parameter Single output/update function is selected (the default).
Periodic single-tasking update function PeriodicUpdate model_output and model_update For blocks in a rate-based model configured for single-tasking, output and update code in separate functions. Model configuration parameter Single output/update function is cleared.
Reset function Reset:slIdentifier, where_slIdentifier_ is the name of the reset function in the model model_ reset-function-name If the model includes a Reset Function block, reset code generated. To reset conditions or state, call the function from the application code.
Simulink Function Simulink Function:slIdentifier, where slIdentifier is the name of theSimulink Function block in the model function-name for a globalSimulink Function block or_model__ function-name for a scoped Simulink Function block For an exported-function model, the exported function for a Simulink Function block.
Terminate function Terminate 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 (Embedded Coder) (set by default).

If you have Embedded Coder software, to facilitate integration of external and generated code and achieve compliance with code standards and guidelines, you can configure how the code generator produces function interfaces from a model or subsystem.

Configure Reusability of Entry-Point Functions

By default, top models generate 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 (component) 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:

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 is set to C and Code interface packaging (component) is set to Reusable function, the code generator produces this entry-point function code for each system target file scenario.

System Target File Interface
GRT-based Reusable, multi-instance C entry-point functions that are reentrant.Packs values of model root-level Inport blocks andOutport 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 asmalloc.
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:

Configure Reusability of Model Reference Entry-Point Functions

By default, model references generate code that is reusable and reentrant. The code generator calls each referenced model function with unique data.

You can configure whether a referenced model entry-point function is reusable with the model configuration parameter Code interface packaging (subcomponent).

By default, nonreusable model references generate code that passes root-level I/O data to entry-point functions as function arguments. If you are using an ERT-based system target file, consider eliminating function arguments by passing root-level I/O data as global variables. Set the model configuration parameter Implement root-level I/O as (Embedded Coder) to Global variables.

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 filertwtypes.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 thert_OneStep function.
  8. If applicable, read data from generated variables that represent model Outport blocks.

For more information, see Deploy Applications to Target Hardware (Embedded Coder).

C Function Interface Customization Limitations

These limitations apply to customizations for generated C function interfaces:

See Also

Simulink Function | Function Caller

Topics