Main Content

Export-Function Models Overview

Export-function models are Simulink® models that generate code for independent functions that can be integrated with an external environment and scheduler. Functions are defined using Function-Call Subsystem, function-call Model, Simulink Function, Message Triggered Subsystem, and S-Function blocks.

The following export-function model contains two functions defined with Function-Call Subsystem blocks. For a step-by-step procedure to create this model, see Create Export-Function Model.

Code generated from this model has two independent functions, one for a delay function and the other for a square function.

      /*
       * File: export_function_model.c    
       * Code generated for Simulink model 'export_function_model'.
       */
		
	void function_call_100ms(void)         /* Sample time: [0.1s, 0.0s] */
	{
	  export_function_model_Y.output_100ms =
	    export_function_model_DW.UnitDelay_DSTATE;
	
	  export_function_model_DW.UnitDelay_DSTATE =
	    export_function_model_Y.output_100ms + 1.0;	
	}

      void function_call_10ms(void)          /* Sample time: [0.01s, 0.0s] */
	{
	    export_function_model_Y.output_10ms = export_function_model_U.input_10ms *
	    export_function_model_U.input_10ms;
	}

Workflows for Export-Function Models

Four common processes for creating export-function models differ in how you simulate and test your model before generating code for the functions.

When function-call sequencing is simple enough to be specified as a model input, simulation using an input matrix is the preferred method for testing an export-function model. For more information, see Test Export-Function Model Simulation Using Input Matrix.

When function-call sequencing is too complicated to specify with an input matrix, create a test model (harness) to mimic the target environment behavior. Use this test model to provide function-call inputs to the export-function model. For more information, see Test Export-Function Model Simulation Using Function-Call Generators, Test Export-Function Model Simulation Using Schedule Editor and Test Export-Function Model Simulation Using Stateflow Chart.

Blocks Allowed at Top Level of Export-Function Models

At the top-level, an export-function model is limited to virtual blocks, blocks with constant (inf) sample time, and the following blocks:

  • Inport

  • Outport

  • Bus Creator

  • Bus Selector

  • In Bus Element

  • Out Bus Element

  • Function-Call Subsystem

  • Model with function-call input ports

  • Simulink Function

  • Function Element

  • Function Element Call

  • Message Triggered Subsystem

  • Initialize Function

  • Reinitialize Function

  • Reset Function

  • Terminate Function

  • Data Store Memory

  • Merge

  • S-Function with function-call input port

  • Function-Call Split

  • Display

  • Scope

For more information, see Nonvirtual and Virtual Blocks and Types of Sample Time.

Requirements for Export-Function Models

For an export-function model to successfully generate function code, the following requirements must be met.

Model Configuration Parameters:

  • Solver Type set to Fixed-step.

  • Solver set to auto or discrete.

  • Code Generation System target file set to ert.tlc. Selecting ert.tlc requires an Embedded Coder® license.

  • For function-call Model blocks, Periodic sample time constraint for the referenced model set to Ensure sample time independent.

Root-level function-call Inport blocks:

  • Output function call check box selected.

  • Cannot receive a signal from an Asynchronous Task Specification block.

Root-level Outport blocks must be connected to one of these blocks or left unconnected or grounded:

  • Function-call block driven by root-level function-call Inport block. This block can be a Function-Call Subsystem block, a function-call Model block, or an S-Function block with function-call input.

  • Simulink Function block.

  • Initialize Function, Reinitialize Function, Reset Function, or Terminate Function block.

  • Merge or Mux block driven exclusively by such blocks.

Root-level data Inport and Outport blocks cannot connect to virtual bus data signals.

Root-level Function-Call Subsystem blocks and function-call Model blocks:

  • All internal blocks within the block must support code generation.

  • If the Trigger block Sample time type is set to:

    • triggered, internal blocks must have Sample time set to -1.

    • periodic, the root-level function-call Inport block must have its Sample time set to a specified discrete time and all internal blocks must have Sample time set to -1 or the specified discrete time.

Designating an Export-Function Model

When you create an export-function model, tell Simulink to treat your model as an export-function model by designating it as an export-function model.

To designate a model as an export-function model, open the Property Inspector. With the root level of the model displayed and with no blocks highlighted, go to the Execution tab and select the Set execution domain check box. Then, from the Domain list, select Export function.

