Documentation

Generating Custom Driver Templates

Using the Driver Authoring Tool

The prerequisites for creating a custom Simulink® Real-Time™ device driver using the Simulink Real-Time Driver Authoring Tool are the same as those for creating a device driver manually. See Expected Background and Before You Start for further information.

The following sections assume that you have identified the following component specifications for the driver. See Before You Start for guidelines for the following driver components, including their data type and size:

Input ports
Output ports
Parameters
Work variables

Setting Up Driver Variables

  1. In the Command Window, change folder to the one in which you want to save the driver code.

  2. Start Simulink Real-Time Driver Authoring Tool. Type

    slrtdrivertool
    The Simulink Real-Time Driver Authoring Tool is displayed.

  3. In the Main tab, enter:

    • Driver name — The name for your driver. The tool will create supporting files using this string as the prefix. For example, type testdriver.

    • Sample time — Select one of the following:

      • Mask parameter — If you want to set the block sample time as a block dialog box parameter (Sample Time).

      • Inherited — If you want the block to inherit its sample time from a connected block. No Sample Time parameter is displayed in the block dialog box.

  4. If you have input ports for the block, click the Input Ports tab.

    The Inport Ports tab is displayed.

  5. Click the Add button. Enter your input port information in the following fields. Repeat for the rest of your input ports.

    • Variable — Enter the name of the input. For example, speed.

    • Size — Enter the maximum size number of storage locations to be allocated for the parameter. If you want this number to be a variable one, enter a value of 0. This setting means that you can pass an additional function argument that contains the size into the start, output, and/or terminate functions along with the port/parameter variable.

    • Type — From the list, select the data type for the input port.

    • Output — Always selected. Passes the input port value into the S-function mdlOutputs callback method.

  6. If you have output ports for the block, click the Output Ports tab.

    The Output Ports tab is displayed.

  7. Click the Add button. Enter your output port information in the following fields. Repeat for the rest of your output ports.

    • Variable — Enter the name of the output. For example, speed.

    • Size — Enter the maximum size number of storage locations to be allocated for the size.

    • Type — From the list, select the data type for the output port.

    • Output — Always selected. Passes the output port value into the S-function mdlOutputs callback method.

  8. If you have parameters for the block, click the Parameters tab.

    The Parameters tab is displayed.

  9. Click the Add button. Enter your parameter information in the following fields. Repeat for the rest of your parameters.

    • Variable — Enter the name of the parameter. For example, speed.

    • Type — From the list, select the data type for the parameter.

    • Size — Enter the maximum size number of storage locations to be allocated for the parameter. If you want this number to be a variable one, enter a value of 0. This means that you can pass an additional function argument that contains the size into the start, output and/or terminate functions along with the port/parameter variable.

    • Start — Select the check box if you want the parameter value to be passed into the S-function mdlStart callback method.

    • Output — Select the check box if you want the parameter value to be passed into the S-function mdlOutputs callback method.

    • Terminate — Select the check box if you want the parameter value to be passed into the S-function mdlTerminate callback method.

  10. If you have work variables to be shared between the start, output, and terminate routines for the block, click the Work Variables tab.

    The Work Variables tab is displayed.

  11. Click the Add button. Enter your work variables information in the following fields. Repeat for the rest of your parameters.

    • Variable — Enter the name of the work variable. For example, speed.

    • Type — From the list, select the data type for the work variable.

    • Size — Enter the maximum size of the work variable.

    • Start — Select the check box if you want the work variable value to be passed into the S-function mdlStart callback method.

    • Output — Select the check box if you want the work variable value to be passed into the S-function mdlOutputs callback method.

    • Terminate — Select the check box if you want the work variable value to be passed into the S-function mdlTerminate callback method.

Saving the Configuration

The Simulink Real-Time Driver Authoring Tool allows you to save your configuration session as a MAT-file.

  1. In the Simulink Real-Time Driver Authoring Tool, click the Main tab.

  2. Click Save settings.

    The tool saves the testdriver.mat file in the current working folder.

You can iteratively change the configuration and resave the MAT-file as often as you like.

Reloading the Configuration

The Simulink Real-Time Driver Authoring Tool allows you to reload your configuration session as a MAT-file.

  1. In the Simulink Real-Time Driver Authoring Tool, click the Main tab.

  2. Click Load settings.

    The tool loads the testdriver.mat file into the tool.

Creating the C File Template

To generate a template for the driver C source code file:

  1. In the Simulink Real-Time Driver Authoring Tool, click the Main tab.

  2. Select Generate C file template.

  3. Click the Build button.

    The tool creates the following files:

    • testdriver.c

    • testdriver.h

    • sfcn_testdriver.c

    • sfcn_testdriver.tlc

  4. With your favorite editor, open the testdriver.c file and edit it. This is the source C code for your driver. The S-function code in sfcn_testdriver.c will reference this C file.

Creating a C MEX File for the Driver

To create a C MEX file for the driver, you can use either the Simulink Real-Time Driver Authoring Tool or the mex function.

Use the Simulink Real-Time Driver Authoring Tool to build the C MEX file if you have not edited the C source code file (testdriver.c). If you have edited this file and want to keep those changes, do not use the Simulink Real-Time Driver Authoring Tool to build the driver. Doing so overwrites your changes to the C source code. Instead, use the mex function (see Creating a C MEX File Using the mex Function).

Creating a C MEX File Using the Authoring Tool

  1. In the Simulink Real-Time Driver Authoring Tool, click the Main tab.

  2. Select Generate block and mask.

  3. Click the Build button.

    The tool creates the file sfcn_testdriver.mexw64.

Creating a C MEX File Using the mex Function

  1. In the Command Window, change folder to the one that contains the driver files.

  2. Compile and link the MEX-file. For example:

    mex sfcn_testdriver.c testdriver.c

    This function creates the sfcn_testdriver.mexw64 file.

Customizing the Device Driver Mask

The Simulink Real-Time Driver Authoring Tool creates a mask for the device driver. For guidelines on customizing this mask, see Driver Mask Guidelines. If you customize the mask, do not use the Simulink Real-Time Driver Authoring Tool again to build your files. Doing so overwrites the driver files and you lose your mask customizations.

Was this topic helpful?