Call Reusable Hardware-Independent External Code for Simulation and Code Generation
Code reuse offers business and technological advantages. From a business perspective, code reuse saves time and resources. From a technological perspective, code reuse promotes consistency and reduces memory requirements. Other considerations include:
Modularizing an application
Reusing an optimized algorithm
Interfacing with a predefined dataset
Developing application variants
Examples of reusable hardware-independent algorithmic code to consider importing into the Simulink® environment for simulation and code generation include:
Utility functions
Lookup tables
Digital filters
Specialized integrators
Proportional-integral-derivative (PID) control modules
Workflow
To call reusable external algorithm code for simulation and code generation, iterate through the tasks listed in this table.
Task | Action | More Information |
---|---|---|
1 | Review your assessment of external code characteristics and integration requirements. | |
2 | Choose an integration technique to add the external code to a Simulink model. | |
3 | Verify algorithm behavior and performance by simulating the model. | |
4 | Define the representation of model data for code generation. | Code Interface Configuration and Exchange Data Between External C/C++ Code and Simulink Model or Generated Code (Embedded Coder) |
5 | Configure the model for code generation. | Generate Code That Matches Appearance of External Code (Embedded Coder) |
6 | Generate code and a code generation report. | |
7 | Review the generated code interface and static code metrics. | |
8 | Build an executable program from the model. | |
9 | Verify that executable program behaves as expected. |
Choose an Integration Technique
Several techniques are available for integrating reusable hardware-independent algorithmic code into the Simulink environment. Some techniques integrate external code directly. Other approaches convert the external code to Simulink or Stateflow® modeling elements. The integration technique that you choose depends on:
Programming language of the external code — MATLAB®, C, C++, or Fortran
Your programming language experience and preference
Performance requirements
Whether the algorithm models continuous time dynamics or you are integrating the algorithm into an application that uses discrete and continuous time
Whether you want to take advantage of Model-Based Design
Level of control required over the code that the code generator produces
To choose an integration technique for a reusable algorithm, see the subsection that matches the programming language of your external code.
Integration Techniques for External MATLAB Code
Multiple techniques are available for integrating external MATLAB code into the Simulink environment. Use this table to choose the best integration technique for your application.
Condition or Requirement | Action | More Information |
---|---|---|
| Add a MATLAB Function block to the model. Embed the MATLAB code in the block. | |
| Add a MATLAB System block to the model. Embed the MATLAB code in the block as a System object™. | |
| Add a Stateflow chart to the model. Call the external code from the chart, using MATLAB as the action language. | |
You want to use the parfor function for
parallel computing. | Add a MATLAB Function block to the model. Embed the MATLAB code in the block. | |
You have C or C++ programming experience and the external MATLAB code is compact and primarily uses C or C++ constructs. | Manually convert the MATLAB code to C or C++ code. Choose an integration approach for C or C++ code. | |
Sections of the external MATLAB code map to built-in blocks. | Develop the algorithm in the context of a model, using the applicable built-in blocks. | |
The external algorithm models continuous state dynamics. | Write a MATLAB S-function and TLC file for the algorithm. Add the S-function to your model. |
To embed external MATLAB code in a MATLAB Function or MATLAB System block or generate C or C++ code from MATLAB code with the MATLAB Coder™, the MATLAB code must use functions and classes supported for C/C++ code generation. For more information, see Functions and Objects Supported for C/C++ Code Generation.
Integration Techniques for External C or C++ Code
Multiple techniques are available for integrating external C or C++ code into the Simulink environment. The following table helps you choose the best integration technique for your application.
Condition or Requirement | Action | More Information |
---|---|---|
You want to integrate external C code with generated C++ code or conversely | Match the language choice for the generated code by modifying the language of the external code. | Modify Programming Language of External Code to Match Generated Code |
| Use the Simulink C Caller block. | Integrate C Code Using C Caller Blocks |
| Use the C Function block | Integrate External C/C++ Code into Simulink Using C Function Blocks |
| Use the C Function block. | |
| Generate S-function and TLC files by using the S-Function Builder. If necessary, refine the generated code manually to meet application requirements. (If you change the generated code, you lose the changes if you regenerate the S-function and TLC files.) | Build S-Functions Automatically Using S-Function Builder |
| Generate S-function and TLC files by using the S-Function Builder. If necessary, refine the generated code manually to meet application requirements. (If you change the generated code, you lose the changes if you regenerate the S-function and TLC files.) | Build S-Functions Automatically Using S-Function Builder |
| Manually write an S-function and TLC file. | |
| Manually write an S-function and TLC file. | |
Your C/C++ algorithm includes design logic that is based on state machines and flow charts. Or, a function that you want to integrate must exchange data with a model by using global variables. The function defines the global variables and uses them to write output rather than returning a value or writing output to an argument. | Add a Stateflow chart to the model. Call the external code from the chart, using C as the action language. In the chart, write code that calls the external function and reads from and writes to the global variables. To perform calculations with output of the external code, the model must read from the global variable during execution. | Integrate External Code with Stateflow Charts |
You want to include external C/C++ code in a Stateflow chart for simulation and code generation. | Configure the model that contains the chart to apply the external C or C++ code. |
|
You want to embed a call to external C/ C++ code from a model quickly. Performance is not an issue. | Call the C/C++ code with the coder.ceval
function from within a MATLAB Function block. |
Modify Programming Language of External Code to Match Generated Code
To integrate external C code with generated C++ code or conversely, modify the language of the external code to match the programming language of the generated code. To match the programing language of the external code with the generated code, use the appropriate approach explained below:
Rewrite the external code to match the programming language of the generated code.
If you are generating C++ code and the external code is C code, for each C function, create a header file that prototypes the function. Use this format:
#ifdef __cplusplus extern "C" { #endif int my_c_function_wrapper(); #ifdef __cplusplus } #endif
The prototype serves as a function wrapper. If your compiler supports C++ code, the value
__cplusplus
is defined. The linkage specificationextern "C"
specifies C linkage without name mangling.If you are generating C code and the external code is C++ code, include an
extern "C"
linkage specification in each.cpp
file. For example, the following example shows C++ code in the filemy_func.cpp
:extern "C" { int my_cpp_function() { ... } }
Integration Approaches for External Fortran Code
To integrate external Fortran code:
Write an S-function and corresponding TLC file.
Add the S-function to your model.
If necessary, add support files and control model code generation and builds within the Simulink environment.
For more information, see Integrate Fortran Code into Simulink.