Property Inspector dialog, with "Set execution domain" check box selected and drop-down menu for "Domain" with "Export function" selected

You can also designate a model as an export-function model by setting the IsExportFunctionModel parameter of the model.

set_param(ModelName,'IsExportFunctionModel','on')

Simulink displays a Badge showing "f()" badge in the lower-left corner of the canvas to indicate that a model is specified as an export-function model.

After you designate a model as an export-function model, Simulink performs compile-time checks to warn you if the model does not meet export-function model requirements.

If you load a model that was created and saved in a release prior to R2022a, Simulink designates the model as an export-function model if the model meets the requirements to be an export-function model. In some cases, you may need to manually designate such a model as an export-function model.

Sample Time for Function-Call Subsystems

In an export-function model, you can specify sample time for root-level function-call Inport blocks and the Trigger block inside a root-level Function-Call Subsystem block or function-call Model block. The following table shows how to specify these sample times.

Trigger block Sample time typeTrigger block Sample timeInport block Sample timeFunction-call rate during simulation

Triggered

Not specified, parameter is inactive.

-1 (inherited)

For simulation, the function-call initiator connected to the Inport block sets the rate of simulation.

Specified discrete time

Function-call initiator, in test model, connected to Inport block must have a sample time equal to the specified discrete time for the Inport block.

For simulation, component executes at the specified discrete rate. If a function-call source uses a different sample, Simulink displays an error message.

Periodic

Periodic function-call run-time checks apply if the export-function model is referenced from a Model block.

-1 (inherited) or the specified discrete time for the Inport block.

-1 (inherited)

This configuration is not allowed. Simulink displays an error message.

Specified discrete time.

For simulation, component executes at the specified discrete sample time. If a function-call source uses a different sample time, Simulink displays an error message.

Execution Order for Root-Level Function-Call Inport Blocks

By specifying sample time and priority for function-call Inport blocks you can control the execution order of function-call subsystems and function-call models during simulation. Alternatively, you can use the Schedule Editor or a Stateflow® chart to test scheduling. For more information, see Test Export-Function Model Simulation Using Schedule Editor and Test Export-Function Model Simulation Using Stateflow Chart.

  1. Specify sample time for simulation execution. Right-click a function-call Inport block, then select Block parameters.

  2. Select the Signal Attributes tab. In the Sample time box, enter a discrete time.

  3. Specify the block priority for simulation. Right-click a function-call Inport block, then select Properties.

  4. In the Priority box, enter a priority value.

  5. Display block execution order for simulation. On the Debug tab, select Information Overlays , then from the drop-down dialog, select Execution Order. This display has no impact on the generated code.

In the following export-function model, Function-Call Subsystem 2 with Sample time for Inport block 2 set to 0.01 (10 ms) runs before Function-Call Subsystem 1 with Sample time for Inport block 1 set to 0.1 (100 ms).

Determine Relative Execution Order

Simulink compares function-call Inport block properties to determine their relative execution order using the following rules:

  1. Priority – higher priority (smaller number) executes first

  2. Sample time – smaller sample time executes first

  3. Port number – smaller port number executes first

  4. If the OrderFunctionsByDependency parameter for the model is set to 'on', blocks that have the same sample times are further sorted based on the relative data dependency of the functions to which the blocks are connected.

To see the effect of the OrderFunctionsByDependency parameter, consider the following model.

Three function-call subsystems, numbered 1, 2, and 3. Number 1 has sample time of 100 ms. Number 2 and Number 3 have sample time of 10 ms. The output of Number 3 is connnected to the input of Number 2. The Inport blocks driving Number 1, Number 2, and Number 3 are annotated with F2, F0 and F1, respectively.

Inport block 1 executes last because it has a longer sample time than Inport blocks 2 and 3, which have the same sample time. If OrderFunctionsByDependency is set to 'off', Inport block 2 executes before Inport block 3 based on their port numbers. The execution order is 2, 3, 1.

The OrderFunctionsByDependency parameter is set to 'off' by default. To turn it on, use this command.

set_param(ModelName,'OrderFunctionsByDependency','on')
With the OrderFunctionsByDependency set to 'on', Inport block 3 executes before Inport block 2 because of the data dependency between function-call subsystems 2 and 3. The execution order is 3, 2, 1.

