MATLAB Examples

Generate C Function Code for Export-Function Model

This example shows how to generate function code for individual Simulink function blocks and function-call subsystems in a model without generating scheduling code.

To generate function code for export:

  1. Create a model that contains the functions for export.
  2. Create a test harness model that schedules execution of the functions during simulation.
  3. Simulate the model that contains the functions by using the test harness model.
  4. Generate code for the model that contains the functions.

Contents

Create Model That Contains Functions for Export

The model with functions for export must satisfy architectural constraints at the model root level. At the root level, valid blocks are:

  • Inport
  • Outport
  • Function-Call Subsystem
  • Simulink Function
  • Goto
  • From
  • Merge

The code generator produces function code for Function-Call Subsystem and Simulink Function blocks and Initialize and Reset Function blocks. For a Function-call Subsystem block, you connect the block input ports to root Inport blocks that assert function-call signals. The subsystem is executed based on the function-call signal that it receives. A Simulink Function block is executed in response to the execution of a corresponding Function Caller block or Stateflow chart. An Initialize Function block is executed on a model initialize event and a Reset Function block is executed on a user-defined reset event.

For exporting functions, model rtwdemo_functions contains two function-call subsystems (f1_alg and f2_alg) and a Simulink Function block (f3) for exporting functions. The model also contains an Initialize Function block (Initialize Function) and a Reset Function block (Reset Function). To calculate initial conditions for blocks with state in other parts of the model, the State Writer blocks are used inside the Initialize and Reset Function blocks.

open_system('rtwdemo_functions')

Create Model That Contains Function Caller Block

Use a Function Caller block to invoke a Simulink Function block. The Function Caller block can be in the same model or in a different model as the Simulink Function block.

Multiple Function Caller blocks can invoke a Simulink Function block. You can place the Function Caller block inside a function-call subsystem. During code generation, the code generator exports a function from the function-call subsystem.

The model rtwdemo_caller exports a function-call subsystem that contains a Function Caller block.

open_system('rtwdemo_caller')

Create Test Harness Model for Simulation

When you export functions, the generated code does not include a scheduler. Create a test harness model to handle scheduling during simulation. Do not use the test harness model to generate code that you deploy.

Model rtwdemo_export_functions is a test harness. The model:

  • Schedules the Simulink Function block with the Function Caller block in rtwdemo_caller.
  • Provides function-call signals to other models in this example to schedule the model contents, including the model initialize and reset events.
open_system('rtwdemo_export_functions')

Simulate the Test Harness Model

Verify that the model containing the functions that you want to export is executed as you expect by simulating the test harness model. For example, simulate rtwdemo_export_functions.

sim('rtwdemo_export_functions')

Generate Function Code and Report

Generate code and a code generation report for the functions that you want to export. For example, generate code for rtwdemo_functions.

rtwbuild('rtwdemo_functions')
### Starting build procedure for model: rtwdemo_functions
### Successful completion of code generation for model: rtwdemo_functions

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 and execute the generated code.
  • rtwdemo_functions.c calls the initialization function, including Initialize Function, and exported functions for model components f1_alg, f2_alg, and f3.
  • rtwdemo_functions.h declares model data structures and a public interface to the exported entry-point functions and data structures.
  • f3.h is a shared file that declares the call interface for the Simulink function f3.
  • rtwtypes.h defines data types, structures, and macros that the generated code requires.

Write Interface Code

Open and review the Code Interface Report. To write the interface code for your execution framework, use the information in that report.

  1. Include the generated header files by adding directives #include rtwdemo_functions.h, #include f3.h, and #include rtwtypes.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.U1 of type real_T with dimension 1
  • rtU.U2 of type real_T with dimension 1

Entry-point functions:

  • Initialize entry-point function, void rtwdemo_functions_initialize(void). At startup, call this function once.
  • Reset entry-point function, void rtwdemo_functions_reset(void). Call this function as needed.
  • Exported function, void f1(void). Call this function as needed.
  • Exported function, void f2(void). Call this function as needed.
  • Simulink function, void f3(real_T rtu_u, real_T *rty_y). Call this function as needed.

Output ports:

  • rtY.Accumulator1 of type int8_T with dimension [2]
  • rtY.Accumulator2 of type int8_T with dimension [2]
  • rtY.TicToc10 of type int8_T with dimension 1

More About

Close Example Models

bdclose('rtwdemo_export_functions')
bdclose('rtwdemo_functions')
bdclose('rtwdemo_caller')