Main Content

Model Explicit Function Invocation with Function-Call Subsystems

Deploy embedded system code from Simulink® models by partitioning a model into function-call subsystems that you build separately.

Function-Call Subsystem Model

Open the example model rtwdemo_explicitinvocation_funccallsubsys. The model is configured to display color-coded sample times with annotations. To see them, after opening the model, update the diagram by pressing Ctrl+D. To display the legend, press Ctrl+J.

This model partitions an algorithm into three function-call subsystems: Rate1s, Rate2s, and DataBuffer. Use function-call subsystems to model multirate systems explicitly.

Subsystems Rate1s and DataBuffer use a sample time of 1 second. Subsystem Rate2s uses a sample time of 2 seconds.

This model design is referred to as export function modeling. Simulink constrains the model to function-call subsystems at the root level. The driving Inport block specifies the function name.

Relevant Model Configuration Parameter Settings

  • Type set to Fixed-step.

  • Solver set to discrete (no continuous states).

  • Treat each discrete rate as a separate task selected. Simulink applies multitasking execution because the model uses multiple sample rates.

Scheduling

Simulink® simulates the model based on the model configuration. Simulink propagates and uses the Inport block sample times to order block execution based on a single-core, multitasking execution platform.

In the sample time legend, red identifies the fastest discrete rate. Magenta identifies rates inherited from exported functions, indicating their execution is outside the context of Simulink scheduling.

Your execution framework must schedule the generated function code and transfer data between functions.

Your execution framework needs to schedule the generated function code and handle data transfers between functions. The generated code is simple and you control the order of execution.

Generate Code and Report

Open the Embedded Coder app. Then, generate code and a code generation report.

Review Generated Code

From the code generation report, review the generated code.

  • ert_main.c is an example main program (execution framework) for the model. This code shows how to call the exported functions. The code also shows how to initialize, execute, and terminate the generated code.

  • rtwdemo_explicitinvocation_funccallsubsys.c calls the initialization function and exported functions for subsystems Rate1s, Rate2s, and DataBuffer.

  • rtwdemo_explicitinvocation_funccallsubsys.h declares model data structures and a public interface to the exported entry point functions and data structures.

  • rtwtypes.h defines data types, structures, and macros that the generated code requires.

Code Interface

Open and review the Code Interface Report. Use the information in that report to write the interface code for your execution framework:

  1. Include the generated header files by adding directives #include Rate1s.h, #include DataBuffer.h, and #include Rate2s.h.

  2. Write input data to the generated code for model Inport blocks.

  3. Call the generated entry-point functions.

  4. Read data from the generated code for model Outport blocks.

Input ports:

  • rtU.In1_1s of type real_T with dimension of 1

  • rtU.In2_2s of type real_T with dimension of 1

Entry-point functions:

  • Initialize entry-point function, void rtwdemo_explicitinvocation_funccallsubsys_initialize(void). At startup, call this function once.

  • Exported function, void CallEvery1s(void). Call this function as needed.

  • Exported function, void CallEvery1s(void). Call this function as needed.

  • Exported function, void CallEvery2sAt1sPriority(void). Call this function as needed.

Output ports:

  • rtY.Out1 of type real_T with dimension of 1

  • rtY.Out2 of type real_T with dimension of 1

More About