Main Content

Call External C Code from Model and Generated Code

Call existing, external functions from a simulation or from the generated code by using the Legacy Code Tool.

Learn how to:

  • Evaluate a C function as part of a Simulink® model simulation.

  • Call a C function from the code that you generate from a model.

For information about the example model, see Prepare a Control Algorithm Model for C Code Generation.

Replacement Process

Open the example model, rtwdemo_PCG_Eval_P4.

For many Model-Based Design applications, in addition to Simulink® models, a design includes a set of existing C functions that have been tested and validated. You can integrate these functions into a Simulink® model and generate code that uses the functions.

In this example, you create a custom Simulink® block that calls an existing C function. You then include the block in a model and test the overall system through model simulation in Simulink®.

In the example model, you can replace the Lookup blocks (lookup tables) in the PI controllers with calls to an existing C function. The function is defined in files SimpleTable.c and SimpleTable.h.

View SimpleTable.c.

View SimpleTable.h.

Create Block That Calls C Function

To specify a call to an existing C function, use an S-Function block. You can automate the creation of the S-Function block by using the Legacy Code Tool. In the tool, you first specify an interface for your existing C function. The tool then uses that interface to create an S-Function block.

Use the Legacy Code Tool to create an S-Function block for the existing C function in SimpleTable.c.

1. Create a structure to contain the definition of the function interface.

def = legacy_code('initialize')

You can use the structure def to define the function interface to the existing C code.

2. Populate the fields of the structure.

3. Create the S-function.


4. Compile the S-function.


5. Create the S-Function block.


The generated S-Function block calls the C function in SimpleTable.c. You can now use this S-Function block in models.

6. Create the TLC file.


This command creates a TLC file, which is the component of an S-Function that specifies how to generate code for the block.

Validate External Code Through Simulation

When you integrate existing C code in a Simulink® model, validate the generated S-Function block.

To validate the replacement of the Lookup blocks, compare the simulation results produced by the Lookup blocks with the results produced by the new S-Function block.

1. Open the validation model.

  • The Sine Wave block produces output values from [-2 : 2].

  • The input range of the lookup table is from [-1 : 1].

  • The lookup table outputs the absolute value of the input.

  • The lookup table clips the output at the input limits.

2. Run the validation model.

The figure shows the validation results. The existing C code and the Simulink® table block produce the same output values.

Validate C Code as Part of Simulink® Model

After validating the existing C function code as a standalone component, validate the S-function in the model. To complete the validation, use a test harness model.

1. Open the test harness.

2. Run the test harness.

The simulation results match the golden values.

Call C Function from the Generated Code

The code generator uses the TLC file to process the S-Function block like any other block. The code generator can implement expression folding with the S-Function block, an operation that combines multiple computations into a single output calculation.

1. Build the full model.

2. Examine the generated code in PI_Control_Reusable.c.

The generated code now calls the SimpleTable C function.

The figures show the generated code before and after the C code integration. Before the integration, the code calls a generated lookup routine. After the integration, the generated code calls the C function SimpleTable.

Related Topics