Contents

Calling External C Code from the Simulink® Model and Generated Code

This example shows how to call external functions from a simulation or generated code by using the Legacy Code Tool.

Time: 45 minutes

Goals

Understand...

  • How to evaluate a C function as part of a Simulink® model simulation

  • How to call a C function from code generated by Simulink® Coder™

Task: Open the model.Task: Open the model.

Replacement Process

Simulink® models are one part of Model-Based Design. For many applications, a design also includes a set of existing C functions that have been tested and validated. The ability to easily integrate these functions into a Simulink® model and generated code is critical to using Simulink® in the controls development process.

This module shows how to create a custom Simulink® block that calls an existing C function. Once the block is part of the model, you can take advantage of the simulation environment to further test the system.

As an example, the Lookup blocks (lookup tables) in the PI controllers are replaced with calls to an existing C function. The function is defined in the files SimpleTable.c and SimpleTable.h.

Task: View SimpleTable.c.Task: View SimpleTable.c.

Task: View SimpleTable.h.Task: View SimpleTable.h.

Creating a Block That Calls a C Function

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

Complete steps 1 through 6 below to create an S-Function block for an existing C function SimpleTable.c. A link to more information on using the Legacy Code Tool is provided at the end of this module.

1. Task: Create the function interface definition structure.Task: Create the function interface definition structure.

def=legacy_code('initialize')

The data structure def defines the function interface to the existing C code.

2. Task: Populate the function interface definition structure.Task: Populate the function interface definition structure.

3. Task: Create the S-function.Task: Create the S-function.

legacy_code('sfcn_cmex_generate',def)

4. Task: Compile the S-function.Task: Compile the S-function.

legacy_code('compile',def)

5. Task: Create the S-Function block.Task: Create the S-Function block.

legacy_code('slblock_generate',def)

S-function creation is a one-time task. Once the block exists, you can reuse it in any model.

6. Task: Create the TLC file.Task: Create the TLC file.

legacy_code('sfcn_tlc_generate',def)

Steps 1 through 5 created an S-function block that calls the specified function at each time step during simulation. Step 6 creates a TLC file, which is the component of an S-Function that specifies how Simulink® Coder™ generates code for the block.

Validating the External Code in the Simulink® Environment

When you integrate existing C code with a Simulink® model, always validate the results.

In this example, you replace Lookup blocks with an existing C function. To validate the replacement, you compare simulation results produced with the Lookup block with results produced with the new S-Function block.

1. Task: Open the validation model.Task: 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 output from the lookup table is the absolute value of the input.

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

2. Task: Run the validation model.Task: Run the validation model.

The following figure shows the validation results. Note that the existing C code and the Simulink® table block provide the same output values.

Validating the C Code as Part of the Simulink® Model

After you validate the functionality of the existing C function code as a standalone component, validate the S-function in the model. Use the test harness model to complete the validation.

1. Task: Open the test harness.Task: Open the test harness.

2. Task: Run the test harness.Task: Run the test harness.

The simulation results match the expected golden values:

Calling the C Function from the Generated Code

Simulink® Coder™ uses the TLC file to process the S-Function block like any other block in the system. Calls to the C code of the S-Function block:

  • Can use data objects

  • Are subject to expression folding, an operation that combines multiple computations into a single output calculation

1. Task: Build the full model.Task: Build the full model.

2. Task: Examine the generated code (PI_Control_Reusable.c).Task: Examine the generated code (PI_Control_Reusable.c).

The generated code now calls the SimpleTable C function.

The following figures show the generated code before and after the C code integration. Before the integration, the generated code called rt_Lookup. After the integration, the generated code calls the C function SimpleTable.

Further Study Topics

Was this topic helpful?