Controlling Model Function Prototypes

Overview

The Real-Time Workshop Embedded Coder software provides a Configure Model Functions button, located on the Interface pane of the Configuration Parameters dialog box, that allows you to control the model function prototypes that are generated for ERT-based Simulink models.

By default, the function prototype of an ERT-based model's generated model_step function resembles the following:

void model_step(void);

The function prototype of an ERT-based model's generated model_init function resembles the following:

void model_init(void);

(For more detailed information about the default calling interface for the model_step function, see the model_step reference page.)

The Configure Model Functions button on the Interface pane provides you flexible control over the model function prototypes that are generated for your model. Clicking Configure Model Functions launches a Model Interface dialog box (see Configuring Model Function Prototypes). Based on the Function specification value you specify for your model function (supported values include Default model initialize and step functions and Model specific C prototypes), you can preview and modify the function prototypes. Once you validate and apply your changes, you can generate code based on your function prototype modifications.

For more information about using the Configure Model Functions button and the Model Interface dialog box, see Model Function Prototypes Example and the demo model rtwdemo_fcnprotoctrl, which is preconfigured to demonstrate function prototype control.

Alternatively, you can use function prototype control functions to programmatically control model function prototypes. For more information, see Configuring Model Function Prototypes Programmatically.

You can also control model function prototypes for nonvirtual subsystems, if you generate subsystem code using right-click build. To launch the Model Interface for subsystem dialog box, use the RTW.configSubsystemBuild function.

Right-click building the subsystem generates the step and initialization functions according to the customizations you make. For more information, see Configuring Function Prototypes for Nonvirtual Subsystems.

For limitations that apply, see Model Function Prototype Control Limitations.

Configuring Model Function Prototypes

Launching the Model Interface Dialog Boxes

Clicking the Configure Model Functions button on the Interface pane of the Configuration Parameters dialog box launches the Model Interface dialog box. This dialog box is the starting point for configuring the model function prototypes that are generated during code generation for ERT-based Simulink models. Based on the Function specification value you select for your model function (supported values include Default model initialize and step functions and Model specific C prototypes), you can preview and modify the function prototype. Once you validate and apply your changes, you can generate code based on your function prototype modifications.

To configure function prototypes for a right-click build of a nonvirtual subsystem, invoke the RTW.configSubsystemBuild function, which launches the Model Interface for subsystem dialog box. For more information, see Configuring Function Prototypes for Nonvirtual Subsystems

Default Model Initialize and Step Functions View

The figure below shows the Model Interface dialog box in the Default model initialize and step functions view.

The Default model initialize and step functions view allows you to validate and preview the predicted default model step and initialization function prototypes. To validate the default function prototype configuration against your model, click the Validate button. If the validation succeeds, the predicted step function prototype appears in the Step function preview subpane.

Model Specific C Prototypes View

Selecting Model specific C prototypes for the Function specification parameter displays the Model specific C prototypes view of your model function prototypes. This view provides controls that you can use to customize the function names, the order of arguments, and argument attributes including name, passing mechanism, and type qualifier for each of the model's root-level I/O ports.

To begin configuring your function control prototype configuration, click the Get Default Configuration button. This activates and initializes the function names and properties in the Configure model initialize and step functions subpane, as shown below. If you click Get Default Configuration again later, only the properties of the step function arguments are reset to default values.

In the Configure model initialize and step functions subpane:

ParameterDescription
Step function nameName of the model_step function.
Initialize function nameName of the model_init function.
OrderOrder of the argument. A return argument is listed as Return.
Port NameName of the port.
Port TypeType of the port.
CategorySpecifies how an argument is passed in or out from the customized step function, either by copying a value (Value) or by a pointer to a memory space (Pointer).
Argument NameName of the argument.
Qualifier (optional)Specifies a const type qualifier for a function argument. The available values are dependent on the Category specified. When you change the Category, if the specified type is no longer available, the Qualifier changes to none. The possible values are:
  • none

  • const (value)

  • const* (value referenced by the pointer)

  • const*const (value referenced by the pointer and the pointer itself)

The Step function preview subpane provides a preview of how your step function prototype is interpreted in generated code. The preview is updated dynamically as you make modifications.

An argument foo whose Category is Pointer is previewed as * foo. If its Category is Value, it is previewed as foo. Notice that argument types and qualifiers are not represented in the Step function preview subpane.

Configuring Function Prototypes for Nonvirtual Subsystems

You can control step and initialization function prototypes for nonvirtual subsystems in ERT-based Simulink models, if you generate subsystem code using right-click build. Function prototype control is supported for the following types of nonvirtual subsystems:

To launch the Model Interface for subsystem dialog box, open the model containing the subsystem and invoke the RTW.configSubsystemBuild function.

The Model Interface dialog box for modifying the model-specific C prototypes for the rtwdemo_counter/Amplifier subsystem appears as follows:

Right-click building the subsystem generates the step and initialization functions according to the customizations you make.

Model Function Prototypes Example