Three function-call subsystems, numbered 1, 2, and 3. Number 1 has sample time of 100 ms. Number 2 and Number 3 have sample time of 10 ms. The output of Number 3 is connnected to the input of Number 2. The Inport blocks driving Number 1, Number 2, and Number 3 are annotated with F2, F1 and F0, respectively.

Note that the OrderFunctionsByDependency parameter affects only the model for which it is set. The parameter does not propagate into referenced models.

When two blocks have different values for the Priority parameter, the block with the higher priority executes first. If the Priority parameter is equal, the block with the faster rate (smaller sample time) executes first. If Priority and sample time are the same for both of the blocks, the block with the lower port number executes first. The exception is when a data dependency exists between the blocks and OrderFunctionsByDependency is set to 'on', in which case the dependent block executes after the block on which it depends.

Note

When the simulation mode of the top model is accelerator or rapid accelerator, Simulink does not perform run-time simulation checks for the execution order of root-level function-call Inport blocks inside referenced export-function models.

Suppose that an export-function model has five root-level function-call Inport blocks, A to E, with block properties as shown in the table. The blocks have no data dependencies, or OrderFunctionsByDependency is set to 'off'. To determine their relative execution order, Simulink compares their Priority parameters, sample times (if distinct and non-inherited), and port numbers.

Root-level function-call Inport blockABCDE
Priority1030404030
Sample Time–10.20.10.1–1
Port Number54321
  • Block A has the highest priority of the five blocks. A executes first.

  • B and E execute after A but before C and D. Since B and E have the same priority, Simulink compares their sample times to determine execution order. E has a sample time of -1 (inherited), which is smaller than 0.2, the sample time of B. E executes before B.

  • C and D have the same priority and the same distinct, non-inherited sample times. The port number for D (2) is smaller than C (3), so D executes before C.

The relative execution order for these function-call Inport blocks is A, E, B, D, C.

Nested Export-Function Models

Nested export-function models provide an additional layer of organization. The following model has two referenced export-function models that are referenced from a Model block.

Note

An export-function model cannot contain a referenced model with asynchronous function-call inputs, but can contain function-call subsystems and function-call models. A model with asynchronous function-call inputs can contain an export-function model, a function-call subsystem, or a function-call model.

Export-Function Model with a Multi-Instanced Function-Call Model

You can use Function-Call Subsystem blocks or function-call Model blocks within an export-function model. If you use a function-call Model block, you can also create multiple instances of the model.

  1. Define your algorithm with a model that contains a Trigger block. Set Trigger type to function-call.

  2. Reference the model from a Model block. The result is a function-call model.

  3. Connect a function-call Inport block and select the Output function call check box. Add signal Inport and Outport blocks. Update the model (Ctrl+D). The result is an export-function model with a function-call model.

  4. Copy the referenced model and port blocks to create a second instance of the model. The two instances are called by different root-level function-call Inport blocks in different tasks.

Note

A top-level non-export-function model that contains a reference model that is set up as a multi-instance export function, does not support code generation. For more information, see Total number of instances allowed per top model.

Export-Function Models and Models with Asynchronous Function-Call Inputs

Export-function model capability is available for models with asynchronous function-call input ports, also referred to as JMAAB-B models. You use these models primarily in the Simulink environment where the Simulink scheduler calls the functions.

Comparison CharacteristicExport-Function ModelsModels with Asynchronous Function-Call Inputs
DefinitionThese models have root-level function-call Inport blocks that are not connected to an Asynchronous Task Specification block. These Inport blocks trigger function-call subsystems or function-call models (Model block with Trigger block).These models have root-level function-call Inport blocks connected to Asynchronous Task Specification blocks. These Inport blocks trigger function-call subsystems or function-call models.
Root-level blocksOnly blocks executing in a function-call context are allowed at the root level.Blocks executing in a non-function-call context are also allowed.
Data transferUse data transfer indicators to interpret simulation results. Data transfer in export-function models is not protected by default in generated code. For more details, see Latched Input Data for Function-Call Subsystems.Use Rate Transition blocks to protect data transferred between function-call subsystems running at different rates. For more information, see Rate Transition.
Simulation supportThese models support standalone simulation and test model simulation in all simulation modes.These models support test model simulation in all simulation modes and standalone simulation in Normal, Accelerator, and Rapid Accelerator modes.
Code generation supportTop-model and standalone code generation are supported.Top-model and standalone code generation are supported.

See Also

| | | | |

Related Topics