Call Existing Code from a Model and Generated Code - MATLAB & Simulink (original) (raw)
Main Content
You can integrate new or existing C code into your model for simulation or for code generation. Simulink® provides several different techniques to integrate C code. Call existing C code from the model if you:
- Have custom behavior implemented in C code that you want to use in your models.
- Have an optimized version of a certain behavior implemented in C code.
- Want to integrate your C code in simulation, code generation, and testing workflows using Simulink and other products.
This example uses the C Caller block to integrate external source code into the model. Depending on the nature of your external code and your goals for using the code in your models, consider the other options described in Choose an External Code Integration Workflow.
Specify Source Code and Dependencies
Specify the external source code file that contains your C functions. For this example, you replace the Product block in the model with a call to the customProd
function, which is defined in the source file customFcns.c
.
- If you have not already done so, follow the steps in the previous part of the workflow, Apply Code and Deployment Configurations to Models, or run the script.
openExample("EvPowertrainController")
MappingEvPowertrainModel - From the model
EvPowertrainController
, on theModeling tab, clickModel Settings. From the top bar of the Configuration Reference dialog box, open the referenced configuration set. - Specify the C files for simulation. On theSimulation Target pane, set these parameters:
- Include headers —
"customFcns.h"
- Source files —
customFcns.c
- Include directories — Optionally set this if header and source files are saved in directories. For this example, leave this blank.
- Include headers —
- Specify the C files for code generation. On the > pane, select Use the same custom code settings as Simulation Target.
If you want to use different custom code for code generation, leave this parameter cleared and specify code information in the Custom code settings section. - Click OK.
Once you specify the files in the configuration set, the C functions become available for selection in the C Caller block.
Call the C Caller Block
Add the C Caller block to the model and configure the ports and parameters. For this example, replace a Product block with a C Caller block that calls thecustomProd
function.
- In the model
EvPowertrainController
, navigate into the subsystemRegen Braking
>Brake Pedal to Total Braking Pressure Request
. - Remove the Product block.
- Add a C Caller block in place of the Product block.
- Double-click the block to open the Block Parameters dialog box. Click the Refresh button to import your source code and its dependencies.
- In the Function name list, select
customProd
. ClickOK.
This list shows the C functions that are available to your model based on the files that you specified in the configuration parameters. You can map C function arguments from your source code to Simulink ports by using the Port specification table. For this example, do not change the port mapping. - Connect the signals to the ports of theC Caller block.
- Save the model.
- Optionally, simulate the model. When you simulate the model, Simulink calls your custom C function.
Maintain C Caller Blocks in a Library
To manage your C Caller blocks, use a library model to store them. A library helps you to encapsulate theC Caller blocks by storing the configuration settings (which specify the source and header files) in the library. With this configuration, only the models that use the C Caller blocks are dependent on the associated source code and you do not need to set the configuration parameters for each of these models. For more information, see Create a Custom C Caller Library.
Generate the C Code
Generate C code from the model and check that the code calls the custom C function customProd
.
- In the model
EvPowertrainController
, open the Embedded Coder® app. - On the C Code tab, clickGenerate Code.
- The Code view opens and displays the generated code. In the file
EvPowertrainController.c
, find the code that calls your custom C code.
rtb_BrkTrqFric = customProd
(get_EvPowertrainController_RegenBraking_func_DecCmd_outside_ex(),
EvPowertrainController_s_Params.BrkPrsMax); - The generated header file
EvPowertrainController.h
includes the custom header filecustomFcns.h
.
/* user code (top of header file) */
#include "customFcns.h"
If your custom C code requires more customizations for integration, you can use other integration options such as the C Function block. For more information, see Choose an External Code Integration Workflow.