Skip to Main Content Skip to Search
Product Documentation

Creating a Custom Driver

The following is a generic procedure for how to create a custom device driver. For additional notes on writing custom xPC Target drivers, see Custom xPC Target Driver Notes. For a description of a tool that helps you create simple custom drivers, see Using the xPC Target Driver Authoring Tool. A simple custom device driver is one which performs no DMA or interrupt handling.

  1. Write your driver in C using the approved I/O functions (see I/O Structures — Alphabetical List and I/O Functions — Alphabetical List). An example device driver for the analog inputs of the Diamond MM-32 board is available at

    matlabroot\toolbox\rtw\targets\xpc\target\build\xpcblocks\addiamondmm32.c
  2. As you write your device driver, you will want to compile and link the driver, then test it. Compile and link the driver into a MEX-file. For example:

    mex driver.c

    This step creates the MEX-file executable, driver.mexw32.

      Note   A MEX-file is used for simulation on the host and to set data structure sizes during code generation. It is not used during target execution.

  3. Create a file of MATLAB code to supplement the main C driver and support the block mask. For an example of this file, see

    matlabroot\toolbox\rtw\targets\xpc\target\build\xpcblocks\diamondmm32.m
  4. Open the Simulink Library Browser and create a new library, for example, your_company_namelib.mdl (see Creating Block Libraries in the Working with Block Libraries in the chapter of Simulink User's Guide). Use a unique library name to prevent conflicts with other libraries.

  5. In the new library, create an S-function block. From the Simulink Library Browser, drag an S-Function block to the new library.

  6. Configure the S-Function block.

    1. In the new library, right-click the S-Function block and select S-Function Parameters.

    2. In S-function name, enter the name, without extension, of the driver. For example, addiamondmm32. (This is the driver C-file you created in step 1).

    3. In S-function parameters, enter the parameters you defined for the driver. The parameter names you enter here must match the names you will later enter for the driver block mask (through the Parameters and Initialization panes of the Mask Editor dialog box). For example, firstChan, numChans, range, sampleTime, baseDec. Step 7 describes the block mask creation.

    4. Leave the S-function modules parameter with the default value, unless you need to separate your driver C-file into multiple files. If that is the case, see SpecifyAdditional Source Files for an S-Function in the Insert S-Function Code chapter of the Simulink Coder User's Guide.

  7. Double-click the S-Function block and create a block mask (see Masking Drivers).

  8. Save and close the S-Function block.

  9. At the bottom of the S-Function block, enter a block name. For example, MM-32.

  10. Save and close the library.

  11. To make your new library visible in the Simulink Library Browser, move it to

    matlabroot\toolbox\rtw\targets\xpc\target\build\xpcblocks\thirdpartydrivers
  12. Copy and paste sample_xpcblocks.m in

    matlabroot\toolbox\rtw\targets\xpc\target\build\xpcblocks\thirdpartydrivers

    Rename this file your_company_namelib_xpcblocks.m and edit this file as follows:

    • Set out.Library to your new library.

      out.Library = 'your_company_namelib';
    • Set out.Name to a string, such as the library name.

      out.Name = 'your_company_namelib Blockset';

      This string will appear in the Simulink Library Browser.

    • Set out.IsFlat to 0.

      out.IsFlat = 0;

      Note   Create a function that calls the out structure.

  13. (Optional for PCI boards) To enable the xpctarget.xpc.getxpcpci function to account for your new board, copy sample_supported.m to a unique file name. For example:

    your_company_namelib_supported.m

    Edit your copy of the file. For each board for which you add a device driver:

    1. Copy one of the commented structures in the file.

    2. Remove the comment symbols (%).

    3. Starting with 1, update the ID number.

        Tip   Number the device structures sequentially, starting with 1.

    4. Replace the field entries with your equivalents.

      A structure entry might look like:

      boards(1).VendorID   = '18f7'; 
      boards(1).DeviceID   = '0004'; 
      boards(1).SubVendorID = '-1';
      boards(1).SubDeviceID = '-1';
      boards(1).DeviceName = '422/2-PCI-335'; 
      boards(1).VendorName = 'Commtech';
      boards(1).DeviceType = 'Serial Ports';
    5. Save and close the file.

    6. To confirm your entries, type getxpcpci('all') in the MATLAB Command Window.

  14. Place all your driver files, including include files, in the directory:

    matlabroot\toolbox\rtw\targets\xpc\target\build\xpcblocks\thirdpartydrivers

      Note   Give all driver files unique names to prevent conflicts.

  15. To update the directories that you added, at the MATLAB Command Window, type

    rehash toolbox

When you are done, your library will appear in the Simulink Library Browser with xPC Target: added to the beginning of the library name.

xPC Target: your_company_namelib Blockset
  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

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