Main Content

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.

  1. Open the example model.

  2. Open the Configure C/C++ Function Interface Dialog Box.

  3. Configure the Simulink function prototype.

  4. 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 C/C++ function prototype as y = SimulinkFunctions_f3(rtu_u). 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, 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. Then, save the model.

Generate and Review Code

Make sure that the Simulink Function block is not selected. Then, generate code for the model.

In the Code view, open the generated file SimulinkFunctions.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;
}

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.

  1. Open the example model.

  2. Change the visibility of the Simulink function.

  3. Open the Configure C/C++ Function Interface dialog box.

  4. Configure the Simulink Function prototype.

  5. 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 void f3(rtu_u, * rty_y). 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 Simulink Function Prototype

Configure the function 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 = SimulinkFunctions_f3(* rty_y, const * rtu_u).

Save your changes by clicking OK. Then, save the model.

Generate and Review Code

Make sure that the Simulink Function block is not selected. Then, 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;

  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;
}

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.

  1. Open the example model.

  2. Change the visibility of the Simulink function.

  3. Change the programming language setting for code generation to C++.

  4. Open the Configure C/C++ Function Interface dialog box.

  5. Configure the Simulink Function prototype.

  6. 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 = rtwdemo_functions_f3(rtu_u). The preview for C/C++ function prototype is updated as you make changes.

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 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 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 = SimulinkFunctions_f3(* rty_y, const * rtu_u).

Save your changes by clicking OK. Then, save the model.

Generate and Review Code

Make sure that the Simulink Function block is not selected. Then, 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.

real_T SimulinkFunctionsModelClass::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;
}

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

Related Topics