Configure Entry-Point Function Interfaces for Simulink Function and Function Caller Blocks
With Embedded Coder®, you can customize the generated C and C++ function interfaces for Simulink Function and Function Caller blocks. Function code interface configuration supports easier integration of generated code with functions or function calls in external code and customizations for coding standards or design requirements.
You can customize generated C and C++ function interfaces for:
Global Simulink® functions
Exported scoped Simulink functions
You cannot customize the generated function interface for a scoped Simulink Function block that is not located at the root level of a model. For more information, see Scoped, Global, and Port-Scoped Simulink Function Blocks Overview.
By opening a dialog box from a selected Simulink Function or Function Caller block, you can customize the function prototype generated for that block. Your changes for the selected block also update other corresponding Simulink Function and Function Caller blocks in the model. The function visibility, global or scoped, set in the function Trigger Port block determines which function attributes you can modify:
For a global function, you can change the function name, and the names, type qualifiers, and order of function arguments.
For an exported scoped function, you can change the argument type qualifiers and the order of arguments. You cannot change the function and argument names.
Your changes do not graphically alter the model and do not affect the Simulink function prototype defined in the block.
You can customize the generated function interface to reuse arguments as input and output using Simulink functions. Doing so eliminates extra memory allocations and data copies involving variables representing the function output, and can potentially improve performance. This procedure requires you to modify the Simulink function prototype defined in the block.
Embedded Coder supports Simulink function code interface configuration for models configured with ERT-based system target files, except for AUTOSAR system target files.
Configure Generated C Function Interface for Global Simulink Function Block
This example shows how to customize the C function interface that the code generator produces for a global Simulink Function block.
Open the example model.
Open the Configure C/C++ Function Interface Dialog Box.
Configure the Simulink function prototype.
Generate and review the code.
Open Example Model
Open example model SimulinkFunctions
. Save a copy of the model to a writable location.
open_system('SimulinkFunctions');
Open Configure C/C++ Function Interface Dialog Box
In the apps gallery, open the Embedded Coder app.
On the C Code tab, select Code Interface > Individual Element Code Mappings.
In the Code Mappings editor, click the Functions tab.
In the row for Simulink Function:f3
, click the function preview hyperlink. The Configure C/C++ Function Interface dialog box shows Simulink function prototype as y = f3(u)
and the C/C++ function prototype as void f3(rtu_u,* rty_y)
. The preview for C/C++ function prototype is updated as you make changes.
Configure Simulink Function Prototype
Configure the function name and arguments.
Modify the function and argument identifier names. Model configuration parameter Subsystem method arguments defines the default naming rule for Simulink Function arguments. Changes that you make in the C/C++ Function Interface dialog box override the default naming rule. In the C/C++ function name field, and in the C/C++ Identifier Name column for each Simulink function argument, you can enter a custom name or naming rule. Specify valid C-identifier characters, identifier format macros, or a combination of characters and macros. To see tips about available macros, place your cursor over C/C++ function name and C/C++ Identifier Name. For more information about identifier naming rules, see Identifier Format Control.
For argument u
, set C/C++ Type Qualifier to Pointer to const
.
Optionally, you can change C/C++ return argument from void
to y
. In that case, the C function prototype is y = f3(const * rtu_u)
.
Reorder the arguments by dragging them to a new location.
Verify your changes by checking the function prototype preview.
Click Apply. Your modifications, whether made to a Simulink Function block or a Function Caller block, affect code generation for the Simulink Function block and corresponding Function Caller blocks in the model.
Save your changes by clicking OK. Then, save the model.
Generate and Review Code
Generate code for the model.
In the Code view, open the generated file SimulinkFunctions.c
and search for f3
. The generated function code reflects the changes to the generated C function prototype. This code is generated for the example model as provided:
void f3(const real_T *rtu_u, real_T *rty_y) { int8_T rtb_Gain; rtY.TicToc10 = rtDWork.Delay_DSTATE; rtb_Gain = (int8_T)-rtY.TicToc10; adder(rtB.Subtract, rtU.U2, *rtu_u, &rtB.FunctionCaller); *rty_y = rtB.FunctionCaller; rtDWork.Delay_DSTATE = rtb_Gain; }
More About
Configure Generated C Function Interface for Exported Scoped Simulink Function Block
This example shows how to customize the C function interface that the code generator produces for an exported, scoped Simulink Function block.
For an exported, scoped function, you can modify the generated return argument, the argument type qualifiers, and the order of arguments. You cannot change the generated function name and argument names.
Open the example model.
Change the visibility of the Simulink function.
Open the Configure C/C++ Function Interface dialog box.
Configure the Simulink Function prototype.
Generate and review the code.
Open Example Model
Open example model SimulinkFunctions
. Save a copy of the model to a writable location.
Change Visibility of Simulink Function
Change the visibility of the global function f3
from global to scoped. Open the Simulink Function block f3
. Inside the function block, double-click the Trigger Port block f3
. In the block parameters dialog box, set Function visibility to scoped
. Click Apply and OK.
Open Configure C/C++ Function Interface Dialog Box
In the apps gallery, open the Embedded Coder app.
On the C Code tab, select Code Interface > Individual Element Code Mappings.
In the Code Mappings editor, click the Functions tab.
In the row for Simulink Function:f3
, click the function preview hyperlink. The Configure C/C++ Function Interface dialog box shows Simulink function prototype as y = f3(u)
and C/C++ function prototype as y = SimulinkFunctions
_f3(rtu_u)
. The preview for C/C++ function prototype is updated as you make changes.
For models that you configure for C multi-instantiable code, the interface preview for scoped Simulink functions includes a self
argument. The self
argument is a pointer to a version of the real-time model (RT_MODEL
) data structure that stores multi-instance data associated with reusable functions.
If model configuration parameter Code interface packaging is set to Reusable function
and Total number of instances allowed per top model is set to Multiple
, the argument appears as self
, indicating that the code generator produces a self structure.
If model configuration parameter Code interface packaging is set to Reusable function
or Total number of instances allowed per top model is set to Multiple
, the argument appears as [self]
, indicating that the argument is optional. The code generator produces a self
structure only if you use the model as a top model and Code interface packaging is set to Reusable function
or if you use the model as a referenced model and Total number of instances allowed per top model is set to Multiple
.
For more information about configuring a model for multi-instantiable code generation, see Generate Reentrant Code from Simulink Function Blocks (Embedded Coder).
Configure Simulink Function Prototype
Configure the function arguments. For this example, configure the u
argument. Set C/C++ type Qualifier to Pointer to const
.
Optionally, you can change C/C++ return argument from y
to void
. In that case, the C function prototype is void SimulinkFunctions_f3(* rty_y, const * rtu_u)
.
Reorder the arguments by dragging them to a new location.
Verify your changes by checking the function prototype preview.
Click Apply. Your modifications, whether made to a Simulink Function block or a Function Caller block, affect code generation for the Simulink Function block and corresponding Function Caller blocks in the model.
Save your changes by clicking OK. Then, save the model.
Generate and Review Code
Generate code for the model.
In the Code view, open the generated file SimulinkFunctions.c
and search for SimulinkFunctions_f3
. The generated function code reflects the changes to the generated C function prototype.
real_T SimulinkFunctions_f3(const real_T *rtu_u) { real_T rty_y_0; int8_T rtb_Gain; rtY.TicToc10 = rtDWork.Delay_DSTATE; rtb_Gain = (int8_T)-rtY.TicToc10; adder(rtB.Subtract, rtU.U2, *rtu_u, &rtB.FunctionCaller); rty_y_0 = rtB.FunctionCaller; rtDWork.Delay_DSTATE = rtb_Gain; return rty_y_0; }
More About
Configure Generated C++ Function Interface for Exported Scoped Simulink Function Block
This example shows how to customize the C++ function interface that the code generator produces for an exported, scoped Simulink Function block.
For an exported, scoped function, you can modify the generated return argument, the argument type qualifiers, and the order of arguments. You cannot change the generated function name and argument names.
Open the example model.
Change the visibility of the Simulink function.
Change the programming language setting for code generation to C++.
Open the Configure C/C++ Function Interface dialog box.
Configure the Simulink Function prototype.
Generate and review the code.
Open Example Model
Open example model SimulinkFunctions
. Save a copy of the model to a writable location.
Change Visibility of Simulink Function
Change the visibility of the global function f3
from global to scoped. Open the Simulink Function block f3
. Inside the function block, double-click the Trigger Port block f3
. In the block parameters dialog box, set Function visibility to scoped
. Click Apply and OK.
Change Language Setting for Code Generation
Set the model configuration parameter Language to C++
.
Open Configure C/C++ Function Interface Dialog Box
In the Simulink Editor, right-click the Simulink Function block f3
. In the context menu, select C/C++ Code > Configure C/C++ Function Interface. The dialog box shows Simulink function prototype as y = f3(u)
and C/C++ function prototype as y = f3(rtu_u)
.
Open the Embedded Coder app. In the Code Mappings editor, click the Functions tab.
In the row for Simulink Function:f3
, click the method preview hyperlink. The Configure C/C++ Function Interface dialog box shows Simulink function prototype as y = f3(u)
and C/C++ function prototype as void f3(rtu_u, * rty_y)
.
Configure Simulink Function Prototype
Configure the function arguments. For this example, configure the u
argument. Set C/C++ type Qualifier to Pointer to const
.
Click Apply. In the Code Mappings, verify your change by checking the method preview. Your modifications, whether made to a Simulink Function block or a Function Caller block, affect code generation for the Simulink Function block and corresponding Function Caller blocks in the model.
Save your changes by clicking OK. Then, save the model.
Generate and Review Code
Generate code for the model.
In the Code view, open the generated file SimulinkFunctions.cpp
and search for function f3
. The generated function code reflects the changes to the generated C++ function prototype.
void SimulinkFunctions::f3(const real_T *rtu_u, real_T *rty_y) { int8_T rtb_Gain; rtY.TicToc10 = rtDWork.Delay_DSTATE; rtb_Gain = static_cast<int8_T>(-rtY.TicToc10); adder(rtB.Subtract, rtU.U2, *rtu_u, &rtB.FunctionCaller); *rty_y = rtB.FunctionCaller; rtDWork.Delay_DSTATE = rtb_Gain; }
More About
Customize Generated C Function Interface to Reuse I/O Using Simulink Function Block
This example shows how to customize the C function interface to reuse arguments as input and output using a Simulink Function block.
Open the example model.
Configure the Simulink function to reuse I/O.
Generate and review the code.
Open Example Model
Open example model SimulinkFunctions
. Save a copy of the model to a writable location.
open_system('SimulinkFunctions');
Configure Simulink Function to Reuse I/O
Click the text displaying the function interface of the Simulink Function block f3
. Edit the text to reuse input value u
as output.
To verify the function interface in the generated code, in the apps gallery, open the Embedded Coder app.
On the C Code tab, select Code Interface > Individual Element Code Mappings.
In the Code Mappings editor, click the Functions tab.
In the row for Simulink Function:f3
, click the function preview hyperlink. The Configure C/C++ Function Interface dialog box shows Simulink function prototype as u = f3(u)
and C/C++ function prototype as void f3(* rtuy_u)
.
Generate and Review Code
Generate code for the model.
In the Code view, open the generated file SimulinkFunctions.c and search for f3
. The generated code for function f3
reuses the argument rtuy_u
as both input and output.
void f3(real_T *rtuy_u) { int8_T rtb_Gain; rtY.TicToc10 = rtDWork.Delay_DSTATE; rtb_Gain = (int8_T)-rtY.TicToc10; adder(rtB.Subtract, rtU.U2, *rtuy_u, &rtB.FunctionCaller); *rtuy_u = rtB.FunctionCaller; rtDWork.Delay_DSTATE = rtb_Gain; }
More About
Simulink Function Code Interface Limitations
For C++ code generation, global Simulink functions are incompatible with C++ class interfaces for model entry-point functions.
Simulink function code interface configuration does not support Simulink functions and function callers in Stateflow®.
The Argument Outport block cannot be a return argument for a Simulink function when its data type is set to one of the following types:
Complex types
Bus types
Image types
Non-scalar types
Multiword types
If your model contains a Function Caller block without a Simulink Function block, and you modify only the C/C++ Identifier Name setting of the function prototype of the caller block, regenerating code will not update the function prototype in the generated code. To update the function prototype, follow these steps:
Modify the function prototype in the Configure C/C++ Function interface dialog box.
Delete the
header file in thesimulinkfunction
.hcodeGenFolder
/slprj/
folder.target
/_sharedutilsRegenerate code.