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 and Global 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.
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 as y = f3(u)
and 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 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
to Pointer to const
.
Reorder the arguments. Drag the y
argument row above the
u
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
to y
. In that case, the C function prototype
is y = 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 for function3
. 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; }
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 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.
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 as y = f3(u)
and C/C++ function
prototype as y = 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. 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.
Configure the arguments. For this example, configure the u
argument. Set C/C++ type Qualifier to Pointer 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
to void
. In that case, the C function prototype
is void = 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 for
rtwdemo_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; }
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 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.
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 as
y = f3(u)
and C/C++ function prototype as
y = 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 to Pointer 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
to void
. In that case, the C++ function
prototype is void = 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 function f3
.
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; }
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®.