主要内容

Customize C Functions Generated from Global Simulink Functions

In the generated model code, each Simulink function corresponds to one of the generated functions. The return value and input arguments of the generated function represent the Argument Inports and Argument Outports of the Simulink function, and the function body corresponds to the Simulink function logic.

A global Simulink® function is a Simulink Function block with the Function visibility parameter of its Trigger block specified as global. Global Simulink functions are callable from any function generated from the model, regardless of the hierarchical relationship between the functions.

This example shows how to customize the generated function name, return value, and input arguments.

Open Model and Explore Global Simulink Function Block

Load the model SimFuncs and open the global Simulink Function block.

sfModel = "SimFuncs";
globalSf = "SimFuncs/globalSimFunc";
load_system(sfModel)
open_system(globalSf)

Simulink Function block globalSimFunc open in model SimFuncs. The name of the trigger block is g_func.

In globablSimFunc, double-click the Trigger block g_func to open its Block Parameters dialog box.

Block Parameters dialog box of g_func.

Treat as Simulink function is selected and Function visibility is set to global.

Prepare to Customize Generated Function

To customize the function, use the Configure C/C++ Function Interface dialog box:

  1. Open the Embedded Coder® app.

  2. Open the Code Mappings editor and navigate to the Functions tab.

  3. Locate the entry with Simulink Function:g_func in the Source column and click on the Function Preview content of the entry.

Code Mappings editor open with the Functions tab selected. In the Source column, the entry Simulink Function:g_func is highlighted, and the mouse cursor is clicking the Function Preview content of the entry.

The Configure C/C++ Function Interface dialog box opens.

Configure C/C++ Function interface dialog box. The Simulink function prototype is [g_out1,g_out2] = g_func(g_in1,g_in2). The C/C++ function prototype is void g_func(*rty_g_out1, *rty_g_out2, rtu_g_in1, rtu_g_in2).

Customize Generated Function Name

You specify the function name as a combination of valid C language characters and macros defined in Identifier Format Tokens. In the C/C++ function name edit box type my_$N, then click Apply. The macro $N is replaced by the Simulink function name, g_func. The name of the function in C/C++ function prototype changes to my_g_func.

Configure C/C++ Function interface dialog box. The value of C/C++ function name is my_$N. The function name in C/C++ function prototype is my_g_func.

In the Simulink Toolstrip, select the C Code tab and in the Generate Code section, click Generate Code.

Simulink Toolstrip with the C Code tab selected. The mouse cursor is clicking the Generate Code button.

In the Code pane, navigate to the generated source code file SimFuncs.c and search for my_g_func to see the definition of the generated function.

Code pane showing the prototype of the generated function in the file SimFuncs.c. The function prototype is void my_g_func(real_T rtu_g_in1, real_T rtu_g_in2, real_T *rty_g_out1, real_T *rty_g_out2).

The name of the generated function, my_g_func, adheres to the naming rule you specified.

Customize Function Arguments

The return value and input arguments of the generated function represent the Argument Inports and Argument Outports of the Simulink function.

  • Each Argument Inport corresponds to one input argument of the generated function. You can optionally specify, for each of these input arguments, the preferred type qualifier.

  • You can optionally designate one of the Argument Outports of the Simulink function to be the return value of the generated function. Each of the remaining Argument Outports corresponds to one pointer input argument of the generated function.

  • You can specify the argument order.

  • You can specify the naming rule for the identifier of each argument.

Customize Arguments that Correspond to Argument Outports

The C language syntax allows only one explicit return value. You can specify the return value of the generated function as either void or as one of the Argument Outports of the Simulink Function block. Except for the Argument Outport you designate to be the return value of the generated function, all Argument Outports become pointer input arguments in the generated function. You can customize the naming rules for the identifiers of these implicit output arguments. In this example, designate the Argument Outport g_out2 as the return value, and specify the naming rule of the Argument Outport g_out1 as $I_$N.

  1. From the Code Mappings editor, open the Configure C/C++ Function Interface dialog box.

  2. From the C/C++ return argument list, select g_out2 to specify it as the return value of the generated function.

  3. In the table at the bottom of the dialog box, locate the entry for g_out1 and specify its identifier as $I_$N. The macro $N is replaced by the identifier of the Argument Outport in the Simulink function, g_out1. The macro $I is replaced by y, indicating that the argument corresponds to an Argument Outport.

The function prototype is updated accordingly.

Configure C/C++ Function Interface: g_func dialog box. The selected C/C++ return argument is g_out2. The identifier of g_out1 is $I_$N. C/C++ function prototype is highlighted with the value g_out2 = my_g_func(*y_g_out1, rtu_g_in1, rtu_g_in2).

You can see the type qualifier for g_out1, Pointer, but you cannot change it.

Customize Arguments that Correspond to Argument Inports

For each input argument that corresponds to an Argument Inport of the Simulink function, you can specify one of two values for C/C++ Type Qualifier, indicating the preferred qualifier for the argument:

  • Pointer to const — The argument is passed as a pointer to a constant in the generated function.

  • Auto — The code generator attempts to have the argument passed by value in the generated function. If it cannot, the argument is passed as a pointer in the generated function.

You can customize the naming rule for the identifier of each of these arguments.

In the table at the bottom of the Configure C/C++ Function Interface dialog box, locate the entries for the input arguments.

  • In the C/C++ Type Qualifier column, select Auto for g_in1, and Pointer to const for g_in2.

  • In the C/C++ Identifier Name column, specify $I_$N for both arguments. The macro $N is replaced by the identifier of the port in the Simulink function. The macro $I is replaced by u, indicating that the arguments correspond to Argument Inports.

Click Apply. The function prototype changes accordingly.

Configure C/C++ Function Interface dialog box. The arguments in the table are g_out1, g_in1, and g_in2. Their C/C++ Type Qualifier values are Pointer, Auto, and Pointer to const, respectively. C/C++ function prototype is g_out2 = my_g_func(*y_g_out1, u_g_in1, const *u_g_in2).

Customize Argument Order

You can customize the order that arguments appear by reordering the table rows at the bottom of the Configure C/C++ Function Interface dialog box. Locate the dotted drag-and-drop area on the left side of the table entries, and adjust the order to be g_in2, g_out1, and g_in1. Click Apply. The order of the arguments in the C/C++ function prototype changes according to the order you specified.

Configure C/C++ Function Interface dialog box. The order of the arguments in the table is g_in2, g_out1, g_in1. The identifier of all arguments is $I_$N. C/C++ function prototype is g_out2 = my_g_func(const *u_g_in2, *y_g_out1, u_g_in1).

Generate model code again, and examine the generated function code in the Code pane.

Code pane showing the definition of my_g_func, with the prototype line highlighted. The function prototype is real_T my_g_func(const real_T *u_g_in2, real_T *y_g_out1, real_T u_g_in1), and the function returns rty_g_out2_0.

In the generated code:

  • The argument order and identifiers adhere to your specifications.

  • The return value is kept, during the running of the function, in the temporary variable rty_g_out2_0, which corresponds to Argument Outport g_out2 of the Simulink function.

Use Combined Inports and Outports

You can customize the generated function to have combined arguments, corresponding each to one Argument Inport and one Argument Outport of the Simulink function. A combined argument is passed by pointer and is used in the generated function for both input and output. To use combined arguments, specify the same identifier for both the Argument Inport and the Argument Outport. In the example, update the Simulink function prototype with the argument g_in_out as the first Argument Inport and the first Argument Outport.

In the Simulink Editor, navigate to the top model. The left block in the lower part is the global Simulink Function block, globalSimFunc. Above it is its Function Caller block, globalSimFuncCaller.

Model SimFuncs. The global Simulink function block and its caller are highlighted.

Update the prototype in the Simulink function block:

  1. Click once on the prototype of the Simulink Function block to turn on editing mode.

  2. Revise the prototype to [g_in_out,g_out2] = g_func(g_in_out,g_in2).

Simulink Function block with prototype [g_in_out,g_out2] = g_func(g_in_out,g_in2).

Update the Function Caller block:

  1. Double-click on the global Simulink Function Caller block, globalSimFuncCaller.

  2. In the Block Parameters dialog box, change Function prototype to the new Simulink function prototype.

  3. Click OK.

Block Parameters dialog box of globalSimFuncCaller. The Function prototype parameter is [g_in_out,g_out2] = g_func(g_in_out,g_in2).

Customize Function Arguments

You need to update the function argument specifications to conform to the new Simulink function prototype.

Open the Configure C/C++ Function Interface dialog box from the Functions tab of the Code Mappings editor.

  1. Specify the return argument as void.

  2. Use the dotted drag-and-drop area on the left side of the table to arrange the arguments in the order g_in_out, g_in2, g_out2.

  3. For g_in2, set C/C++ Type Qualifier to Auto. This way the code generator tries to generate the input argument g_in2 as passed by value.

  4. For all three identifiers, specify C/C++ Identifier Name as gArg_$I_$N. The $I represents the type of argument. For Argument Inports it is replaced by u, for Argument Outports it is replaced by y, and for combined arguments it is replaced by uy. $N is replaced by the identifier of the port in the Simulink function.

Configure C/C++ Function Interface dialog box with parameters set to customize function arguments.

Click Apply. In the Configure C/C++ Function Interface dialog box:

  • Simulink function prototype changes to [g_in_out,g_out2] = g_func(g_in_out,g_in2).

  • C/C++ function prototype changes to void my_g_func(*gArg_uy_g_in_out, gArg_y_g_in2, *gArg_y_g_out2).

Generate code from the model and examine the generated function in the Code pane.

Code pane showing the definition of the generated function. The prototype is void my_g_func(real_T *gArg_uy_g_in_out, real_T gArg_u_g_in2, real_T *gArg_y_g_out2). The argument gArg_uy_g_in_out is highlighted in the function prototype and twice in the function body, where it is being used.

In the generated function:

  • The $I part of the naming rule is replaced by uy in the combined argument.

  • The combined argument is used for both input and output in the function body. Its value is used in the calculation and is being updated.

See Also

|

Topics