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.
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 example model
rtwdemo_functions
. Save a copy of the model to a writable location.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 C/C++ Function Interface dialog box shows Simulink function prototype asy = f3(u)
and C/C++ function prototype asvoid f3(rtu_u, * rty_y)
. The preview for C/C++ function prototype is updated as you make changes.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, enter a custom name or naming rule. Specify valid C-identifier characters, identifier format macros, or a combination of characters and macros. For this example, name the function
function3
and, for the arguments, enter naming rule$N$M
.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 toPointer to const
.Reorder the arguments. Drag the
y
argument row above theu
argument row.
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.
Optionally, you can change C/C++ return argument from
void
toy
. In that case, the C function prototype isy = function3(const * u)
.Save your changes by clicking OK.
Save the model.
Make sure that the Simulink Function block is not selected. Then, generate code for the model.
In the Code view, open the generated file
rtwdemo_functions.c
and search forfunction3
. The generated function code reflects the changes to the generated C function prototype.void function3(real_T *y, const real_T *u) { rtY.TicToc10 = rtDWork.Delay_DSTATE; rtDWork.Delay_DSTATE = (int8_T)(int32_T)-(int32_T)rtY.TicToc10; adder(rtB.Subtract, rtU.U2, *u, &rtB.FunctionCaller); *y = rtB.FunctionCaller; }
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 model
rtwdemo_functions
. Save it to a writable location.Change the visibility of the global function
f3
from global to scoped. Open the Simulink Function blockf3
. Inside the function block, double-click the Trigger Port blockf3
. In the block parameters dialog box, set Function visibility toscoped
. Click Apply and OK.In the apps gallery, open the Embedded Coder app.
In the C Code tab, click the Code Mappings - C tab.
In the Code Mappings editor, click the Functions tab.
In the row for function
f3
, click the function preview hyperlink. The C/C++ Function Interface dialog box shows Simulink function prototype asy = f3(u)
and C/C++ function prototype asy = rtwdemo_functions_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. Theself
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 toMultiple
, the argument appears asself
, 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 toMultiple
, the argument appears as[self]
, indicating that the argument is optional. The code generator produces aself
structure only if you use the model as a top model and Code interface packaging is set toReusable function
or if you use the model as a referenced model and Total number of instances allowed per top model is set toMultiple
.For more information about configuring a model for multi-instantiable code generation, see Generate Reentrant Code from Simulink Function Blocks.
Configure the arguments. For this example, configure the
u
argument. Set C/C++ type Qualifier toPointer to const
.Verify your change 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.
Optionally, you can change C/C++ return argument from
y
tovoid
. In that case, the C function prototype isvoid = rtwdemo_functions_f3(* rty_y, const * rtu_u)
.Save your change by clicking OK.
Save the model.
Make sure that the Simulink Function block is not selected. Then, generate code for the model.
In the Code view, open the generated file
rtwdemo_functions.c
and search forrtwdemo_functions_f3
. The generated function code reflects the changes to the generated C function prototype.real_T rtwdemo_functions_f3(const real_T *rtu_u) { real_T rty_y_0; rtY.TicToc10 = rtDWork.Delay_DSTATE; rtDWork.Delay_DSTATE = (int8_T)(int32_T)-(int32_T)rtY.TicToc10; adder(rtB.Subtract, rtU.U2, *rtu_u, &rtB.FunctionCaller); rty_y_0 = rtB.FunctionCaller; return rty_y_0; }
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 model
rtwdemo_functions
. Save it to a writable work area.Change the visibility of the global function
f3
from global to scoped. Open the Simulink Function blockf3
. Inside the function block, double-click the Trigger Port blockf3
. In the block parameters dialog box, set Function visibility toscoped
. Click Apply and OK.In the apps gallery, open the Embedded Coder app.
Set the model configuration parameter Language to
C++
.Open the Configure C/C++ Function Interface dialog box. 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 asy = f3(u)
and C/C++ function prototype asy = rtwdemo_functions_f3(rtu_u)
. The preview for C/C++ function prototype is updated as you make changes.Configure the arguments. For this example, configure the
u
argument. Set C/C++ type Qualifier toPointer to const
.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.
Optionally, you can change C/C++ return argument from
y
tovoid
. In that case, the C++ function prototype isvoid = rtwdemo_functions_f3(* rty_y, const * rtu_u)
.Save your change by clicking OK.
Save the model.
Make sure that the Simulink Function block is not selected. Then, generate code for the model.
In the Code view, open the generated file
rtwdemo_functions.cpp
and search for functionf3
. The generated function code reflects the changes to the generated C++ function prototype.real_T rtwdemo_functionsModelClass::f3(const real_T *rtu_u) { real_T rty_y_0; rtY.TicToc10 = rtDWork.Delay_DSTATE; rtDWork.Delay_DSTATE = static_cast<int8_T>(static_cast<int32_T> (-static_cast<int32_T>(rtY.TicToc10))); adder(rtB.Subtract, rtU.U2, *rtu_u, &rtB.FunctionCaller); rty_y_0 = rtB.FunctionCaller; return rty_y_0; }
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