Override Default C Step Function Interface

This example shows how to customize the step function name and arguments for a rate-based model.To adhere to code guidelines and standards or easily integrate generated code with external code, you can customize these aspects of a step function:

  • Function name

  • Argument names

  • Order of the arguments

  • Return value and argument data qualifiers

  • Buffering optimizations for arguments

You can make step function customizations by using a model-specific configuration dialog box that you open from the Code Mappings editor.

Open the Configuration Dialog Box

1. Open and save a copy of model rtwdemo_fcnprotoctrl to a writable location.

2. Open the Embedded Coder app.

3. Click the Code Mappings - C tab.

4. In the Code Mappings editor, click the Functions tab. To refresh the view, click the Update code mappings button.

5. For the step function row, in the Function Preview column, click the prototype hyperlink to open the Configure C Step Function Interface dialog box.

In the Configure C Step Function Interface dialog box, the C function prototype field displays a preview of changes that you make to the function prototype.

For models configured to generate multi-instance code, see Generate Reentrant Code from Top Models.

Customize the Function Name

1. In the Configure C Step Function Interface dialog box, set C Step Function Name to fcnprotoctrl_run. The function preview is updated to reflect the new function name.

2. Click Apply, close the dialog box, and save the model.

3. Generate code and verify the name change.

  • Click the Code tab. In the Code view file list, select file rtwdemo_fcnprotoctrl.h. In the Search field, verify that the list of code elements includes function name fcnprotoctrl_run. To view the declaration, select the function name.

The declaration is highlighted in the code:

  • Verify the name change in file rtwdemo_fcnprotoctrl.c. To view the source code (definition), in the Search field, select fcnprotoctrl_run.

For more examples, see Override Default Naming for Individual C Entry-Point Functions.

Customize Function Arguments

Configure Global Data Structures

By default, a top or referenced model uses a void-void step function to pass data. This type of function enables generated code to communicate with external code by accessing global data stored as data structures that reside in shared memory.

To configure a model to have a void-void step function, open the Configure C Step Function Interface dialog box and clear the Configure arguments for Step function prototype check box. Verify the change in the C function prototype preview.

Configure Arguments

Rate-based model step functions can use arguments to pass data. You can customize these argument settings:

  • Return value

  • Type qualifiers

  • Names

  • Order

Example

1. Open the Configure C Step Function Interface dialog box. Click the Code Mappings - C tab. In the Functions tab, click the function preview for the step function.

2. In the Configure C Step Function Interface dialog box, select Configure arguments for Step function prototype.

3. Click Get default. The dialog box expands to show a field for configuring the function return value and a table that lists input and output arguments with default settings.

4. Customize the return value of the step function. Set C return argument to either void or one of the output arguments. For this example, select void.

5. For each argument, specify a C Type Qualifier.

Type Qualifiers:

  • Value: Value, for example, arg

  • Const: Value with the const qualifier, for example, const arg

  • Pointer to const: Value with const qualifier and referenced by pointer, for example, const *arg

  • Pointer: Reference by pointer, for example, *arg

  • Const pointer to const: Value with const qualifier, reference by pointer, and the pointer itself, for example, const *const arg

Models that include a referenced model

  • For a referenced model, the type qualifier for a root input argument in the step function interface is set to Auto. The code generator uses the interface specification by generating a type cast that discards the const qualifier from the source signal.

  • For the parent of the reference model, the type qualifier for the source signal is set to a value other than Auto. To override this behavior, add a const type qualifier to the referenced model.

For this example, make no changes to the C type qualifier.

6. Customize the argument names by editing values in the C Identifier Name column.

For this example, change the names of the arguments by removing the underscores, for example, change arg_In1 to argIn1.

7. Customize the argument order by dragging table rows to new positions.

For this example, move Out1 to the first position. Move Out 2 to the third position.

8. Validate, apply your changes, close the dialog box, and save the model.

9. Generate code and verify the changes in the Code view.

  • Verify your changes in file rtwdemo_fcnprotoctrl.h by checking that the Search field includes function fcnprotoctrl_run.To view the function declaration, select the function name.

  • Verify your changes in file rtwdemo_fcnprotoctrl.c.To view the definition for the function, select function name fcnprotoctrl_run.

Prototype Different From Preview

In the Configure C Step Function Interface dialog box, the function prototype preview for a referenced model can show fewer arguments than the function prototype in the generated code.

For example, consider a model named mdlref_counter with an inport (arg_input), an outport (arg_output), and a saturation block with limits that have workspace parameter argument names lower_saturation_limit and upper_saturation_limit. The Configure C Step Function Interface dialog box shows a function prototype preview as:

mdlref_counter_custom(arg_input, arg_output)

In the generated code, the prototype includes the parameter arguments:

mdlref_counter_custom(real_T arg_input, real_T arg_output, real_T 
  rtp_lower_saturation_limit, real_T rtp_upper_saturation_limit)

Optimize Buffering for Arguments

You can optimize the buffering required for I/O arguments in the step function for a model. If you adhere to these requirements when configuring an inport and outport pair, the code generator merges the corresponding arguments and reuses the associated buffer.

  • The Inport and Outport blocks must have the same properties, including data type, dimension, and sample rate.

  • The sample rate of the Inport and Outport blocks must be the same as the base rate of the model.

  • A conditionally executed subsystem cannot drive the Outport block.

  • A single, nonvirtual block output must drive the Outport block. For example, a Mux block, which merges multiple buffers, cannot drive the Outport block.

  • In the Configure C Step Function Interface dialog box, you must configure the Inport and Outport with the same C type qualifier and identifier name.

This example shows how to merge arguments for ports In3 and Out2 for example model rtwdemo_fcnprotoctrl.

1. Open the Configure C Step Function Interface dialog box for the model rtwdemo_fcnprotoctrl.

2. Open the display for the arguments by selecting Configure arguments for Step function prototype.

3. For ports In3 and Out2, set C Type Qualifier to Pointer and C Identifier Name to sharedArg.

4. Verify that the preview shows the merged argument for argIn3 and argOut2.

5. Validate, apply your changes, close the dialog box, and save the model.

6. Generate and view the code. In the Code view, search for the fcnprotoctrl_run function and check the function interface. For this example, the shared argument appears in read code for the Inport block and write code for the Outport block.

Related Topics