The following procedure demonstrates how to use the Configure Model Functions button on the Interface pane of the Configuration Parameters dialog box to control the model function prototypes that the Real-Time Workshop Embedded Coder software generates for your Simulink model.

  1. Open a MATLAB session and launch the rtwdemo_counter demo model.

  2. In the rtwdemo_counter Model Editor, double-click the Generate Code Using Real-Time Workshop Embedded Coder (double-click) button to generate code for an ERT-based version of rtwdemo_counter. The code generation report for rtwdemo_counter appears.

  3. In the code generation report, click the link for rtwdemo_counter.c.

  4. In the rtwdemo_counter.c code display, locate and examine the generated code for the rtwdemo_counter_step and the rtwdemo_counter_initialize functions:

    /* Model step function */
    void rtwdemo_counter_step(void)
    {
     ...
    }
    /* Model initialize function */
    void rtwdemo_counter_initialize(void)
    {
     ...
    }

    You can close the report window after you have examined the generated code. Optionally, you can save rtwdemo_counter.c and any other generated files of interest to a different location for later comparison.

  5. From the rtwdemo_counter model, open the Configuration Parameters dialog box.

  6. Navigate to the Real-Time Workshop > Interface pane and click the Configure Model Functions button. The Model Interface dialog box appears.

  7. In the initial (Default model initialize and step funtions) view of the Model Interface dialog box, click the Validate button to validate and preview the default function prototype for the rtwdemo_counter_step function. The function prototype arguments under Step function preview should correspond to the default prototype in step 4.

  8. In the Model Interface dialog box, set Function specification field to Model specific C prototypes. Making this selection switches the dialog box from the Default model initialize and step functions view to the Model specific C prototypes view.

  9. In the Model specific C prototypes view, click the Get Default Configuration button to activate the Configure model initialize and step functions subpane.

  10. In the Configure model initialize and step functions subpane, change Initialize function name to rtwdemo_counter_cust_init.

  11. In the Configure model initialize and step functions subpane, in the row for the Input argument, change the value of Category from Value to Pointer and change the value of Qualifier from none to const *. The preview reflects your changes.

  12. Click the Validate button to validate the modified function prototype. The Validation subpane displays a message that the validation succeeded.

  13. Click OK to exit the Model Interface dialog box.

  14. Generate code for the model. When the build completes, the code generation report for rtwdemo_counter appears.

  15. In the code generation report, click the link for rtwdemo_counter.c.

  16. Locate and examine the generated code for the rtwdemo_counter_custom and rtwdemo_counter_cust_init functions:

    /* Customized model step function */
    void rtwdemo_counter_custom(const int32_T *arg_Input, int32_T *arg_Output)
    {
     ...
    }
     /* Model initialize function */
    void rtwdemo_counter_cust_init(void)
    {
     ...
    }
  17. Verify that the generated code is consistent with the function prototype modifications that you specified in the Model Interface dialog box.

Configuring Model Function Prototypes Programmatically

You can use the function prototype control functions (listed in Function Prototype Control Functions), to programmatically control model function prototypes. Typical uses of these functions include:

Function Prototype Control Functions

FunctionDescription
addArgConfAdd step function argument configuration information for Simulink model port to model-specific C function prototype
attachToModel (Function Prototype Control)Attach model-specific C function prototype to loaded ERT-based Simulink model
getArgCategory (Function Prototype Control)Get step function argument category for Simulink model port from model-specific C function prototype
getArgName (Function Prototype Control)Get step function argument name for Simulink model port from model-specific C function prototype
getArgPosition (Function Prototype Control)Get step function argument position for Simulink model port from model-specific C function prototype
getArgQualifier (Function Prototype Control)Get step function argument type qualifier for Simulink model port from model-specific C function prototype
getDefaultConf (Function Prototype Control)Get default configuration information for model-specific C function prototype from Simulink model to which it is attached
getFunctionNameGet function names from model-specific C function prototype
getNumArgs (Function Prototype Control)Get number of step function arguments from model-specific C function prototype
getPreviewGet model-specific C function prototype code previews
RTW.configSubsystemBuildLaunch GUI to configure C function prototype or C++ encapsulation interface for right-click build of specified subsystem
RTW.getFunctionSpecificationGet handle to model-specific C function prototype object
runValidation (Function Prototype Control)Validate model-specific C function prototype against Simulink model to which it is attached
setArgCategory (Function Prototype Control)Set step function argument category for Simulink model port in model-specific C function prototype
setArgName (Function Prototype Control)Set step function argument name for Simulink model port in model-specific C function prototype
setArgPosition (Function Prototype Control)Set step function argument position for Simulink model port in model-specific C function prototype
setArgQualifier (Function Prototype Control)Set step function argument type qualifier for Simulink model port in model-specific C function prototype
setFunctionNameSet function names in model-specific C function prototype

Sample M Script for Configuring Model Function Prototypes

The following sample M script configures the model function prototypes for the rtwdemo_counter model, using the Function Prototype Control Functions.

%% Open the rtwdemo_counter model
rtwdemo_counter

%% Select ert.tlc as the System Target File for the model
set_param(gcs,'SystemTargetFile','ert.tlc')

%% Create a model-specific C function prototype
a=RTW.ModelSpecificCPrototype

%% Add argument configuration information for Input and Output ports
addArgConf(a,'Input','Pointer','inputArg','const *')
addArgConf(a,'Output','Pointer','outputArg','none')

%% Attach the model-specific C function prototype to the model
attachToModel(a,gcs)

%% Rename the initialization function
setFunctionName(a,'InitFunction','init')

%% Rename the step function and change some argument attributes
setFunctionName(a,'StepFunction','step')
setArgPosition(a,'Output',1)
setArgCategory(a,'Input','Value')
setArgName(a,'Input','InputArg')
setArgQualifier(a,'Input','none')

%% Validate the function prototype against the model
[status,message]=runValidation(a)

%% if validation succeeded, generate code and build
if status
    rtwbuild(gcs)
end

Verifying Generated Code for Customized Functions

You can use software-in-the-loop (SIL) testing to verify the generated code for your customized step and initialization functions. This involves generating an ERT S-function wrapper for your generated code, which then can be integrated into a Simulink model to verify that the generated code provides the same result as the original model or nonvirtual subsystem. For more information, see Automatic S-Function Wrapper Generation and Verifying Generated Code with Software-in-the-Loop.

Model Function Prototype Control Limitations

The following limitations apply to controlling model function prototypes:

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS