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:

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.

  1. 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
  2. From the modelEvPowertrainController, on theModeling tab, clickModel Settings. From the top bar of the Configuration Reference dialog box, open the referenced configuration set.
  3. Specify the C files for simulation. On theSimulation Target pane, set these parameters:
    • Include headers"customFcns.h"
    • Source filescustomFcns.c
    • Include directories — Optionally set this if header and source files are saved in directories. For this example, leave this blank.
  4. 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.
  5. 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.

  1. In the modelEvPowertrainController, navigate into the subsystem Regen Braking > Brake Pedal to Total Braking Pressure Request.
  2. Remove the Product block.
  3. Add a C Caller block in place of the Product block.
  4. Double-click the block to open the Block Parameters dialog box. Click the Refresh button to import your source code and its dependencies.
  5. In the Function name list, select customProd. ClickOK.
    Block parameters dialog box showing the Function name set to customProd.
    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.
  6. Connect the signals to the ports of theC Caller block.
    Subsystem showing the C Caller block that calls customProd connected to the signals.
  7. Save the model.
  8. 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.

  1. In the modelEvPowertrainController, open the Embedded Coder® app.
  2. On the C Code tab, clickGenerate Code.
  3. The Code view opens and displays the generated code. In the fileEvPowertrainController.c, find the code that calls your custom C code.
    rtb_BrkTrqFric = customProd
    (get_EvPowertrainController_RegenBraking_func_DecCmd_outside_ex(),
    EvPowertrainController_s_Params.BrkPrsMax);
  4. The generated header fileEvPowertrainController.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.

See Also

C Caller

Topics